@@ -34,6 +34,7 @@ import (
3434 "github.com/intelsdi-x/snap/core/ctypes"
3535 "github.com/intelsdi-x/snap/core/serror"
3636 "github.com/intelsdi-x/snap/pkg/schedule"
37+ "github.com/intelsdi-x/snap/scheduler/fixtures"
3738 "github.com/intelsdi-x/snap/scheduler/wmap"
3839)
3940
@@ -250,6 +251,8 @@ func TestCreateTask(t *testing.T) {
250251 })
251252 })
252253 Convey ("should not error when the schedule is valid" , func () {
254+ lse := fixtures .NewListenToSchedulerEvent ()
255+ s .eventManager .RegisterHandler ("Scheduler.TaskEnded" , lse )
253256 start := time .Now ().Add (startWait )
254257 stop := time .Now ().Add (startWait + windowSize )
255258 sch := schedule .NewWindowedSchedule (interval , & start , & stop , 0 )
@@ -260,11 +263,11 @@ func TestCreateTask(t *testing.T) {
260263 task := s .tasks .Get (tsk .ID ())
261264 task .Spin ()
262265 Convey ("the task should be ended after reaching the end of window" , func () {
263- // wait for the end of determined window
264- time . Sleep ( startWait + windowSize )
265- // wait an interval to be sure that the task state has been updated
266- // we are ok to extend sleeping by 100ms to allow to complete post-schedule activities
267- time . Sleep ( interval + time . Millisecond * 100 )
266+ // wait for task ended event (or timeout)
267+ select {
268+ case <- lse . Ended :
269+ case <- time . After ( stop . Add ( 1 * time . Second ). Sub ( start )):
270+ }
268271 // check if the task is ended
269272 So (tsk .State (), ShouldEqual , core .TaskEnded )
270273 })
@@ -290,6 +293,8 @@ func TestCreateTask(t *testing.T) {
290293 })
291294 })
292295 Convey ("Single run task firing on defined start time" , func () {
296+ lse := fixtures .NewListenToSchedulerEvent ()
297+ s .eventManager .RegisterHandler ("Scheduler.TaskEnded" , lse )
293298 count := uint (1 )
294299 start := time .Now ().Add (startWait )
295300 sch := schedule .NewWindowedSchedule (interval , & start , nil , count )
@@ -300,11 +305,11 @@ func TestCreateTask(t *testing.T) {
300305 task := s .tasks .Get (tsk .ID ())
301306 task .Spin ()
302307 Convey ("the task should be ended after reaching the end of window" , func () {
303- // wait for the end of determined window
304- time . Sleep ( startWait )
305- // wait an interval to be sure that the task state has been updated
306- // we are ok to extend sleeping by 100ms to allow to complete post-schedule activities
307- time . Sleep ( interval + time . Millisecond * 100 )
308+ // wait for task ended event (or timeout)
309+ select {
310+ case <- lse . Ended :
311+ case <- time . After ( time . Duration ( interval . Nanoseconds () * int64 ( count ) + interval . Nanoseconds ()) + 1 * time . Second ):
312+ }
308313 // check if the task is ended
309314 So (tsk .State (), ShouldEqual , core .TaskEnded )
310315 })
@@ -400,6 +405,8 @@ func TestStopTask(t *testing.T) {
400405 })
401406 })
402407 Convey ("Calling StopTask on an ended task" , t , func () {
408+ lse := fixtures .NewListenToSchedulerEvent ()
409+ s .eventManager .RegisterHandler ("Scheduler.TaskEnded" , lse )
403410 start := time .Now ().Add (startWait )
404411 stop := time .Now ().Add (startWait + windowSize )
405412
@@ -412,11 +419,11 @@ func TestStopTask(t *testing.T) {
412419 task := s .tasks .Get (tsk .ID ())
413420 task .Spin ()
414421
415- // wait for the end of determined window
416- time . Sleep ( startWait + windowSize )
417- // wait an interval to be sure that the task state has been updated
418- // we are ok to extend sleeping by 100ms to allow to complete post-schedule activities
419- time . Sleep ( interval + time . Millisecond * 100 )
422+ // wait for task ended event (or timeout)
423+ select {
424+ case <- lse . Ended :
425+ case <- time . After ( stop . Add ( 1 * time . Second ). Sub ( start )):
426+ }
420427 // check if the task is ended
421428 So (tsk .State (), ShouldEqual , core .TaskEnded )
422429
@@ -486,6 +493,8 @@ func TestStartTask(t *testing.T) {
486493 })
487494 })
488495 Convey ("Calling StartTask on an ended windowed task" , t , func () {
496+ lse := fixtures .NewListenToSchedulerEvent ()
497+ s .eventManager .RegisterHandler ("Scheduler.TaskEnded" , lse )
489498 start := time .Now ().Add (startWait )
490499 stop := time .Now ().Add (startWait + windowSize )
491500
@@ -498,11 +507,11 @@ func TestStartTask(t *testing.T) {
498507 task := s .tasks .Get (tsk .ID ())
499508 task .Spin ()
500509
501- // wait for the end of determined window
502- time . Sleep ( startWait + windowSize )
503- // wait an interval to be sure that the task state has been updated
504- // we are ok to extend sleeping by 100ms to allow to complete post-schedule activities
505- time . Sleep ( interval + time . Millisecond * 100 )
510+ // wait for task ended event (or timeout)
511+ select {
512+ case <- lse . Ended :
513+ case <- time . After ( stop . Add ( 1 * time . Second ). Sub ( start )):
514+ }
506515
507516 // check if the task is ended
508517 So (tsk .State (), ShouldEqual , core .TaskEnded )
@@ -583,6 +592,8 @@ func TestEnableTask(t *testing.T) {
583592 })
584593 })
585594 Convey ("Calling EnableTask on an ended task" , t , func () {
595+ lse := fixtures .NewListenToSchedulerEvent ()
596+ s .eventManager .RegisterHandler ("Scheduler.TaskEnded" , lse )
586597 start := time .Now ().Add (startWait )
587598 stop := time .Now ().Add (startWait + windowSize )
588599
@@ -595,12 +606,11 @@ func TestEnableTask(t *testing.T) {
595606 task := s .tasks .Get (tsk .ID ())
596607 task .Spin ()
597608
598- // wait for the end of determined window
599- time .Sleep (startWait + windowSize )
600- // wait an interval to be sure that the task state has been updated
601- /// we are ok to extend sleeping by 100ms to allow to complete post-schedule activities
602- time .Sleep (interval + time .Millisecond * 100 )
603-
609+ // wait for task ended event (or timeout)
610+ select {
611+ case <- lse .Ended :
612+ case <- time .After (stop .Add (1 * time .Second ).Sub (start )):
613+ }
604614 // check if the task is ended
605615 So (tsk .State (), ShouldEqual , core .TaskEnded )
606616
0 commit comments