Skip to content

Commit

Permalink
Tidy up test a bit and add a context
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianmoisey committed Oct 23, 2024
1 parent f6ada00 commit d16c5e7
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions vertical-pod-autoscaler/pkg/updater/logic/updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,10 @@ func TestNewEventRecorder(t *testing.T) {
fakeClient := fake.NewSimpleClientset()
er := newEventRecorder(fakeClient)

maxRetries := 5
retryDelay := 100 * time.Millisecond
contextTimeout := 5 * time.Second

testCases := []struct {
reason string
object runtime.Object
Expand All @@ -387,12 +391,14 @@ func TestNewEventRecorder(t *testing.T) {
var events *apiv1.EventList
var err error
// Add delay for fake client to catch up due to be being asynchronous
for i := 0; i < 5; i++ {
events, err = fakeClient.CoreV1().Events("default").List(context.TODO(), metav1.ListOptions{})
for i := 0; i < maxRetries; i++ {
ctx, cancel := context.WithTimeout(context.Background(), contextTimeout)
defer cancel()
events, err = fakeClient.CoreV1().Events("default").List(ctx, metav1.ListOptions{})
if err == nil && len(events.Items) > 0 {
break
}
time.Sleep(100 * time.Millisecond)
time.Sleep(retryDelay)
}

assert.NoError(t, err, "should be able to list events")
Expand Down

0 comments on commit d16c5e7

Please sign in to comment.