Skip to content

Commit ae0b4d0

Browse files
committed
Rework tests to cater for out of order events
Looks like the ordering for events received from the channel is non-deterministic. I had changed the tests in this PR to expect events in an exact order, which passed reliably on my local machine but failed when run by the GitHub Actions workflow. In this commit I've reverted the existing big picture test (the one with no exclusion rules) to not check the payload, and modified the new test (including an exclusion rule) to: a) wait 1 second for all events to be received on the channel b) verify that the excluded event is not received regardless of order c) verify that the expected number of events is received
1 parent e3ae45e commit ae0b4d0

File tree

1 file changed

+19
-29
lines changed

1 file changed

+19
-29
lines changed

pkg/sloop/ingress/kubewatcher_test.go

+19-29
Original file line numberDiff line numberDiff line change
@@ -82,39 +82,21 @@ func Test_bigPicture(t *testing.T) {
8282
kubeContext := "" // empty string makes things work
8383
enableGranularMetrics := true
8484
exclusionRules := map[string][]any{}
85-
8685
kw, err := NewKubeWatcherSource(kubeClient, outChan, resync, includeCrds, time.Duration(10*time.Second), masterURL, kubeContext, enableGranularMetrics, exclusionRules)
8786
assert.NoError(t, err)
8887

89-
// create namespace
88+
// create service and await corresponding event
9089
ns := "ns"
9190
_, err = kubeClient.CoreV1().Namespaces().Create(context.TODO(), &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: ns}}, metav1.CreateOptions{})
9291
if err != nil {
9392
t.FailNow()
9493
}
95-
96-
// create first service
97-
svc := &corev1.Service{ObjectMeta: metav1.ObjectMeta{Name: "s1"}}
94+
svc := &corev1.Service{ObjectMeta: metav1.ObjectMeta{Name: "s"}}
9895
_, err = kubeClient.CoreV1().Services(ns).Create(context.TODO(), svc, metav1.CreateOptions{})
9996
if err != nil {
10097
t.Fatalf("Error creating service: %v\n", err)
10198
}
102-
103-
// create second service
104-
svc = &corev1.Service{ObjectMeta: metav1.ObjectMeta{Name: "s2"}}
105-
_, err = kubeClient.CoreV1().Services(ns).Create(context.TODO(), svc, metav1.CreateOptions{})
106-
if err != nil {
107-
t.Fatalf("Error creating service: %v\n", err)
108-
}
109-
110-
// await events
111-
result1 := <-outChan
112-
result2 := <-outChan
113-
result3 := <-outChan
114-
115-
assert.Contains(t, result1.Payload, `"name":"ns"`)
116-
assert.Contains(t, result2.Payload, `"name":"s1"`)
117-
assert.Contains(t, result3.Payload, `"name":"s2"`)
99+
_ = <-outChan
118100

119101
kw.Stop()
120102
}
@@ -174,14 +156,22 @@ func Test_bigPictureWithExclusionRules(t *testing.T) {
174156
t.Fatalf("Error creating service: %v\n", err)
175157
}
176158

177-
// await events
178-
result1 := <-outChan
179-
result2 := <-outChan
180-
result3 := <-outChan
181-
182-
assert.Contains(t, result1.Payload, `"name":"ns"`)
183-
assert.Contains(t, result2.Payload, `"name":"s1"`)
184-
assert.Contains(t, result3.Payload, `"name":"s3"`) // s2 should've been excluded so expect s3
159+
eventCount := 0
160+
loop:
161+
for {
162+
select {
163+
case <-time.After(1 * time.Second):
164+
break loop
165+
case result, ok := <-outChan:
166+
if ok {
167+
eventCount++
168+
assert.NotContains(t, result.Payload, `"name":"s2"`)
169+
} else {
170+
t.Fatalf("Channel closed unexpectedly: %v\n", ok)
171+
}
172+
}
173+
}
174+
assert.Equal(t, 3, eventCount) // assert no event for service named s2
185175

186176
kw.Stop()
187177
}

0 commit comments

Comments
 (0)