Skip to content

Commit 9597b22

Browse files
committed
add test that uses NewCancelRunnable
Signed-off-by: Tim Ramlot <[email protected]>
1 parent 57edf50 commit 9597b22

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

pkg/internal/controller/controller_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
"sigs.k8s.io/controller-runtime/pkg/handler"
4343
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/internal/controller/metrics"
4444
"sigs.k8s.io/controller-runtime/pkg/internal/log"
45+
"sigs.k8s.io/controller-runtime/pkg/manager"
4546
"sigs.k8s.io/controller-runtime/pkg/predicate"
4647
"sigs.k8s.io/controller-runtime/pkg/reconcile"
4748
"sigs.k8s.io/controller-runtime/pkg/source"
@@ -345,6 +346,30 @@ var _ = Describe("controller", func() {
345346
Expect(err.Error()).To(Equal("can not restart a stopped controller, you should create a new one"))
346347
})
347348

349+
It("should be possible to cancel the controller using NewCancelRunnable", func() {
350+
stoppedChan := make(chan struct{})
351+
cancellableController, cancel := manager.NewCancelRunnable(ctrl)
352+
353+
go func() {
354+
defer close(stoppedChan)
355+
defer GinkgoRecover()
356+
Expect(cancellableController.Start(context.Background())).NotTo(HaveOccurred())
357+
}()
358+
359+
var started bool
360+
for !started {
361+
func() {
362+
ctrl.mu.Lock()
363+
defer ctrl.mu.Unlock()
364+
started = ctrl.Started
365+
}()
366+
}
367+
368+
cancel()
369+
370+
<-stoppedChan
371+
Eventually(ctrl.Stopped).Should(BeTrue())
372+
})
348373
})
349374

350375
Describe("Processing queue items from a Controller", func() {

0 commit comments

Comments
 (0)