@@ -150,7 +150,7 @@ func TestRunJob(t *testing.T) {
150150 label := fmt .Sprintf ("RunJob(%q)" , tt .command )
151151 logger , channel := newTestLogger ()
152152
153- err := runJob (tt .context , tt .command , logger , false )
153+ err := runJob (tt .context , tt .command , logger , false , time . Now (), false )
154154 if tt .success {
155155 assert .Nil (t , err , label )
156156 } else {
@@ -198,7 +198,7 @@ func TestStartJobExitsOnRequest(t *testing.T) {
198198 ctx , cancel := context .WithCancel (context .Background ())
199199 cancel ()
200200
201- StartJob (& wg , & basicContext , & job , ctx , logger , false , false , & PROM_METRICS )
201+ StartJob (& wg , & basicContext , & job , ctx , logger , false , false , false , & PROM_METRICS )
202202
203203 wg .Wait ()
204204}
@@ -218,7 +218,7 @@ func TestStartJobRunsJob(t *testing.T) {
218218
219219 logger , channel := newTestLogger ()
220220
221- StartJob (& wg , & basicContext , & job , ctx , logger , false , false , & PROM_METRICS )
221+ StartJob (& wg , & basicContext , & job , ctx , logger , false , false , false , & PROM_METRICS )
222222
223223 select {
224224 case entry := <- channel :
@@ -266,6 +266,62 @@ func TestStartJobRunsJob(t *testing.T) {
266266 wg .Wait ()
267267}
268268
269+ func TestStartJobReplacesPreviousJobs (t * testing.T ) {
270+ job := crontab.Job {
271+ CrontabLine : crontab.CrontabLine {
272+ Expression : & testExpression {2 * time .Second },
273+ Schedule : "always!" ,
274+ Command : "sleep 100" ,
275+ },
276+ Position : 1 ,
277+ }
278+
279+ var wg sync.WaitGroup
280+ ctx , cancel := context .WithCancel (context .Background ())
281+
282+ logger , channel := newTestLogger ()
283+
284+ StartJob (& wg , & basicContext , & job , ctx , logger , false , true , false , & PROM_METRICS )
285+
286+ select {
287+ case entry := <- channel :
288+ assert .Regexp (t , regexp .MustCompile ("job will run next" ), entry .Message )
289+ case <- time .After (time .Second ):
290+ t .Fatalf ("timed out waiting for schedule" )
291+ }
292+
293+ select {
294+ case entry := <- channel :
295+ assert .Regexp (t , regexp .MustCompile ("starting" ), entry .Message )
296+ case <- time .After (3 * time .Second ):
297+ t .Fatalf ("timed out waiting for start" )
298+ }
299+
300+ select {
301+ case entry := <- channel :
302+ assert .Regexp (t , regexp .MustCompile ("replacing job" ), entry .Message )
303+ case <- time .After (3 * time .Second ):
304+ t .Fatalf ("timed out waiting for job replace warning" )
305+ }
306+
307+ select {
308+ case entry := <- channel :
309+ assert .Regexp (t , regexp .MustCompile ("killed" ), entry .Message )
310+ case <- time .After (time .Second ):
311+ t .Fatalf ("timed out waiting for job kill" )
312+ }
313+
314+ select {
315+ case entry := <- channel :
316+ assert .Regexp (t , regexp .MustCompile ("job will run next" ), entry .Message )
317+ case <- time .After (time .Second ):
318+ t .Fatalf ("timed out waiting for schedule of the second job iteration" )
319+ }
320+
321+ cancel ()
322+ wg .Wait ()
323+ }
324+
269325func TestStartFuncWaitsForCompletion (t * testing.T ) {
270326 // We use startFunc to start a function, wait for it to start, then
271327 // tell the whole thing to exit, and verify that it waits for the
@@ -281,12 +337,12 @@ func TestStartFuncWaitsForCompletion(t *testing.T) {
281337 ctxStep1 , step1Done := context .WithCancel (context .Background ())
282338 ctxStep2 , step2Done := context .WithCancel (context .Background ())
283339
284- testFn := func (t0 time.Time , jobLogger * logrus.Entry ) {
340+ testFn := func (t0 time.Time , jobLogger * logrus.Entry , replacing bool ) {
285341 step1Done ()
286342 <- ctxStep2 .Done ()
287343 }
288344
289- startFunc (& wg , ctxStartFunc , logger , false , expr , time .Local , testFn )
345+ startFunc (& wg , ctxStartFunc , logger , false , false , expr , time .Local , testFn )
290346 go func () {
291347 wg .Wait ()
292348 allDone ()
@@ -329,12 +385,12 @@ func TestStartFuncDoesNotRunOverlappingJobs(t *testing.T) {
329385 ctxStartFunc , cancelStartFunc := context .WithCancel (context .Background ())
330386 ctxAllDone , allDone := context .WithCancel (context .Background ())
331387
332- testFn := func (t0 time.Time , jobLogger * logrus.Entry ) {
388+ testFn := func (t0 time.Time , jobLogger * logrus.Entry , replacing bool ) {
333389 testChan <- nil
334390 <- ctxAllDone .Done ()
335391 }
336392
337- startFunc (& wg , ctxStartFunc , logger , false , expr , time .Local , testFn )
393+ startFunc (& wg , ctxStartFunc , logger , false , false , expr , time .Local , testFn )
338394
339395 select {
340396 case <- testChan :
@@ -368,12 +424,12 @@ func TestStartFuncRunsOverlappingJobs(t *testing.T) {
368424 ctxStartFunc , cancelStartFunc := context .WithCancel (context .Background ())
369425 ctxAllDone , allDone := context .WithCancel (context .Background ())
370426
371- testFn := func (t0 time.Time , jobLogger * logrus.Entry ) {
427+ testFn := func (t0 time.Time , jobLogger * logrus.Entry , replacing bool ) {
372428 testChan <- nil
373429 <- ctxAllDone .Done ()
374430 }
375431
376- startFunc (& wg , ctxStartFunc , logger , true , expr , time .Local , testFn )
432+ startFunc (& wg , ctxStartFunc , logger , true , false , expr , time .Local , testFn )
377433
378434 for i := 0 ; i < 5 ; i ++ {
379435 select {
@@ -406,7 +462,7 @@ func TestStartFuncUsesTz(t *testing.T) {
406462
407463 it := 0
408464
409- testFn := func (t0 time.Time , jobLogger * logrus.Entry ) {
465+ testFn := func (t0 time.Time , jobLogger * logrus.Entry , replacing bool ) {
410466 testChan <- t0 .Location ()
411467 it += 1
412468
@@ -422,7 +478,7 @@ func TestStartFuncUsesTz(t *testing.T) {
422478 }
423479 }
424480
425- startFunc (& wg , ctxStartFunc , logger , false , expr , loc , testFn )
481+ startFunc (& wg , ctxStartFunc , logger , false , false , expr , loc , testFn )
426482
427483 for i := 0 ; i < 5 ; i ++ {
428484 select {
0 commit comments