Skip to content

Commit

Permalink
Rework tests to cater for out of order events
Browse files Browse the repository at this point in the history
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
  • Loading branch information
acbox committed May 15, 2023
1 parent e3ae45e commit ae0b4d0
Showing 1 changed file with 19 additions and 29 deletions.
48 changes: 19 additions & 29 deletions pkg/sloop/ingress/kubewatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,39 +82,21 @@ func Test_bigPicture(t *testing.T) {
kubeContext := "" // empty string makes things work
enableGranularMetrics := true
exclusionRules := map[string][]any{}

kw, err := NewKubeWatcherSource(kubeClient, outChan, resync, includeCrds, time.Duration(10*time.Second), masterURL, kubeContext, enableGranularMetrics, exclusionRules)
assert.NoError(t, err)

// create namespace
// create service and await corresponding event
ns := "ns"
_, err = kubeClient.CoreV1().Namespaces().Create(context.TODO(), &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: ns}}, metav1.CreateOptions{})
if err != nil {
t.FailNow()
}

// create first service
svc := &corev1.Service{ObjectMeta: metav1.ObjectMeta{Name: "s1"}}
svc := &corev1.Service{ObjectMeta: metav1.ObjectMeta{Name: "s"}}
_, err = kubeClient.CoreV1().Services(ns).Create(context.TODO(), svc, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Error creating service: %v\n", err)
}

// create second service
svc = &corev1.Service{ObjectMeta: metav1.ObjectMeta{Name: "s2"}}
_, err = kubeClient.CoreV1().Services(ns).Create(context.TODO(), svc, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Error creating service: %v\n", err)
}

// await events
result1 := <-outChan
result2 := <-outChan
result3 := <-outChan

assert.Contains(t, result1.Payload, `"name":"ns"`)
assert.Contains(t, result2.Payload, `"name":"s1"`)
assert.Contains(t, result3.Payload, `"name":"s2"`)
_ = <-outChan

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

// await events
result1 := <-outChan
result2 := <-outChan
result3 := <-outChan

assert.Contains(t, result1.Payload, `"name":"ns"`)
assert.Contains(t, result2.Payload, `"name":"s1"`)
assert.Contains(t, result3.Payload, `"name":"s3"`) // s2 should've been excluded so expect s3
eventCount := 0
loop:
for {
select {
case <-time.After(1 * time.Second):
break loop
case result, ok := <-outChan:
if ok {
eventCount++
assert.NotContains(t, result.Payload, `"name":"s2"`)
} else {
t.Fatalf("Channel closed unexpectedly: %v\n", ok)
}
}
}
assert.Equal(t, 3, eventCount) // assert no event for service named s2

kw.Stop()
}
Expand Down

0 comments on commit ae0b4d0

Please sign in to comment.