xref: /netbsd-src/external/mit/libuv/dist/test/test-queue-foreach-delete.c (revision 0e552da7216834a96e91ad098e59272b41087480)
1*0e552da7Schristos /* Copyright The libuv project and contributors. All rights reserved.
2*0e552da7Schristos  *
3*0e552da7Schristos  * Permission is hereby granted, free of charge, to any person obtaining a copy
4*0e552da7Schristos  * of this software and associated documentation files (the "Software"), to
5*0e552da7Schristos  * deal in the Software without restriction, including without limitation the
6*0e552da7Schristos  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7*0e552da7Schristos  * sell copies of the Software, and to permit persons to whom the Software is
8*0e552da7Schristos  * furnished to do so, subject to the following conditions:
9*0e552da7Schristos  *
10*0e552da7Schristos  * The above copyright notice and this permission notice shall be included in
11*0e552da7Schristos  * all copies or substantial portions of the Software.
12*0e552da7Schristos  *
13*0e552da7Schristos  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14*0e552da7Schristos  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15*0e552da7Schristos  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16*0e552da7Schristos  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17*0e552da7Schristos  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18*0e552da7Schristos  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19*0e552da7Schristos  * IN THE SOFTWARE.
20*0e552da7Schristos  */
21*0e552da7Schristos 
22*0e552da7Schristos #include "uv.h"
23*0e552da7Schristos #include "task.h"
24*0e552da7Schristos 
25*0e552da7Schristos #include <string.h>
26*0e552da7Schristos 
27*0e552da7Schristos 
28*0e552da7Schristos /*
29*0e552da7Schristos  * The idea behind the test is as follows.
30*0e552da7Schristos  * Certain handle types are stored in a queue internally.
31*0e552da7Schristos  * Extra care should be taken for removal of a handle from the queue while iterating over the queue.
32*0e552da7Schristos  * (i.e., QUEUE_REMOVE() called within QUEUE_FOREACH())
33*0e552da7Schristos  * This usually happens when someone closes or stops a handle from within its callback.
34*0e552da7Schristos  * So we need to check that we haven't screwed the queue on close/stop.
35*0e552da7Schristos  * To do so we do the following (for each handle type):
36*0e552da7Schristos  *  1. Create and start 3 handles (#0, #1, and #2).
37*0e552da7Schristos  *
38*0e552da7Schristos  *     The queue after the start() calls:
39*0e552da7Schristos  *     ..=> [queue head] <=> [handle] <=> [handle #1] <=> [handle] <=..
40*0e552da7Schristos  *
41*0e552da7Schristos  *  2. Trigger handles to fire (for uv_idle_t, uv_prepare_t, and uv_check_t there is nothing to do).
42*0e552da7Schristos  *
43*0e552da7Schristos  *  3. In the callback for the first-executed handle (#0 or #2 depending on handle type)
44*0e552da7Schristos  *     stop the handle and the next one (#1).
45*0e552da7Schristos  *     (for uv_idle_t, uv_prepare_t, and uv_check_t callbacks are executed in the reverse order as they are start()'ed,
46*0e552da7Schristos  *     so callback for handle #2 will be called first)
47*0e552da7Schristos  *
48*0e552da7Schristos  *     The queue after the stop() calls:
49*0e552da7Schristos  *                                correct foreach "next"  |
50*0e552da7Schristos  *                                                       \/
51*0e552da7Schristos  *     ..=> [queue head] <==============================> [handle] <=..
52*0e552da7Schristos  *          [          ] <-  [handle] <=> [handle #1]  -> [      ]
53*0e552da7Schristos  *                                       /\
54*0e552da7Schristos  *                  wrong foreach "next"  |
55*0e552da7Schristos  *
56*0e552da7Schristos  *  4. The callback for handle #1 shouldn't be called because the handle #1 is stopped in the previous step.
57*0e552da7Schristos  *     However, if QUEUE_REMOVE() is not handled properly within QUEUE_FOREACH(), the callback _will_ be called.
58*0e552da7Schristos  */
59*0e552da7Schristos 
60*0e552da7Schristos static const unsigned first_handle_number_idle     = 2;
61*0e552da7Schristos static const unsigned first_handle_number_prepare  = 2;
62*0e552da7Schristos static const unsigned first_handle_number_check    = 2;
63*0e552da7Schristos #ifdef __linux__
64*0e552da7Schristos static const unsigned first_handle_number_fs_event = 0;
65*0e552da7Schristos #endif
66*0e552da7Schristos 
67*0e552da7Schristos 
68*0e552da7Schristos #define DEFINE_GLOBALS_AND_CBS(name, ...)                                     \
69*0e552da7Schristos   static uv_##name##_t (name)[3];                                             \
70*0e552da7Schristos   static unsigned name##_cb_calls[3];                                         \
71*0e552da7Schristos                                                                               \
72*0e552da7Schristos   static void name##2_cb(__VA_ARGS__) {                                       \
73*0e552da7Schristos     ASSERT(handle == &(name)[2]);                                             \
74*0e552da7Schristos     if (first_handle_number_##name == 2) {                                    \
75*0e552da7Schristos       uv_close((uv_handle_t*)&(name)[2], NULL);                               \
76*0e552da7Schristos       uv_close((uv_handle_t*)&(name)[1], NULL);                               \
77*0e552da7Schristos     }                                                                         \
78*0e552da7Schristos     name##_cb_calls[2]++;                                                     \
79*0e552da7Schristos   }                                                                           \
80*0e552da7Schristos                                                                               \
81*0e552da7Schristos   static void name##1_cb(__VA_ARGS__) {                                       \
82*0e552da7Schristos     ASSERT(handle == &(name)[1]);                                             \
83*0e552da7Schristos     ASSERT(0 && "Shouldn't be called" && (&name[0]));                         \
84*0e552da7Schristos   }                                                                           \
85*0e552da7Schristos                                                                               \
86*0e552da7Schristos   static void name##0_cb(__VA_ARGS__) {                                       \
87*0e552da7Schristos     ASSERT(handle == &(name)[0]);                                             \
88*0e552da7Schristos     if (first_handle_number_##name == 0) {                                    \
89*0e552da7Schristos       uv_close((uv_handle_t*)&(name)[0], NULL);                               \
90*0e552da7Schristos       uv_close((uv_handle_t*)&(name)[1], NULL);                               \
91*0e552da7Schristos     }                                                                         \
92*0e552da7Schristos     name##_cb_calls[0]++;                                                     \
93*0e552da7Schristos   }                                                                           \
94*0e552da7Schristos                                                                               \
95*0e552da7Schristos   static const uv_##name##_cb name##_cbs[] = {                                \
96*0e552da7Schristos     name##0_cb,                                                               \
97*0e552da7Schristos     name##1_cb,                                                               \
98*0e552da7Schristos     name##2_cb,                                                               \
99*0e552da7Schristos   };
100*0e552da7Schristos 
101*0e552da7Schristos #define INIT_AND_START(name, loop)                                            \
102*0e552da7Schristos   do {                                                                        \
103*0e552da7Schristos     size_t i;                                                                 \
104*0e552da7Schristos     for (i = 0; i < ARRAY_SIZE(name); i++) {                                  \
105*0e552da7Schristos       int r;                                                                  \
106*0e552da7Schristos       r = uv_##name##_init((loop), &(name)[i]);                               \
107*0e552da7Schristos       ASSERT(r == 0);                                                         \
108*0e552da7Schristos                                                                               \
109*0e552da7Schristos       r = uv_##name##_start(&(name)[i], name##_cbs[i]);                       \
110*0e552da7Schristos       ASSERT(r == 0);                                                         \
111*0e552da7Schristos     }                                                                         \
112*0e552da7Schristos   } while (0)
113*0e552da7Schristos 
114*0e552da7Schristos #define END_ASSERTS(name)                                                     \
115*0e552da7Schristos   do {                                                                        \
116*0e552da7Schristos     ASSERT(name##_cb_calls[0] == 1);                                          \
117*0e552da7Schristos     ASSERT(name##_cb_calls[1] == 0);                                          \
118*0e552da7Schristos     ASSERT(name##_cb_calls[2] == 1);                                          \
119*0e552da7Schristos   } while (0)
120*0e552da7Schristos 
121*0e552da7Schristos DEFINE_GLOBALS_AND_CBS(idle, uv_idle_t* handle)
122*0e552da7Schristos DEFINE_GLOBALS_AND_CBS(prepare, uv_prepare_t* handle)
123*0e552da7Schristos DEFINE_GLOBALS_AND_CBS(check, uv_check_t* handle)
124*0e552da7Schristos 
125*0e552da7Schristos #ifdef __linux__
126*0e552da7Schristos DEFINE_GLOBALS_AND_CBS(fs_event,
127*0e552da7Schristos                        uv_fs_event_t* handle,
128*0e552da7Schristos                        const char* filename,
129*0e552da7Schristos                        int events,
130*0e552da7Schristos                        int status)
131*0e552da7Schristos 
132*0e552da7Schristos static const char watched_dir[] = ".";
133*0e552da7Schristos static uv_timer_t timer;
134*0e552da7Schristos static unsigned helper_timer_cb_calls;
135*0e552da7Schristos 
136*0e552da7Schristos 
init_and_start_fs_events(uv_loop_t * loop)137*0e552da7Schristos static void init_and_start_fs_events(uv_loop_t* loop) {
138*0e552da7Schristos   size_t i;
139*0e552da7Schristos   for (i = 0; i < ARRAY_SIZE(fs_event); i++) {
140*0e552da7Schristos     int r;
141*0e552da7Schristos     r = uv_fs_event_init(loop, &fs_event[i]);
142*0e552da7Schristos     ASSERT(r == 0);
143*0e552da7Schristos 
144*0e552da7Schristos     r = uv_fs_event_start(&fs_event[i],
145*0e552da7Schristos                           (uv_fs_event_cb)fs_event_cbs[i],
146*0e552da7Schristos                           watched_dir,
147*0e552da7Schristos                           0);
148*0e552da7Schristos     ASSERT(r == 0);
149*0e552da7Schristos   }
150*0e552da7Schristos }
151*0e552da7Schristos 
helper_timer_cb(uv_timer_t * thandle)152*0e552da7Schristos static void helper_timer_cb(uv_timer_t* thandle) {
153*0e552da7Schristos   int r;
154*0e552da7Schristos   uv_fs_t fs_req;
155*0e552da7Schristos 
156*0e552da7Schristos   /* fire all fs_events */
157*0e552da7Schristos   r = uv_fs_utime(thandle->loop, &fs_req, watched_dir, 0, 0, NULL);
158*0e552da7Schristos   ASSERT(r == 0);
159*0e552da7Schristos   ASSERT(fs_req.result == 0);
160*0e552da7Schristos   ASSERT(fs_req.fs_type == UV_FS_UTIME);
161*0e552da7Schristos   ASSERT(strcmp(fs_req.path, watched_dir) == 0);
162*0e552da7Schristos   uv_fs_req_cleanup(&fs_req);
163*0e552da7Schristos 
164*0e552da7Schristos   helper_timer_cb_calls++;
165*0e552da7Schristos }
166*0e552da7Schristos #endif
167*0e552da7Schristos 
168*0e552da7Schristos 
TEST_IMPL(queue_foreach_delete)169*0e552da7Schristos TEST_IMPL(queue_foreach_delete) {
170*0e552da7Schristos   uv_loop_t* loop;
171*0e552da7Schristos   int r;
172*0e552da7Schristos 
173*0e552da7Schristos   loop = uv_default_loop();
174*0e552da7Schristos 
175*0e552da7Schristos   INIT_AND_START(idle,    loop);
176*0e552da7Schristos   INIT_AND_START(prepare, loop);
177*0e552da7Schristos   INIT_AND_START(check,   loop);
178*0e552da7Schristos 
179*0e552da7Schristos #ifdef __linux__
180*0e552da7Schristos   init_and_start_fs_events(loop);
181*0e552da7Schristos 
182*0e552da7Schristos   /* helper timer to trigger async and fs_event callbacks */
183*0e552da7Schristos   r = uv_timer_init(loop, &timer);
184*0e552da7Schristos   ASSERT(r == 0);
185*0e552da7Schristos 
186*0e552da7Schristos   r = uv_timer_start(&timer, helper_timer_cb, 0, 0);
187*0e552da7Schristos   ASSERT(r == 0);
188*0e552da7Schristos #endif
189*0e552da7Schristos 
190*0e552da7Schristos   r = uv_run(loop, UV_RUN_NOWAIT);
191*0e552da7Schristos   ASSERT(r == 1);
192*0e552da7Schristos 
193*0e552da7Schristos   END_ASSERTS(idle);
194*0e552da7Schristos   END_ASSERTS(prepare);
195*0e552da7Schristos   END_ASSERTS(check);
196*0e552da7Schristos 
197*0e552da7Schristos #ifdef __linux__
198*0e552da7Schristos   ASSERT(helper_timer_cb_calls == 1);
199*0e552da7Schristos #endif
200*0e552da7Schristos 
201*0e552da7Schristos   MAKE_VALGRIND_HAPPY();
202*0e552da7Schristos 
203*0e552da7Schristos   return 0;
204*0e552da7Schristos }
205