From aec5eac8bc63a8fecab651aa04dbfa0d5d899230 Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Fri, 15 Apr 2011 19:32:28 +0200 Subject: [PATCH] Add oio_timeout test --- Makefile | 3 +- test/test-fail-always.c | 1 + test/test-list.h | 5 +++- test/test-ping-pong.c | 2 +- test/test-timeout.c | 63 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 test/test-timeout.c diff --git a/Makefile b/Makefile index 8036b919..1ac988fb 100644 --- a/Makefile +++ b/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 diff --git a/test/test-fail-always.c b/test/test-fail-always.c index c19c491b..02a4758a 100644 --- a/test/test-fail-always.c +++ b/test/test-fail-always.c @@ -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; } \ No newline at end of file diff --git a/test/test-list.h b/test/test-list.h index a2cb092c..9a81910c 100644 --- a/test/test-list.h +++ b/test/test-list.h @@ -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) diff --git a/test/test-ping-pong.c b/test/test-ping-pong.c index 71501b8c..c8d08a7e 100644 --- a/test/test-ping-pong.c +++ b/test/test-ping-pong.c @@ -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); diff --git a/test/test-timeout.c b/test/test-timeout.c new file mode 100644 index 00000000..7a3b71fb --- /dev/null +++ b/test/test-timeout.c @@ -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; +} \ No newline at end of file