@@ -59,15 +59,13 @@ var _ = Describe("runnables", func() {
59
59
})
60
60
61
61
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 {
64
64
<- c .Done ()
65
65
return nil
66
66
},
67
- WarmupFunc : func (c context.Context ) error {
68
- return nil
69
- },
70
- }
67
+ func (c context.Context ) error { return nil },
68
+ )
71
69
72
70
r := newRunnables (defaultBaseContext , errCh )
73
71
Expect (r .Add (warmupRunnable )).To (Succeed ())
@@ -77,18 +75,14 @@ var _ = Describe("runnables", func() {
77
75
})
78
76
79
77
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 {
82
80
<- c .Done ()
83
81
return nil
84
82
},
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
+ )
92
86
93
87
r := newRunnables (defaultBaseContext , errCh )
94
88
Expect (r .Add (warmupRunnable )).To (Succeed ())
@@ -99,18 +93,14 @@ var _ = Describe("runnables", func() {
99
93
})
100
94
101
95
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 {
104
98
<- c .Done ()
105
99
return nil
106
100
},
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
+ )
114
104
115
105
r := newRunnables (defaultBaseContext , errCh )
116
106
Expect (r .Add (warmupRunnable )).To (Succeed ())
@@ -123,16 +113,16 @@ var _ = Describe("runnables", func() {
123
113
It ("should execute the Warmup function when Warmup group is started" , func () {
124
114
var warmupExecuted atomic.Bool
125
115
126
- warmupRunnable := warmupRunnableFunc {
127
- StartFunc : func (c context.Context ) error {
116
+ warmupRunnable := newWarmupRunnableFunc (
117
+ func (c context.Context ) error {
128
118
<- c .Done ()
129
119
return nil
130
120
},
131
- WarmupFunc : func (c context.Context ) error {
121
+ func (c context.Context ) error {
132
122
warmupExecuted .Store (true )
133
123
return nil
134
124
},
135
- }
125
+ )
136
126
137
127
r := newRunnables (defaultBaseContext , errCh )
138
128
Expect (r .Add (warmupRunnable )).To (Succeed ())
@@ -150,15 +140,13 @@ var _ = Describe("runnables", func() {
150
140
It ("should propagate errors from Warmup function to error channel" , func () {
151
141
expectedErr := fmt .Errorf ("expected warmup error" )
152
142
153
- warmupRunnable := warmupRunnableFunc {
154
- StartFunc : func (c context.Context ) error {
143
+ warmupRunnable := newWarmupRunnableFunc (
144
+ func (c context.Context ) error {
155
145
<- c .Done ()
156
146
return nil
157
147
},
158
- WarmupFunc : func (c context.Context ) error {
159
- return expectedErr
160
- },
161
- }
148
+ func (c context.Context ) error { return expectedErr },
149
+ )
162
150
163
151
testErrChan := make (chan error , 1 )
164
152
r := newRunnables (defaultBaseContext , testErrChan )
@@ -355,72 +343,72 @@ var _ = Describe("runnableGroup", func() {
355
343
})
356
344
})
357
345
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 {}
370
347
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
+ didWarmupFinishChan : make (chan struct {}),
356
+ }
373
357
}
374
358
375
- var _ warmupRunnable = & warmupRunnableFunc {}
376
-
377
359
// warmupRunnableFunc is a helper struct that implements WarmupRunnable
378
360
// for testing purposes.
379
361
type 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
+ // didWarmupFinishChan is closed when warmup is finished
366
+ didWarmupFinishChan chan struct {}
367
+
368
+ // didWarmupFinishSuccessfully is set to true if warmup was successful
369
+ didWarmupFinishSuccessfully atomic.Bool
383
370
}
384
371
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 )
387
374
}
388
375
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 .didWarmupFinishChan )
392
380
return err
393
381
}
394
382
395
- func (r warmupRunnableFunc ) WaitForWarmupComplete (ctx context.Context ) bool {
396
- return <- r .didWarmupFinish
383
+ func (r * warmupRunnableFunc ) WaitForWarmupComplete (ctx context.Context ) bool {
384
+ <- r .didWarmupFinishChan
385
+ return r .didWarmupFinishSuccessfully .Load ()
397
386
}
398
387
399
388
var _ LeaderElectionRunnable = & leaderElectionAndWarmupRunnable {}
400
389
var _ warmupRunnable = & leaderElectionAndWarmupRunnable {}
401
390
402
391
// leaderElectionAndWarmupRunnable implements both WarmupRunnable and LeaderElectionRunnable
403
392
type 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
418
395
}
419
396
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
+ didWarmupFinishChan : make (chan struct {}),
407
+ },
408
+ needLeaderElection : needLeaderElection ,
409
+ }
422
410
}
423
411
424
412
func (r leaderElectionAndWarmupRunnable ) NeedLeaderElection () bool {
425
- return r .NeedLeaderElectionFunc ()
413
+ return r .needLeaderElection
426
414
}
0 commit comments