• 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 uv_idle_t idle_handle;
27 static uv_check_t check_handle;
28 static uv_timer_t timer_handle;
29 
30 static int idle_cb_called = 0;
31 static int check_cb_called = 0;
32 static int timer_cb_called = 0;
33 static int close_cb_called = 0;
34 
35 
close_cb(uv_handle_t * handle)36 static void close_cb(uv_handle_t* handle) {
37   close_cb_called++;
38 }
39 
40 
timer_cb(uv_timer_t * handle)41 static void timer_cb(uv_timer_t* handle) {
42   ASSERT(handle == &timer_handle);
43 
44   uv_close((uv_handle_t*) &idle_handle, close_cb);
45   uv_close((uv_handle_t*) &check_handle, close_cb);
46   uv_close((uv_handle_t*) &timer_handle, close_cb);
47 
48   timer_cb_called++;
49   fprintf(stderr, "timer_cb %d\n", timer_cb_called);
50   fflush(stderr);
51 }
52 
53 
idle_cb(uv_idle_t * handle)54 static void idle_cb(uv_idle_t* handle) {
55   ASSERT(handle == &idle_handle);
56 
57   idle_cb_called++;
58   fprintf(stderr, "idle_cb %d\n", idle_cb_called);
59   fflush(stderr);
60 }
61 
62 
check_cb(uv_check_t * handle)63 static void check_cb(uv_check_t* handle) {
64   ASSERT(handle == &check_handle);
65 
66   check_cb_called++;
67   fprintf(stderr, "check_cb %d\n", check_cb_called);
68   fflush(stderr);
69 }
70 
71 
TEST_IMPL(idle_starvation)72 TEST_IMPL(idle_starvation) {
73   int r;
74 
75   r = uv_idle_init(uv_default_loop(), &idle_handle);
76   ASSERT(r == 0);
77   r = uv_idle_start(&idle_handle, idle_cb);
78   ASSERT(r == 0);
79 
80   r = uv_check_init(uv_default_loop(), &check_handle);
81   ASSERT(r == 0);
82   r = uv_check_start(&check_handle, check_cb);
83   ASSERT(r == 0);
84 
85   r = uv_timer_init(uv_default_loop(), &timer_handle);
86   ASSERT(r == 0);
87   r = uv_timer_start(&timer_handle, timer_cb, 50, 0);
88   ASSERT(r == 0);
89 
90   r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
91   ASSERT(r == 0);
92 
93   ASSERT(idle_cb_called > 0);
94   ASSERT(timer_cb_called == 1);
95   ASSERT(close_cb_called == 3);
96 
97   MAKE_VALGRIND_HAPPY();
98   return 0;
99 }
100 
101 
idle_stop(uv_idle_t * handle)102 static void idle_stop(uv_idle_t* handle) {
103   uv_idle_stop(handle);
104 }
105 
106 
TEST_IMPL(idle_check)107 TEST_IMPL(idle_check) {
108   ASSERT_EQ(0, uv_idle_init(uv_default_loop(), &idle_handle));
109   ASSERT_EQ(0, uv_idle_start(&idle_handle, idle_stop));
110 
111   ASSERT_EQ(0, uv_check_init(uv_default_loop(), &check_handle));
112   ASSERT_EQ(0, uv_check_start(&check_handle, check_cb));
113 
114   ASSERT_EQ(1, uv_run(uv_default_loop(), UV_RUN_ONCE));
115   ASSERT_EQ(1, check_cb_called);
116 
117   ASSERT_EQ(0, close_cb_called);
118   uv_close((uv_handle_t*) &idle_handle, close_cb);
119   uv_close((uv_handle_t*) &check_handle, close_cb);
120   ASSERT_EQ(0, uv_run(uv_default_loop(), UV_RUN_ONCE));
121   ASSERT_EQ(2, close_cb_called);
122 
123   MAKE_VALGRIND_HAPPY();
124   return 0;
125 }
126