Skip to content

Commit d2a110e

Browse files
authored
Merge pull request #5427 from vincepri/fix-test-manager
🐛 ClusterCacheTracker test should wait and close its reconciler
2 parents e9e62d0 + 5e2a751 commit d2a110e

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

controllers/remote/cluster_cache_tracker_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"testing"
2222

23+
"github.com/davecgh/go-spew/spew"
2324
. "github.com/onsi/gomega"
2425
corev1 "k8s.io/api/core/v1"
2526
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -81,7 +82,7 @@ func TestClusterCacheTracker(t *testing.T) {
8182
go func() {
8283
g.Expect(mgr.Start(mgrContext)).To(Succeed())
8384
}()
84-
<-env.Manager.Elected()
85+
<-mgr.Elected()
8586

8687
k8sClient = mgr.GetClient()
8788

@@ -115,11 +116,14 @@ func TestClusterCacheTracker(t *testing.T) {
115116

116117
teardown := func(t *testing.T, g *WithT, ns *corev1.Namespace) {
117118
t.Helper()
119+
defer close(c.ch)
118120

119121
t.Log("Deleting any Secrets")
120122
g.Expect(cleanupTestSecrets(ctx, k8sClient)).To(Succeed())
121123
t.Log("Deleting any Clusters")
122124
g.Expect(cleanupTestClusters(ctx, k8sClient)).To(Succeed())
125+
g.Expect(<-c.ch).To(Equal("mapped-" + clusterA.Name))
126+
g.Consistently(c.ch).ShouldNot(Receive())
123127
t.Log("Deleting Namespace")
124128
g.Expect(env.Delete(ctx, ns)).To(Succeed())
125129
t.Log("Stopping the manager")
@@ -144,9 +148,7 @@ func TestClusterCacheTracker(t *testing.T) {
144148
g.Expect(<-c.ch).To(Equal("mapped-" + clusterA.Name))
145149

146150
t.Log("Ensuring no additional watch notifications arrive")
147-
g.Consistently(func() int {
148-
return len(c.ch)
149-
}).Should(Equal(0))
151+
g.Consistently(c.ch).ShouldNot(Receive())
150152

151153
t.Log("Updating the cluster")
152154
clusterA.Annotations = map[string]string{
@@ -158,9 +160,7 @@ func TestClusterCacheTracker(t *testing.T) {
158160
g.Expect(<-c.ch).To(Equal("mapped-" + clusterA.Name))
159161

160162
t.Log("Ensuring no additional watch notifications arrive")
161-
g.Consistently(func() int {
162-
return len(c.ch)
163-
}).Should(Equal(0))
163+
g.Consistently(c.ch).ShouldNot(Receive())
164164

165165
t.Log("Creating the same watch a second time")
166166
g.Expect(cct.Watch(ctx, WatchInput{
@@ -172,9 +172,7 @@ func TestClusterCacheTracker(t *testing.T) {
172172
})).To(Succeed())
173173

174174
t.Log("Ensuring no additional watch notifications arrive")
175-
g.Consistently(func() int {
176-
return len(c.ch)
177-
}).Should(Equal(0))
175+
g.Consistently(c.ch).ShouldNot(Receive())
178176

179177
t.Log("Updating the cluster")
180178
clusterA.Annotations["update1"] = "2"
@@ -184,9 +182,7 @@ func TestClusterCacheTracker(t *testing.T) {
184182
g.Expect(<-c.ch).To(Equal("mapped-" + clusterA.Name))
185183

186184
t.Log("Ensuring no additional watch notifications arrive")
187-
g.Consistently(func() int {
188-
return len(c.ch)
189-
}).Should(Equal(0))
185+
g.Consistently(c.ch).ShouldNot(Receive())
190186
})
191187
})
192188
}
@@ -196,7 +192,11 @@ type testController struct {
196192
}
197193

198194
func (c *testController) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
199-
c.ch <- req.Name
195+
spew.Dump(req)
196+
select {
197+
case <-ctx.Done():
198+
case c.ch <- req.Name:
199+
}
200200
return ctrl.Result{}, nil
201201
}
202202

internal/envtest/environment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ func (e *Environment) start(ctx context.Context) {
274274
panic(fmt.Sprintf("Failed to start the test environment manager: %v", err))
275275
}
276276
}()
277-
e.Manager.Elected()
277+
<-e.Manager.Elected()
278278
e.WaitForWebhooks()
279279
}
280280

0 commit comments

Comments
 (0)