summaryrefslogtreecommitdiff
path: root/tests/common/test_loop.c
blob: a77b6a5d8efd001b054f7d606d55f76537b6d706 (plain)
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#define _GNU_SOURCE
#define _DARWIN_C_SOURCE
#include "common/private_loop.h"

#include <assert.h>
#include <errno.h>
#include <poll.h>
#include <pthread.h>
#include <stdint.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>

static void sleep_ms(int ms)
{
    struct timespec ts = {.tv_sec = ms / 1000, .tv_nsec = (long)(ms % 1000) * 1000000L};
    nanosleep(&ts, NULL);
}

/* Fail instead of hanging when a wake never arrives. */
static void on_alarm(int signo)
{
    (void)signo;
    static const char msg[] = "loop test timed out\n";
    ssize_t ignored = write(STDERR_FILENO, msg, sizeof(msg) - 1);
    (void)ignored;
    _exit(1);
}

struct post_log {
    struct yasl_loop *loop;
    char order[8];
    int count;
};

static void post_a(void *ud);

static void post_c(void *ud)
{
    struct post_log *log = ud;
    log->order[log->count++] = 'c';
}

static void post_a(void *ud)
{
    struct post_log *log = ud;
    log->order[log->count++] = 'a';
    assert(yasl_private_loop_post(log->loop, post_c, log));
}

static void post_b(void *ud)
{
    struct post_log *log = ud;
    log->order[log->count++] = 'b';
}

static void test_post_order(void)
{
    struct yasl_loop *l = yasl_loop_create();
    assert(l);
    assert(!yasl_private_loop_post(NULL, post_b, NULL));
    assert(!yasl_private_loop_post(l, NULL, NULL));

    struct post_log log = {.loop = l};
    assert(yasl_private_loop_post(l, post_a, &log));
    assert(yasl_private_loop_post(l, post_b, &log));
    assert(yasl_loop_step(l, 0) >= 1);
    /* A post made while posts run waits for a later step. */
    assert(log.count == 2 && memcmp(log.order, "ab", 2) == 0);
    assert(yasl_loop_step(l, 0) >= 1);
    assert(log.count == 3 && log.order[2] == 'c');

    /* Destroy drops posts that never ran. */
    assert(yasl_private_loop_post(l, post_b, &log));
    yasl_loop_destroy(l);
    assert(log.count == 3);
}

struct thread_ctx {
    struct yasl_loop *loop;
    bool kick;
    int signo;
    _Atomic bool ran;
};

static void mark_ran(void *ud)
{
    struct thread_ctx *ctx = ud;
    ctx->ran = true;
}

static void *wake_thread(void *ud)
{
    struct thread_ctx *ctx = ud;
    sleep_ms(30);
    if (ctx->signo)
        kill(getpid(), ctx->signo);
    else if (ctx->kick)
        yasl_private_loop_kick(ctx->loop);
    else
        assert(yasl_private_loop_post(ctx->loop, mark_ran, ctx));
    return NULL;
}

static void test_cross_thread(void)
{
    struct yasl_loop *l = yasl_loop_create();
    assert(l);

    struct thread_ctx post = {.loop = l};
    pthread_t t;
    assert(pthread_create(&t, NULL, wake_thread, &post) == 0);
    while (!post.ran)
        assert(yasl_loop_step(l, -1) >= 0);
    pthread_join(t, NULL);

    /* A kick ends an indefinite wait without running anything. */
    struct thread_ctx kick = {.loop = l, .kick = true};
    assert(pthread_create(&t, NULL, wake_thread, &kick) == 0);
    assert(yasl_loop_step(l, -1) >= 1);
    pthread_join(t, NULL);

    yasl_loop_destroy(l);
}

struct signal_log {
    struct yasl_loop *loop;
    int seen[NSIG];
    int total;
    bool unwatch;
};

static void on_signal(void *ud, int signo)
{
    struct signal_log *log = ud;
    assert(signo > 0 && signo < NSIG);
    log->seen[signo]++;
    log->total++;
    if (log->unwatch)
        yasl_loop_unwatch_signals(log->loop);
}

static volatile sig_atomic_t previous_handler_hits;

static void previous_handler(int signo)
{
    (void)signo;
    previous_handler_hits++;
}

static void step_until(struct yasl_loop *l, const int *counter, int want)
{
    while (*counter < want)
        assert(yasl_loop_step(l, -1) >= 0);
}

