mirror of
https://github.com/troglobit/libuev.git
synced 2026-11-01 12:20:30 +00:00
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 <troglobit@gmail.com>
This commit is contained in:
1 parent
9df7cbfc52
commit
505a7214a8
4 files changed
+29
-6
No files matched your search
+4
-1
@@ -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));
|
||||
|
||||
+17
-3
@@ -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;
|
||||
|
||||
+4
-1
@@ -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;
|
||||
|
||||
|
||||
+4
-1
@@ -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);
|
||||
|
||||
|
||||
Reference in new issue
Block a user