Skip to content

Commit 82a78f9

Browse files
authored
Merge pull request #738 from knabben/leak-stop
🏃 Verification of leak goroutine on controller and manager
2 parents b97ebda + 87a0264 commit 82a78f9

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

pkg/controller/controller_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ package controller_test
1818

1919
import (
2020
"fmt"
21+
rt "runtime"
2122

2223
. "github.com/onsi/ginkgo"
2324
. "github.com/onsi/gomega"
25+
2426
"sigs.k8s.io/controller-runtime/pkg/client"
2527
"sigs.k8s.io/controller-runtime/pkg/controller"
2628
"sigs.k8s.io/controller-runtime/pkg/manager"
@@ -90,6 +92,27 @@ var _ = Describe("controller.Controller", func() {
9092

9193
close(done)
9294
})
95+
96+
It("should not leak goroutines when stop", func(done Done) {
97+
// TODO(directxman12): After closing the proper leaks on watch this must be reduced to 0
98+
// The leaks currently come from the event-related code (as in corev1.Event).
99+
threshold := 3
100+
101+
m, err := manager.New(cfg, manager.Options{})
102+
Expect(err).NotTo(HaveOccurred())
103+
104+
_, err = controller.New("new-controller", m, controller.Options{Reconciler: rec})
105+
Expect(err).NotTo(HaveOccurred())
106+
107+
startGoroutines := rt.NumGoroutine()
108+
s := make(chan struct{})
109+
close(s)
110+
111+
Expect(m.Start(s)).NotTo(HaveOccurred())
112+
Expect(rt.NumGoroutine() - startGoroutines).To(BeNumerically("<=", threshold))
113+
114+
close(done)
115+
})
93116
})
94117
})
95118

pkg/manager/manager_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ import (
2121
"io/ioutil"
2222
"net"
2323
"net/http"
24+
rt "runtime"
2425

2526
"github.com/go-logr/logr"
2627
. "github.com/onsi/ginkgo"
2728
. "github.com/onsi/gomega"
2829
"github.com/prometheus/client_golang/prometheus"
30+
2931
"k8s.io/apimachinery/pkg/api/meta"
3032
"k8s.io/apimachinery/pkg/runtime"
3133
"k8s.io/client-go/rest"
@@ -748,6 +750,23 @@ var _ = Describe("manger.Manager", func() {
748750
})
749751
})
750752

753+
It("should not leak goroutines when stop", func(done Done) {
754+
// TODO(directxman12): After closing the proper leaks on watch this must be reduced to 0
755+
// The leaks currently come from the event-related code (as in corev1.Event).
756+
threshold := 3
757+
758+
m, err := New(cfg, Options{})
759+
Expect(err).NotTo(HaveOccurred())
760+
startGoruntime := rt.NumGoroutine()
761+
762+
s := make(chan struct{})
763+
close(s)
764+
Expect(m.Start(s)).NotTo(HaveOccurred())
765+
766+
Expect(rt.NumGoroutine() - startGoruntime).To(BeNumerically("<=", threshold))
767+
close(done)
768+
})
769+
751770
It("should provide a function to get the Config", func() {
752771
m, err := New(cfg, Options{})
753772
Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)