1// +build go1.7 2 3package credentials 4 5import ( 6 "context" 7 "testing" 8) 9 10func TestCredentialsGetWithContext(t *testing.T) { 11 stub := &stubProviderConcurrent{ 12 stubProvider: stubProvider{ 13 creds: Value{ 14 AccessKeyID: "AKIDEXAMPLE", 15 SecretAccessKey: "KEYEXAMPLE", 16 }, 17 }, 18 done: make(chan struct{}), 19 } 20 21 c := NewCredentials(stub) 22 23 ctx, cancel1 := context.WithCancel(context.Background()) 24 ctx1 := &ContextWaiter{Context: ctx, waiting: make(chan struct{}, 1)} 25 ctx2 := &ContextWaiter{Context: context.Background(), waiting: make(chan struct{}, 1)} 26 27 var err1, err2 error 28 var creds1, creds2 Value 29 30 done1 := make(chan struct{}) 31 go func() { 32 creds1, err1 = c.GetWithContext(ctx1) 33 close(done1) 34 }() 35 <-ctx1.waiting 36 <-ctx1.waiting 37 38 done2 := make(chan struct{}) 39 go func() { 40 creds2, err2 = c.GetWithContext(ctx2) 41 close(done2) 42 }() 43 <-ctx2.waiting 44 45 cancel1() 46 <-done1 47 48 close(stub.done) 49 <-done2 50 51 if err1 == nil { 52 t.Errorf("expect first to have error") 53 } 54 if creds1.HasKeys() { 55 t.Errorf("expect first not to have keys, %v", creds1) 56 } 57 58 if err2 != nil { 59 t.Errorf("expect second not to have error, %v", err2) 60 } 61 if !creds2.HasKeys() { 62 t.Errorf("expect second to have keys") 63 } 64} 65 66type ContextWaiter struct { 67 context.Context 68 waiting chan struct{} 69} 70 71func (c *ContextWaiter) Done() <-chan struct{} { 72 go func() { 73 c.waiting <- struct{}{} 74 }() 75 76 return c.Context.Done() 77} 78