From 78c2930e252b2f38781a21dc47a606d0630366c4 Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Thu, 19 May 2011 01:58:07 +0200 Subject: [PATCH] Test for uv_timer_again ,~get_repeat, ~set_repeat --- msvs/libuv-test.vcxproj | 1 + test/test-list.h | 3 + test/test-timer-again.c | 143 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 test/test-timer-again.c diff --git a/msvs/libuv-test.vcxproj b/msvs/libuv-test.vcxproj index 14f25c07..7d1922a3 100644 --- a/msvs/libuv-test.vcxproj +++ b/msvs/libuv-test.vcxproj @@ -155,6 +155,7 @@ + diff --git a/test/test-list.h b/test/test-list.h index e6db49a5..fb85295b 100644 --- a/test/test-list.h +++ b/test/test-list.h @@ -30,6 +30,7 @@ TEST_DECLARE (bind_error_inval) TEST_DECLARE (connection_fail) TEST_DECLARE (callback_stack) TEST_DECLARE (timer) +TEST_DECLARE (timer_again) TEST_DECLARE (loop_handles) TEST_DECLARE (async) TEST_DECLARE (fail_always) @@ -62,6 +63,8 @@ TASK_LIST_START TEST_ENTRY (timer) + TEST_ENTRY (timer_again) + TEST_ENTRY (loop_handles) TEST_ENTRY (async) diff --git a/test/test-timer-again.c b/test/test-timer-again.c new file mode 100644 index 00000000..5c0aa5d7 --- /dev/null +++ b/test/test-timer-again.c @@ -0,0 +1,143 @@ +/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "../uv.h" +#include "task.h" + + +static int close_cb_called = 0; +static int repeat_1_cb_called = 0; +static int repeat_2_cb_called = 0; + +static int repeat_2_cb_allowed = 0; + +static uv_handle_t dummy, repeat_1, repeat_2; + +static int64_t start_time; + + +static void close_cb(uv_handle_t* handle, int status) { + ASSERT(handle != NULL); + ASSERT(status == 0); + + close_cb_called++; +} + + +static void repeat_1_cb(uv_handle_t* handle, int status) { + int r; + + ASSERT(handle == &repeat_1); + ASSERT(status == 0); + + ASSERT(uv_timer_get_repeat(handle) == 50); + + LOGF("repeat_1_cb called after %ld ms\n", uv_now() - start_time); + + repeat_1_cb_called++; + + r = uv_timer_again(&repeat_2); + ASSERT(r == 0); + + if (uv_now() >= start_time + 500) { + uv_close(handle); + /* We're not calling uv_timer_again on repeat_2 any more, so after this */ + /* timer_2_cb is expected. */ + repeat_2_cb_allowed = 1; + return; + } +} + + +static void repeat_2_cb(uv_handle_t* handle, int status) { + ASSERT(handle == &repeat_2); + ASSERT(status == 0); + + LOGF("repeat_2_cb called after %ld ms\n", uv_now() - start_time); + + repeat_2_cb_called++; + + if (!uv_is_active(handle)) { + ASSERT(uv_timer_get_repeat(handle) == 0); + uv_close(handle); + return; + } + + ASSERT(uv_timer_get_repeat(handle) == 100); + + /* This shouldn't take effect immediately. */ + uv_timer_set_repeat(&repeat_2, 0); +} + + +static uv_buf_t alloc_cb(uv_handle_t* handle, size_t size) { + uv_buf_t buf = {0, 0}; + FATAL("alloc should not be called"); + return buf; +} + + +TEST_IMPL(timer_again) { + int r; + + uv_init(alloc_cb); + + start_time = uv_now(); + ASSERT(0 < start_time); + + /* Verify that it is not possible to uv_timer_again a never-started timer. */ + r = uv_timer_init(&dummy, NULL, NULL); + ASSERT(r == 0); + r = uv_timer_again(&dummy); + ASSERT(r == -1); + ASSERT(uv_last_error().code == UV_EINVAL); + uv_unref(); + + /* Start timer repeat_1. */ + r = uv_timer_init(&repeat_1, close_cb, NULL); + ASSERT(r == 0); + r = uv_timer_start(&repeat_1, repeat_1_cb, 50, 0); + ASSERT(r == 0); + ASSERT(uv_timer_get_repeat(&repeat_1) == 0); + + /* Actually make repeat_1 repeating. */ + uv_timer_set_repeat(&repeat_1, 50); + ASSERT(uv_timer_get_repeat(&repeat_1) == 50); + + /* Start another repeating timer. It'll be again()ed by the repeat_1 so */ + /* it should not time out until repeat_1 stops. */ + r = uv_timer_init(&repeat_2, close_cb, NULL); + ASSERT(r == 0); + r = uv_timer_start(&repeat_2, repeat_2_cb, 100, 100); + ASSERT(r == 0); + ASSERT(uv_timer_get_repeat(&repeat_2) == 100); + + uv_run(); + + ASSERT(repeat_1_cb_called == 10); + ASSERT(repeat_2_cb_called == 2); + ASSERT(close_cb_called == 2); + + LOGF("Test took %ld ms (expected ~700 ms)\n", uv_now() - start_time); + ASSERT(700 <= uv_now() - start_time); + + return 0; +} \ No newline at end of file