From 028fef84b8244501f644732b83a2faaef3600820 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sat, 26 May 2012 02:09:35 +0200 Subject: [PATCH] unix: update timer if already active uv_timer_start() no longer returns an error when the timer is already active, now it just updates the timer. Consistent with the uv-win implementation. Fixes #425. --- src/unix/timer.c | 11 ++++++----- test/test-list.h | 2 ++ test/test-timer.c | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/unix/timer.c b/src/unix/timer.c index 8463088a..e50a35ab 100644 --- a/src/unix/timer.c +++ b/src/unix/timer.c @@ -52,11 +52,12 @@ int uv_timer_init(uv_loop_t* loop, uv_timer_t* timer) { } -int uv_timer_start(uv_timer_t* timer, uv_timer_cb cb, int64_t timeout, - int64_t repeat) { - if (uv__is_active(timer)) { - return -1; - } +int uv_timer_start(uv_timer_t* timer, + uv_timer_cb cb, + int64_t timeout, + int64_t repeat) { + if (uv__is_active(timer)) + uv_timer_stop(timer); timer->timer_cb = cb; diff --git a/test/test-list.h b/test/test-list.h index 2466b570..63683f28 100644 --- a/test/test-list.h +++ b/test/test-list.h @@ -77,6 +77,7 @@ TEST_DECLARE (callback_stack) TEST_DECLARE (error_message) TEST_DECLARE (timer) TEST_DECLARE (timer_again) +TEST_DECLARE (timer_start_twice) TEST_DECLARE (idle_starvation) TEST_DECLARE (loop_handles) TEST_DECLARE (get_loadavg) @@ -266,6 +267,7 @@ TASK_LIST_START TEST_ENTRY (timer) TEST_ENTRY (timer_again) + TEST_ENTRY (timer_start_twice) TEST_ENTRY (idle_starvation) diff --git a/test/test-timer.c b/test/test-timer.c index 3436cec5..c1b629b2 100644 --- a/test/test-timer.c +++ b/test/test-timer.c @@ -131,3 +131,22 @@ TEST_IMPL(timer) { return 0; } + + +TEST_IMPL(timer_start_twice) { + uv_timer_t once; + int r; + + r = uv_timer_init(uv_default_loop(), &once); + ASSERT(r == 0); + r = uv_timer_start(&once, never_cb, 86400 * 1000, 0); + ASSERT(r == 0); + r = uv_timer_start(&once, once_cb, 10, 0); + ASSERT(r == 0); + r = uv_run(uv_default_loop()); + ASSERT(r == 0); + + ASSERT(once_cb_called == 1); + + return 0; +}