static void test_signals(void)
{
    struct sigaction prev = {.sa_handler = previous_handler};
    sigemptyset(&prev.sa_mask);
    assert(sigaction(SIGUSR2, &prev, NULL) == 0);

    struct yasl_loop *l = yasl_loop_create();
    struct yasl_loop *other = yasl_loop_create();
    assert(l && other);
    struct signal_log log = {.loop = l};

    const int bad[] = {SIGKILL};
    assert(yasl_loop_watch_signals(l, bad, 1, on_signal, &log) == -EINVAL);
    const int sigs[] = {SIGUSR1, SIGUSR2, SIGCHLD};
    assert(yasl_loop_watch_signals(l, sigs, 0, on_signal, &log) == -EINVAL);
    assert(yasl_loop_watch_signals(l, sigs, 3, NULL, &log) == -EINVAL);
    assert(yasl_loop_watch_signals(l, sigs, 3, on_signal, &log) == 0);

    /* One signal belongs to one loop at a time. */
    assert(yasl_loop_watch_signals(other, sigs, 1, on_signal, &log) == -EBUSY);

    /* Deliveries before the callback runs coalesce per signal. */
    raise(SIGUSR1);
    raise(SIGUSR1);
    raise(SIGUSR2);
    assert(yasl_loop_step(l, 0) >= 1);
    assert(log.seen[SIGUSR1] == 1 && log.seen[SIGUSR2] == 1);
    assert(previous_handler_hits == 0);

    /* A signal sent from another thread wakes an indefinite wait. */
    struct thread_ctx ctx = {.loop = l, .signo = SIGUSR1};
    pthread_t t;
    assert(pthread_create(&t, NULL, wake_thread, &ctx) == 0);
    step_until(l, &log.seen[SIGUSR1], 2);
    pthread_join(t, NULL);

    /* SIGCHLD arrives on the loop thread when a child exits. */
    pid_t child = fork();
    assert(child >= 0);
    if (child == 0) _exit(0);
    step_until(l, &log.seen[SIGCHLD], 1);
    int status;
    assert(waitpid(child, &status, 0) == child);

    /* A loop has one watch, so a second cannot silently drop the first one's
     * signals. Unwatching restores the previous actions. */
    const int only_usr1[] = {SIGUSR1};
    assert(yasl_loop_watch_signals(l, only_usr1, 1, on_signal, &log) == -EBUSY);
    yasl_loop_unwatch_signals(l);
    assert(yasl_loop_watch_signals(l, only_usr1, 1, on_signal, &log) == 0);
    raise(SIGUSR2);
    assert(previous_handler_hits == 1);
    assert(yasl_loop_watch_signals(other, (const int[]){SIGUSR2}, 1, on_signal, &log) == 0);
    yasl_loop_unwatch_signals(other);
    assert(previous_handler_hits == 1);
    raise(SIGUSR2);
    assert(previous_handler_hits == 2);

    /* A callback may unwatch; the previous default action comes back. */
    log.unwatch = true;
    raise(SIGUSR1);
    step_until(l, &log.seen[SIGUSR1], 3);
    struct sigaction now;
    assert(sigaction(SIGUSR1, NULL, &now) == 0);
    assert(now.sa_handler == SIG_DFL);
    yasl_loop_unwatch_signals(l);

    /* Destroy unwatches. */
    log.unwatch = false;
    assert(yasl_loop_watch_signals(l, only_usr1, 1, on_signal, &log) == 0);
    assert(yasl_loop_destroy(l) == 0);
    assert(sigaction(SIGUSR1, NULL, &now) == 0);
    assert(now.sa_handler == SIG_DFL);
    yasl_loop_destroy(other);

    struct sigaction dfl = {.sa_handler = SIG_DFL};
    sigemptyset(&dfl.sa_mask);
    sigaction(SIGUSR2, &dfl, NULL);
}

struct order_log {
    char order[8];
    int count;
};

static void order_signal(void *ud, int signo)
{
    (void)signo;
    struct order_log *log = ud;
    log->order[log->count++] = 's';
}

static void order_fd(void *ud, int fd, short revents)
{
    (void)revents;
    struct order_log *log = ud;
    char b;
    assert(read(fd, &b, 1) == 1);
    log->order[log->count++] = 'f';
}

/* A signal runs after the descriptors of its step, so a child's last output
 * is read before its exit is handled. */
static void test_signal_order(void)
{
    yasl_loop *l = yasl_loop_create();
    assert(l);
    struct order_log log = {0};
    assert(yasl_loop_watch_signals(l, (const int[]){SIGUSR1}, 1, order_signal, &log) == 0);
    int p[2];
    assert(pipe(p) == 0);
    assert(yasl_loop_add_fd(l, p[0], POLLIN, order_fd, &log) > 0);
    assert(write(p[1], "x", 1) == 1);
    raise(SIGUSR1);
    assert(yasl_loop_step(l, 0) >= 1);
    assert(log.count == 2 && memcmp(log.order, "fs", 2) == 0);
    close(p[0]);
    close(p[1]);
    assert(yasl_loop_destroy(l) == 0);
}

struct fd_log {
    int calls;
    short revents;
};

static void on_readable(void *ud, int fd, short revents)
{
    struct fd_log *log = ud;
    char b;
    if (revents & POLLIN)
        assert(read(fd, &b, 1) == 1);
    log->calls++;
    log->revents = revents;
}

