mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 03:41:45 +00:00
cronjob: fix flaky unit test TestController2_updateCronJob
This commit is contained in:
parent
fb6349c15b
commit
602435ccb9
@ -20,7 +20,6 @@ import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -311,12 +310,23 @@ func Test_syncCronJob(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
type fakeQueue struct {
|
||||
workqueue.RateLimitingInterface
|
||||
t time.Duration
|
||||
key interface{}
|
||||
}
|
||||
|
||||
func (f *fakeQueue) AddAfter(key interface{}, t time.Duration) {
|
||||
f.t = t
|
||||
f.key = key
|
||||
}
|
||||
|
||||
// this test will take around 61 seconds to complete
|
||||
func TestController2_updateCronJob(t *testing.T) {
|
||||
cjc := &fakeCJControl{}
|
||||
jc := &fakeJobControl{}
|
||||
type fields struct {
|
||||
queue workqueue.RateLimitingInterface
|
||||
queue *fakeQueue
|
||||
recorder record.EventRecorder
|
||||
jobControl jobControlInterface
|
||||
cronJobControl cjControlInterface
|
||||
@ -332,12 +342,11 @@ func TestController2_updateCronJob(t *testing.T) {
|
||||
fields fields
|
||||
args args
|
||||
deltaTimeForQueue time.Duration
|
||||
roundOffTimeDuration time.Duration
|
||||
}{
|
||||
{
|
||||
name: "spec.template changed",
|
||||
fields: fields{
|
||||
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "test-update-cronjob"),
|
||||
queue: &fakeQueue{RateLimitingInterface: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "test-update-cronjob")},
|
||||
recorder: record.NewFakeRecorder(10),
|
||||
jobControl: jc,
|
||||
cronJobControl: cjc,
|
||||
@ -359,12 +368,11 @@ func TestController2_updateCronJob(t *testing.T) {
|
||||
},
|
||||
},
|
||||
deltaTimeForQueue: 0 * time.Second,
|
||||
roundOffTimeDuration: 500*time.Millisecond + nextScheduleDelta,
|
||||
},
|
||||
{
|
||||
name: "spec.schedule changed",
|
||||
fields: fields{
|
||||
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "test-update-cronjob"),
|
||||
queue: &fakeQueue{RateLimitingInterface: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "test-update-cronjob")},
|
||||
recorder: record.NewFakeRecorder(10),
|
||||
jobControl: jc,
|
||||
cronJobControl: cjc,
|
||||
@ -374,7 +382,6 @@ func TestController2_updateCronJob(t *testing.T) {
|
||||
newJobSchedule: "*/1 * * * *",
|
||||
},
|
||||
deltaTimeForQueue: 1*time.Second + nextScheduleDelta,
|
||||
roundOffTimeDuration: 750 * time.Millisecond,
|
||||
},
|
||||
// TODO: Add more test cases for updating scheduling.
|
||||
}
|
||||
@ -401,22 +408,10 @@ func TestController2_updateCronJob(t *testing.T) {
|
||||
cronJobControl: tt.fields.cronJobControl,
|
||||
}
|
||||
jm.now = justASecondBeforeTheHour
|
||||
now := time.Now()
|
||||
then := time.Now()
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
now = time.Now()
|
||||
jm.queue.Get()
|
||||
then = time.Now()
|
||||
wg.Done()
|
||||
return
|
||||
}()
|
||||
jm.updateCronJob(&cj, &newCj)
|
||||
wg.Wait()
|
||||
d := then.Sub(now)
|
||||
if d.Round(tt.roundOffTimeDuration).Seconds() != tt.deltaTimeForQueue.Round(tt.roundOffTimeDuration).Seconds() {
|
||||
t.Errorf("Expected %#v got %#v", tt.deltaTimeForQueue.Round(tt.roundOffTimeDuration).String(), d.Round(tt.roundOffTimeDuration).String())
|
||||
|
||||
if tt.fields.queue.t.Seconds() != tt.deltaTimeForQueue.Seconds() {
|
||||
t.Errorf("Expected %#v got %#v", tt.deltaTimeForQueue.Seconds(), tt.fields.queue.t.Seconds())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user