Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
unix: update timer if already active
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bnoordhuis committed May 26, 2012
1 parent ae9d4c2 commit 028fef8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/unix/timer.c
Expand Up @@ -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;

Expand Down
2 changes: 2 additions & 0 deletions test/test-list.h
Expand Up @@ -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)
Expand Down Expand Up @@ -266,6 +267,7 @@ TASK_LIST_START

TEST_ENTRY (timer)
TEST_ENTRY (timer_again)
TEST_ENTRY (timer_start_twice)

TEST_ENTRY (idle_starvation)

Expand Down
19 changes: 19 additions & 0 deletions test/test-timer.c
Expand Up @@ -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;
}

0 comments on commit 028fef8

Please sign in to comment.