libuv-svace-build
453 строки · 12.0 Кб
1/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2*
3* Permission is hereby granted, free of charge, to any person obtaining a copy
4* of this software and associated documentation files (the "Software"), to
5* deal in the Software without restriction, including without limitation the
6* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7* sell copies of the Software, and to permit persons to whom the Software is
8* furnished to do so, subject to the following conditions:
9*
10* The above copyright notice and this permission notice shall be included in
11* all copies or substantial portions of the Software.
12*
13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19* IN THE SOFTWARE.
20*/
21
22#include "uv.h"23#include "task.h"24
25
26static int once_cb_called = 0;27static int once_close_cb_called = 0;28static int twice_cb_called = 0;29static int twice_close_cb_called = 0;30static int repeat_cb_called = 0;31static int repeat_close_cb_called = 0;32static int order_cb_called = 0;33static int timer_check_double_call_called = 0;34static int zero_timeout_cb_calls = 0;35static uint64_t start_time;36static uv_timer_t tiny_timer;37static uv_timer_t huge_timer1;38static uv_timer_t huge_timer2;39
40
41static void once_close_cb(uv_handle_t* handle) {42printf("ONCE_CLOSE_CB\n");43
44ASSERT_NOT_NULL(handle);45ASSERT_OK(uv_is_active(handle));46
47once_close_cb_called++;48}
49
50
51static void once_cb(uv_timer_t* handle) {52printf("ONCE_CB %d\n", once_cb_called);53
54ASSERT_NOT_NULL(handle);55ASSERT_OK(uv_is_active((uv_handle_t*) handle));56
57once_cb_called++;58
59uv_close((uv_handle_t*)handle, once_close_cb);60
61/* Just call this randomly for the code coverage. */62uv_update_time(uv_default_loop());63}
64
65static void twice_close_cb(uv_handle_t* handle) {66printf("TWICE_CLOSE_CB\n");67
68ASSERT_NOT_NULL(handle);69ASSERT_OK(uv_is_active(handle));70
71twice_close_cb_called++;72}
73
74static void twice_cb(uv_timer_t* handle) {75printf("TWICE_CB %d\n", twice_cb_called);76
77ASSERT_NOT_NULL(handle);78ASSERT_OK(uv_is_active((uv_handle_t*) handle));79
80twice_cb_called++;81
82uv_close((uv_handle_t*)handle, twice_close_cb);83}
84
85
86
87static void repeat_close_cb(uv_handle_t* handle) {88printf("REPEAT_CLOSE_CB\n");89
90ASSERT_NOT_NULL(handle);91
92repeat_close_cb_called++;93}
94
95
96static void repeat_cb(uv_timer_t* handle) {97printf("REPEAT_CB\n");98
99ASSERT_NOT_NULL(handle);100ASSERT_EQ(1, uv_is_active((uv_handle_t*) handle));101
102repeat_cb_called++;103
104if (repeat_cb_called == 5) {105uv_close((uv_handle_t*)handle, repeat_close_cb);106}107}
108
109
110static void never_cb(uv_timer_t* handle) {111FATAL("never_cb should never be called");112}
113
114
115TEST_IMPL(timer) {116uv_timer_t once_timers[10];117uv_timer_t *once;118uv_timer_t repeat, never;119unsigned int i;120int r;121
122start_time = uv_now(uv_default_loop());123ASSERT_LT(0, start_time);124
125/* Let 10 timers time out in 500 ms total. */126for (i = 0; i < ARRAY_SIZE(once_timers); i++) {127once = once_timers + i;128r = uv_timer_init(uv_default_loop(), once);129ASSERT_OK(r);130r = uv_timer_start(once, once_cb, i * 50, 0);131ASSERT_OK(r);132}133
134/* The 11th timer is a repeating timer that runs 4 times */135r = uv_timer_init(uv_default_loop(), &repeat);136ASSERT_OK(r);137r = uv_timer_start(&repeat, repeat_cb, 100, 100);138ASSERT_OK(r);139
140/* The 12th timer should not do anything. */141r = uv_timer_init(uv_default_loop(), &never);142ASSERT_OK(r);143r = uv_timer_start(&never, never_cb, 100, 100);144ASSERT_OK(r);145r = uv_timer_stop(&never);146ASSERT_OK(r);147uv_unref((uv_handle_t*)&never);148
149uv_run(uv_default_loop(), UV_RUN_DEFAULT);150
151ASSERT_EQ(10, once_cb_called);152ASSERT_EQ(10, once_close_cb_called);153printf("repeat_cb_called %d\n", repeat_cb_called);154ASSERT_EQ(5, repeat_cb_called);155ASSERT_EQ(1, repeat_close_cb_called);156
157ASSERT_LE(500, uv_now(uv_default_loop()) - start_time);158
159MAKE_VALGRIND_HAPPY(uv_default_loop());160return 0;161}
162
163
164TEST_IMPL(timer_start_twice) {165uv_timer_t once;166int r;167
168r = uv_timer_init(uv_default_loop(), &once);169ASSERT_OK(r);170r = uv_timer_start(&once, never_cb, 86400 * 1000, 0);171ASSERT_OK(r);172r = uv_timer_start(&once, twice_cb, 10, 0);173ASSERT_OK(r);174r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);175ASSERT_OK(r);176
177ASSERT_EQ(1, twice_cb_called);178
179MAKE_VALGRIND_HAPPY(uv_default_loop());180return 0;181}
182
183
184TEST_IMPL(timer_init) {185uv_timer_t handle;186
187ASSERT_OK(uv_timer_init(uv_default_loop(), &handle));188ASSERT_OK(uv_timer_get_repeat(&handle));189ASSERT_UINT64_LE(0, uv_timer_get_due_in(&handle));190ASSERT_OK(uv_is_active((uv_handle_t*) &handle));191
192MAKE_VALGRIND_HAPPY(uv_default_loop());193return 0;194}
195
196
197static void order_cb_a(uv_timer_t *handle) {198ASSERT_EQ(order_cb_called++, *(int*)handle->data);199}
200
201
202static void order_cb_b(uv_timer_t *handle) {203ASSERT_EQ(order_cb_called++, *(int*)handle->data);204}
205
206
207TEST_IMPL(timer_order) {208int first;209int second;210uv_timer_t handle_a;211uv_timer_t handle_b;212
213first = 0;214second = 1;215ASSERT_OK(uv_timer_init(uv_default_loop(), &handle_a));216ASSERT_OK(uv_timer_init(uv_default_loop(), &handle_b));217
218/* Test for starting handle_a then handle_b */219handle_a.data = &first;220ASSERT_OK(uv_timer_start(&handle_a, order_cb_a, 0, 0));221handle_b.data = &second;222ASSERT_OK(uv_timer_start(&handle_b, order_cb_b, 0, 0));223ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));224
225ASSERT_EQ(2, order_cb_called);226
227ASSERT_OK(uv_timer_stop(&handle_a));228ASSERT_OK(uv_timer_stop(&handle_b));229
230/* Test for starting handle_b then handle_a */231order_cb_called = 0;232handle_b.data = &first;233ASSERT_OK(uv_timer_start(&handle_b, order_cb_b, 0, 0));234
235handle_a.data = &second;236ASSERT_OK(uv_timer_start(&handle_a, order_cb_a, 0, 0));237ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));238
239ASSERT_EQ(2, order_cb_called);240
241MAKE_VALGRIND_HAPPY(uv_default_loop());242return 0;243}
244
245
246static void zero_timeout_cb(uv_timer_t* handle) {247ASSERT_OK(uv_timer_start(handle, zero_timeout_cb, 0, 0));248uv_stop(handle->loop);249zero_timeout_cb_calls++;250}
251
252
253TEST_IMPL(timer_zero_timeout) {254uv_timer_t timer;255uv_loop_t* loop;256
257loop = uv_default_loop();258ASSERT_OK(uv_timer_init(loop, &timer));259ASSERT_OK(uv_timer_start(&timer, zero_timeout_cb, 0, 0));260ASSERT_EQ(1, uv_run(loop, UV_RUN_DEFAULT)); /* because of uv_stop() */261ASSERT_EQ(1, zero_timeout_cb_calls);262uv_close((uv_handle_t*) &timer, NULL);263ASSERT_OK(uv_run(loop, UV_RUN_DEFAULT));264ASSERT_EQ(1, zero_timeout_cb_calls);265
266MAKE_VALGRIND_HAPPY(loop);267return 0;268}
269
270
271static void tiny_timer_cb(uv_timer_t* handle) {272ASSERT_PTR_EQ(handle, &tiny_timer);273uv_close((uv_handle_t*) &tiny_timer, NULL);274uv_close((uv_handle_t*) &huge_timer1, NULL);275uv_close((uv_handle_t*) &huge_timer2, NULL);276}
277
278
279TEST_IMPL(timer_huge_timeout) {280ASSERT_OK(uv_timer_init(uv_default_loop(), &tiny_timer));281ASSERT_OK(uv_timer_init(uv_default_loop(), &huge_timer1));282ASSERT_OK(uv_timer_init(uv_default_loop(), &huge_timer2));283ASSERT_OK(uv_timer_start(&tiny_timer, tiny_timer_cb, 1, 0));284ASSERT_OK(uv_timer_start(&huge_timer1,285tiny_timer_cb,2860xffffffffffffLL,2870));288ASSERT_OK(uv_timer_start(&huge_timer2, tiny_timer_cb, (uint64_t) -1, 0));289ASSERT_UINT64_EQ(1, uv_timer_get_due_in(&tiny_timer));290ASSERT_UINT64_EQ(281474976710655, uv_timer_get_due_in(&huge_timer1));291ASSERT_UINT64_LE(0, uv_timer_get_due_in(&huge_timer2));292ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));293MAKE_VALGRIND_HAPPY(uv_default_loop());294return 0;295}
296
297
298static void huge_repeat_cb(uv_timer_t* handle) {299static int ncalls;300
301if (ncalls == 0)302ASSERT_PTR_EQ(handle, &huge_timer1);303else304ASSERT_PTR_EQ(handle, &tiny_timer);305
306if (++ncalls == 10) {307uv_close((uv_handle_t*) &tiny_timer, NULL);308uv_close((uv_handle_t*) &huge_timer1, NULL);309}310}
311
312
313TEST_IMPL(timer_huge_repeat) {314ASSERT_OK(uv_timer_init(uv_default_loop(), &tiny_timer));315ASSERT_OK(uv_timer_init(uv_default_loop(), &huge_timer1));316ASSERT_OK(uv_timer_start(&tiny_timer, huge_repeat_cb, 2, 2));317ASSERT_OK(uv_timer_start(&huge_timer1, huge_repeat_cb, 1, (uint64_t) -1));318ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));319MAKE_VALGRIND_HAPPY(uv_default_loop());320return 0;321}
322
323
324static unsigned int timer_run_once_timer_cb_called;325
326
327static void timer_run_once_timer_cb(uv_timer_t* handle) {328timer_run_once_timer_cb_called++;329}
330
331
332TEST_IMPL(timer_run_once) {333uv_timer_t timer_handle;334
335ASSERT_OK(uv_timer_init(uv_default_loop(), &timer_handle));336ASSERT_OK(uv_timer_start(&timer_handle, timer_run_once_timer_cb, 0, 0));337ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_ONCE));338ASSERT_EQ(1, timer_run_once_timer_cb_called);339
340ASSERT_OK(uv_timer_start(&timer_handle, timer_run_once_timer_cb, 1, 0));341ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_ONCE));342ASSERT_EQ(2, timer_run_once_timer_cb_called);343
344uv_close((uv_handle_t*) &timer_handle, NULL);345ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_ONCE));346
347MAKE_VALGRIND_HAPPY(uv_default_loop());348return 0;349}
350
351
352TEST_IMPL(timer_is_closing) {353uv_timer_t handle;354
355ASSERT_OK(uv_timer_init(uv_default_loop(), &handle));356uv_close((uv_handle_t *)&handle, NULL);357
358ASSERT_EQ(UV_EINVAL, uv_timer_start(&handle, never_cb, 100, 100));359
360MAKE_VALGRIND_HAPPY(uv_default_loop());361return 0;362}
363
364
365TEST_IMPL(timer_null_callback) {366uv_timer_t handle;367
368ASSERT_OK(uv_timer_init(uv_default_loop(), &handle));369ASSERT_EQ(UV_EINVAL, uv_timer_start(&handle, NULL, 100, 100));370
371MAKE_VALGRIND_HAPPY(uv_default_loop());372return 0;373}
374
375
376static uint64_t timer_early_check_expected_time;377
378
379static void timer_early_check_cb(uv_timer_t* handle) {380uint64_t hrtime = uv_hrtime() / 1000000;381ASSERT_GE(hrtime, timer_early_check_expected_time);382}
383
384
385TEST_IMPL(timer_early_check) {386uv_timer_t timer_handle;387const uint64_t timeout_ms = 10;388
389timer_early_check_expected_time = uv_now(uv_default_loop()) + timeout_ms;390
391ASSERT_OK(uv_timer_init(uv_default_loop(), &timer_handle));392ASSERT_OK(uv_timer_start(&timer_handle,393timer_early_check_cb,394timeout_ms,3950));396ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));397
398uv_close((uv_handle_t*) &timer_handle, NULL);399ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));400
401MAKE_VALGRIND_HAPPY(uv_default_loop());402return 0;403}
404
405static void timer_check_double_call(uv_timer_t* handle) {406timer_check_double_call_called++;407}
408
409TEST_IMPL(timer_no_double_call_once) {410uv_timer_t timer_handle;411const uint64_t timeout_ms = 10;412
413ASSERT_OK(uv_timer_init(uv_default_loop(), &timer_handle));414ASSERT_OK(uv_timer_start(&timer_handle,415timer_check_double_call,416timeout_ms,417timeout_ms));418uv_sleep(timeout_ms * 2);419ASSERT_EQ(1, uv_run(uv_default_loop(), UV_RUN_ONCE));420ASSERT_EQ(1, timer_check_double_call_called);421
422MAKE_VALGRIND_HAPPY(uv_default_loop());423return 0;424}
425
426TEST_IMPL(timer_no_double_call_nowait) {427uv_timer_t timer_handle;428const uint64_t timeout_ms = 10;429
430ASSERT_OK(uv_timer_init(uv_default_loop(), &timer_handle));431ASSERT_OK(uv_timer_start(&timer_handle,432timer_check_double_call,433timeout_ms,434timeout_ms));435uv_sleep(timeout_ms * 2);436ASSERT_EQ(1, uv_run(uv_default_loop(), UV_RUN_NOWAIT));437ASSERT_EQ(1, timer_check_double_call_called);438
439MAKE_VALGRIND_HAPPY(uv_default_loop());440return 0;441}
442
443TEST_IMPL(timer_no_run_on_unref) {444uv_timer_t timer_handle;445
446ASSERT_OK(uv_timer_init(uv_default_loop(), &timer_handle));447ASSERT_OK(uv_timer_start(&timer_handle, (uv_timer_cb) abort, 0, 0));448uv_unref((uv_handle_t*) &timer_handle);449ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));450
451MAKE_VALGRIND_HAPPY(uv_default_loop());452return 0;453}
454