@@ -59,15 +59,13 @@ var _ = Describe("runnables", func() {
5959 })
6060
6161 It ("should add WarmupRunnable to the Warmup and LeaderElection group" , func () {
62- warmupRunnable := warmupRunnableFunc {
63- StartFunc : func (c context.Context ) error {
62+ warmupRunnable := newWarmupRunnableFunc (
63+ func (c context.Context ) error {
6464 <- c .Done ()
6565 return nil
6666 },
67- WarmupFunc : func (c context.Context ) error {
68- return nil
69- },
70- }
67+ func (c context.Context ) error { return nil },
68+ )
7169
7270 r := newRunnables (defaultBaseContext , errCh )
7371 Expect (r .Add (warmupRunnable )).To (Succeed ())
@@ -77,18 +75,14 @@ var _ = Describe("runnables", func() {
7775 })
7876
7977 It ("should add WarmupRunnable that doesn't needs leader election to warmup group only" , func () {
80- warmupRunnable := leaderElectionAndWarmupRunnable {
81- StartFunc : func (c context.Context ) error {
78+ warmupRunnable := newLeaderElectionAndWarmupRunnable (
79+ func (c context.Context ) error {
8280 <- c .Done ()
8381 return nil
8482 },
85- WarmupFunc : func (c context.Context ) error {
86- return nil
87- },
88- NeedLeaderElectionFunc : func () bool {
89- return false
90- },
91- }
83+ func (c context.Context ) error { return nil },
84+ false ,
85+ )
9286
9387 r := newRunnables (defaultBaseContext , errCh )
9488 Expect (r .Add (warmupRunnable )).To (Succeed ())
@@ -99,18 +93,14 @@ var _ = Describe("runnables", func() {
9993 })
10094
10195 It ("should add WarmupRunnable that needs leader election to Warmup and LeaderElection group, not Others" , func () {
102- warmupRunnable := leaderElectionAndWarmupRunnable {
103- StartFunc : func (c context.Context ) error {
96+ warmupRunnable := newLeaderElectionAndWarmupRunnable (
97+ func (c context.Context ) error {
10498 <- c .Done ()
10599 return nil
106100 },
107- WarmupFunc : func (c context.Context ) error {
108- return nil
109- },
110- NeedLeaderElectionFunc : func () bool {
111- return true
112- },
113- }
101+ func (c context.Context ) error { return nil },
102+ true ,
103+ )
114104
115105 r := newRunnables (defaultBaseContext , errCh )
116106 Expect (r .Add (warmupRunnable )).To (Succeed ())
@@ -123,16 +113,16 @@ var _ = Describe("runnables", func() {
123113 It ("should execute the Warmup function when Warmup group is started" , func () {
124114 var warmupExecuted atomic.Bool
125115
126- warmupRunnable := warmupRunnableFunc {
127- StartFunc : func (c context.Context ) error {
116+ warmupRunnable := newWarmupRunnableFunc (
117+ func (c context.Context ) error {
128118 <- c .Done ()
129119 return nil
130120 },
131- WarmupFunc : func (c context.Context ) error {
121+ func (c context.Context ) error {
132122 warmupExecuted .Store (true )
133123 return nil
134124 },
135- }
125+ )
136126
137127 r := newRunnables (defaultBaseContext , errCh )
138128 Expect (r .Add (warmupRunnable )).To (Succeed ())
@@ -150,15 +140,13 @@ var _ = Describe("runnables", func() {
150140 It ("should propagate errors from Warmup function to error channel" , func () {
151141 expectedErr := fmt .Errorf ("expected warmup error" )
152142
153- warmupRunnable := warmupRunnableFunc {
154- StartFunc : func (c context.Context ) error {
143+ warmupRunnable := newWarmupRunnableFunc (
144+ func (c context.Context ) error {
155145 <- c .Done ()
156146 return nil
157147 },
158- WarmupFunc : func (c context.Context ) error {
159- return expectedErr
160- },
161- }
148+ func (c context.Context ) error { return expectedErr },
149+ )
162150
163151 testErrChan := make (chan error , 1 )
164152 r := newRunnables (defaultBaseContext , testErrChan )
@@ -355,72 +343,72 @@ var _ = Describe("runnableGroup", func() {
355343 })
356344})
357345
358- var _ LeaderElectionRunnable = & leaderElectionRunnableFunc {}
359-
360- // leaderElectionRunnableFunc is a helper struct that implements LeaderElectionRunnable
361- // for testing purposes.
362- type leaderElectionRunnableFunc struct {
363- StartFunc func (context.Context ) error
364- NeedLeaderElectionFunc func () bool
365- }
366-
367- func (r leaderElectionRunnableFunc ) Start (ctx context.Context ) error {
368- return r .StartFunc (ctx )
369- }
346+ var _ warmupRunnable = & warmupRunnableFunc {}
370347
371- func (r leaderElectionRunnableFunc ) NeedLeaderElection () bool {
372- return r .NeedLeaderElectionFunc ()
348+ func newWarmupRunnableFunc (
349+ startFunc func (context.Context ) error ,
350+ warmupFunc func (context.Context ) error ,
351+ ) * warmupRunnableFunc {
352+ return & warmupRunnableFunc {
353+ startFunc : startFunc ,
354+ warmupFunc : warmupFunc ,
355+ didWarmupFinish : make (chan struct {}),
356+ }
373357}
374358
375- var _ warmupRunnable = & warmupRunnableFunc {}
376-
377359// warmupRunnableFunc is a helper struct that implements WarmupRunnable
378360// for testing purposes.
379361type warmupRunnableFunc struct {
380- StartFunc func (context.Context ) error
381- WarmupFunc func (context.Context ) error
382- didWarmupFinish chan bool
362+ startFunc func (context.Context ) error
363+ warmupFunc func (context.Context ) error
364+
365+ // didWarmupFinish is closed when warmup is finished
366+ didWarmupFinish chan struct {}
367+
368+ // didWarmupFinishSuccessfully is set to true if warmup was successful
369+ didWarmupFinishSuccessfully atomic.Bool
383370}
384371
385- func (r warmupRunnableFunc ) Start (ctx context.Context ) error {
386- return r .StartFunc (ctx )
372+ func (r * warmupRunnableFunc ) Start (ctx context.Context ) error {
373+ return r .startFunc (ctx )
387374}
388375
389- func (r warmupRunnableFunc ) Warmup (ctx context.Context ) error {
390- err := r .WarmupFunc (ctx )
391- r .didWarmupFinish <- (err == nil )
376+ func (r * warmupRunnableFunc ) Warmup (ctx context.Context ) error {
377+ err := r .warmupFunc (ctx )
378+ r .didWarmupFinishSuccessfully .Store (err == nil )
379+ close (r .didWarmupFinish )
392380 return err
393381}
394382
395- func (r warmupRunnableFunc ) WaitForWarmupComplete (ctx context.Context ) bool {
396- return <- r .didWarmupFinish
383+ func (r * warmupRunnableFunc ) WaitForWarmupComplete (ctx context.Context ) bool {
384+ <- r .didWarmupFinish
385+ return r .didWarmupFinishSuccessfully .Load ()
397386}
398387
399388var _ LeaderElectionRunnable = & leaderElectionAndWarmupRunnable {}
400389var _ warmupRunnable = & leaderElectionAndWarmupRunnable {}
401390
402391// leaderElectionAndWarmupRunnable implements both WarmupRunnable and LeaderElectionRunnable
403392type leaderElectionAndWarmupRunnable struct {
404- StartFunc func (context.Context ) error
405- WarmupFunc func (context.Context ) error
406- NeedLeaderElectionFunc func () bool
407- didWarmupFinish chan bool
408- }
409-
410- func (r leaderElectionAndWarmupRunnable ) Start (ctx context.Context ) error {
411- return r .StartFunc (ctx )
412- }
413-
414- func (r leaderElectionAndWarmupRunnable ) Warmup (ctx context.Context ) error {
415- err := r .WarmupFunc (ctx )
416- r .didWarmupFinish <- (err == nil )
417- return err
393+ * warmupRunnableFunc
394+ needLeaderElection bool
418395}
419396
420- func (r leaderElectionAndWarmupRunnable ) WaitForWarmupComplete (ctx context.Context ) bool {
421- return <- r .didWarmupFinish
397+ func newLeaderElectionAndWarmupRunnable (
398+ startFunc func (context.Context ) error ,
399+ warmupFunc func (context.Context ) error ,
400+ needLeaderElection bool ,
401+ ) * leaderElectionAndWarmupRunnable {
402+ return & leaderElectionAndWarmupRunnable {
403+ warmupRunnableFunc : & warmupRunnableFunc {
404+ startFunc : startFunc ,
405+ warmupFunc : warmupFunc ,
406+ didWarmupFinish : make (chan struct {}),
407+ },
408+ needLeaderElection : needLeaderElection ,
409+ }
422410}
423411
424412func (r leaderElectionAndWarmupRunnable ) NeedLeaderElection () bool {
425- return r .NeedLeaderElectionFunc ()
413+ return r .needLeaderElection
426414}
0 commit comments