|
Packit |
b5b901 |
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
|
Packit |
b5b901 |
*
|
|
Packit |
b5b901 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
Packit |
b5b901 |
* of this software and associated documentation files (the "Software"), to
|
|
Packit |
b5b901 |
* deal in the Software without restriction, including without limitation the
|
|
Packit |
b5b901 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
Packit |
b5b901 |
* sell copies of the Software, and to permit persons to whom the Software is
|
|
Packit |
b5b901 |
* furnished to do so, subject to the following conditions:
|
|
Packit |
b5b901 |
*
|
|
Packit |
b5b901 |
* The above copyright notice and this permission notice shall be included in
|
|
Packit |
b5b901 |
* all copies or substantial portions of the Software.
|
|
Packit |
b5b901 |
*
|
|
Packit |
b5b901 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
Packit |
b5b901 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
Packit |
b5b901 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
Packit |
b5b901 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
Packit |
b5b901 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
Packit |
b5b901 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
Packit |
b5b901 |
* IN THE SOFTWARE.
|
|
Packit |
b5b901 |
*/
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
#include "uv.h"
|
|
Packit |
b5b901 |
#include "task.h"
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
static int once_cb_called = 0;
|
|
Packit |
b5b901 |
static int once_close_cb_called = 0;
|
|
Packit |
b5b901 |
static int repeat_cb_called = 0;
|
|
Packit |
b5b901 |
static int repeat_close_cb_called = 0;
|
|
Packit |
b5b901 |
static int order_cb_called = 0;
|
|
Packit |
b5b901 |
static uint64_t start_time;
|
|
Packit |
b5b901 |
static uv_timer_t tiny_timer;
|
|
Packit |
b5b901 |
static uv_timer_t huge_timer1;
|
|
Packit |
b5b901 |
static uv_timer_t huge_timer2;
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
static void once_close_cb(uv_handle_t* handle) {
|
|
Packit |
b5b901 |
printf("ONCE_CLOSE_CB\n");
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
ASSERT(handle != NULL);
|
|
Packit |
b5b901 |
ASSERT(0 == uv_is_active(handle));
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
once_close_cb_called++;
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
static void once_cb(uv_timer_t* handle) {
|
|
Packit |
b5b901 |
printf("ONCE_CB %d\n", once_cb_called);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
ASSERT(handle != NULL);
|
|
Packit |
b5b901 |
ASSERT(0 == uv_is_active((uv_handle_t*) handle));
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
once_cb_called++;
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
uv_close((uv_handle_t*)handle, once_close_cb);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
/* Just call this randomly for the code coverage. */
|
|
Packit |
b5b901 |
uv_update_time(uv_default_loop());
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
static void repeat_close_cb(uv_handle_t* handle) {
|
|
Packit |
b5b901 |
printf("REPEAT_CLOSE_CB\n");
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
ASSERT(handle != NULL);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
repeat_close_cb_called++;
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
static void repeat_cb(uv_timer_t* handle) {
|
|
Packit |
b5b901 |
printf("REPEAT_CB\n");
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
ASSERT(handle != NULL);
|
|
Packit |
b5b901 |
ASSERT(1 == uv_is_active((uv_handle_t*) handle));
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
repeat_cb_called++;
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
if (repeat_cb_called == 5) {
|
|
Packit |
b5b901 |
uv_close((uv_handle_t*)handle, repeat_close_cb);
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
static void never_cb(uv_timer_t* handle) {
|
|
Packit |
b5b901 |
FATAL("never_cb should never be called");
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
TEST_IMPL(timer) {
|
|
Packit |
b5b901 |
uv_timer_t once_timers[10];
|
|
Packit |
b5b901 |
uv_timer_t *once;
|
|
Packit |
b5b901 |
uv_timer_t repeat, never;
|
|
Packit |
b5b901 |
unsigned int i;
|
|
Packit |
b5b901 |
int r;
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
start_time = uv_now(uv_default_loop());
|
|
Packit |
b5b901 |
ASSERT(0 < start_time);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
/* Let 10 timers time out in 500 ms total. */
|
|
Packit |
b5b901 |
for (i = 0; i < ARRAY_SIZE(once_timers); i++) {
|
|
Packit |
b5b901 |
once = once_timers + i;
|
|
Packit |
b5b901 |
r = uv_timer_init(uv_default_loop(), once);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
r = uv_timer_start(once, once_cb, i * 50, 0);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
/* The 11th timer is a repeating timer that runs 4 times */
|
|
Packit |
b5b901 |
r = uv_timer_init(uv_default_loop(), &repeat);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
r = uv_timer_start(&repeat, repeat_cb, 100, 100);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
/* The 12th timer should not do anything. */
|
|
Packit |
b5b901 |
r = uv_timer_init(uv_default_loop(), &never);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
r = uv_timer_start(&never, never_cb, 100, 100);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
r = uv_timer_stop(&never);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
uv_unref((uv_handle_t*)&never);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
ASSERT(once_cb_called == 10);
|
|
Packit |
b5b901 |
ASSERT(once_close_cb_called == 10);
|
|
Packit |
b5b901 |
printf("repeat_cb_called %d\n", repeat_cb_called);
|
|
Packit |
b5b901 |
ASSERT(repeat_cb_called == 5);
|
|
Packit |
b5b901 |
ASSERT(repeat_close_cb_called == 1);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
ASSERT(500 <= uv_now(uv_default_loop()) - start_time);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
MAKE_VALGRIND_HAPPY();
|
|
Packit |
b5b901 |
return 0;
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
TEST_IMPL(timer_start_twice) {
|
|
Packit |
b5b901 |
uv_timer_t once;
|
|
Packit |
b5b901 |
int r;
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
r = uv_timer_init(uv_default_loop(), &once);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
r = uv_timer_start(&once, never_cb, 86400 * 1000, 0);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
r = uv_timer_start(&once, once_cb, 10, 0);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
ASSERT(once_cb_called == 1);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
MAKE_VALGRIND_HAPPY();
|
|
Packit |
b5b901 |
return 0;
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
TEST_IMPL(timer_init) {
|
|
Packit |
b5b901 |
uv_timer_t handle;
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
ASSERT(0 == uv_timer_init(uv_default_loop(), &handle));
|
|
Packit |
b5b901 |
ASSERT(0 == uv_timer_get_repeat(&handle));
|
|
Packit |
b5b901 |
ASSERT(0 == uv_is_active((uv_handle_t*) &handle));
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
MAKE_VALGRIND_HAPPY();
|
|
Packit |
b5b901 |
return 0;
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
static void order_cb_a(uv_timer_t *handle) {
|
|
Packit |
b5b901 |
ASSERT(order_cb_called++ == *(int*)handle->data);
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
static void order_cb_b(uv_timer_t *handle) {
|
|
Packit |
b5b901 |
ASSERT(order_cb_called++ == *(int*)handle->data);
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
TEST_IMPL(timer_order) {
|
|
Packit |
b5b901 |
int first;
|
|
Packit |
b5b901 |
int second;
|
|
Packit |
b5b901 |
uv_timer_t handle_a;
|
|
Packit |
b5b901 |
uv_timer_t handle_b;
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
first = 0;
|
|
Packit |
b5b901 |
second = 1;
|
|
Packit |
b5b901 |
ASSERT(0 == uv_timer_init(uv_default_loop(), &handle_a));
|
|
Packit |
b5b901 |
ASSERT(0 == uv_timer_init(uv_default_loop(), &handle_b));
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
/* Test for starting handle_a then handle_b */
|
|
Packit |
b5b901 |
handle_a.data = &first;
|
|
Packit |
b5b901 |
ASSERT(0 == uv_timer_start(&handle_a, order_cb_a, 0, 0));
|
|
Packit |
b5b901 |
handle_b.data = &second;
|
|
Packit |
b5b901 |
ASSERT(0 == uv_timer_start(&handle_b, order_cb_b, 0, 0));
|
|
Packit |
b5b901 |
ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
ASSERT(order_cb_called == 2);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
ASSERT(0 == uv_timer_stop(&handle_a));
|
|
Packit |
b5b901 |
ASSERT(0 == uv_timer_stop(&handle_b));
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
/* Test for starting handle_b then handle_a */
|
|
Packit |
b5b901 |
order_cb_called = 0;
|
|
Packit |
b5b901 |
handle_b.data = &first;
|
|
Packit |
b5b901 |
ASSERT(0 == uv_timer_start(&handle_b, order_cb_b, 0, 0));
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
handle_a.data = &second;
|
|
Packit |
b5b901 |
ASSERT(0 == uv_timer_start(&handle_a, order_cb_a, 0, 0));
|
|
Packit |
b5b901 |
ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
ASSERT(order_cb_called == 2);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
MAKE_VALGRIND_HAPPY();
|
|
Packit |
b5b901 |
return 0;
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
static void tiny_timer_cb(uv_timer_t* handle) {
|
|
Packit |
b5b901 |
ASSERT(handle == &tiny_timer);
|
|
Packit |
b5b901 |
uv_close((uv_handle_t*) &tiny_timer, NULL);
|
|
Packit |
b5b901 |
uv_close((uv_handle_t*) &huge_timer1, NULL);
|
|
Packit |
b5b901 |
uv_close((uv_handle_t*) &huge_timer2, NULL);
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
TEST_IMPL(timer_huge_timeout) {
|
|
Packit |
b5b901 |
ASSERT(0 == uv_timer_init(uv_default_loop(), &tiny_timer));
|
|
Packit |
b5b901 |
ASSERT(0 == uv_timer_init(uv_default_loop(), &huge_timer1));
|
|
Packit |
b5b901 |
ASSERT(0 == uv_timer_init(uv_default_loop(), &huge_timer2));
|
|
Packit |
b5b901 |
ASSERT(0 == uv_timer_start(&tiny_timer, tiny_timer_cb, 1, 0));
|
|
Packit |
b5b901 |
ASSERT(0 == uv_timer_start(&huge_timer1, tiny_timer_cb, 0xffffffffffffLL, 0));
|
|
Packit |
b5b901 |
ASSERT(0 == uv_timer_start(&huge_timer2, tiny_timer_cb, (uint64_t) -1, 0));
|
|
Packit |
b5b901 |
ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
|
|
Packit |
b5b901 |
MAKE_VALGRIND_HAPPY();
|
|
Packit |
b5b901 |
return 0;
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
static void huge_repeat_cb(uv_timer_t* handle) {
|
|
Packit |
b5b901 |
static int ncalls;
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
if (ncalls == 0)
|
|
Packit |
b5b901 |
ASSERT(handle == &huge_timer1);
|
|
Packit |
b5b901 |
else
|
|
Packit |
b5b901 |
ASSERT(handle == &tiny_timer);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
if (++ncalls == 10) {
|
|
Packit |
b5b901 |
uv_close((uv_handle_t*) &tiny_timer, NULL);
|
|
Packit |
b5b901 |
uv_close((uv_handle_t*) &huge_timer1, NULL);
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
TEST_IMPL(timer_huge_repeat) {
|
|
Packit |
b5b901 |
ASSERT(0 == uv_timer_init(uv_default_loop(), &tiny_timer));
|
|
Packit |
b5b901 |
ASSERT(0 == uv_timer_init(uv_default_loop(), &huge_timer1));
|
|
Packit |
b5b901 |
ASSERT(0 == uv_timer_start(&tiny_timer, huge_repeat_cb, 2, 2));
|
|
Packit |
b5b901 |
ASSERT(0 == uv_timer_start(&huge_timer1, huge_repeat_cb, 1, (uint64_t) -1));
|
|
Packit |
b5b901 |
ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
|
|
Packit |
b5b901 |
MAKE_VALGRIND_HAPPY();
|
|
Packit |
b5b901 |
return 0;
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
static unsigned int timer_run_once_timer_cb_called;
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
static void timer_run_once_timer_cb(uv_timer_t* handle) {
|
|
Packit |
b5b901 |
timer_run_once_timer_cb_called++;
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
TEST_IMPL(timer_run_once) {
|
|
Packit |
b5b901 |
uv_timer_t timer_handle;
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
ASSERT(0 == uv_timer_init(uv_default_loop(), &timer_handle));
|
|
Packit |
b5b901 |
ASSERT(0 == uv_timer_start(&timer_handle, timer_run_once_timer_cb, 0, 0));
|
|
Packit |
b5b901 |
ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_ONCE));
|
|
Packit |
b5b901 |
ASSERT(1 == timer_run_once_timer_cb_called);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
ASSERT(0 == uv_timer_start(&timer_handle, timer_run_once_timer_cb, 1, 0));
|
|
Packit |
b5b901 |
ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_ONCE));
|
|
Packit |
b5b901 |
ASSERT(2 == timer_run_once_timer_cb_called);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
uv_close((uv_handle_t*) &timer_handle, NULL);
|
|
Packit |
b5b901 |
ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_ONCE));
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
MAKE_VALGRIND_HAPPY();
|
|
Packit |
b5b901 |
return 0;
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
TEST_IMPL(timer_null_callback) {
|
|
Packit |
b5b901 |
uv_timer_t handle;
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
ASSERT(0 == uv_timer_init(uv_default_loop(), &handle));
|
|
Packit |
b5b901 |
ASSERT(UV_EINVAL == uv_timer_start(&handle, NULL, 100, 100));
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
MAKE_VALGRIND_HAPPY();
|
|
Packit |
b5b901 |
return 0;
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
static uint64_t timer_early_check_expected_time;
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
static void timer_early_check_cb(uv_timer_t* handle) {
|
|
Packit |
b5b901 |
uint64_t hrtime = uv_hrtime() / 1000000;
|
|
Packit |
b5b901 |
ASSERT(hrtime >= timer_early_check_expected_time);
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
TEST_IMPL(timer_early_check) {
|
|
Packit |
b5b901 |
uv_timer_t timer_handle;
|
|
Packit |
b5b901 |
const uint64_t timeout_ms = 10;
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
timer_early_check_expected_time = uv_now(uv_default_loop()) + timeout_ms;
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
ASSERT(0 == uv_timer_init(uv_default_loop(), &timer_handle));
|
|
Packit |
b5b901 |
ASSERT(0 == uv_timer_start(&timer_handle, timer_early_check_cb, timeout_ms, 0));
|
|
Packit |
b5b901 |
ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
uv_close((uv_handle_t*) &timer_handle, NULL);
|
|
Packit |
b5b901 |
ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
MAKE_VALGRIND_HAPPY();
|
|
Packit |
b5b901 |
return 0;
|
|
Packit |
b5b901 |
}
|