From 505a7214a864e937268af12a25e0cc2088c9845a Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Tue, 14 Nov 2017 08:32:18 +0100 Subject: [PATCH] tests: Add basic error handling to callbacks, for reference Many users look at both examples *and* unit tests to learn how to use a library. We should pride ourselves in that we have not just working but also correct and readable tests. Signed-off-by: Joachim Nilsson --- tests/active.c | 5 ++++- tests/complete.c | 20 +++++++++++++++++--- tests/cronrun.c | 5 ++++- tests/timer.c | 5 ++++- 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/tests/active.c b/tests/active.c index dabcc1e..63c9033 100644 --- a/tests/active.c +++ b/tests/active.c @@ -12,8 +12,11 @@ uev_t timer3; uev_t cron1; uev_t cron2; -static void cb(uev_t *w, void *UNUSED(arg), int UNUSED(events)) +static void cb(uev_t *w, void *UNUSED(arg), int events) { + if (UEV_ERROR == events) + fprintf(stderr, "timer watcher failed, ignoring ...\n"); + fail_unless(w == &timer1); fail_unless(uev_timer_active(&timer2)); diff --git a/tests/complete.c b/tests/complete.c index fdb0e83..c1234c0 100644 --- a/tests/complete.c +++ b/tests/complete.c @@ -33,15 +33,21 @@ static int in, out; static int period = 0; static uev_t *watchdog; -static void lifetime_cb(uev_t *w, void *arg, int UNUSED(events)) +static void lifetime_cb(uev_t *w, void *arg, int events) { + if (UEV_ERROR == events) + fprintf(stderr, "timer watcher failed, ignoring ...\n"); + fprintf(stderr, "\nLifetime exceeded, program completed successfully! (arg:%p)\n", arg); uev_exit(w->ctx); } /* The pipe watchdog, if it triggers we haven't received data in time. */ -static void timeout_cb(uev_t *w, void *arg, int UNUSED(events)) +static void timeout_cb(uev_t *w, void *arg, int events) { + if (UEV_ERROR == events) + fprintf(stderr, "timer watcher failed, ignoring ...\n"); + watchdog = NULL; fprintf(stderr, "\nTimeout exceeded %p\n", arg); @@ -54,8 +60,13 @@ static void periodic_task(uev_t UNUSED(*w), void UNUSED(*arg), int UNUSED(events fprintf(stderr, "|"); } -static void signal_cb(uev_t *w, void *UNUSED(arg), int UNUSED(events)) +static void signal_cb(uev_t *w, void *UNUSED(arg), int events) { + if (UEV_ERROR == events) { + fprintf(stderr, "Signal watcher failed, unrecoverable error.\n"); + return; + } + fprintf(stderr, w->signo == SIGINT ? "^Cv" : "^\v"); } @@ -78,6 +89,9 @@ static void pipe_write_cb(uev_t *w, void *arg, int UNUSED(events)) my_t *my = arg; char *msg = "TESTING"; + if (UEV_ERROR == events) + fprintf(stderr, "timer watcher failed, ignoring ...\n"); + if (write(out, msg, my->counter) < 0) { perror("\nFailed writing to pipe"); return; diff --git a/tests/cronrun.c b/tests/cronrun.c index b6e951f..66e3d3c 100644 --- a/tests/cronrun.c +++ b/tests/cronrun.c @@ -30,11 +30,14 @@ struct timeval tv; -static void cron_job(uev_t *w, void *UNUSED(arg), int UNUSED(events)) +static void cron_job(uev_t *w, void *UNUSED(arg), int events) { static int laps = 3; struct timeval now; + if (UEV_ERROR == events) + fprintf(stderr, "cron watcher failed, ignoring ...\n"); + /* Update timer every lap */ tv.tv_sec += TIMEOUT; diff --git a/tests/timer.c b/tests/timer.c index e643b85..79d06cc 100644 --- a/tests/timer.c +++ b/tests/timer.c @@ -5,10 +5,13 @@ #define TIMEOUT 2 /* 2 sec */ struct timeval start; -static void cb(uev_t *w, void *UNUSED(arg), int UNUSED(events)) +static void cb(uev_t *w, void *UNUSED(arg), int events) { struct timeval now; + if (UEV_ERROR == events) + fprintf(stderr, "timer watcher failed, ignoring ...\n"); + gettimeofday(&now, NULL); fail_unless(now.tv_sec == start.tv_sec + TIMEOUT);