diff --git a/pkg/internal/controller/controller_test.go b/pkg/internal/controller/controller_test.go index 9f23fa2abc..7c947c5984 100644 --- a/pkg/internal/controller/controller_test.go +++ b/pkg/internal/controller/controller_test.go @@ -154,6 +154,32 @@ var _ = Describe("controller", func() { Expect(err.Error()).To(ContainSubstring("failed to wait for testcontroller caches to sync: timed out waiting for cache to be synced")) }) + It("should not error when context cancelled", func() { + ctrl.CacheSyncTimeout = 1 * time.Second + + sourceSynced := make(chan struct{}) + c, err := cache.New(cfg, cache.Options{}) + Expect(err).NotTo(HaveOccurred()) + c = &cacheWithIndefinitelyBlockingGetInformer{c} + ctrl.startWatches = []watchDescription{{ + src: &singnallingSourceWrapper{ + SyncingSource: source.NewKindWithCache(&appsv1.Deployment{}, c), + cacheSyncDone: sourceSynced, + }, + }} + ctrl.Name = "testcontroller" + + ctx, cancel := context.WithCancel(context.TODO()) + go func() { + defer GinkgoRecover() + err = ctrl.Start(ctx) + Expect(err).To(Succeed()) + }() + + cancel() + <-sourceSynced + }) + It("should not error when cache sync timeout is of sufficiently high", func() { ctrl.CacheSyncTimeout = 1 * time.Second diff --git a/pkg/source/source.go b/pkg/source/source.go index 8f649eaac4..8e5f966da7 100644 --- a/pkg/source/source.go +++ b/pkg/source/source.go @@ -175,6 +175,9 @@ func (ks *Kind) WaitForSync(ctx context.Context) error { return err case <-ctx.Done(): ks.startCancel() + if ctx.Err() == context.Canceled { + return nil + } return errors.New("timed out waiting for cache to be synced") } }