Add oio_timeout test
This commit is contained in:
parent
a2c24c67d6
commit
aec5eac8bc
3
Makefile
3
Makefile
@ -4,7 +4,8 @@ TESTS=test/echo-server.c \
|
||||
test/test-pass-always.c \
|
||||
test/test-fail-always.c \
|
||||
test/test-ping-pong.c \
|
||||
test/test-callback-stack.c
|
||||
test/test-callback-stack.c \
|
||||
test/test-timeout.c
|
||||
|
||||
test/test-runner: test/*.h test/test-runner.c test/test-runner-unix.c $(TESTS) oio.a
|
||||
$(CC) -ansi -g -lm -o test/test-runner test/test-runner.c test/test-runner-unix.c $(TESTS) oio.a
|
||||
|
||||
@ -3,4 +3,5 @@
|
||||
TEST_IMPL(fail_always) {
|
||||
/* This test always fails. It is used to test the test runner. */
|
||||
FATAL("Yes, it always fails")
|
||||
return 2;
|
||||
}
|
||||
@ -1,8 +1,9 @@
|
||||
TEST_DECLARE (echo_server)
|
||||
TEST_DECLARE (ping_pong)
|
||||
TEST_DECLARE (close_cb_stack);
|
||||
TEST_DECLARE (pass_always)
|
||||
TEST_DECLARE (timeout);
|
||||
TEST_DECLARE (fail_always)
|
||||
TEST_DECLARE (pass_always)
|
||||
|
||||
TEST_LIST_START
|
||||
TEST_ENTRY (ping_pong)
|
||||
@ -10,6 +11,8 @@ TEST_LIST_START
|
||||
|
||||
TEST_ENTRY (close_cb_stack)
|
||||
|
||||
TEST_ENTRY (timeout)
|
||||
|
||||
TEST_ENTRY (fail_always)
|
||||
|
||||
TEST_ENTRY (pass_always)
|
||||
|
||||
@ -45,7 +45,7 @@ void pinger_after_write(oio_req *req) {
|
||||
|
||||
void pinger_write_ping(pinger_t* pinger) {
|
||||
oio_req *req;
|
||||
|
||||
|
||||
req = (oio_req*)malloc(sizeof(*req));
|
||||
oio_req_init(req, &pinger->handle, pinger_after_write);
|
||||
|
||||
|
||||
63
test/test-timeout.c
Normal file
63
test/test-timeout.c
Normal file
@ -0,0 +1,63 @@
|
||||
#include "../oio.h"
|
||||
#include "test.h"
|
||||
|
||||
|
||||
int expected = 0;
|
||||
int timeouts = 0;
|
||||
|
||||
|
||||
void timeout_cb(oio_req *req) {
|
||||
ASSERT(req != NULL);
|
||||
free(req);
|
||||
timeouts++;
|
||||
}
|
||||
|
||||
void exit_timeout_cb(oio_req *req) {
|
||||
ASSERT(req != NULL);
|
||||
free(req);
|
||||
ASSERT(timeouts == expected);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void dummy_timeout_cb(oio_req *req) {
|
||||
/* Should never be called */
|
||||
FATAL(dummy_timer_cb should never be called)
|
||||
}
|
||||
|
||||
|
||||
TEST_IMPL(timeout) {
|
||||
oio_req *req;
|
||||
oio_req exit_req;
|
||||
oio_req dummy_req;
|
||||
int i;
|
||||
|
||||
oio_init();
|
||||
|
||||
/* Let 10 timers time out it 500 ms. */
|
||||
for (i = 0; i < 10; i++) {
|
||||
req = (oio_req*)malloc(sizeof(*req));
|
||||
ASSERT(req != NULL)
|
||||
|
||||
oio_req_init(req, NULL, timeout_cb);
|
||||
|
||||
if (oio_timeout(req, i * 50) < 1)
|
||||
FATAL(oio_timeout failed)
|
||||
|
||||
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)
|
||||
|
||||
/* 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)
|
||||
|
||||
oio_run();
|
||||
|
||||
FATAL(should never get here)
|
||||
return 2;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user