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