test-timer.c
Go to the documentation of this file.
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;
35 
36 
38  printf("ONCE_CLOSE_CB\n");
39 
40  ASSERT(handle != NULL);
41  ASSERT(0 == uv_is_active(handle));
42 
44 }
45 
46 
47 static void once_cb(uv_timer_t* handle) {
48  printf("ONCE_CB %d\n", once_cb_called);
49 
50  ASSERT(handle != NULL);
52 
54 
56 
57  /* Just call this randomly for the code coverage. */
59 }
60 
61 
63  printf("REPEAT_CLOSE_CB\n");
64 
65  ASSERT(handle != NULL);
66 
68 }
69 
70 
71 static void repeat_cb(uv_timer_t* handle) {
72  printf("REPEAT_CB\n");
73 
74  ASSERT(handle != NULL);
76 
78 
79  if (repeat_cb_called == 5) {
81  }
82 }
83 
84 
85 static void never_cb(uv_timer_t* handle) {
86  FATAL("never_cb should never be called");
87 }
88 
89 
91  uv_timer_t once_timers[10];
93  uv_timer_t repeat, never;
94  unsigned int i;
95  int r;
96 
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;
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. */
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);
123 
125 
126  ASSERT(once_cb_called == 10);
128  printf("repeat_cb_called %d\n", repeat_cb_called);
129  ASSERT(repeat_cb_called == 5);
131 
133 
135  return 0;
136 }
137 
138 
139 TEST_IMPL(timer_start_twice) {
141  int r;
142 
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);
150  ASSERT(r == 0);
151 
152  ASSERT(once_cb_called == 1);
153 
155  return 0;
156 }
157 
158 
161 
165 
167  return 0;
168 }
169 
170 
171 static void order_cb_a(uv_timer_t *handle) {
172  ASSERT(order_cb_called++ == *(int*)handle->data);
173 }
174 
175 
176 static void order_cb_b(uv_timer_t *handle) {
177  ASSERT(order_cb_called++ == *(int*)handle->data);
178 }
179 
180 
181 TEST_IMPL(timer_order) {
182  int first;
183  int second;
184  uv_timer_t handle_a;
185  uv_timer_t handle_b;
186 
187  first = 0;
188  second = 1;
189  ASSERT(0 == uv_timer_init(uv_default_loop(), &handle_a));
190  ASSERT(0 == uv_timer_init(uv_default_loop(), &handle_b));
191 
192  /* Test for starting handle_a then handle_b */
193  handle_a.data = &first;
194  ASSERT(0 == uv_timer_start(&handle_a, order_cb_a, 0, 0));
195  handle_b.data = &second;
196  ASSERT(0 == uv_timer_start(&handle_b, order_cb_b, 0, 0));
198 
199  ASSERT(order_cb_called == 2);
200 
201  ASSERT(0 == uv_timer_stop(&handle_a));
202  ASSERT(0 == uv_timer_stop(&handle_b));
203 
204  /* Test for starting handle_b then handle_a */
205  order_cb_called = 0;
206  handle_b.data = &first;
207  ASSERT(0 == uv_timer_start(&handle_b, order_cb_b, 0, 0));
208 
209  handle_a.data = &second;
210  ASSERT(0 == uv_timer_start(&handle_a, order_cb_a, 0, 0));
212 
213  ASSERT(order_cb_called == 2);
214 
216  return 0;
217 }
218 
219 
221  ASSERT(handle == &tiny_timer);
222  uv_close((uv_handle_t*) &tiny_timer, NULL);
223  uv_close((uv_handle_t*) &huge_timer1, NULL);
224  uv_close((uv_handle_t*) &huge_timer2, NULL);
225 }
226 
227 
228 TEST_IMPL(timer_huge_timeout) {
233  ASSERT(0 == uv_timer_start(&huge_timer1, tiny_timer_cb, 0xffffffffffffLL, 0));
237  return 0;
238 }
239 
240 
242  static int ncalls;
243 
244  if (ncalls == 0)
246  else
247  ASSERT(handle == &tiny_timer);
248 
249  if (++ncalls == 10) {
250  uv_close((uv_handle_t*) &tiny_timer, NULL);
251  uv_close((uv_handle_t*) &huge_timer1, NULL);
252  }
253 }
254 
255 
256 TEST_IMPL(timer_huge_repeat) {
263  return 0;
264 }
265 
266 
267 static unsigned int timer_run_once_timer_cb_called;
268 
269 
272 }
273 
274 
275 TEST_IMPL(timer_run_once) {
277 
282 
286 
287  uv_close((uv_handle_t*) &timer_handle, NULL);
289 
291  return 0;
292 }
293 
294 
295 TEST_IMPL(timer_is_closing) {
297 
299  uv_close((uv_handle_t *)&handle, NULL);
300 
301  ASSERT(UV_EINVAL == uv_timer_start(&handle, never_cb, 100, 100));
302 
304  return 0;
305 }
306 
307 
308 TEST_IMPL(timer_null_callback) {
310 
312  ASSERT(UV_EINVAL == uv_timer_start(&handle, NULL, 100, 100));
313 
315  return 0;
316 }
317 
318 
320 
321 
323  uint64_t hrtime = uv_hrtime() / 1000000;
325 }
326 
327 
328 TEST_IMPL(timer_early_check) {
330  const uint64_t timeout_ms = 10;
331 
333 
337 
338  uv_close((uv_handle_t*) &timer_handle, NULL);
340 
342  return 0;
343 }
ARRAY_SIZE
#define ARRAY_SIZE(array)
Definition: bloaty.cc:101
uv_timer_get_repeat
UV_EXTERN uint64_t uv_timer_get_repeat(const uv_timer_t *handle)
Definition: timer.c:128
order_cb_b
static void order_cb_b(uv_timer_t *handle)
Definition: test-timer.c:176
task.h
uv_now
UV_EXTERN uint64_t uv_now(const uv_loop_t *)
Definition: uv-common.c:537
start_time
static uint64_t start_time
Definition: test-timer.c:31
printf
_Use_decl_annotations_ int __cdecl printf(const char *_Format,...)
Definition: cs_driver.c:91
ASSERT
#define ASSERT(expr)
Definition: task.h:102
TEST_IMPL
TEST_IMPL(timer)
Definition: test-timer.c:90
timer_init
static void timer_init(grpc_timer *timer, grpc_core::Timestamp deadline, grpc_closure *closure)
Definition: timer_generic.cc:332
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
uv_timer_stop
UV_EXTERN int uv_timer_stop(uv_timer_t *handle)
Definition: timer.c:97
second
StrT second
Definition: cxa_demangle.cpp:4885
timer_early_check_expected_time
static uint64_t timer_early_check_expected_time
Definition: test-timer.c:319
uv_unref
UV_EXTERN void uv_unref(uv_handle_t *)
Definition: uv-common.c:522
timer_run_once_timer_cb_called
static unsigned int timer_run_once_timer_cb_called
Definition: test-timer.c:267
uv_close
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: unix/core.c:112
uv_update_time
UV_EXTERN void uv_update_time(uv_loop_t *)
Definition: unix/core.c:413
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
order_cb_a
static void order_cb_a(uv_timer_t *handle)
Definition: test-timer.c:171
once_close_cb_called
static int once_close_cb_called
Definition: test-timer.c:27
once_cb
static void once_cb(uv_timer_t *handle)
Definition: test-timer.c:47
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
uv_is_active
UV_EXTERN int uv_is_active(const uv_handle_t *handle)
Definition: unix/core.c:418
repeat_close_cb
static void repeat_close_cb(uv_handle_t *handle)
Definition: test-timer.c:62
timer_run_once_timer_cb
static void timer_run_once_timer_cb(uv_timer_t *handle)
Definition: test-timer.c:270
repeat_cb_called
static int repeat_cb_called
Definition: test-timer.c:28
uv_timer_s
Definition: uv.h:850
huge_timer2
static uv_timer_t huge_timer2
Definition: test-timer.c:34
once_close_cb
static void once_close_cb(uv_handle_t *handle)
Definition: test-timer.c:37
grpc_core::never
Poll< int > never()
Definition: race_test.cc:24
repeat_close_cb_called
static int repeat_close_cb_called
Definition: test-timer.c:29
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
order_cb_called
static int order_cb_called
Definition: test-timer.c:30
huge_timer1
static uv_timer_t huge_timer1
Definition: test-timer.c:33
FATAL
#define FATAL(msg)
Definition: task.h:88
once
absl::once_flag once
Definition: bm_opencensus_plugin.cc:38
never_cb
static void never_cb(uv_timer_t *handle)
Definition: test-timer.c:85
uv_hrtime
UV_EXTERN uint64_t uv_hrtime(void)
Definition: unix/core.c:107
fix_build_deps.r
r
Definition: fix_build_deps.py:491
first
StrT first
Definition: cxa_demangle.cpp:4884
timeout_ms
int timeout_ms
Definition: rls_end2end_test.cc:239
tiny_timer_cb
static void tiny_timer_cb(uv_timer_t *handle)
Definition: test-timer.c:220
UV_RUN_ONCE
@ UV_RUN_ONCE
Definition: uv.h:255
handle
static csh handle
Definition: test_arm_regression.c:16
tiny_timer
static uv_timer_t tiny_timer
Definition: test-timer.c:32
uv_handle_s
Definition: uv.h:441
uv_timer_start
UV_EXTERN int uv_timer_start(uv_timer_t *handle, uv_timer_cb cb, uint64_t timeout, uint64_t repeat)
Definition: timer.c:66
timer_early_check_cb
static void timer_early_check_cb(uv_timer_t *handle)
Definition: test-timer.c:322
uv_timer_init
UV_EXTERN int uv_timer_init(uv_loop_t *, uv_timer_t *handle)
Definition: timer.c:58
timer_handle
static uv_timer_t timer_handle
Definition: benchmark-loop-count.c:32
once_cb_called
static int once_cb_called
Definition: test-timer.c:26
repeat_cb
static void repeat_cb(uv_timer_t *handle)
Definition: test-timer.c:71
huge_repeat_cb
static void huge_repeat_cb(uv_timer_t *handle)
Definition: test-timer.c:241
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
timer
static uv_timer_t timer
Definition: test-callback-stack.c:34


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:30