Skip to content

Commit 3fb2cfd

Browse files
authored
Merge pull request kubernetes-sigs#1808 from erikgb/fix/issue-1786
🐛 Cache sync cancelled should not error with timeout
2 parents 5b2eaae + 5ed64c9 commit 3fb2cfd

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

pkg/internal/controller/controller_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,32 @@ var _ = Describe("controller", func() {
154154
Expect(err.Error()).To(ContainSubstring("failed to wait for testcontroller caches to sync: timed out waiting for cache to be synced"))
155155
})
156156

157+
It("should not error when context cancelled", func() {
158+
ctrl.CacheSyncTimeout = 1 * time.Second
159+
160+
sourceSynced := make(chan struct{})
161+
c, err := cache.New(cfg, cache.Options{})
162+
Expect(err).NotTo(HaveOccurred())
163+
c = &cacheWithIndefinitelyBlockingGetInformer{c}
164+
ctrl.startWatches = []watchDescription{{
165+
src: &singnallingSourceWrapper{
166+
SyncingSource: source.NewKindWithCache(&appsv1.Deployment{}, c),
167+
cacheSyncDone: sourceSynced,
168+
},
169+
}}
170+
ctrl.Name = "testcontroller"
171+
172+
ctx, cancel := context.WithCancel(context.TODO())
173+
go func() {
174+
defer GinkgoRecover()
175+
err = ctrl.Start(ctx)
176+
Expect(err).To(Succeed())
177+
}()
178+
179+
cancel()
180+
<-sourceSynced
181+
})
182+
157183
It("should not error when cache sync timeout is of sufficiently high", func() {
158184
ctrl.CacheSyncTimeout = 1 * time.Second
159185

pkg/source/source.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ func (ks *Kind) WaitForSync(ctx context.Context) error {
175175
return err
176176
case <-ctx.Done():
177177
ks.startCancel()
178+
if ctx.Err() == context.Canceled {
179+
return nil
180+
}
178181
return errors.New("timed out waiting for cache to be synced")
179182
}
180183
}

0 commit comments

Comments
 (0)