test-timer-again.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 close_cb_called = 0;
27 static int repeat_1_cb_called = 0;
28 static int repeat_2_cb_called = 0;
29 
30 static int repeat_2_cb_allowed = 0;
31 
33 
35 
36 
37 static void close_cb(uv_handle_t* handle) {
38  ASSERT(handle != NULL);
39 
41 }
42 
43 
44 static void repeat_1_cb(uv_timer_t* handle) {
45  int r;
46 
47  ASSERT(handle == &repeat_1);
49 
50  fprintf(stderr, "repeat_1_cb called after %ld ms\n",
51  (long int)(uv_now(uv_default_loop()) - start_time));
52  fflush(stderr);
53 
55 
57  ASSERT(r == 0);
58 
59  if (repeat_1_cb_called == 10) {
61  /* We're not calling uv_timer_again on repeat_2 any more, so after this
62  * timer_2_cb is expected. */
64  return;
65  }
66 }
67 
68 
69 static void repeat_2_cb(uv_timer_t* handle) {
70  ASSERT(handle == &repeat_2);
72 
73  fprintf(stderr, "repeat_2_cb called after %ld ms\n",
74  (long int)(uv_now(uv_default_loop()) - start_time));
75  fflush(stderr);
76 
78 
79  if (uv_timer_get_repeat(&repeat_2) == 0) {
82  return;
83  }
84 
85  fprintf(stderr, "uv_timer_get_repeat %ld ms\n",
86  (long int)uv_timer_get_repeat(&repeat_2));
87  fflush(stderr);
89 
90  /* This shouldn't take effect immediately. */
92 }
93 
94 
95 TEST_IMPL(timer_again) {
96  int r;
97 
99  ASSERT(0 < start_time);
100 
101  /* Verify that it is not possible to uv_timer_again a never-started timer. */
103  ASSERT(r == 0);
104  r = uv_timer_again(&dummy);
105  ASSERT(r == UV_EINVAL);
107 
108  /* Start timer repeat_1. */
110  ASSERT(r == 0);
111  r = uv_timer_start(&repeat_1, repeat_1_cb, 50, 0);
112  ASSERT(r == 0);
114 
115  /* Actually make repeat_1 repeating. */
118 
119  /*
120  * Start another repeating timer. It'll be again()ed by the repeat_1 so
121  * it should not time out until repeat_1 stops.
122  */
124  ASSERT(r == 0);
125  r = uv_timer_start(&repeat_2, repeat_2_cb, 100, 100);
126  ASSERT(r == 0);
128 
130 
131  ASSERT(repeat_1_cb_called == 10);
133  ASSERT(close_cb_called == 2);
134 
135  fprintf(stderr, "Test took %ld ms (expected ~700 ms)\n",
136  (long int)(uv_now(uv_default_loop()) - start_time));
137  fflush(stderr);
138 
140  return 0;
141 }
repeat_2_cb
static void repeat_2_cb(uv_timer_t *handle)
Definition: test-timer-again.c:69
uv_timer_get_repeat
UV_EXTERN uint64_t uv_timer_get_repeat(const uv_timer_t *handle)
Definition: timer.c:128
task.h
repeat_1_cb_called
static int repeat_1_cb_called
Definition: test-timer-again.c:27
uv_timer_again
UV_EXTERN int uv_timer_again(uv_timer_t *handle)
Definition: timer.c:110
repeat_2_cb_allowed
static int repeat_2_cb_allowed
Definition: test-timer-again.c:30
uv_now
UV_EXTERN uint64_t uv_now(const uv_loop_t *)
Definition: uv-common.c:537
ASSERT
#define ASSERT(expr)
Definition: task.h:102
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
uv_unref
UV_EXTERN void uv_unref(uv_handle_t *)
Definition: uv-common.c:522
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
uv_close
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: unix/core.c:112
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
repeat_1
static uv_timer_t repeat_1
Definition: test-timer-again.c:32
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
dummy
static uv_timer_t dummy
Definition: test-timer-again.c:32
close_cb
static void close_cb(uv_handle_t *handle)
Definition: test-timer-again.c:37
uv_timer_s
Definition: uv.h:850
uv_timer_set_repeat
UV_EXTERN void uv_timer_set_repeat(uv_timer_t *handle, uint64_t repeat)
Definition: timer.c:123
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
repeat_2
static uv_timer_t repeat_2
Definition: test-timer-again.c:32
repeat_1_cb
static void repeat_1_cb(uv_timer_t *handle)
Definition: test-timer-again.c:44
fix_build_deps.r
r
Definition: fix_build_deps.py:491
TEST_IMPL
TEST_IMPL(timer_again)
Definition: test-timer-again.c:95
close_cb_called
static int close_cb_called
Definition: test-timer-again.c:26
handle
static csh handle
Definition: test_arm_regression.c:16
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
start_time
static uint64_t start_time
Definition: test-timer-again.c:34
uv_timer_init
UV_EXTERN int uv_timer_init(uv_loop_t *, uv_timer_t *handle)
Definition: timer.c:58
repeat_2_cb_called
static int repeat_2_cb_called
Definition: test-timer-again.c:28


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