• 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 twice_cb_called = 0;
29 static int twice_close_cb_called = 0;
30 static int repeat_cb_called = 0;
31 static int repeat_close_cb_called = 0;
32 static int order_cb_called = 0;
33 static int timer_check_double_call_called = 0;
34 static int zero_timeout_cb_calls = 0;
35 static uint64_t start_time;
36 static uv_timer_t tiny_timer;
37 static uv_timer_t huge_timer1;
38 static uv_timer_t huge_timer2;
39 
40 
once_close_cb(uv_handle_t * handle)41 static void once_close_cb(uv_handle_t* handle) {
42   printf("ONCE_CLOSE_CB\n");
43 
44   ASSERT_NOT_NULL(handle);
45   ASSERT_OK(uv_is_active(handle));
46 
47   once_close_cb_called++;
48 }
49 
50 
once_cb(uv_timer_t * handle)51 static void once_cb(uv_timer_t* handle) {
52   printf("ONCE_CB %d\n", once_cb_called);
53 
54   ASSERT_NOT_NULL(handle);
55   ASSERT_OK(uv_is_active((uv_handle_t*) handle));
56 
57   once_cb_called++;
58 
59   uv_close((uv_handle_t*)handle, once_close_cb);
60 
61   /* Just call this randomly for the code coverage. */
62   uv_update_time(uv_default_loop());
63 }
64 
twice_close_cb(uv_handle_t * handle)65 static void twice_close_cb(uv_handle_t* handle) {
66   printf("TWICE_CLOSE_CB\n");
67 
68   ASSERT_NOT_NULL(handle);
69   ASSERT_OK(uv_is_active(handle));
70 
71   twice_close_cb_called++;
72 }
73 
twice_cb(uv_timer_t * handle)74 static void twice_cb(uv_timer_t* handle) {
75   printf("TWICE_CB %d\n", twice_cb_called);
76 
77   ASSERT_NOT_NULL(handle);
78   ASSERT_OK(uv_is_active((uv_handle_t*) handle));
79 
80   twice_cb_called++;
81 
82   uv_close((uv_handle_t*)handle, twice_close_cb);
83 }
84 
85 
86 
repeat_close_cb(uv_handle_t * handle)87 static void repeat_close_cb(uv_handle_t* handle) {
88   printf("REPEAT_CLOSE_CB\n");
89 
90   ASSERT_NOT_NULL(handle);
91 
92   repeat_close_cb_called++;
93 }
94 
95 
repeat_cb(uv_timer_t * handle)96 static void repeat_cb(uv_timer_t* handle) {
97   printf("REPEAT_CB\n");
98 
99   ASSERT_NOT_NULL(handle);
100   ASSERT_EQ(1, uv_is_active((uv_handle_t*) handle));
101 
102   repeat_cb_called++;
103 
104   if (repeat_cb_called == 5) {
105     uv_close((uv_handle_t*)handle, repeat_close_cb);
106   }
107 }
108 
109 
never_cb(uv_timer_t * handle)110 static void never_cb(uv_timer_t* handle) {
111   FATAL("never_cb should never be called");
112 }
113 
114 
TEST_IMPL(timer)115 TEST_IMPL(timer) {
116   uv_timer_t once_timers[10];
117   uv_timer_t *once;
118   uv_timer_t repeat, never;
119   unsigned int i;
120   int r;
121 
122   start_time = uv_now(uv_default_loop());
123   ASSERT_LT(0, start_time);
124 
125   /* Let 10 timers time out in 500 ms total. */
126   for (i = 0; i < ARRAY_SIZE(once_timers); i++) {
127     once = once_timers + i;
128     r = uv_timer_init(uv_default_loop(), once);
129     ASSERT_OK(r);
130     r = uv_timer_start(once, once_cb, i * 50, 0);
131     ASSERT_OK(r);
132   }
133 
134   /* The 11th timer is a repeating timer that runs 4 times */
135   r = uv_timer_init(uv_default_loop(), &repeat);
136   ASSERT_OK(r);
137   r = uv_timer_start(&repeat, repeat_cb, 100, 100);
138   ASSERT_OK(r);
139 
140   /* The 12th timer should not do anything. */
141   r = uv_timer_init(uv_default_loop(), &never);
142   ASSERT_OK(r);
143   r = uv_timer_start(&never, never_cb, 100, 100);
144   ASSERT_OK(r);
145   r = uv_timer_stop(&never);
146   ASSERT_OK(r);
147   uv_unref((uv_handle_t*)&never);
148 
149   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
150 
151   ASSERT_EQ(10, once_cb_called);
152   ASSERT_EQ(10, once_close_cb_called);
153   printf("repeat_cb_called %d\n", repeat_cb_called);
154   ASSERT_EQ(5, repeat_cb_called);
155   ASSERT_EQ(1, repeat_close_cb_called);
156 
157   ASSERT_LE(500, uv_now(uv_default_loop()) - start_time);
158 
159   MAKE_VALGRIND_HAPPY(uv_default_loop());
160   return 0;
161 }
162 
163 
TEST_IMPL(timer_start_twice)164 TEST_IMPL(timer_start_twice) {
165   uv_timer_t once;
166   int r;
167 
168   r = uv_timer_init(uv_default_loop(), &once);
169   ASSERT_OK(r);
170   r = uv_timer_start(&once, never_cb, 86400 * 1000, 0);
171   ASSERT_OK(r);
172   r = uv_timer_start(&once, twice_cb, 10, 0);
173   ASSERT_OK(r);
174   r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
175   ASSERT_OK(r);
176 
177   ASSERT_EQ(1, twice_cb_called);
178 
179   MAKE_VALGRIND_HAPPY(uv_default_loop());
180   return 0;
181 }
182 
183 
TEST_IMPL(timer_init)184 TEST_IMPL(timer_init) {
185   uv_timer_t handle;
186 
187   ASSERT_OK(uv_timer_init(uv_default_loop(), &handle));
188   ASSERT_OK(uv_timer_get_repeat(&handle));
189   ASSERT_UINT64_LE(0, uv_timer_get_due_in(&handle));
190   ASSERT_OK(uv_is_active((uv_handle_t*) &handle));
191 
192   MAKE_VALGRIND_HAPPY(uv_default_loop());
193   return 0;
194 }
195 
196 
order_cb_a(uv_timer_t * handle)197 static void order_cb_a(uv_timer_t *handle) {
198   ASSERT_EQ(order_cb_called++, *(int*)handle->data);
199 }
200 
201 
order_cb_b(uv_timer_t * handle)202 static void order_cb_b(uv_timer_t *handle) {
203   ASSERT_EQ(order_cb_called++, *(int*)handle->data);
204 }
205 
206 
TEST_IMPL(timer_order)207 TEST_IMPL(timer_order) {
208   int first;
209   int second;
210   uv_timer_t handle_a;
211   uv_timer_t handle_b;
212 
213   first = 0;
214   second = 1;
215   ASSERT_OK(uv_timer_init(uv_default_loop(), &handle_a));
216   ASSERT_OK(uv_timer_init(uv_default_loop(), &handle_b));
217 
218   /* Test for starting handle_a then handle_b */
219   handle_a.data = &first;
220   ASSERT_OK(uv_timer_start(&handle_a, order_cb_a, 0, 0));
221   handle_b.data = &second;
222   ASSERT_OK(uv_timer_start(&handle_b, order_cb_b, 0, 0));
223   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
224 
225   ASSERT_EQ(2, order_cb_called);
226 
227   ASSERT_OK(uv_timer_stop(&handle_a));
228   ASSERT_OK(uv_timer_stop(&handle_b));
229 
230   /* Test for starting handle_b then handle_a */
231   order_cb_called = 0;
232   handle_b.data = &first;
233   ASSERT_OK(uv_timer_start(&handle_b, order_cb_b, 0, 0));
234 
235   handle_a.data = &second;
236   ASSERT_OK(uv_timer_start(&handle_a, order_cb_a, 0, 0));
237   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
238 
239   ASSERT_EQ(2, order_cb_called);
240 
241   MAKE_VALGRIND_HAPPY(uv_default_loop());
242   return 0;
243 }
244 
245 
zero_timeout_cb(uv_timer_t * handle)246 static void zero_timeout_cb(uv_timer_t* handle) {
247   //ASSERT_OK(uv_timer_start(handle, zero_timeout_cb, 0, 0));
248   ASSERT_OK(uv_timer_start(handle, zero_timeout_cb, 1, 0)); // modify for ohos
249   uv_stop(handle->loop);
250   zero_timeout_cb_calls++;
251 }
252 
253 
TEST_IMPL(timer_zero_timeout)254 TEST_IMPL(timer_zero_timeout) {
255   uv_timer_t timer;
256   uv_loop_t* loop;
257 
258   loop = uv_default_loop();
259   ASSERT_OK(uv_timer_init(loop, &timer));
260   ASSERT_OK(uv_timer_start(&timer, zero_timeout_cb, 0, 0));
261   ASSERT_EQ(1, uv_run(loop, UV_RUN_DEFAULT));  /* because of uv_stop() */
262   ASSERT_EQ(1, zero_timeout_cb_calls);
263   uv_close((uv_handle_t*) &timer, NULL);
264   ASSERT_OK(uv_run(loop, UV_RUN_DEFAULT));
265   ASSERT_EQ(1, zero_timeout_cb_calls);
266 
267   MAKE_VALGRIND_HAPPY(loop);
268   return 0;
269 }
270 
271 
tiny_timer_cb(uv_timer_t * handle)272 static void tiny_timer_cb(uv_timer_t* handle) {
273   ASSERT_PTR_EQ(handle, &tiny_timer);
274   uv_close((uv_handle_t*) &tiny_timer, NULL);
275   uv_close((uv_handle_t*) &huge_timer1, NULL);
276   uv_close((uv_handle_t*) &huge_timer2, NULL);
277 }
278 
279 
TEST_IMPL(timer_huge_timeout)280 TEST_IMPL(timer_huge_timeout) {
281   ASSERT_OK(uv_timer_init(uv_default_loop(), &tiny_timer));
282   ASSERT_OK(uv_timer_init(uv_default_loop(), &huge_timer1));
283   ASSERT_OK(uv_timer_init(uv_default_loop(), &huge_timer2));
284   ASSERT_OK(uv_timer_start(&tiny_timer, tiny_timer_cb, 1, 0));
285   ASSERT_OK(uv_timer_start(&huge_timer1,
286                            tiny_timer_cb,
287                            0xffffffffffffLL,
288                            0));
289   ASSERT_OK(uv_timer_start(&huge_timer2, tiny_timer_cb, (uint64_t) -1, 0));
290   ASSERT_UINT64_EQ(1, uv_timer_get_due_in(&tiny_timer));
291   ASSERT_UINT64_EQ(281474976710655, uv_timer_get_due_in(&huge_timer1));
292   ASSERT_UINT64_LE(0, uv_timer_get_due_in(&huge_timer2));
293   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
294   MAKE_VALGRIND_HAPPY(uv_default_loop());
295   return 0;
296 }
297 
298 
huge_repeat_cb(uv_timer_t * handle)299 static void huge_repeat_cb(uv_timer_t* handle) {
300   static int ncalls;
301 
302   if (ncalls == 0)
303     ASSERT_PTR_EQ(handle, &huge_timer1);
304   else
305     ASSERT_PTR_EQ(handle, &tiny_timer);
306 
307   if (++ncalls == 10) {
308     uv_close((uv_handle_t*) &tiny_timer, NULL);
309     uv_close((uv_handle_t*) &huge_timer1, NULL);
310   }
311 }
312 
313 
TEST_IMPL(timer_huge_repeat)314 TEST_IMPL(timer_huge_repeat) {
315   ASSERT_OK(uv_timer_init(uv_default_loop(), &tiny_timer));
316   ASSERT_OK(uv_timer_init(uv_default_loop(), &huge_timer1));
317   ASSERT_OK(uv_timer_start(&tiny_timer, huge_repeat_cb, 2, 2));
318   ASSERT_OK(uv_timer_start(&huge_timer1, huge_repeat_cb, 1, (uint64_t) -1));
319   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
320   MAKE_VALGRIND_HAPPY(uv_default_loop());
321   return 0;
322 }
323 
324 
325 static unsigned int timer_run_once_timer_cb_called;
326 
327 
timer_run_once_timer_cb(uv_timer_t * handle)328 static void timer_run_once_timer_cb(uv_timer_t* handle) {
329   timer_run_once_timer_cb_called++;
330 }
331 
332 
TEST_IMPL(timer_run_once)333 TEST_IMPL(timer_run_once) {
334   uv_timer_t timer_handle;
335 
336   ASSERT_OK(uv_timer_init(uv_default_loop(), &timer_handle));
337   ASSERT_OK(uv_timer_start(&timer_handle, timer_run_once_timer_cb, 0, 0));
338   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_ONCE));
339   ASSERT_EQ(1, timer_run_once_timer_cb_called);
340 
341   ASSERT_OK(uv_timer_start(&timer_handle, timer_run_once_timer_cb, 1, 0));
342   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_ONCE));
343   ASSERT_EQ(2, timer_run_once_timer_cb_called);
344 
345   uv_close((uv_handle_t*) &timer_handle, NULL);
346   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_ONCE));
347 
348   MAKE_VALGRIND_HAPPY(uv_default_loop());
349   return 0;
350 }
351 
352 
TEST_IMPL(timer_is_closing)353 TEST_IMPL(timer_is_closing) {
354   uv_timer_t handle;
355 
356   ASSERT_OK(uv_timer_init(uv_default_loop(), &handle));
357   uv_close((uv_handle_t *)&handle, NULL);
358 
359   ASSERT_EQ(UV_EINVAL, uv_timer_start(&handle, never_cb, 100, 100));
360 
361   MAKE_VALGRIND_HAPPY(uv_default_loop());
362   return 0;
363 }
364 
365 
TEST_IMPL(timer_null_callback)366 TEST_IMPL(timer_null_callback) {
367   uv_timer_t handle;
368 
369   ASSERT_OK(uv_timer_init(uv_default_loop(), &handle));
370   ASSERT_EQ(UV_EINVAL, uv_timer_start(&handle, NULL, 100, 100));
371 
372   MAKE_VALGRIND_HAPPY(uv_default_loop());
373   return 0;
374 }
375 
376 
377 static uint64_t timer_early_check_expected_time;
378 
379 
timer_early_check_cb(uv_timer_t * handle)380 static void timer_early_check_cb(uv_timer_t* handle) {
381   uint64_t hrtime = uv_hrtime() / 1000000;
382   ASSERT_GE(hrtime, timer_early_check_expected_time);
383 }
384 
385 
TEST_IMPL(timer_early_check)386 TEST_IMPL(timer_early_check) {
387   uv_timer_t timer_handle;
388   const uint64_t timeout_ms = 10;
389 
390   timer_early_check_expected_time = uv_now(uv_default_loop()) + timeout_ms;
391 
392   ASSERT_OK(uv_timer_init(uv_default_loop(), &timer_handle));
393   ASSERT_OK(uv_timer_start(&timer_handle,
394                            timer_early_check_cb,
395                            timeout_ms,
396                            0));
397   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
398 
399   uv_close((uv_handle_t*) &timer_handle, NULL);
400   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
401 
402   MAKE_VALGRIND_HAPPY(uv_default_loop());
403   return 0;
404 }
405 
timer_check_double_call(uv_timer_t * handle)406 static void timer_check_double_call(uv_timer_t* handle) {
407   timer_check_double_call_called++;
408 }
409 
TEST_IMPL(timer_no_double_call_once)410 TEST_IMPL(timer_no_double_call_once) {
411   uv_timer_t timer_handle;
412   const uint64_t timeout_ms = 10;
413 
414   ASSERT_OK(uv_timer_init(uv_default_loop(), &timer_handle));
415   ASSERT_OK(uv_timer_start(&timer_handle,
416                            timer_check_double_call,
417                            timeout_ms,
418                            timeout_ms));
419   uv_sleep(timeout_ms * 2);
420   ASSERT_EQ(1, uv_run(uv_default_loop(), UV_RUN_ONCE));
421   //ASSERT_EQ(1, timer_check_double_call_called);
422   ASSERT_EQ(2, timer_check_double_call_called); // modify for ohos
423 
424   MAKE_VALGRIND_HAPPY(uv_default_loop());
425   return 0;
426 }
427 
TEST_IMPL(timer_no_double_call_nowait)428 TEST_IMPL(timer_no_double_call_nowait) {
429   uv_timer_t timer_handle;
430   const uint64_t timeout_ms = 10;
431 
432   ASSERT_OK(uv_timer_init(uv_default_loop(), &timer_handle));
433   ASSERT_OK(uv_timer_start(&timer_handle,
434                            timer_check_double_call,
435                            timeout_ms,
436                            timeout_ms));
437   uv_sleep(timeout_ms * 2);
438   ASSERT_EQ(1, uv_run(uv_default_loop(), UV_RUN_NOWAIT));
439   ASSERT_EQ(1, timer_check_double_call_called);
440 
441   MAKE_VALGRIND_HAPPY(uv_default_loop());
442   return 0;
443 }
444 
TEST_IMPL(timer_no_run_on_unref)445 TEST_IMPL(timer_no_run_on_unref) {
446   uv_timer_t timer_handle;
447 
448   ASSERT_OK(uv_timer_init(uv_default_loop(), &timer_handle));
449   ASSERT_OK(uv_timer_start(&timer_handle, (uv_timer_cb) abort, 0, 0));
450   uv_unref((uv_handle_t*) &timer_handle);
451   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
452 
453   MAKE_VALGRIND_HAPPY(uv_default_loop());
454   return 0;
455 }
456