test-queue-foreach-delete.c
Go to the documentation of this file.
1 /* Copyright The libuv project and 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 #include <string.h>
26 
27 
28 /*
29  * The idea behind the test is as follows.
30  * Certain handle types are stored in a queue internally.
31  * Extra care should be taken for removal of a handle from the queue while iterating over the queue.
32  * (i.e., QUEUE_REMOVE() called within QUEUE_FOREACH())
33  * This usually happens when someone closes or stops a handle from within its callback.
34  * So we need to check that we haven't screwed the queue on close/stop.
35  * To do so we do the following (for each handle type):
36  * 1. Create and start 3 handles (#0, #1, and #2).
37  *
38  * The queue after the start() calls:
39  * ..=> [queue head] <=> [handle] <=> [handle #1] <=> [handle] <=..
40  *
41  * 2. Trigger handles to fire (for uv_idle_t, uv_prepare_t, and uv_check_t there is nothing to do).
42  *
43  * 3. In the callback for the first-executed handle (#0 or #2 depending on handle type)
44  * stop the handle and the next one (#1).
45  * (for uv_idle_t, uv_prepare_t, and uv_check_t callbacks are executed in the reverse order as they are start()'ed,
46  * so callback for handle #2 will be called first)
47  *
48  * The queue after the stop() calls:
49  * correct foreach "next" |
50  * \/
51  * ..=> [queue head] <==============================> [handle] <=..
52  * [ ] <- [handle] <=> [handle #1] -> [ ]
53  * /\
54  * wrong foreach "next" |
55  *
56  * 4. The callback for handle #1 shouldn't be called because the handle #1 is stopped in the previous step.
57  * However, if QUEUE_REMOVE() is not handled properly within QUEUE_FOREACH(), the callback _will_ be called.
58  */
59 
60 static const unsigned first_handle_number_idle = 2;
61 static const unsigned first_handle_number_prepare = 2;
62 static const unsigned first_handle_number_check = 2;
63 #ifdef __linux__
64 static const unsigned first_handle_number_fs_event = 0;
65 #endif
66 
67 
68 #define DEFINE_GLOBALS_AND_CBS(name, ...) \
69  static uv_##name##_t (name)[3]; \
70  static unsigned name##_cb_calls[3]; \
71  \
72  static void name##2_cb(__VA_ARGS__) { \
73  ASSERT(handle == &(name)[2]); \
74  if (first_handle_number_##name == 2) { \
75  uv_close((uv_handle_t*)&(name)[2], NULL); \
76  uv_close((uv_handle_t*)&(name)[1], NULL); \
77  } \
78  name##_cb_calls[2]++; \
79  } \
80  \
81  static void name##1_cb(__VA_ARGS__) { \
82  ASSERT(handle == &(name)[1]); \
83  ASSERT(0 && "Shouldn't be called" && (&name[0])); \
84  } \
85  \
86  static void name##0_cb(__VA_ARGS__) { \
87  ASSERT(handle == &(name)[0]); \
88  if (first_handle_number_##name == 0) { \
89  uv_close((uv_handle_t*)&(name)[0], NULL); \
90  uv_close((uv_handle_t*)&(name)[1], NULL); \
91  } \
92  name##_cb_calls[0]++; \
93  } \
94  \
95  static const uv_##name##_cb name##_cbs[] = { \
96  name##0_cb, \
97  name##1_cb, \
98  name##2_cb, \
99  };
100 
101 #define INIT_AND_START(name, loop) \
102  do { \
103  size_t i; \
104  for (i = 0; i < ARRAY_SIZE(name); i++) { \
105  int r; \
106  r = uv_##name##_init((loop), &(name)[i]); \
107  ASSERT(r == 0); \
108  \
109  r = uv_##name##_start(&(name)[i], name##_cbs[i]); \
110  ASSERT(r == 0); \
111  } \
112  } while (0)
113 
114 #define END_ASSERTS(name) \
115  do { \
116  ASSERT(name##_cb_calls[0] == 1); \
117  ASSERT(name##_cb_calls[1] == 0); \
118  ASSERT(name##_cb_calls[2] == 1); \
119  } while (0)
120 
124 
125 #ifdef __linux__
128  const char* filename,
129  int events,
130  int status)
131 
132 static const char watched_dir[] = ".";
133 static uv_timer_t timer;
134 static unsigned helper_timer_cb_calls;
135 
136 
137 static void init_and_start_fs_events(uv_loop_t* loop) {
138  size_t i;
139  for (i = 0; i < ARRAY_SIZE(fs_event); i++) {
140  int r;
142  ASSERT(r == 0);
143 
145  (uv_fs_event_cb)fs_event_cbs[i],
146  watched_dir,
147  0);
148  ASSERT(r == 0);
149  }
150 }
151 
152 static void helper_timer_cb(uv_timer_t* thandle) {
153  int r;
154  uv_fs_t fs_req;
155 
156  /* fire all fs_events */
157  r = uv_fs_utime(thandle->loop, &fs_req, watched_dir, 0, 0, NULL);
158  ASSERT(r == 0);
159  ASSERT(fs_req.result == 0);
160  ASSERT(fs_req.fs_type == UV_FS_UTIME);
161  ASSERT(strcmp(fs_req.path, watched_dir) == 0);
163 
164  helper_timer_cb_calls++;
165 }
166 #endif
167 
168 
169 TEST_IMPL(queue_foreach_delete) {
170  uv_loop_t* loop;
171  int r;
172 
173  loop = uv_default_loop();
174 
178 
179 #ifdef __linux__
180  init_and_start_fs_events(loop);
181 
182  /* helper timer to trigger async and fs_event callbacks */
183  r = uv_timer_init(loop, &timer);
184  ASSERT(r == 0);
185 
186  r = uv_timer_start(&timer, helper_timer_cb, 0, 0);
187  ASSERT(r == 0);
188 #endif
189 
191  ASSERT(r == 1);
192 
193  END_ASSERTS(idle);
196 
197 #ifdef __linux__
198  ASSERT(helper_timer_cb_calls == 1);
199 #endif
200 
202 
203  return 0;
204 }
async_greeter_server_with_graceful_shutdown.loop
loop
Definition: async_greeter_server_with_graceful_shutdown.py:59
uv_fs_event_s
Definition: uv.h:1533
END_ASSERTS
#define END_ASSERTS(name)
Definition: test-queue-foreach-delete.c:114
UV_FS_UTIME
@ UV_FS_UTIME
Definition: uv.h:1257
filename
const char * filename
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
idle
static uv_idle_t idle
Definition: test-poll-oob.c:37
ARRAY_SIZE
#define ARRAY_SIZE(array)
Definition: bloaty.cc:101
task.h
uv_prepare_s
Definition: uv.h:804
UV_RUN_NOWAIT
@ UV_RUN_NOWAIT
Definition: uv.h:256
string.h
TEST_IMPL
TEST_IMPL(queue_foreach_delete)
Definition: test-queue-foreach-delete.c:169
ASSERT
#define ASSERT(expr)
Definition: task.h:102
status
absl::Status status
Definition: rls.cc:251
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
uv_fs_s
Definition: uv.h:1294
first_handle_number_check
static const unsigned first_handle_number_check
Definition: test-queue-foreach-delete.c:62
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
uv_check_s
Definition: uv.h:814
first_handle_number_prepare
static const unsigned first_handle_number_prepare
Definition: test-queue-foreach-delete.c:61
fs_req
Definition: test-thread.c:41
fs_event
static uv_fs_event_t fs_event
Definition: test-fs-event.c:49
DEFINE_GLOBALS_AND_CBS
#define DEFINE_GLOBALS_AND_CBS(name,...)
Definition: test-queue-foreach-delete.c:68
prepare
static uv_prepare_t prepare
Definition: test-async.c:30
uv_timer_s
Definition: uv.h:850
uv_fs_utime
UV_EXTERN int uv_fs_utime(uv_loop_t *loop, uv_fs_t *req, const char *path, double atime, double mtime, uv_fs_cb cb)
Definition: unix/fs.c:1981
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
INIT_AND_START
#define INIT_AND_START(name, loop)
Definition: test-queue-foreach-delete.c:101
uv_idle_s
Definition: uv.h:824
uv_fs_event_cb
void(* uv_fs_event_cb)(uv_fs_event_t *handle, const char *filename, int events, int status)
Definition: uv.h:366
fix_build_deps.r
r
Definition: fix_build_deps.py:491
check
static void check(upb_inttable *t)
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.c:1715
uv_fs_event_start
UV_EXTERN int uv_fs_event_start(uv_fs_event_t *handle, uv_fs_event_cb cb, const char *path, unsigned int flags)
Definition: aix.c:727
uv_fs_req_cleanup
UV_EXTERN void uv_fs_req_cleanup(uv_fs_t *req)
Definition: unix/fs.c:2024
handle
static csh handle
Definition: test_arm_regression.c:16
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
uv_loop_s
Definition: uv.h:1767
uv_timer_init
UV_EXTERN int uv_timer_init(uv_loop_t *, uv_timer_t *handle)
Definition: timer.c:58
uv_fs_event_init
UV_EXTERN int uv_fs_event_init(uv_loop_t *loop, uv_fs_event_t *handle)
Definition: aix.c:717
first_handle_number_idle
static const unsigned first_handle_number_idle
Definition: test-queue-foreach-delete.c:60
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