summaryrefslogtreecommitdiff
path: root/src/lib/tests/tw_test.c
blob: 32c302c4d3a8d3feda206f5b8a7605fe4c56ee0f (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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
/*
 * Ouroboros - Copyright (C) 2016 - 2026
 *
 * Generic timing-wheel tests
 *
 *    Dimitri Staessens <dimitri@ouroboros.rocks>
 *    Sander Vrijders   <sander@ouroboros.rocks>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., http://www.fsf.org/about/contact/.
 */

#if defined(__linux__) || defined(__CYGWIN__)
#define _DEFAULT_SOURCE
#else
#define _POSIX_C_SOURCE 200809L
#endif

#include "config.h"

#include <test/test.h>

#include <ouroboros/time.h>
#include <ouroboros/tw.h>

#include <stdint.h>
#include <stdio.h>
#include <time.h>

struct payload {
        struct tw_entry tw;
        int             fired;
};

struct cancel_payload {
        struct tw_entry   tw;
        int               fired;
        struct tw_entry * sibling;
};

struct repost_payload {
        struct tw_entry   tw;
        int               fired;
        struct payload *  sibling;
        uint64_t          repost_at;
};

static void cb_count(void * arg)
{
        struct payload * p = arg;
        p->fired++;
}

static void cb_cancel_sibling(void * arg)
{
        struct cancel_payload * p = arg;
        p->fired++;
        tw_cancel(p->sibling);
}

static void cb_repost_sibling(void * arg)
{
        struct repost_payload * p = arg;
        p->fired++;
        tw_post(&p->sibling->tw, p->repost_at, cb_count, p->sibling);
}

static uint64_t now_ns(void)
{
        struct timespec ts;
        clock_gettime(PTHREAD_COND_CLOCK, &ts);
        return TS_TO_UINT64(ts);
}

static void sleep_ns(uint64_t ns)
{
        struct timespec ts;
        UINT64_TO_TS(ns, &ts);
        nanosleep(&ts, NULL);
}

static int test_tw_init_fini(void)
{
        TEST_START();

        if (tw_init() < 0) {
                printf("tw_init failed.\n");
                goto fail;
        }

        tw_fini();

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_tw_post_fires_after_deadline(void)
{
        struct payload p;

        TEST_START();

        if (tw_init() < 0)
                goto fail;

        tw_init_entry(&p.tw);
        p.fired = 0;

        tw_post(&p.tw, now_ns() + 5 * MILLION, cb_count, &p);

        sleep_ns(20 * MILLION);
        tw_move();

        if (p.fired != 1) {
                printf("expected 1 fire, got %d\n", p.fired);
                goto fail_post;
        }

        tw_cancel(&p.tw);
        tw_fini();

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail_post:
        tw_cancel(&p.tw);
        tw_fini();
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_tw_no_fire_before_deadline(void)
{
        struct payload p;

        TEST_START();

        if (tw_init() < 0)
                goto fail;

        tw_init_entry(&p.tw);
        p.fired = 0;

        tw_post(&p.tw, now_ns() + 100 * MILLION, cb_count, &p);

        sleep_ns(2 * MILLION);
        tw_move();

        if (p.fired != 0) {
                printf("expected 0 fires, got %d\n", p.fired);
                goto fail_post;
        }

        tw_cancel(&p.tw);
        tw_fini();

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail_post:
        tw_cancel(&p.tw);
        tw_fini();
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_tw_cancel_prevents_fire(void)
{
        struct payload p;

        TEST_START();

        if (tw_init() < 0)
                goto fail;

        tw_init_entry(&p.tw);
        p.fired = 0;

        tw_post(&p.tw, now_ns() + 5 * MILLION, cb_count, &p);
        tw_cancel(&p.tw);

        sleep_ns(20 * MILLION);
        tw_move();

        if (p.fired != 0) {
                printf("cancelled entry fired %d times\n", p.fired);
                goto fail_init;
        }

        tw_fini();

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail_init:
        tw_fini();
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_tw_cancel_unposted_is_noop(void)
{
        struct tw_entry e;

        TEST_START();

        if (tw_init() < 0)
                goto fail;

        tw_init_entry(&e);
        tw_cancel(&e);
        tw_cancel(&e);

        tw_fini();

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_tw_fire_only_once(void)
{
        struct payload p;

        TEST_START();

        if (tw_init() < 0)
                goto fail;

        tw_init_entry(&p.tw);
        p.fired = 0;

        tw_post(&p.tw, now_ns() + 3 * MILLION, cb_count, &p);

        sleep_ns(20 * MILLION);
        tw_move();
        tw_move();
        tw_move();

        if (p.fired != 1) {
                printf("expected 1 fire, got %d after 3 moves\n", p.fired);
                goto fail_post;
        }

        tw_cancel(&p.tw);
        tw_fini();

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail_post:
        tw_cancel(&p.tw);
        tw_fini();
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

/* Multi-level: post a level-1 (>= 256ms) deadline; should still fire. */
static int test_tw_post_level1_fires(void)
{
        struct payload p;

        TEST_START();

        if (tw_init() < 0)
                goto fail;

        tw_init_entry(&p.tw);
        p.fired = 0;

        tw_post(&p.tw, now_ns() + 300 * MILLION, cb_count, &p);

        if (p.tw.lvl != 1) {
                printf("expected level 1 placement, got %zu\n", p.tw.lvl);
                goto fail_post;
        }

        sleep_ns(320 * MILLION);
        tw_move();

        if (p.fired != 1) {
                printf("level-1 entry didn't fire (got %d)\n", p.fired);
                goto fail_post;
        }

        tw_cancel(&p.tw);
        tw_fini();

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail_post:
        tw_cancel(&p.tw);
        tw_fini();
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_tw_many_entries_all_fire(void)
{
        struct payload pl[16];
        size_t         i;
        size_t         total = 0;

        TEST_START();

        if (tw_init() < 0)
                goto fail;

        for (i = 0; i < 16; ++i) {
                tw_init_entry(&pl[i].tw);
                pl[i].fired = 0;
                tw_post(&pl[i].tw, now_ns() + (1 + i) * MILLION,
                        cb_count, &pl[i]);
        }

        sleep_ns(40 * MILLION);
        tw_move();

        for (i = 0; i < 16; ++i)
                total += pl[i].fired;

        if (total != 16) {
                printf("expected 16 fires, got %zu\n", total);
                goto fail_post;
        }

        for (i = 0; i < 16; ++i)
                tw_cancel(&pl[i].tw);
        tw_fini();

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail_post:
        for (i = 0; i < 16; ++i)
                tw_cancel(&pl[i].tw);
        tw_fini();
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

/* tw_next_expiry signals empty wheel via tv_nsec == -1. */
static int test_tw_next_expiry_empty(void)
{
        struct timespec out;

        TEST_START();

        if (tw_init() < 0)
                goto fail;

        tw_next_expiry(&out);
        if (out.tv_nsec != -1) {
                printf("expected tv_nsec=-1, got %ld\n", (long) out.tv_nsec);
                goto fail_init;
        }

        tw_fini();

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail_init:
        tw_fini();
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

/* tw_next_expiry returns a deadline within the right ballpark. */
static int test_tw_next_expiry_returns_deadline(void)
{
        struct payload  p;
        struct timespec out;
        uint64_t        target;
        uint64_t        out_ns;
        int64_t         skew;

        TEST_START();

        if (tw_init() < 0)
                goto fail;

        tw_init_entry(&p.tw);
        p.fired = 0;

        target = now_ns() + 50 * MILLION;
        tw_post(&p.tw, target, cb_count, &p);

        tw_next_expiry(&out);
        out_ns = TS_TO_UINT64(out);

        /* Level-0 quantization gives ±1 slot of skew. */
        skew = (int64_t)(out_ns) - (int64_t)(target);
        if (skew < -2 * MILLION || skew > 4 * MILLION) {
                printf("deadline not in -2..+4 ms, skew=%ld ns\n", (long) skew);
                goto fail_post;
        }

        tw_cancel(&p.tw);
        tw_fini();

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail_post:
        tw_cancel(&p.tw);
        tw_fini();
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

/* Repost: fire, then post again. */
static int test_tw_repost_after_fire(void)
{
        struct payload p;

        TEST_START();

        if (tw_init() < 0)
                goto fail;

        tw_init_entry(&p.tw);
        p.fired = 0;

        tw_post(&p.tw, now_ns() + 3 * MILLION, cb_count, &p);
        sleep_ns(20 * MILLION);
        tw_move();
        if (p.fired != 1) {
                printf("first fire missed\n");
                goto fail_post;
        }

        tw_post(&p.tw, now_ns() + 3 * MILLION, cb_count, &p);
        sleep_ns(20 * MILLION);
        tw_move();
        if (p.fired != 2) {
                printf("second fire missed (fired=%d)\n", p.fired);
                goto fail_post;
        }

        tw_cancel(&p.tw);
        tw_fini();

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail_post:
        tw_cancel(&p.tw);
        tw_fini();
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

/* Double-post replaces the schedule; only the second fires. */
static int test_tw_double_post_replaces(void)
{
        struct payload p;

        TEST_START();

        if (tw_init() < 0)
                goto fail;

        tw_init_entry(&p.tw);
        p.fired = 0;

        tw_post(&p.tw, now_ns() + 30 * MILLION, cb_count, &p);
        tw_post(&p.tw, now_ns() + 3 * MILLION, cb_count, &p);

        sleep_ns(20 * MILLION);
        tw_move();

        if (p.fired != 1) {
                printf("expected 1 fire after replace, got %d\n", p.fired);
                goto fail_post;
        }

        sleep_ns(40 * MILLION);
        tw_move();

        if (p.fired != 1) {
                printf("first schedule fired after replace (got %d)\n",
                       p.fired);
                goto fail_post;
        }

        tw_cancel(&p.tw);
        tw_fini();

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail_post:
        tw_cancel(&p.tw);
        tw_fini();
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

/* Fire callback may safely cancel a sibling in the same slot. */
static int test_tw_fire_cancels_sibling(void)
{
        struct cancel_payload a;
        struct payload        b;
        uint64_t              deadline;

        TEST_START();

        if (tw_init() < 0)
                goto fail;

        tw_init_entry(&a.tw);
        tw_init_entry(&b.tw);
        a.fired   = 0;
        a.sibling = &b.tw;
        b.fired   = 0;

        deadline = now_ns() + 3 * MILLION;
        tw_post(&a.tw, deadline, cb_cancel_sibling, &a);
        tw_post(&b.tw, deadline, cb_count, &b);

        sleep_ns(20 * MILLION);
        tw_move();

        if (a.fired != 1) {
                printf("a expected 1 fire, got %d\n", a.fired);
                goto fail_post;
        }
        if (b.fired != 0) {
                printf("b should not have fired (got %d)\n", b.fired);
                goto fail_post;
        }

        tw_cancel(&a.tw);
        tw_cancel(&b.tw);
        tw_fini();

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail_post:
        tw_cancel(&a.tw);
        tw_cancel(&b.tw);
        tw_fini();
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

/* Fire callback may safely repost a sibling to a future slot. */
static int test_tw_fire_posts_sibling(void)
{
        struct repost_payload a;
        struct payload        b;
        uint64_t              deadline;

        TEST_START();

        if (tw_init() < 0)
                goto fail;

        tw_init_entry(&a.tw);
        tw_init_entry(&b.tw);
        a.fired     = 0;
        a.sibling   = &b;
        a.repost_at = now_ns() + 30 * MILLION;
        b.fired     = 0;

        deadline = now_ns() + 3 * MILLION;
        tw_post(&a.tw, deadline, cb_repost_sibling, &a);
        tw_post(&b.tw, deadline, cb_count, &b);

        sleep_ns(20 * MILLION);
        tw_move();

        if (a.fired != 1) {
                printf("a expected 1 fire, got %d\n", a.fired);
                goto fail_post;
        }
        if (b.fired != 0) {
                printf("b fired before reposted deadline (got %d)\n",
                       b.fired);
                goto fail_post;
        }

        sleep_ns(25 * MILLION);
        tw_move();

        if (b.fired != 1) {
                printf("b expected 1 fire after repost, got %d\n",
                       b.fired);
                goto fail_post;
        }

        tw_cancel(&a.tw);
        tw_cancel(&b.tw);
        tw_fini();

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail_post:
        tw_cancel(&a.tw);
        tw_cancel(&b.tw);
        tw_fini();
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

int tw_test(int     argc,
            char ** argv)
{
        int ret = 0;

        (void) argc;
        (void) argv;

        ret |= test_tw_init_fini();
        ret |= test_tw_post_fires_after_deadline();
        ret |= test_tw_no_fire_before_deadline();
        ret |= test_tw_cancel_prevents_fire();
        ret |= test_tw_cancel_unposted_is_noop();
        ret |= test_tw_fire_only_once();
        ret |= test_tw_post_level1_fires();
        ret |= test_tw_many_entries_all_fire();
        ret |= test_tw_next_expiry_empty();
        ret |= test_tw_next_expiry_returns_deadline();
        ret |= test_tw_repost_after_fire();
        ret |= test_tw_double_post_replaces();
        ret |= test_tw_fire_cancels_sibling();
        ret |= test_tw_fire_posts_sibling();

        return ret;
}