cronjob: fix flaky unit test TestController2_updateCronJob

This commit is contained in:
Alay Patel 2021-02-28 11:28:37 -05:00
parent fb6349c15b
commit 602435ccb9

View File

@ -20,7 +20,6 @@ import (
"fmt" "fmt"
"reflect" "reflect"
"strings" "strings"
"sync"
"testing" "testing"
"time" "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 // this test will take around 61 seconds to complete
func TestController2_updateCronJob(t *testing.T) { func TestController2_updateCronJob(t *testing.T) {
cjc := &fakeCJControl{} cjc := &fakeCJControl{}
jc := &fakeJobControl{} jc := &fakeJobControl{}
type fields struct { type fields struct {
queue workqueue.RateLimitingInterface queue *fakeQueue
recorder record.EventRecorder recorder record.EventRecorder
jobControl jobControlInterface jobControl jobControlInterface
cronJobControl cjControlInterface cronJobControl cjControlInterface
@ -328,16 +338,15 @@ func TestController2_updateCronJob(t *testing.T) {
newJobSchedule string newJobSchedule string
} }
tests := []struct { tests := []struct {
name string name string
fields fields fields fields
args args args args
deltaTimeForQueue time.Duration deltaTimeForQueue time.Duration
roundOffTimeDuration time.Duration
}{ }{
{ {
name: "spec.template changed", name: "spec.template changed",
fields: fields{ fields: fields{
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "test-update-cronjob"), queue: &fakeQueue{RateLimitingInterface: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "test-update-cronjob")},
recorder: record.NewFakeRecorder(10), recorder: record.NewFakeRecorder(10),
jobControl: jc, jobControl: jc,
cronJobControl: cjc, cronJobControl: cjc,
@ -358,13 +367,12 @@ func TestController2_updateCronJob(t *testing.T) {
Spec: jobSpec(), Spec: jobSpec(),
}, },
}, },
deltaTimeForQueue: 0 * time.Second, deltaTimeForQueue: 0 * time.Second,
roundOffTimeDuration: 500*time.Millisecond + nextScheduleDelta,
}, },
{ {
name: "spec.schedule changed", name: "spec.schedule changed",
fields: fields{ fields: fields{
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "test-update-cronjob"), queue: &fakeQueue{RateLimitingInterface: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "test-update-cronjob")},
recorder: record.NewFakeRecorder(10), recorder: record.NewFakeRecorder(10),
jobControl: jc, jobControl: jc,
cronJobControl: cjc, cronJobControl: cjc,
@ -373,8 +381,7 @@ func TestController2_updateCronJob(t *testing.T) {
oldJobSchedule: "30 * * * *", oldJobSchedule: "30 * * * *",
newJobSchedule: "*/1 * * * *", newJobSchedule: "*/1 * * * *",
}, },
deltaTimeForQueue: 1*time.Second + nextScheduleDelta, deltaTimeForQueue: 1*time.Second + nextScheduleDelta,
roundOffTimeDuration: 750 * time.Millisecond,
}, },
// TODO: Add more test cases for updating scheduling. // TODO: Add more test cases for updating scheduling.
} }
@ -401,22 +408,10 @@ func TestController2_updateCronJob(t *testing.T) {
cronJobControl: tt.fields.cronJobControl, cronJobControl: tt.fields.cronJobControl,
} }
jm.now = justASecondBeforeTheHour 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) jm.updateCronJob(&cj, &newCj)
wg.Wait()
d := then.Sub(now) if tt.fields.queue.t.Seconds() != tt.deltaTimeForQueue.Seconds() {
if d.Round(tt.roundOffTimeDuration).Seconds() != tt.deltaTimeForQueue.Round(tt.roundOffTimeDuration).Seconds() { t.Errorf("Expected %#v got %#v", tt.deltaTimeForQueue.Seconds(), tt.fields.queue.t.Seconds())
t.Errorf("Expected %#v got %#v", tt.deltaTimeForQueue.Round(tt.roundOffTimeDuration).String(), d.Round(tt.roundOffTimeDuration).String())
} }
}) })
} }