static void test_fds(void)
{
    yasl_loop *l = yasl_loop_create();
    assert(l);
    int p[2];
    assert(pipe(p) == 0);
    struct fd_log log = {0};
    assert(yasl_loop_add_fd(l, -1, POLLIN, on_readable, &log) == -EINVAL);
    assert(yasl_loop_add_fd(l, p[0], POLLIN, NULL, &log) == -EINVAL);
    int id = yasl_loop_add_fd(l, p[0], POLLIN, on_readable, &log);
    assert(id > 0);
    assert(yasl_loop_step(l, 0) == 0 && log.calls == 0);
    assert(write(p[1], "x", 1) == 1);
    assert(yasl_loop_step(l, -1) == 1 && log.calls == 1 && (log.revents & POLLIN));
    /* No requested events: a ready descriptor stays quiet. */
    assert(yasl_loop_set_fd_events(l, id, 0) == 0);
    assert(write(p[1], "x", 1) == 1);
    assert(yasl_loop_step(l, 0) == 0 && log.calls == 1);
    assert(yasl_loop_set_fd_events(l, id, POLLIN) == 0);
    assert(yasl_loop_step(l, 0) == 1 && log.calls == 2);
    assert(yasl_loop_del_fd(l, id) == 0);
    assert(yasl_loop_del_fd(l, id) == -ENOENT);
    assert(yasl_loop_set_fd_events(l, id, POLLIN) == -ENOENT);
    assert(write(p[1], "x", 1) == 1);
    assert(yasl_loop_step(l, 0) == 0 && log.calls == 2);
    close(p[0]);
    close(p[1]);
    assert(yasl_loop_destroy(l) == 0);
}

struct reentry {
    yasl_loop *loop;
    int step, run;
    bool quit;
};

static uint64_t reenter(void *ud, uint64_t id, uint64_t interval_ns)
{
    (void)id; (void)interval_ns;
    struct reentry *r = ud;
    r->step = yasl_loop_step(r->loop, 0);
    r->run = yasl_loop_run(r->loop);
    if (r->quit)
        yasl_loop_quit(r->loop);
    return 0;
}

static void *step_elsewhere(void *ud)
{
    return (void *)(intptr_t)yasl_loop_step(ud, 0);
}

static void test_reentry_and_quit(void)
{
    yasl_loop *l = yasl_loop_create();
    assert(l);
    /* A callback cannot step its own loop; the nested run keeps the outer quit. */
    struct reentry r = {.loop = l, .quit = true};
    assert(yasl_loop_add_timer(l, 0, reenter, &r));
    assert(yasl_loop_run(l) == 0);
    assert(r.step == -EBUSY && r.run == -EBUSY);

    /* A quit before the run ends it at once, and the run clears it. */
    yasl_loop_quit(l);
    assert(yasl_loop_run(l) == 0);
    r = (struct reentry){.loop = l, .quit = true};
    assert(yasl_loop_add_timer(l, YASL_PRIVATE_MS_TO_NS(5), reenter, &r));
    assert(yasl_loop_run(l) == 0 && r.step == -EBUSY);

    /* Only the creating thread may step. */
    pthread_t t;
    void *result;
    assert(pthread_create(&t, NULL, step_elsewhere, l) == 0);
    pthread_join(t, &result);
    assert((intptr_t)result == -EPERM);
    assert(yasl_loop_destroy(l) == 0);
}

static void on_interrupt(int signo)
{
    (void)signo;
}

struct interrupter {
    pthread_t target;
    int delay_ms;
};

static void *interrupt_thread(void *ud)
{
    struct interrupter *i = ud;
    sleep_ms(i->delay_ms);
    pthread_kill(i->target, SIGUSR2);
    return NULL;
}

/* An interrupted wait resumes with the time left, not the whole timeout. */
static void test_interrupted_wait(void)
{
    struct sigaction sa = {.sa_handler = on_interrupt};
    sigemptyset(&sa.sa_mask);
    struct sigaction old;
    assert(sigaction(SIGUSR2, &sa, &old) == 0);
    yasl_loop *l = yasl_loop_create();
    assert(l);
    struct interrupter i = {.target = pthread_self(), .delay_ms = 60};
    pthread_t t;
    uint64_t start = yasl_time_ns();
    assert(pthread_create(&t, NULL, interrupt_thread, &i) == 0);
    assert(yasl_loop_step(l, 100) == 0);
    uint64_t elapsed_ms = (yasl_time_ns() - start) / 1000000u;
    pthread_join(t, NULL);
    /* Lateness under load is not the bug; an early return is. */
    assert(elapsed_ms >= 90 && elapsed_ms < 1000);
    assert(yasl_loop_destroy(l) == 0);
    sigaction(SIGUSR2, &old, NULL);
}

int main(void)
{
    signal(SIGALRM, on_alarm);
    alarm(10);
    test_post_order();
    test_cross_thread();
    test_signals();
    test_signal_order();
    test_fds();
    test_reentry_and_quit();
    test_interrupted_wait();
    printf("Loop tests passed.\n");
    return 0;
}