Skip to content

Commit

Permalink
test/reactor: add tests for early timers
Browse files Browse the repository at this point in the history
Ensure that reactor timers fire at or after the
scheduled timeout.
  • Loading branch information
garlick committed Aug 23, 2016
1 parent 3690517 commit 67fbace
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/common/libflux/test/reactor.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ static void oneshot (flux_reactor_t *r, flux_watcher_t *w,
static void test_timer (flux_reactor_t *reactor)
{
flux_watcher_t *w;
double t0, t[] = { 0.001, 0.010, 0.100, 1.000 };
int i, rc;

errno = 0;
ok (!flux_timer_watcher_create (reactor, -1, 0, oneshot, NULL)
Expand All @@ -237,10 +239,12 @@ static void test_timer (flux_reactor_t *reactor)
ok ((w = flux_timer_watcher_create (reactor, 0, 0, oneshot, NULL)) != NULL,
"timer: creating zero timeout works");
flux_watcher_start (w);
t0 = flux_reactor_now (reactor);
ok (flux_reactor_run (reactor, 0) == 0,
"timer: reactor ran to completion (single oneshot)");
ok (oneshot_ran == true,
"timer: oneshot was executed");
"timer: oneshot was executed after %.3fs",
flux_reactor_now (reactor) - t0);
oneshot_ran = false;
ok (flux_reactor_run (reactor, 0) == 0,
"timer: reactor ran to completion (expired oneshot)");
Expand All @@ -255,16 +259,32 @@ static void test_timer (flux_reactor_t *reactor)
flux_watcher_stop (w);
flux_watcher_destroy (w);

ok ((w = flux_timer_watcher_create (reactor, 0.01, 0.01, repeat, NULL))
ok ((w = flux_timer_watcher_create (reactor, 0.001, 0.001, repeat, NULL))
!= NULL,
"timer: creating 1ms timeout with 1ms repeat works");
flux_watcher_start (w);
t0 = flux_reactor_now (reactor);
ok (flux_reactor_run (reactor, 0) == 0,
"timer: reactor ran to completion (single repeat)");
ok (repeat_countdown == 0,
"timer: repeat timer stopped itself after countdown");
flux_watcher_stop (w);
flux_watcher_destroy (w);

oneshot_errno = 0;
ok ((w = flux_timer_watcher_create (reactor, 0, 0, oneshot, NULL)) != NULL,
"timer: creating timer watcher works");
for (i = 0; i < sizeof (t) / sizeof (t[0]); i++) {
flux_timer_watcher_reset (w, t[i], 0);
flux_watcher_start (w);
t0 = flux_reactor_now (reactor);
oneshot_ran = false;
rc = flux_reactor_run (reactor, 0);
double elapsed = flux_reactor_now (reactor) - t0;
ok (rc == 0 && oneshot_ran && elapsed >= t[i],
"timer: reactor ran %.3fs oneshot at >= time (%.3fs)", t[i], elapsed);
}

}


Expand Down

0 comments on commit 67fbace

Please sign in to comment.