• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
26 static int once_cb_called = 0;
27 static int once_close_cb_called = 0;
28 static int repeat_cb_called = 0;
29 static int repeat_close_cb_called = 0;
30 static int order_cb_called = 0;
31 static uint64_t start_time;
32 static uv_timer_t tiny_timer;
33 static uv_timer_t huge_timer1;
34 static uv_timer_t huge_timer2;
35 
36 
once_close_cb(uv_handle_t * handle)37 static void once_close_cb(uv_handle_t* handle) {
38   printf("ONCE_CLOSE_CB\n");
39 
40   ASSERT_NOT_NULL(handle);
41   ASSERT(0 == uv_is_active(handle));
42 
43   once_close_cb_called++;
44 }
45 
46 
once_cb(uv_timer_t * handle)47 static void once_cb(uv_timer_t* handle) {
48   printf("ONCE_CB %d\n", once_cb_called);
49 
50   ASSERT_NOT_NULL(handle);
51   ASSERT(0 == uv_is_active((uv_handle_t*) handle));
52 
53   once_cb_called++;
54 
55   uv_close((uv_handle_t*)handle, once_close_cb);
56 
57   /* Just call this randomly for the code coverage. */
58   uv_update_time(uv_default_loop());
59 }
60 
61 
repeat_close_cb(uv_handle_t * handle)62 static void repeat_close_cb(uv_handle_t* handle) {
63   printf("REPEAT_CLOSE_CB\n");
64 
65   ASSERT_NOT_NULL(handle);
66 
67   repeat_close_cb_called++;
68 }
69 
70 
repeat_cb(uv_timer_t * handle)71 static void repeat_cb(uv_timer_t* handle) {
72   printf("REPEAT_CB\n");
73 
74   ASSERT_NOT_NULL(handle);
75   ASSERT(1 == uv_is_active((uv_handle_t*) handle));
76 
77   repeat_cb_called++;
78 
79   if (repeat_cb_called == 5) {
80     uv_close((uv_handle_t*)handle, repeat_close_cb);
81   }
82 }
83 
84 
never_cb(uv_timer_t * handle)85 static void never_cb(uv_timer_t* handle) {
86   FATAL("never_cb should never be called");
87 }
88 
89 
TEST_IMPL(timer)90 TEST_IMPL(timer) {
91   uv_timer_t once_timers[10];
92   uv_timer_t *once;
93   uv_timer_t repeat, never;
94   unsigned int i;
95   int r;
96 
97   start_time = uv_now(uv_default_loop());
98   ASSERT(0 < start_time);
99 
100   /* Let 10 timers time out in 500 ms total. */
101   for (i = 0; i < ARRAY_SIZE(once_timers); i++) {
102     once = once_timers + i;
103     r = uv_timer_init(uv_default_loop(), once);
104     ASSERT(r == 0);
105     r = uv_timer_start(once, once_cb, i * 50, 0);
106     ASSERT(r == 0);
107   }
108 
109   /* The 11th timer is a repeating timer that runs 4 times */
110   r = uv_timer_init(uv_default_loop(), &repeat);
111   ASSERT(r == 0);
112   r = uv_timer_start(&repeat, repeat_cb, 100, 100);
113   ASSERT(r == 0);
114 
115   /* The 12th timer should not do anything. */
116   r = uv_timer_init(uv_default_loop(), &never);
117   ASSERT(r == 0);
118   r = uv_timer_start(&never, never_cb, 100, 100);
119   ASSERT(r == 0);
120   r = uv_timer_stop(&never);
121   ASSERT(r == 0);
122   uv_unref((uv_handle_t*)&never);
123 
124   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
125 
126   ASSERT(once_cb_called == 10);
127   ASSERT(once_close_cb_called == 10);
128   printf("repeat_cb_called %d\n", repeat_cb_called);
129   ASSERT(repeat_cb_called == 5);
130   ASSERT(repeat_close_cb_called == 1);
131 
132   ASSERT(500 <= uv_now(uv_default_loop()) - start_time);
133 
134   MAKE_VALGRIND_HAPPY();
135   return 0;
136 }
137 
138 
TEST_IMPL(timer_start_twice)139 TEST_IMPL(timer_start_twice) {
140   uv_timer_t once;
141   int r;
142 
143   r = uv_timer_init(uv_default_loop(), &once);
144   ASSERT(r == 0);
145   r = uv_timer_start(&once, never_cb, 86400 * 1000, 0);
146   ASSERT(r == 0);
147   r = uv_timer_start(&once, once_cb, 10, 0);
148   ASSERT(r == 0);
149   r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
150   ASSERT(r == 0);
151 
152   ASSERT(once_cb_called == 1);
153 
154   MAKE_VALGRIND_HAPPY();
155   return 0;
156 }
157 
158 
TEST_IMPL(timer_init)159 TEST_IMPL(timer_init) {
160   uv_timer_t handle;
161 
162   ASSERT(0 == uv_timer_init(uv_default_loop(), &handle));
163   ASSERT(0 == uv_timer_get_repeat(&handle));
164   ASSERT_UINT64_LE(0, uv_timer_get_due_in(&handle));
165   ASSERT(0 == uv_is_active((uv_handle_t*) &handle));
166 
167   MAKE_VALGRIND_HAPPY();
168   return 0;
169 }
170 
171 
order_cb_a(uv_timer_t * handle)172 static void order_cb_a(uv_timer_t *handle) {
173   ASSERT(order_cb_called++ == *(int*)handle->data);
174 }
175 
176 
order_cb_b(uv_timer_t * handle)177 static void order_cb_b(uv_timer_t *handle) {
178   ASSERT(order_cb_called++ == *(int*)handle->data);
179 }
180 
181 
TEST_IMPL(timer_order)182 TEST_IMPL(timer_order) {
183   int first;
184   int second;
185   uv_timer_t handle_a;
186   uv_timer_t handle_b;
187 
188   first = 0;
189   second = 1;
190   ASSERT(0 == uv_timer_init(uv_default_loop(), &handle_a));
191   ASSERT(0 == uv_timer_init(uv_default_loop(), &handle_b));
192 
193   /* Test for starting handle_a then handle_b */
194   handle_a.data = &first;
195   ASSERT(0 == uv_timer_start(&handle_a, order_cb_a, 0, 0));
196   handle_b.data = &second;
197   ASSERT(0 == uv_timer_start(&handle_b, order_cb_b, 0, 0));
198   ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
199 
200   ASSERT(order_cb_called == 2);
201 
202   ASSERT(0 == uv_timer_stop(&handle_a));
203   ASSERT(0 == uv_timer_stop(&handle_b));
204 
205   /* Test for starting handle_b then handle_a */
206   order_cb_called = 0;
207   handle_b.data = &first;
208   ASSERT(0 == uv_timer_start(&handle_b, order_cb_b, 0, 0));
209 
210   handle_a.data = &second;
211   ASSERT(0 == uv_timer_start(&handle_a, order_cb_a, 0, 0));
212   ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
213 
214   ASSERT(order_cb_called == 2);
215 
216   MAKE_VALGRIND_HAPPY();
217   return 0;
218 }
219 
220 
tiny_timer_cb(uv_timer_t * handle)221 static void tiny_timer_cb(uv_timer_t* handle) {
222   ASSERT(handle == &tiny_timer);
223   uv_close((uv_handle_t*) &tiny_timer, NULL);
224   uv_close((uv_handle_t*) &huge_timer1, NULL);
225   uv_close((uv_handle_t*) &huge_timer2, NULL);
226 }
227 
228 
TEST_IMPL(timer_huge_timeout)229 TEST_IMPL(timer_huge_timeout) {
230   ASSERT(0 == uv_timer_init(uv_default_loop(), &tiny_timer));
231   ASSERT(0 == uv_timer_init(uv_default_loop(), &huge_timer1));
232   ASSERT(0 == uv_timer_init(uv_default_loop(), &huge_timer2));
233   ASSERT(0 == uv_timer_start(&tiny_timer, tiny_timer_cb, 1, 0));
234   ASSERT(0 == uv_timer_start(&huge_timer1, tiny_timer_cb, 0xffffffffffffLL, 0));
235   ASSERT(0 == uv_timer_start(&huge_timer2, tiny_timer_cb, (uint64_t) -1, 0));
236   ASSERT_UINT64_EQ(1, uv_timer_get_due_in(&tiny_timer));
237   ASSERT_UINT64_EQ(281474976710655, uv_timer_get_due_in(&huge_timer1));
238   ASSERT_UINT64_LE(0, uv_timer_get_due_in(&huge_timer2));
239   ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
240   MAKE_VALGRIND_HAPPY();
241   return 0;
242 }
243 
244 
huge_repeat_cb(uv_timer_t * handle)245 static void huge_repeat_cb(uv_timer_t* handle) {
246   static int ncalls;
247 
248   if (ncalls == 0)
249     ASSERT(handle == &huge_timer1);
250   else
251     ASSERT(handle == &tiny_timer);
252 
253   if (++ncalls == 10) {
254     uv_close((uv_handle_t*) &tiny_timer, NULL);
255     uv_close((uv_handle_t*) &huge_timer1, NULL);
256   }
257 }
258 
259 
TEST_IMPL(timer_huge_repeat)260 TEST_IMPL(timer_huge_repeat) {
261   ASSERT(0 == uv_timer_init(uv_default_loop(), &tiny_timer));
262   ASSERT(0 == uv_timer_init(uv_default_loop(), &huge_timer1));
263   ASSERT(0 == uv_timer_start(&tiny_timer, huge_repeat_cb, 2, 2));
264   ASSERT(0 == uv_timer_start(&huge_timer1, huge_repeat_cb, 1, (uint64_t) -1));
265   ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
266   MAKE_VALGRIND_HAPPY();
267   return 0;
268 }
269 
270 
271 static unsigned int timer_run_once_timer_cb_called;
272 
273 
timer_run_once_timer_cb(uv_timer_t * handle)274 static void timer_run_once_timer_cb(uv_timer_t* handle) {
275   timer_run_once_timer_cb_called++;
276 }
277 
278 
TEST_IMPL(timer_run_once)279 TEST_IMPL(timer_run_once) {
280   uv_timer_t timer_handle;
281 
282   ASSERT(0 == uv_timer_init(uv_default_loop(), &timer_handle));
283   ASSERT(0 == uv_timer_start(&timer_handle, timer_run_once_timer_cb, 0, 0));
284   ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_ONCE));
285   ASSERT(1 == timer_run_once_timer_cb_called);
286 
287   ASSERT(0 == uv_timer_start(&timer_handle, timer_run_once_timer_cb, 1, 0));
288   ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_ONCE));
289   ASSERT(2 == timer_run_once_timer_cb_called);
290 
291   uv_close((uv_handle_t*) &timer_handle, NULL);
292   ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_ONCE));
293 
294   MAKE_VALGRIND_HAPPY();
295   return 0;
296 }
297 
298 
TEST_IMPL(timer_is_closing)299 TEST_IMPL(timer_is_closing) {
300   uv_timer_t handle;
301 
302   ASSERT(0 == uv_timer_init(uv_default_loop(), &handle));
303   uv_close((uv_handle_t *)&handle, NULL);
304 
305   ASSERT(UV_EINVAL == uv_timer_start(&handle, never_cb, 100, 100));
306 
307   MAKE_VALGRIND_HAPPY();
308   return 0;
309 }
310 
311 
TEST_IMPL(timer_null_callback)312 TEST_IMPL(timer_null_callback) {
313   uv_timer_t handle;
314 
315   ASSERT(0 == uv_timer_init(uv_default_loop(), &handle));
316   ASSERT(UV_EINVAL == uv_timer_start(&handle, NULL, 100, 100));
317 
318   MAKE_VALGRIND_HAPPY();
319   return 0;
320 }
321 
322 
323 static uint64_t timer_early_check_expected_time;
324 
325 
timer_early_check_cb(uv_timer_t * handle)326 static void timer_early_check_cb(uv_timer_t* handle) {
327   uint64_t hrtime = uv_hrtime() / 1000000;
328   ASSERT(hrtime >= timer_early_check_expected_time);
329 }
330 
331 
TEST_IMPL(timer_early_check)332 TEST_IMPL(timer_early_check) {
333   uv_timer_t timer_handle;
334   const uint64_t timeout_ms = 10;
335 
336   timer_early_check_expected_time = uv_now(uv_default_loop()) + timeout_ms;
337 
338   ASSERT(0 == uv_timer_init(uv_default_loop(), &timer_handle));
339   ASSERT(0 == uv_timer_start(&timer_handle, timer_early_check_cb, timeout_ms, 0));
340   ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
341 
342   uv_close((uv_handle_t*) &timer_handle, NULL);
343   ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
344 
345   MAKE_VALGRIND_HAPPY();
346   return 0;
347 }
348