From 6a19fa7e93bba7db9b266ebc95774cad09499d46 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Tue, 22 Nov 2016 01:27:49 +0100 Subject: [PATCH] cronrun: Update cron test to verify interval as well Signed-off-by: Joachim Nilsson --- tests/cronrun.c | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/tests/cronrun.c b/tests/cronrun.c index a9281ff..89efebb 100644 --- a/tests/cronrun.c +++ b/tests/cronrun.c @@ -21,20 +21,29 @@ * THE SOFTWARE. */ -#include #include #include -#include "uev.h" +#include "check.h" -#define UNUSED(arg) arg __attribute__ ((unused)) +#define TIMEOUT 2 +#define INTERVAL 2 +struct timeval tv; -static void cron_job(uev_t *w, void *arg, int events) +static void cron_job(uev_t *w, void *UNUSED(arg), int UNUSED(events)) { - struct timeval tv; + static int laps = 3; + struct timeval now; - gettimeofday(&tv, NULL); - printf("Cron job HELO %s", ctime(&tv.tv_sec)); + /* Update timer every lap */ + tv.tv_sec += TIMEOUT; + + gettimeofday(&now, NULL); + printf("Cron job HELO %s", ctime(&now.tv_sec)); + fail_unless(now.tv_sec == tv.tv_sec); + + if (!laps--) + uev_exit(w->ctx); } int main(void) @@ -42,20 +51,16 @@ int main(void) uev_t cron_watcher; uev_ctx_t ctx; time_t when, interval; - struct timeval tv; - /* Initialize libuEv */ uev_init(&ctx); - /* Cron job */ gettimeofday(&tv, NULL); - when = tv.tv_sec + 30; - interval = 30; - uev_cron_init(&ctx, &cron_watcher, cron_job, NULL, when, interval); - - /* Start event loop */ printf("Start of test %s", ctime(&tv.tv_sec)); printf("Expected cron %s", ctime(&when)); + + when = tv.tv_sec + TIMEOUT; + interval = INTERVAL; + uev_cron_init(&ctx, &cron_watcher, cron_job, NULL, when, interval); return uev_run(&ctx, 0); }