From 5841852703c02e46d7220f1eb8d89bb8414d7cf3 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 26 Jun 2013 13:02:39 +0200 Subject: [PATCH] test: add 'start timer from check handle' test Check that a timer that is started from a check handle gets picked up correctly, i.e. that it influences the timeout used in the next tick of the event loop. --- build.mk | 1 + test/test-list.h | 2 + test/test-timer-from-check.c | 80 ++++++++++++++++++++++++++++++++++++ uv.gyp | 1 + 4 files changed, 84 insertions(+) create mode 100644 test/test-timer-from-check.c diff --git a/build.mk b/build.mk index 5ff713e0..7d8a3be7 100644 --- a/build.mk +++ b/build.mk @@ -130,6 +130,7 @@ TESTS= \ test/test-threadpool.o \ test/test-threadpool-cancel.o \ test/test-timer-again.o \ + test/test-timer-from-check.o \ test/test-timer.o \ test/test-tty.o \ test/test-udp-dgram-too-big.o \ diff --git a/test/test-list.h b/test/test-list.h index 4c015858..7a9b1a29 100644 --- a/test/test-list.h +++ b/test/test-list.h @@ -101,6 +101,7 @@ TEST_DECLARE (timer_start_twice) TEST_DECLARE (timer_order) TEST_DECLARE (timer_huge_timeout) TEST_DECLARE (timer_huge_repeat) +TEST_DECLARE (timer_from_check) TEST_DECLARE (idle_starvation) TEST_DECLARE (loop_handles) TEST_DECLARE (get_loadavg) @@ -348,6 +349,7 @@ TASK_LIST_START TEST_ENTRY (timer_order) TEST_ENTRY (timer_huge_timeout) TEST_ENTRY (timer_huge_repeat) + TEST_ENTRY (timer_from_check) TEST_ENTRY (idle_starvation) diff --git a/test/test-timer-from-check.c b/test/test-timer-from-check.c new file mode 100644 index 00000000..2aa3fe41 --- /dev/null +++ b/test/test-timer-from-check.c @@ -0,0 +1,80 @@ +/* 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 uv_prepare_t prepare_handle; +static uv_check_t check_handle; +static uv_timer_t timer_handle; + +static int prepare_cb_called; +static int check_cb_called; +static int timer_cb_called; + + +static void prepare_cb(uv_prepare_t* handle, int status) { + ASSERT(0 == uv_prepare_stop(&prepare_handle)); + ASSERT(0 == prepare_cb_called); + ASSERT(1 == check_cb_called); + ASSERT(0 == timer_cb_called); + prepare_cb_called++; +} + + +static void timer_cb(uv_timer_t* handle, int status) { + ASSERT(0 == uv_timer_stop(&timer_handle)); + ASSERT(1 == prepare_cb_called); + ASSERT(1 == check_cb_called); + ASSERT(0 == timer_cb_called); + timer_cb_called++; +} + + +static void check_cb(uv_check_t* handle, int status) { + ASSERT(0 == uv_check_stop(&check_handle)); + ASSERT(0 == uv_timer_stop(&timer_handle)); /* Runs before timer_cb. */ + ASSERT(0 == uv_timer_start(&timer_handle, timer_cb, 50, 0)); + ASSERT(0 == uv_prepare_start(&prepare_handle, prepare_cb)); + ASSERT(0 == prepare_cb_called); + ASSERT(0 == check_cb_called); + ASSERT(0 == timer_cb_called); + check_cb_called++; +} + + +TEST_IMPL(timer_from_check) { + ASSERT(0 == uv_prepare_init(uv_default_loop(), &prepare_handle)); + ASSERT(0 == uv_check_init(uv_default_loop(), &check_handle)); + ASSERT(0 == uv_check_start(&check_handle, check_cb)); + ASSERT(0 == uv_timer_init(uv_default_loop(), &timer_handle)); + ASSERT(0 == uv_timer_start(&timer_handle, timer_cb, 50, 0)); + ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT)); + ASSERT(1 == prepare_cb_called); + ASSERT(1 == check_cb_called); + ASSERT(1 == timer_cb_called); + uv_close((uv_handle_t*) &prepare_handle, NULL); + uv_close((uv_handle_t*) &check_handle, NULL); + uv_close((uv_handle_t*) &timer_handle, NULL); + ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_ONCE)); + MAKE_VALGRIND_HAPPY(); + return 0; +} diff --git a/uv.gyp b/uv.gyp index 54c771ee..f61ebb53 100644 --- a/uv.gyp +++ b/uv.gyp @@ -353,6 +353,7 @@ 'test/test-barrier.c', 'test/test-condvar.c', 'test/test-timer-again.c', + 'test/test-timer-from-check.c', 'test/test-timer.c', 'test/test-tty.c', 'test/test-udp-dgram-too-big.c',