libuv/test/test-timeout.c

75 lines
1.4 KiB
C
Raw Normal View History

2011-04-15 17:32:28 +00:00
#include "../oio.h"
#include "test.h"
2011-04-15 18:20:59 +00:00
static int expected = 0;
static int timeouts = 0;
2011-04-15 17:32:28 +00:00
2011-04-18 06:53:09 +00:00
static int start_time;
2011-04-15 17:32:28 +00:00
2011-04-15 18:20:59 +00:00
static void timeout_cb(oio_req *req) {
2011-04-15 17:32:28 +00:00
ASSERT(req != NULL);
free(req);
timeouts++;
2011-04-18 06:53:09 +00:00
/* Just call this randomly for the code coverage. */
oio_update_time();
2011-04-15 17:32:28 +00:00
}
2011-04-15 18:20:59 +00:00
static void exit_timeout_cb(oio_req *req) {
2011-04-18 06:53:09 +00:00
int64_t now = oio_now();
2011-04-15 17:32:28 +00:00
ASSERT(req != NULL);
ASSERT(timeouts == expected);
2011-04-18 06:53:09 +00:00
ASSERT(start_time < now);
2011-04-15 17:32:28 +00:00
exit(0);
}
2011-04-15 18:20:59 +00:00
static void dummy_timeout_cb(oio_req *req) {
2011-04-15 17:32:28 +00:00
/* Should never be called */
FATAL("dummy_timer_cb should never be called");
2011-04-15 17:32:28 +00:00
}
TEST_IMPL(timeout) {
oio_req *req;
oio_req exit_req;
oio_req dummy_req;
int i;
oio_init();
2011-04-18 06:53:09 +00:00
start_time = oio_now();
ASSERT(0 < start_time);
2011-04-15 17:43:28 +00:00
/* Let 10 timers time out in 500 ms total. */
2011-04-15 17:32:28 +00:00
for (i = 0; i < 10; i++) {
req = (oio_req*)malloc(sizeof(*req));
ASSERT(req != NULL);
2011-04-15 17:32:28 +00:00
oio_req_init(req, NULL, timeout_cb);
if (oio_timeout(req, i * 50) < 0) {
FATAL("oio_timeout failed");
}
2011-04-15 17:32:28 +00:00
expected++;
}
/* The 11th timer exits the test and runs after 1 s. */
oio_req_init(&exit_req, NULL, exit_timeout_cb);
if (oio_timeout(&exit_req, 1000) < 0) {
FATAL("oio_timeout failed");
}
2011-04-15 17:32:28 +00:00
/* The 12th timer should never run. */
oio_req_init(&dummy_req, NULL, dummy_timeout_cb);
if (oio_timeout(&dummy_req, 2000)) {
FATAL("oio_timeout failed");
}
2011-04-15 17:32:28 +00:00
oio_run();
FATAL("should never get here");
2011-04-15 17:32:28 +00:00
return 2;
2011-04-15 18:20:59 +00:00
}