Skip to content

Commit

Permalink
fix flaky k8s event tests
Browse files Browse the repository at this point in the history
This commits fixes flaky k8s event test, #5748. The tests are flaky
because we have a timer `eventsFromChannel` and it may lead to some
events are not collected and cause errors.
  • Loading branch information
Yongxuanzhang committed Nov 14, 2022
1 parent 38c739a commit 38d3511
Showing 1 changed file with 17 additions and 29 deletions.
46 changes: 17 additions & 29 deletions test/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,39 +52,27 @@ func CheckEventsUnordered(t *testing.T, eventChan chan string, testName string,
// expects to receive. The events must be received in the same order they appear in the
// wantEvents list. Any extra or too few received events are considered errors.
func eventsFromChannel(c chan string, wantEvents []string) error {
// We get events from a channel, so the timeout is here to avoid waiting
// on the channel forever if fewer than expected events are received.
// We only hit the timeout in case of failure of the test, so the actual value
// of the timeout is not so relevant, it's only used when tests are going to fail.
// on the channel forever if fewer than expected events are received
timer := time.NewTimer(10 * time.Millisecond)
// we loop the channel to collect all events, if the collected events are not
// expected events we will return error

foundEvents := []string{}
for ii := 0; ii < len(wantEvents)+1; ii++ {
// We loop over all the events that we expect. Once they are all received
// we exit the loop. If we never receive enough events, the timeout takes us
// out of the loop.
select {
case event := <-c:
foundEvents = append(foundEvents, event)
if ii > len(wantEvents)-1 {
return fmt.Errorf("received event \"%s\" but not more expected", event)
}
wantEvent := wantEvents[ii]
matching, err := regexp.MatchString(wantEvent, event)
if err == nil {
if !matching {
return fmt.Errorf("expected event \"%s\" but got \"%s\" instead", wantEvent, event)
}
} else {
return fmt.Errorf("something went wrong matching the event: %s", err)
}
case <-timer.C:
if len(foundEvents) != len(wantEvents) {
return fmt.Errorf("received %d events but %d expected. Found events: %#v", len(foundEvents), len(wantEvents), foundEvents)
channelEvents := len(c)
for ii := 0; ii < channelEvents; ii++ {
event := <-c
foundEvents = append(foundEvents, event)
wantEvent := wantEvents[ii]
matching, err := regexp.MatchString(wantEvent, event)
if err == nil {
if !matching {
return fmt.Errorf("expected event \"%s\" but got \"%s\" instead", wantEvent, event)
}
return nil
} else {
return fmt.Errorf("something went wrong matching the event: %s", err)
}
}
if len(foundEvents) != len(wantEvents) {
return fmt.Errorf("received %d events but %d expected. Found events: %#v", len(foundEvents), len(wantEvents), foundEvents)
}
return nil
}

Expand Down

0 comments on commit 38d3511

Please sign in to comment.