Skip to content

Commit bb473ba

Browse files
committed
Deflake should execute the Warmup function when Warmup group is started
The test is flaky as visible in both PRs and the periodic. The reason is that it calls Start() which asynchronously starts a runnable and expects said runnable to finish immediately after which may or may not work out depending on how busy the box the test is run on is. Use `synctest` instead, as that allows us to explicitly block until all asynchronous operations that can finish are finished.
1 parent 4b90f71 commit bb473ba

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

pkg/manager/runnable_group_test.go

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"errors"
66
"fmt"
77
"sync/atomic"
8+
"testing"
9+
"testing/synctest"
810
"time"
911

1012
. "github.com/onsi/ginkgo/v2"
@@ -110,30 +112,6 @@ var _ = Describe("runnables", func() {
110112
Expect(r.Others.startQueue).To(BeEmpty())
111113
})
112114

113-
It("should execute the Warmup function when Warmup group is started", func(ctx SpecContext) {
114-
var warmupExecuted atomic.Bool
115-
116-
warmupRunnable := newWarmupRunnableFunc(
117-
func(c context.Context) error {
118-
<-c.Done()
119-
return nil
120-
},
121-
func(c context.Context) error {
122-
warmupExecuted.Store(true)
123-
return nil
124-
},
125-
)
126-
127-
r := newRunnables(defaultBaseContext, errCh)
128-
Expect(r.Add(warmupRunnable)).To(Succeed())
129-
130-
// Start the Warmup group
131-
Expect(r.Warmup.Start(ctx)).To(Succeed())
132-
133-
// Verify warmup function was called
134-
Expect(warmupExecuted.Load()).To(BeTrue())
135-
})
136-
137115
It("should propagate errors from Warmup function to error channel", func(ctx SpecContext) {
138116
expectedErr := fmt.Errorf("expected warmup error")
139117

@@ -384,3 +362,33 @@ func newLeaderElectionAndWarmupRunnable(
384362
func (r leaderElectionAndWarmupRunnable) NeedLeaderElection() bool {
385363
return r.needLeaderElection
386364
}
365+
366+
func TestWarmupFunctionIsExecutedWhenWarmupGroupIsStarted(t *testing.T) {
367+
t.Parallel()
368+
synctest.Test(t, func(t *testing.T) {
369+
g := NewWithT(t)
370+
var warmupExecuted atomic.Bool
371+
372+
warmupRunnable := newWarmupRunnableFunc(
373+
func(c context.Context) error {
374+
<-c.Done()
375+
return nil
376+
},
377+
func(c context.Context) error {
378+
warmupExecuted.Store(true)
379+
return nil
380+
},
381+
)
382+
383+
r := newRunnables(defaultBaseContext, make(chan error))
384+
g.Expect(r.Add(warmupRunnable)).To(Succeed())
385+
386+
// Start the Warmup group
387+
g.Expect(r.Warmup.Start(t.Context())).To(Succeed())
388+
synctest.Wait()
389+
390+
// Verify warmup function was called
391+
g.Expect(warmupExecuted.Load()).To(BeTrue())
392+
r.Warmup.StopAndWait(t.Context())
393+
})
394+
}

0 commit comments

Comments
 (0)