@@ -2,6 +2,7 @@ package reconciler
22
33import (
44 "context"
5+ "fmt"
56 "time"
67
78 . "github.com/onsi/ginkgo/v2"
@@ -298,14 +299,13 @@ var _ = Describe("CronDurationJob", func() {
298299
299300 Context ("scheduleJob" , func () {
300301 It ("should schedule a job and update status" , func () {
301- // wait a bit if we are too close to second 59...
302- // this is to ensure calculations based on minutes and seconds
303- waitIfTimeIsTooCloseTo59 ( )
304-
302+ // set the schedule time to now plus 10 seconds so we can check
303+ // later knowning the expected value.
304+ scheduleTime := time . Now (). Add ( 10 * time . Second )
305+ schedule . Spec . Schedule = fmt . Sprintf ( "%d %d %d * * *" , scheduleTime . Second (), scheduleTime . Minute (), scheduleTime . Hour ())
305306 job , err := newCronDurationJob (ctx , schedule , scheduler , k8sclient )
306307 Expect (err ).NotTo (HaveOccurred ())
307308
308- timeBeforeSchedule := time .Now ()
309309 err = job .scheduleJob (ctx )
310310 Expect (err ).NotTo (HaveOccurred ())
311311
@@ -319,33 +319,32 @@ var _ = Describe("CronDurationJob", func() {
319319 err = k8sclient .Get (ctx , types.NamespacedName {Name : "test-schedule" , Namespace : "default" }, updatedSchedule )
320320 Expect (err ).NotTo (HaveOccurred ())
321321 Expect (updatedSchedule .Status .Active ).To (BeFalse ())
322- // time should be the next minute and 0 seconds
323- Expect (updatedSchedule .Status .NextStartTime .Time .Second ()).To (BeZero ())
324- Expect (updatedSchedule .Status .NextStartTime .Time .Minute ()).To (Equal (timeBeforeSchedule .Add (time .Minute ).Minute ()))
325- Expect (updatedSchedule .Status .NextStartTime .Time .Hour ()).To (Equal (timeBeforeSchedule .Hour ()))
326- // Expect(updatedSchedule.Status.NextStartTime.Time).To(BeTemporally("~", timeBeforeSchedule.Add(time.Minute), 2*time.Second))
322+
323+ Expect (updatedSchedule .Status .NextStartTime .Time .Second ()).To (Equal (scheduleTime .Second ()))
324+ Expect (updatedSchedule .Status .NextStartTime .Time .Minute ()).To (Equal (scheduleTime .Minute ()))
325+ Expect (updatedSchedule .Status .NextStartTime .Time .Hour ()).To (Equal (scheduleTime .Hour ()))
327326 Expect (updatedSchedule .Status .MatchingClusters ).To (Equal (job .MatchingClusters ))
328327 })
329328 })
330329
331330 Context ("updateJob" , func () {
332331 It ("should update an existing job" , func () {
333- // wait a bit if we are too close to second 59...
334- // this is to ensure calculations based on minutes and seconds
335- waitIfTimeIsTooCloseTo59 ( )
336-
332+ // set the schedule time to now plus 10 seconds so we can check
333+ // later knowning the expected value.
334+ scheduleTime := time . Now (). Add ( 10 * time . Second )
335+ schedule . Spec . Schedule = fmt . Sprintf ( "%d %d %d * * *" , scheduleTime . Second (), scheduleTime . Minute (), scheduleTime . Hour ())
337336 job , err := newCronDurationJob (ctx , schedule , scheduler , k8sclient )
338337 Expect (err ).NotTo (HaveOccurred ())
339338
340339 err = job .scheduleJob (ctx )
341340 Expect (err ).NotTo (HaveOccurred ())
342341
343342 // Modify schedule to force an update
344- schedule .Spec .Schedule = "0 */2 * * * *"
343+ newScheduleTime := time .Now ().Add (30 * time .Second )
344+ schedule .Spec .Schedule = fmt .Sprintf ("%d %d %d * * *" , newScheduleTime .Second (), newScheduleTime .Minute (), newScheduleTime .Hour ())
345345 updatedJob , err := newCronDurationJob (ctx , schedule , scheduler , k8sclient )
346346 Expect (err ).NotTo (HaveOccurred ())
347347
348- timeBeforeUpdate := time .Now ()
349348 err = updatedJob .updateJob (ctx )
350349 Expect (err ).NotTo (HaveOccurred ())
351350
@@ -361,18 +360,9 @@ var _ = Describe("CronDurationJob", func() {
361360 Expect (err ).NotTo (HaveOccurred ())
362361 Expect (updatedSchedule .Status .Active ).To (BeFalse ())
363362
364- // next fire time should be the next even minute and 0 seconds
365- Expect (updatedSchedule .Status .NextStartTime .Time .Second ()).To (BeZero ())
366- if timeBeforeUpdate .Minute ()% 2 == 0 {
367- Expect (updatedSchedule .Status .NextStartTime .Time .Minute ()).To (Equal (timeBeforeUpdate .Add (2 * time .Minute ).Minute ()))
368- } else {
369- Expect (updatedSchedule .Status .NextStartTime .Time .Minute ()).To (Equal (timeBeforeUpdate .Add (1 * time .Minute ).Minute ()))
370- }
371- if timeBeforeUpdate .Minute () < 59 {
372- Expect (updatedSchedule .Status .NextStartTime .Time .Hour ()).To (Equal (timeBeforeUpdate .Hour ()))
373- } else {
374- Expect (updatedSchedule .Status .NextStartTime .Time .Hour ()).To (Equal (timeBeforeUpdate .Add (1 * time .Hour ).Hour ()))
375- }
363+ Expect (updatedSchedule .Status .NextStartTime .Time .Second ()).To (Equal (newScheduleTime .Second ()))
364+ Expect (updatedSchedule .Status .NextStartTime .Time .Minute ()).To (Equal (newScheduleTime .Minute ()))
365+ Expect (updatedSchedule .Status .NextStartTime .Time .Hour ()).To (Equal (newScheduleTime .Hour ()))
376366 Expect (updatedSchedule .Status .MatchingClusters ).To (Equal (job .MatchingClusters ))
377367 })
378368 })
@@ -552,14 +542,3 @@ var _ = Describe("CronDurationJob", func() {
552542 })
553543 })
554544})
555-
556- func waitIfTimeIsTooCloseTo59 () {
557- for {
558- now := time .Now ()
559- // if we are up to second 55... just break
560- if now .Second () < 55 {
561- break
562- }
563- time .Sleep (200 * time .Millisecond )
564- }
565- }
0 commit comments