xref: /libuv/test/test-timer-from-check.c (revision 011a1ac1)
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 static uv_prepare_t prepare_handle;
26 static uv_check_t check_handle;
27 static uv_timer_t timer_handle;
28 
29 static int prepare_cb_called;
30 static int check_cb_called;
31 static int timer_cb_called;
32 
33 
prepare_cb(uv_prepare_t * handle)34 static void prepare_cb(uv_prepare_t* handle) {
35   ASSERT_OK(uv_prepare_stop(&prepare_handle));
36   ASSERT_OK(prepare_cb_called);
37   ASSERT_EQ(1, check_cb_called);
38   ASSERT_OK(timer_cb_called);
39   prepare_cb_called++;
40 }
41 
42 
timer_cb(uv_timer_t * handle)43 static void timer_cb(uv_timer_t* handle) {
44   ASSERT_OK(uv_timer_stop(&timer_handle));
45   ASSERT_EQ(1, prepare_cb_called);
46   ASSERT_EQ(1, check_cb_called);
47   ASSERT_OK(timer_cb_called);
48   timer_cb_called++;
49 }
50 
51 
check_cb(uv_check_t * handle)52 static void check_cb(uv_check_t* handle) {
53   ASSERT_OK(uv_check_stop(&check_handle));
54   ASSERT_OK(uv_timer_stop(&timer_handle));  /* Runs before timer_cb. */
55   ASSERT_OK(uv_timer_start(&timer_handle, timer_cb, 50, 0));
56   ASSERT_OK(uv_prepare_start(&prepare_handle, prepare_cb));
57   ASSERT_OK(prepare_cb_called);
58   ASSERT_OK(check_cb_called);
59   ASSERT_OK(timer_cb_called);
60   check_cb_called++;
61 }
62 
63 
TEST_IMPL(timer_from_check)64 TEST_IMPL(timer_from_check) {
65   ASSERT_OK(uv_prepare_init(uv_default_loop(), &prepare_handle));
66   ASSERT_OK(uv_check_init(uv_default_loop(), &check_handle));
67   ASSERT_OK(uv_check_start(&check_handle, check_cb));
68   ASSERT_OK(uv_timer_init(uv_default_loop(), &timer_handle));
69   ASSERT_OK(uv_timer_start(&timer_handle, timer_cb, 50, 0));
70   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
71   ASSERT_EQ(1, prepare_cb_called);
72   ASSERT_EQ(1, check_cb_called);
73   ASSERT_EQ(1, timer_cb_called);
74   uv_close((uv_handle_t*) &prepare_handle, NULL);
75   uv_close((uv_handle_t*) &check_handle, NULL);
76   uv_close((uv_handle_t*) &timer_handle, NULL);
77   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_ONCE));
78   MAKE_VALGRIND_HAPPY(uv_default_loop());
79   return 0;
80 }
81