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