1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */
#include "test-lib.h"
#include "net.h"
#include "time-util.h"
#include "ioloop.h"
#include "istream.h"
#include <unistd.h>
struct test_ctx {
bool got_left;
bool got_right;
bool got_to;
};
static void timeout_callback(struct timeval *tv)
{
i_gettimeofday(tv);
io_loop_stop(current_ioloop);
}
static void test_ioloop_fd_cb_left(struct test_ctx *ctx)
{
ctx->got_left = TRUE;
if (ctx->got_left && ctx->got_right)
io_loop_stop(current_ioloop);
}
static void test_ioloop_fd_cb_right(struct test_ctx *ctx)
{
ctx->got_right = TRUE;
if (ctx->got_left && ctx->got_right)
io_loop_stop(current_ioloop);
}
static void test_ioloop_fd_to(struct test_ctx *ctx)
{
ctx->got_to = TRUE;
io_loop_stop(current_ioloop);
}
static void test_ioloop_fd(void)
{
test_begin("ioloop fd");
struct test_ctx test_ctx;
int fds[2];
int ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
test_assert(ret == 0);
if (ret < 0) {
i_error("socketpair() failed: %m");
test_end();
return;
}
i_zero(&test_ctx);
struct ioloop *ioloop = io_loop_create();
struct io *io_left =
io_add(fds[0], IO_READ,
test_ioloop_fd_cb_left, &test_ctx);
struct io *io_right =
io_add(fds[1], IO_READ,
test_ioloop_fd_cb_right, &test_ctx);
struct timeout *to = timeout_add(2000, test_ioloop_fd_to, &test_ctx);
if (write(fds[0], "ltr", 3) != 3 ||
write(fds[1], "rtl", 3) != 3)
i_fatal("write() failed: %m");
io_loop_run(ioloop);
timeout_remove(&to);
io_remove(&io_left);
io_remove(&io_right);
test_assert(test_ctx.got_to == FALSE);
test_assert(test_ctx.got_left == TRUE);
test_assert(test_ctx.got_right == TRUE);
io_loop_destroy(&ioloop);
i_close_fd(&fds[0]);
i_close_fd(&fds[1]);
test_end();
}
static void test_ioloop_timeout(void)
{
struct ioloop *ioloop, *ioloop2;
struct timeout *to, *to2;
struct timeval tv_start, tv_callback;
test_begin("ioloop timeout");
ioloop = io_loop_create();
/* add a timeout by moving it from another ioloop */
ioloop2 = io_loop_create();
test_assert(io_loop_is_empty(ioloop));
test_assert(io_loop_is_empty(ioloop2));
to2 = timeout_add(1000, timeout_callback, &tv_callback);
test_assert(io_loop_is_empty(ioloop));
test_assert(!io_loop_is_empty(ioloop2));
io_loop_set_current(ioloop);
to2 = io_loop_move_timeout(&to2);
test_assert(!io_loop_is_empty(ioloop));
test_assert(io_loop_is_empty(ioloop2));
io_loop_set_current(ioloop2);
io_loop_destroy(&ioloop2);
sleep(1);
/* add & remove immediately */
to = timeout_add(1000, timeout_callback, &tv_callback);
timeout_remove(&to);
/* add the timeout we're actually testing below */
to = timeout_add(1000, timeout_callback, &tv_callback);
i_gettimeofday(&tv_start);
io_loop_run(ioloop);
test_assert(timeval_diff_msecs(&tv_callback, &tv_start) >= 500);
timeout_remove(&to);
timeout_remove(&to2);
test_assert(io_loop_is_empty(ioloop));
io_loop_destroy(&ioloop);
test_end();
}
static void zero_timeout_callback(unsigned int *counter)
{
*counter += 1;
}
static void test_ioloop_zero_timeout(void)
{
struct ioloop *ioloop;
struct timeout *to;
struct io *io;
unsigned int counter = 0;
int fd[2];
test_begin("ioloop zero timeout");
if (pipe(fd) < 0)
i_fatal("pipe() failed: %m");
switch (fork()) {
case (pid_t)-1:
i_fatal("fork() failed: %m");
case 0:
sleep(1);
char c = 0;
if (write(fd[1], &c, 1) < 0)
i_fatal("write(pipe) failed: %m");
test_exit(0);
default:
break;
}
ioloop = io_loop_create();
to = timeout_add_short(0, zero_timeout_callback, &counter);
io = io_add(fd[0], IO_READ, io_loop_stop, ioloop);
io_loop_run(ioloop);
test_assert_ucmp(counter, >, 1000);
timeout_remove(&to);
io_remove(&io);
io_loop_destroy(&ioloop);
test_end();
}
struct zero_timeout_recreate_ctx {
struct timeout *to;
unsigned int counter;
};
static void
zero_timeout_recreate_callback(struct zero_timeout_recreate_ctx *ctx)
{
timeout_remove(&ctx->to);
ctx->to = timeout_add_short(0, zero_timeout_recreate_callback, ctx);
ctx->counter++;
}
static void test_ioloop_zero_timeout_recreate(void)
{
struct ioloop *ioloop;
struct io *io;
struct zero_timeout_recreate_ctx ctx = { .counter = 0 };
int fd[2];
test_begin("ioloop zero timeout recreate");
if (pipe(fd) < 0)
i_fatal("pipe() failed: %m");
switch (fork()) {
case (pid_t)-1:
i_fatal("fork() failed: %m");
case 0:
sleep(1);
char c = 0;
if (write(fd[1], &c, 1) < 0)
i_fatal("write(pipe) failed: %m");
test_exit(0);
default:
break;
}
ioloop = io_loop_create();
ctx.to = timeout_add_short(0, zero_timeout_recreate_callback, &ctx);
io = io_add(fd[0], IO_READ, io_loop_stop, ioloop);
io_loop_run(ioloop);
test_assert_ucmp(ctx.counter, >, 1000);
timeout_remove(&ctx.to);
io_remove(&io);
io_loop_destroy(&ioloop);
test_end();
}
static void io_callback(void *context ATTR_UNUSED)
{
}
static void test_ioloop_find_fd_conditions(void)
{
static struct {
enum io_condition condition;
int fd[2];
struct io *io;
} tests[] = {
{ IO_ERROR, { -1, -1 }, NULL },
{ IO_READ, { -1, -1 }, NULL },
{ IO_WRITE, { -1, -1 }, NULL },
{ IO_READ | IO_WRITE, { -1, -1 }, NULL },
{ IO_READ, { -1, -1 }, NULL } /* read+write as separate ios */
};
struct ioloop *ioloop;
struct io *io;
unsigned int i;
test_begin("ioloop find fd conditions");
ioloop = io_loop_create();
for (i = 0; i < N_ELEMENTS(tests); i++) {
if (socketpair(AF_UNIX, SOCK_STREAM, 0, tests[i].fd) < 0)
i_fatal("socketpair() failed: %m");
tests[i].io = io_add(tests[i].fd[0], tests[i].condition, io_callback, NULL);
}
io = io_add(tests[i-1].fd[0], IO_WRITE, io_callback, NULL);
tests[i-1].condition |= IO_WRITE;
for (i = 0; i < N_ELEMENTS(tests); i++)
test_assert_idx(io_loop_find_fd_conditions(ioloop, tests[i].fd[0]) == tests[i].condition, i);
io_remove(&io);
for (i = 0; i < N_ELEMENTS(tests); i++) {
io_remove(&tests[i].io);
i_close_fd(&tests[i].fd[0]);
i_close_fd(&tests[i].fd[1]);
}
io_loop_destroy(&ioloop);
test_end();
}
static void io_callback_pending_io(void *context ATTR_UNUSED)
{
io_loop_stop(current_ioloop);
}
static void test_ioloop_pending_io(void)
{
test_begin("ioloop pending io");
struct istream *is = i_stream_create_from_data("data", 4);
struct ioloop *ioloop = io_loop_create();
test_assert(io_loop_is_empty(ioloop));
struct io *io = io_add_istream(is, io_callback_pending_io, NULL);
test_assert(!io_loop_is_empty(ioloop));
io_loop_set_current(ioloop);
io_set_pending(io);
io_loop_run(ioloop);
io_remove(&io);
i_stream_unref(&is);
io_loop_destroy(&ioloop);
test_end();
}
static void test_ioloop_context_callback(struct ioloop_context *ctx)
{
test_assert(io_loop_get_current_context(current_ioloop) == ctx);
io_loop_stop(current_ioloop);
}
static void test_ioloop_context(void)
{
test_begin("ioloop context");
struct ioloop *ioloop = io_loop_create();
struct ioloop_context *ctx = io_loop_context_new(ioloop);
test_assert(io_loop_get_current_context(current_ioloop) == NULL);
io_loop_context_activate(ctx);
test_assert(io_loop_get_current_context(current_ioloop) == ctx);
struct timeout *to = timeout_add(0, test_ioloop_context_callback, ctx);
io_loop_run(ioloop);
test_assert(io_loop_get_current_context(current_ioloop) == NULL);
/* test that we don't crash at deinit if we leave the context active */
io_loop_context_activate(ctx);
test_assert(io_loop_get_current_context(current_ioloop) == ctx);
timeout_remove(&to);
io_loop_context_unref(&ctx);
io_loop_destroy(&ioloop);
test_end();
}
static void test_ioloop_context_events_run(struct event *root_event)
{
struct ioloop *ioloop = io_loop_create();
struct ioloop_context *ctx1, *ctx2;
/* create context 1 */
ctx1 = io_loop_context_new(ioloop);
io_loop_context_switch(ctx1);
struct event *ctx1_event1 = event_create(NULL);
event_push_global(ctx1_event1);
struct event *ctx1_event2 = event_create(NULL);
event_push_global(ctx1_event2);
io_loop_context_deactivate(ctx1);
test_assert(event_get_global() == root_event);
/* create context 2 */
ctx2 = io_loop_context_new(ioloop);
io_loop_context_switch(ctx2);
struct event *ctx2_event1 = event_create(NULL);
event_push_global(ctx2_event1);
io_loop_context_deactivate(ctx2);
test_assert(event_get_global() == root_event);
/* test switching contexts */
io_loop_context_switch(ctx1);
test_assert(event_get_global() == ctx1_event2);
io_loop_context_switch(ctx2);
test_assert(event_get_global() == ctx2_event1);
/* test popping away events */
io_loop_context_switch(ctx1);
event_pop_global(ctx1_event2);
io_loop_context_switch(ctx2);
event_pop_global(ctx2_event1);
io_loop_context_switch(ctx1);
test_assert(event_get_global() == ctx1_event1);
io_loop_context_switch(ctx2);
test_assert(event_get_global() == root_event);
io_loop_context_deactivate(ctx2);
io_loop_context_unref(&ctx1);
io_loop_context_unref(&ctx2);
io_loop_destroy(&ioloop);
event_unref(&ctx1_event1);
event_unref(&ctx1_event2);
event_unref(&ctx2_event1);
}
static void test_ioloop_context_events(void)
{
test_begin("ioloop context - no root event");
test_ioloop_context_events_run(NULL);
test_end();
test_begin("ioloop context - with root event");
struct event *root_event = event_create(NULL);
event_push_global(root_event);
test_ioloop_context_events_run(root_event);
event_pop_global(root_event);
event_unref(&root_event);
test_end();
}
void test_ioloop(void)
{
test_ioloop_timeout();
test_ioloop_zero_timeout();
test_ioloop_zero_timeout_recreate();
test_ioloop_find_fd_conditions();
test_ioloop_pending_io();
test_ioloop_fd();
test_ioloop_context();
test_ioloop_context_events();
}
|