@@ -83,16 +83,17 @@ var _ = Describe("Multi-Namespace Informer Cache", func() {
8383func CacheTest (createCacheFunc func (config * rest.Config , opts cache.Options ) (cache.Cache , error )) {
8484 Describe ("Cache test" , func () {
8585 var (
86- informerCache cache.Cache
87- stop chan struct {}
88- knownPod1 runtime.Object
89- knownPod2 runtime.Object
90- knownPod3 runtime.Object
91- knownPod4 runtime.Object
86+ informerCache cache.Cache
87+ informerCacheCtx context.Context
88+ informerCacheCancel context.CancelFunc
89+ knownPod1 runtime.Object
90+ knownPod2 runtime.Object
91+ knownPod3 runtime.Object
92+ knownPod4 runtime.Object
9293 )
9394
9495 BeforeEach (func () {
95- stop = make ( chan struct {} )
96+ informerCacheCtx , informerCacheCancel = context . WithCancel ( context . Background () )
9697 Expect (cfg ).NotTo (BeNil ())
9798
9899 By ("creating three pods" )
@@ -123,11 +124,11 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
123124 Expect (err ).NotTo (HaveOccurred ())
124125 By ("running the cache and waiting for it to sync" )
125126 // pass as an arg so that we don't race between close and re-assign
126- go func (stopCh chan struct {} ) {
127+ go func (ctx context. Context ) {
127128 defer GinkgoRecover ()
128- Expect (informerCache .Start (stopCh )).To (Succeed ())
129- }(stop )
130- Expect (informerCache .WaitForCacheSync (stop )).To (BeTrue ())
129+ Expect (informerCache .Start (ctx )).To (Succeed ())
130+ }(informerCacheCtx )
131+ Expect (informerCache .WaitForCacheSync (informerCacheCtx )).To (BeTrue ())
131132 })
132133
133134 AfterEach (func () {
@@ -137,7 +138,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
137138 deletePod (knownPod3 )
138139 deletePod (knownPod4 )
139140
140- close ( stop )
141+ informerCacheCancel ( )
141142 })
142143
143144 Describe ("as a Reader" , func () {
@@ -394,11 +395,13 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
394395 Expect (err ).NotTo (HaveOccurred ())
395396
396397 By ("running the cache and waiting for it to sync" )
398+ ctx , cancel := context .WithCancel (context .Background ())
399+ defer cancel ()
397400 go func () {
398401 defer GinkgoRecover ()
399- Expect (namespacedCache .Start (stop )).To (Succeed ())
402+ Expect (namespacedCache .Start (ctx )).To (Succeed ())
400403 }()
401- Expect (namespacedCache .WaitForCacheSync (stop )).NotTo (BeFalse ())
404+ Expect (namespacedCache .WaitForCacheSync (ctx )).NotTo (BeFalse ())
402405
403406 By ("listing pods in all namespaces" )
404407 out := & unstructured.UnstructuredList {}
@@ -572,11 +575,13 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
572575 Expect (informer .IndexField (context .TODO (), pod , "spec.restartPolicy" , indexFunc )).To (Succeed ())
573576
574577 By ("running the cache and waiting for it to sync" )
578+ ctx , cancel := context .WithCancel (context .Background ())
579+ defer cancel ()
575580 go func () {
576581 defer GinkgoRecover ()
577- Expect (informer .Start (stop )).To (Succeed ())
582+ Expect (informer .Start (ctx )).To (Succeed ())
578583 }()
579- Expect (informer .WaitForCacheSync (stop )).NotTo (BeFalse ())
584+ Expect (informer .WaitForCacheSync (ctx )).NotTo (BeFalse ())
580585
581586 By ("listing Pods with restartPolicyOnFailure" )
582587 listObj := & kcorev1.PodList {}
@@ -636,7 +641,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
636641 Object : map [string ]interface {}{
637642 "spec" : map [string ]interface {}{
638643 "containers" : []map [string ]interface {}{
639- map [ string ] interface {} {
644+ {
640645 "name" : "nginx" ,
641646 "image" : "nginx" ,
642647 },
@@ -700,11 +705,13 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
700705 Expect (informer .IndexField (context .TODO (), pod , "spec.restartPolicy" , indexFunc )).To (Succeed ())
701706
702707 By ("running the cache and waiting for it to sync" )
708+ ctx , cancel := context .WithCancel (context .Background ())
709+ defer cancel ()
703710 go func () {
704711 defer GinkgoRecover ()
705- Expect (informer .Start (stop )).To (Succeed ())
712+ Expect (informer .Start (ctx )).To (Succeed ())
706713 }()
707- Expect (informer .WaitForCacheSync (stop )).NotTo (BeFalse ())
714+ Expect (informer .WaitForCacheSync (ctx )).NotTo (BeFalse ())
708715
709716 By ("listing Pods with restartPolicyOnFailure" )
710717 listObj := & unstructured.UnstructuredList {}
0 commit comments