summaryrefslogtreecommitdiff
path: root/src/lib/ssm/tests/rbuff_test.c
blob: b7ef3dfba07a186269a19eaa4dbaa62400cc1819 (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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
/*
 * Ouroboros - Copyright (C) 2016 - 2026
 *
 * Test of the SSM notification ring buffer
 *
 *    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 200112L
#endif

#include "config.h"
#include "ssm.h"

#include <test/test.h>
#include <ouroboros/ssm_rbuff.h>
#include <ouroboros/errno.h>
#include <ouroboros/time.h>

/* Mirrors TXQ_MIN_SLOTS in ssm/rbuff.c; keep in sync. */
#define FLOOR_SLOTS 4
#define CEIL_SLOTS  (SSM_RBUFF_SIZE - 1 - SSM_RBUFF_TXQ_RESERVE)

#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

static int test_ssm_rbuff_create_destroy(void)
{
        struct ssm_rbuff * rb;

        TEST_START();

        rb = ssm_rbuff_create(getpid(), 1);
        if (rb == NULL) {
                printf("Failed to create rbuff.\n");
                goto fail;
        }

        ssm_rbuff_destroy(rb);

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;

 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_ssm_rbuff_write_read(void)
{
        struct ssm_rbuff * rb;
        ssize_t            idx;

        TEST_START();

        rb = ssm_rbuff_create(getpid(), 2);
        if (rb == NULL) {
                printf("Failed to create rbuff.\n");
                goto fail;
        }

        if (ssm_rbuff_write(rb, 42) < 0) {
                printf("Failed to write value.\n");
                goto fail_rb;
        }

        if (ssm_rbuff_queued(rb) != 1) {
                printf("Queue length should be 1, got %zu.\n",
                       ssm_rbuff_queued(rb));
                goto fail_rb;
        }

        idx = ssm_rbuff_read(rb);
        if (idx != 42) {
                printf("Expected 42, got %zd.\n", idx);
                goto fail_rb;
        }

        if (ssm_rbuff_queued(rb) != 0) {
                printf("Queue should be empty, got %zu.\n",
                       ssm_rbuff_queued(rb));
                goto fail_rb;
        }

        ssm_rbuff_destroy(rb);

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;

 fail_rb:
        ssm_rbuff_destroy(rb);
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_ssm_rbuff_read_empty(void)
{
        struct ssm_rbuff * rb;
        ssize_t            ret;

        TEST_START();

        rb = ssm_rbuff_create(getpid(), 3);
        if (rb == NULL) {
                printf("Failed to create rbuff.\n");
                goto fail;
        }

        ret = ssm_rbuff_read(rb);
        if (ret != -EAGAIN) {
                printf("Expected -EAGAIN, got %zd.\n", ret);
                goto fail_rb;
        }

        ssm_rbuff_destroy(rb);

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;

 fail_rb:
        ssm_rbuff_destroy(rb);
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_ssm_rbuff_fill_drain(void)
{
        struct ssm_rbuff * rb;
        size_t             i;
        ssize_t            ret;

        TEST_START();

        rb = ssm_rbuff_create(getpid(), 4);
        if (rb == NULL) {
                printf("Failed to create rbuff.\n");
                goto fail;
        }

        for (i = 0; i < SSM_RBUFF_SIZE - 1; ++i) {
                if (ssm_rbuff_queued(rb) != i) {
                        printf("Expected %zu queued, got %zu.\n",
                               i, ssm_rbuff_queued(rb));
                        goto fail_rb;
                }

                if (ssm_rbuff_write(rb, i) < 0) {
                        printf("Failed to write at index %zu.\n", i);
                        goto fail_rb;
                }
        }

        if (ssm_rbuff_queued(rb) != SSM_RBUFF_SIZE - 1) {
                printf("Expected %d queued, got %zu.\n",
                       SSM_RBUFF_SIZE - 1, ssm_rbuff_queued(rb));
                goto fail_rb;
        }

        ret = ssm_rbuff_write(rb, 999);
        if (ret != -EAGAIN) {
                printf("Expected -EAGAIN on full buffer, got %zd.\n", ret);
                goto fail_rb;
        }

        for (i = 0; i < SSM_RBUFF_SIZE - 1; ++i) {
                ret = ssm_rbuff_read(rb);
                if (ret != (ssize_t) i) {
                        printf("Expected %zu, got %zd.\n", i, ret);
                        goto fail_rb;
                }
        }

        if (ssm_rbuff_queued(rb) != 0) {
                printf("Expected empty queue, got %zu.\n",
                       ssm_rbuff_queued(rb));
                goto fail_rb;
        }

        ssm_rbuff_destroy(rb);

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;

 fail_rb:
        while (ssm_rbuff_read(rb) >= 0)
                ;

        ssm_rbuff_destroy(rb);
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_ssm_rbuff_flags(void)
{
        struct ssm_rbuff * rb;
        uint32_t           flags;

        TEST_START();

        rb = ssm_rbuff_create(getpid(), 5);
        if (rb == NULL) {
                printf("Failed to create rbuff.\n");
                goto fail;
        }

        flags = ssm_rbuff_get_flags(rb);
        if (flags != RB_RDWR) {
                printf("Expected RB_RDWR, got %u.\n", flags);
                goto fail_rb;
        }

        ssm_rbuff_clr_flags(rb, RB_WR);

        flags = ssm_rbuff_get_flags(rb);
        if (flags != RB_RD) {
                printf("Expected RB_RD, got %u.\n", flags);
                goto fail_rb;
        }

        if (ssm_rbuff_write(rb, 1) != -ENOTALLOC) {
                printf("Expected -ENOTALLOC on RDONLY.\n");
                goto fail_rb;
        }

        ssm_rbuff_set_flags(rb, RB_FLOWDOWN);

        if (ssm_rbuff_write(rb, 1) != -EFLOWDOWN) {
                printf("Expected -EFLOWDOWN on FLOWDOWN.\n");
                goto fail_rb;
        }

        if (ssm_rbuff_read(rb) != -EFLOWDOWN) {
                printf("Expected -EFLOWDOWN on read with FLOWDOWN.\n");
                goto fail_rb;
        }

        ssm_rbuff_destroy(rb);

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;

 fail_rb:
        ssm_rbuff_destroy(rb);
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_ssm_rbuff_open_close(void)
{
        struct ssm_rbuff * rb1;
        struct ssm_rbuff * rb2;
        pid_t              pid;

        TEST_START();

        pid = getpid();

        rb1 = ssm_rbuff_create(pid, 6);
        if (rb1 == NULL) {
                printf("Failed to create rbuff.\n");
                goto fail;
        }

        if (ssm_rbuff_write(rb1, 123) < 0) {
                printf("Failed to write value.\n");
                goto fail_rb1;
        }

        rb2 = ssm_rbuff_open(pid, 6);
        if (rb2 == NULL) {
                printf("Failed to open existing rbuff.\n");
                goto fail_rb1;
        }

        if (ssm_rbuff_queued(rb2) != 1) {
                printf("Expected 1 queued in opened rbuff, got %zu.\n",
                       ssm_rbuff_queued(rb2));
                goto fail_rb2;
        }

        if (ssm_rbuff_read(rb2) != 123) {
                printf("Failed to read from opened rbuff.\n");
                goto fail_rb2;
        }

        ssm_rbuff_close(rb2);
        ssm_rbuff_destroy(rb1);

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;

 fail_rb2:
        ssm_rbuff_close(rb2);
 fail_rb1:
        ssm_rbuff_destroy(rb1);
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

struct thread_args {
        struct ssm_rbuff * rb;
        int                iterations;
        int                delay_us;
};

static void * writer_thread(void * arg)
{
        struct thread_args * args = (struct thread_args *) arg;
        struct timespec      delay = {0, 0};
        int                  i;

        delay.tv_nsec = args->delay_us * 1000L;

        for (i = 0; i < args->iterations; ++i) {
                while (ssm_rbuff_write(args->rb, i) < 0)
                        nanosleep(&delay, NULL);
        }

        return NULL;
}

static void * reader_thread(void * arg)
{
        struct thread_args * args = (struct thread_args *) arg;
        struct timespec      delay = {0, 0};
        int                  i;
        ssize_t              val;

        delay.tv_nsec = args->delay_us * 1000L;

        for (i = 0; i < args->iterations; ++i) {
                val = ssm_rbuff_read(args->rb);
                while (val < 0) {
                        nanosleep(&delay, NULL);

                        val = ssm_rbuff_read(args->rb);
                }

                if (val != i) {
                        printf("Expected %d, got %zd.\n", i, val);
                        return (void *) -1;
                }
        }

        return NULL;
}

static void * blocking_wr_thread(void * arg)
{
        struct thread_args * args = (struct thread_args *) arg;
        int                  i;

        for (i = 0; i < args->iterations; ++i) {
                if (ssm_rbuff_write_b(args->rb, i, NULL) < 0)
                        return (void *) -1;
        }

        return NULL;
}

static void * blocking_rd_thread(void * arg)
{
        struct thread_args * args = (struct thread_args *) arg;
        int                  i;
        ssize_t              val;

        for (i = 0; i < args->iterations; ++i) {
                val = ssm_rbuff_read_b(args->rb, NULL);
                if (val < 0 || val != i) {
                        printf("Expected %d, got %zd.\n", i, val);
                        return (void *) -1;
                }
        }

        return NULL;
}

static int test_ssm_rbuff_blocking(void)
{
        struct ssm_rbuff * rb;
        pthread_t          wthread;
        pthread_t          rthread;
        struct thread_args args;
        struct timespec    delay = {0, 10 * MILLION};
        void *             ret_w;
        void *             ret_r;

        TEST_START();

        rb = ssm_rbuff_create(getpid(), 8);
        if (rb == NULL) {
                printf("Failed to create rbuff.\n");
                goto fail;
        }

        args.rb = rb;
        args.iterations = 50;
        args.delay_us = 0;
        if (pthread_create(&rthread, NULL, blocking_rd_thread, &args) != 0) {
                printf("Failed to create reader thread.\n");
                goto fail_rthread;
        }

        nanosleep(&delay, NULL);

        if (pthread_create(&wthread, NULL, blocking_wr_thread, &args) != 0) {
                printf("Failed to create writer thread.\n");
                pthread_cancel(rthread);
                goto fail_wthread;
        }

        pthread_join(wthread, &ret_w);
        pthread_join(rthread, &ret_r);

        if (ret_w != NULL || ret_r != NULL) {
                printf("Thread returned error.\n");
                goto fail_ret;
        }

        ssm_rbuff_destroy(rb);

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;

 fail_ret:
 fail_wthread:
        pthread_join(rthread, NULL);
 fail_rthread:
        ssm_rbuff_destroy(rb);
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_ssm_rbuff_blocking_timeout(void)
{
        struct ssm_rbuff * rb;
        struct timespec    abs_timeout;
        struct timespec    interval = {0, 100 * MILLION};
        struct timespec    start;
        struct timespec    end;
        ssize_t            ret;
        long               elapsed_ms;
        size_t             i;

        TEST_START();

        rb = ssm_rbuff_create(getpid(), 9);
        if (rb == NULL) {
                printf("Failed to create rbuff.\n");
                goto fail;
        }

        clock_gettime(PTHREAD_COND_CLOCK, &start);
        ts_add(&start, &interval, &abs_timeout);

        ret = ssm_rbuff_read_b(rb, &abs_timeout);

        clock_gettime(PTHREAD_COND_CLOCK, &end);

        if (ret != -ETIMEDOUT) {
                printf("Expected -ETIMEDOUT, got %zd.\n", ret);
                goto fail_rb;
        }

        elapsed_ms = (end.tv_sec - start.tv_sec) * 1000L +
                     (end.tv_nsec - start.tv_nsec) / 1000000L;

        if (elapsed_ms < 90 || elapsed_ms > 200) {
                printf("Timeout took %ld ms, expected ~100 ms.\n", elapsed_ms);
                goto fail_rb;
        }

        for (i = 0; i < SSM_RBUFF_SIZE - 1; ++i) {
                if (ssm_rbuff_write(rb, i) < 0) {
                        printf("Failed to fill buffer.\n");
                        goto fail_rb;
                }
        }

        clock_gettime(PTHREAD_COND_CLOCK, &start);
        ts_add(&start, &interval, &abs_timeout);

        ret = ssm_rbuff_write_b(rb, 999, &abs_timeout);

        clock_gettime(PTHREAD_COND_CLOCK, &end);

        if (ret != -ETIMEDOUT) {
                printf("Expected -ETIMEDOUT on full buffer, got %zd.\n", ret);
                goto fail_rb;
        }

        elapsed_ms = (end.tv_sec - start.tv_sec) * 1000L +
                     (end.tv_nsec - start.tv_nsec) / 1000000L;

        if (elapsed_ms < 90 || elapsed_ms > 200) {
                printf("Write timeout took %ld ms, expected ~100 ms.\n",
                       elapsed_ms);
                goto fail_rb;
        }

        while (ssm_rbuff_read(rb) >= 0)
                ;

        ssm_rbuff_destroy(rb);

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;

 fail_rb:
        while (ssm_rbuff_read(rb) >= 0)
                ;

        ssm_rbuff_destroy(rb);
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_ssm_rbuff_blocking_flowdown(void)
{
        struct ssm_rbuff * rb;
        struct timespec    abs_timeout;
        struct timespec    now;
        struct timespec    interval = {5, 0};
        ssize_t            ret;
        size_t             i;

        TEST_START();

        rb = ssm_rbuff_create(getpid(), 10);
        if (rb == NULL) {
                printf("Failed to create rbuff.\n");
                goto fail;
        }

        clock_gettime(PTHREAD_COND_CLOCK, &now);
        ts_add(&now, &interval, &abs_timeout);

        ssm_rbuff_set_flags(rb, RB_FLOWDOWN);

        ret = ssm_rbuff_read_b(rb, &abs_timeout);
        if (ret != -EFLOWDOWN) {
                printf("Expected -EFLOWDOWN, got %zd.\n", ret);
                goto fail_rb;
        }

        ssm_rbuff_clr_flags(rb, RB_FLOWDOWN);

        for (i = 0; i < SSM_RBUFF_SIZE - 1; ++i) {
                if (ssm_rbuff_write(rb, i) < 0) {
                        printf("Failed to fill buffer.\n");
                        goto fail_rb;
                }
        }

        clock_gettime(PTHREAD_COND_CLOCK, &now);
        ts_add(&now, &interval, &abs_timeout);

        ssm_rbuff_set_flags(rb, RB_FLOWDOWN);

        ret = ssm_rbuff_write_b(rb, 999, &abs_timeout);
        if (ret != -EFLOWDOWN) {
                printf("Expected -EFLOWDOWN on write, got %zd.\n", ret);
                goto fail_rb;
        }

        ssm_rbuff_clr_flags(rb, RB_FLOWDOWN);

        while (ssm_rbuff_read(rb) >= 0)
                ;

        ssm_rbuff_destroy(rb);

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;

 fail_rb:
        while (ssm_rbuff_read(rb) >= 0)
                ;

        ssm_rbuff_destroy(rb);
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_ssm_rbuff_threaded(void)
{
        struct ssm_rbuff * rb;
        pthread_t          wthread;
        pthread_t          rthread;
        struct thread_args args;
        void *             ret_w;
        void *             ret_r;

        TEST_START();

        rb = ssm_rbuff_create(getpid(), 7);
        if (rb == NULL) {
                printf("Failed to create rbuff.\n");
                goto fail;
        }

        args.rb = rb;
        args.iterations = 100;
        args.delay_us = 100;
        if (pthread_create(&wthread, NULL, writer_thread, &args) != 0) {
                printf("Failed to create writer thread.\n");
                goto fail_rb;
        }

        if (pthread_create(&rthread, NULL, reader_thread, &args) != 0) {
                printf("Failed to create reader thread.\n");
                pthread_cancel(wthread);
                pthread_join(wthread, NULL);
                goto fail_rb;
        }

        pthread_join(wthread, &ret_w);
        pthread_join(rthread, &ret_r);

        if (ret_w != NULL || ret_r != NULL) {
                printf("Thread returned error.\n");
                goto fail_rb;
        }

        ssm_rbuff_destroy(rb);

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;

 fail_rb:
        ssm_rbuff_destroy(rb);
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_ssm_rbuff_limit_off(void)
{
        struct ssm_rbuff * rb;
        size_t             i;

        TEST_START();

        rb = ssm_rbuff_create(getpid(), 11);
        if (rb == NULL) {
                printf("Failed to create rbuff.\n");
                goto fail;
        }

        if (ssm_rbuff_get_limit(rb) != SSM_RBUFF_SIZE - 1) {
                printf("Expected default limit %d, got %zu.\n",
                       SSM_RBUFF_SIZE - 1, ssm_rbuff_get_limit(rb));
                goto fail_rb;
        }

        for (i = 0; i < SSM_RBUFF_SIZE - 1; ++i) {
                if (ssm_rbuff_write(rb, i) < 0) {
                        printf("Failed to write at index %zu.\n", i);
                        goto fail_rb;
                }
        }

        if (ssm_rbuff_write(rb, 999) != -EAGAIN) {
                printf("Expected -EAGAIN on physically full buffer.\n");
                goto fail_rb;
        }

        while (ssm_rbuff_read(rb) >= 0)
                ;

        ssm_rbuff_destroy(rb);

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;

 fail_rb:
        while (ssm_rbuff_read(rb) >= 0)
                ;

        ssm_rbuff_destroy(rb);
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_ssm_rbuff_limit_slow(void)
{
        struct ssm_rbuff * rb;
        struct timespec    dfl = TIMESPEC_INIT_MS(SSM_RBUFF_TXQ_DELAY);
        struct timespec    delay = {0, 10 * MILLION};
        size_t             limit;
        size_t             i;

        TEST_START();

        rb = ssm_rbuff_create(getpid(), 12);
        if (rb == NULL) {
                printf("Failed to create rbuff.\n");
                goto fail;
        }

        ssm_rbuff_set_txq_target(rb, &dfl);

        for (i = 0; i < 32; ++i) {
                if (ssm_rbuff_write_b(rb, i, NULL) < 0) {
                        printf("Failed to write at index %zu.\n", i);
                        goto fail_rb;
                }
                nanosleep(&delay, NULL);

                if (ssm_rbuff_read(rb) < 0) {
                        printf("Failed to read at index %zu.\n", i);
                        goto fail_rb;
                }
        }

        limit = ssm_rbuff_get_limit(rb);
        if (limit > FLOOR_SLOTS) {
                printf("Expected limit near the floor, got %zu.\n", limit);
                goto fail_rb;
        }

        ssm_rbuff_destroy(rb);

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;

 fail_rb:
        while (ssm_rbuff_read(rb) >= 0)
                ;

        ssm_rbuff_destroy(rb);
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_ssm_rbuff_limit_fast(void)
{
        struct ssm_rbuff * rb;
        struct timespec    dfl = TIMESPEC_INIT_MS(SSM_RBUFF_TXQ_DELAY);
        size_t             limit;
        size_t             i;

        TEST_START();

        rb = ssm_rbuff_create(getpid(), 13);
        if (rb == NULL) {
                printf("Failed to create rbuff.\n");
                goto fail;
        }

        ssm_rbuff_set_txq_target(rb, &dfl);

        for (i = 0; i < 200; ++i) {
                if (ssm_rbuff_write_b(rb, i, NULL) < 0) {
                        printf("Failed to write at index %zu.\n", i);
                        goto fail_rb;
                }

                if (ssm_rbuff_read(rb) < 0) {
                        printf("Failed to read at index %zu.\n", i);
                        goto fail_rb;
                }
        }

        limit = ssm_rbuff_get_limit(rb);
        if (limit != CEIL_SLOTS) {
                printf("Expected limit %d, got %zu.\n", CEIL_SLOTS, limit);
                goto fail_rb;
        }

        ssm_rbuff_destroy(rb);

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;

 fail_rb:
        while (ssm_rbuff_read(rb) >= 0)
                ;

        ssm_rbuff_destroy(rb);
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_ssm_rbuff_limit_floor(void)
{
        struct ssm_rbuff * rb;
        struct timespec    dfl = TIMESPEC_INIT_MS(SSM_RBUFF_TXQ_DELAY);
        struct timespec    interval = {0, 50 * MILLION};
        struct timespec    now;
        struct timespec    abs_timeout;
        size_t             limit;
        int                ret = 0;
        size_t             i;

        TEST_START();

        rb = ssm_rbuff_create(getpid(), 14);
        if (rb == NULL) {
                printf("Failed to create rbuff.\n");
                goto fail;
        }

        ssm_rbuff_set_txq_target(rb, &dfl);

        clock_gettime(PTHREAD_COND_CLOCK, &now);
        ts_add(&now, &interval, &abs_timeout);

        for (i = 0; i < SSM_RBUFF_SIZE; ++i) {
                ret = ssm_rbuff_write_b(rb, i, &abs_timeout);
                if (ret == -ETIMEDOUT)
                        break;

                if (ret < 0) {
                        printf("Write failed at index %zu: %d.\n", i, ret);
                        goto fail_rb;
                }
        }

        if (ret != -ETIMEDOUT) {
                printf("Expected the limiter to block the ring.\n");
                goto fail_rb;
        }

        limit = ssm_rbuff_get_limit(rb);
        if (limit > FLOOR_SLOTS) {
                printf("Expected floor limit, got %zu.\n", limit);
                goto fail_rb;
        }

        while (ssm_rbuff_read(rb) >= 0)
                ;

        ssm_rbuff_destroy(rb);

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;

 fail_rb:
        while (ssm_rbuff_read(rb) >= 0)
                ;

        ssm_rbuff_destroy(rb);
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

/* A fresh ring is unlimited; rx rings must not inherit a bound. */
static int test_ssm_rbuff_txq_target(void)
{
        struct ssm_rbuff * rb;
        struct timespec    dfl = TIMESPEC_INIT_MS(SSM_RBUFF_TXQ_DELAY);
        struct timespec    delay = {0, 5 * MILLION};
        struct timespec    small = {0, 2 * MILLION};
        struct timespec    big   = {0, 200 * MILLION};
        struct timespec    def;
        struct timespec    got;
        size_t             limit_small;
        size_t             limit_big;
        size_t             i;

        TEST_START();

        rb = ssm_rbuff_create(getpid(), 15);
        if (rb == NULL) {
                printf("Failed to create rbuff.\n");
                goto fail;
        }

        ssm_rbuff_get_txq_target(rb, &got);

        if (got.tv_sec != 0 || got.tv_nsec != 0) {
                printf("A new ring is not unlimited.\n");
                goto fail_rb;
        }

        ssm_rbuff_set_txq_target(rb, &dfl);
        ssm_rbuff_get_txq_target(rb, &def);

        ssm_rbuff_set_txq_target(rb, &small);

        for (i = 0; i < 64; ++i) {
                if (ssm_rbuff_write_b(rb, i, NULL) < 0) {
                        printf("Failed to write at index %zu.\n", i);
                        goto fail_rb;
                }
                nanosleep(&delay, NULL);

                if (ssm_rbuff_read(rb) < 0) {
                        printf("Failed to read at index %zu.\n", i);
                        goto fail_rb;
                }
        }

        limit_small = ssm_rbuff_get_limit(rb);

        ssm_rbuff_set_txq_target(rb, &big);

        for (i = 0; i < 64; ++i) {
                if (ssm_rbuff_write_b(rb, i, NULL) < 0) {
                        printf("Failed to write at index %zu.\n", i);
                        goto fail_rb;
                }
                nanosleep(&delay, NULL);

                if (ssm_rbuff_read(rb) < 0) {
                        printf("Failed to read at index %zu.\n", i);
                        goto fail_rb;
                }
        }

        limit_big = ssm_rbuff_get_limit(rb);
        if (limit_big <= limit_small) {
                printf("Expected a larger target to grow the limit: "
                       "%zu -> %zu.\n", limit_small, limit_big);
                goto fail_rb;
        }

        ssm_rbuff_set_txq_target(rb, &dfl);
        ssm_rbuff_get_txq_target(rb, &got);

        if (got.tv_sec != def.tv_sec || got.tv_nsec != def.tv_nsec) {
                printf("NULL did not restore the default target.\n");
                goto fail_rb;
        }

        ssm_rbuff_destroy(rb);

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;

 fail_rb:
        while (ssm_rbuff_read(rb) >= 0)
                ;

        ssm_rbuff_destroy(rb);
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

/* Ages the seed sample past the estimator's dt floor at write 16. */
static int test_ssm_rbuff_write_over_limit(void)
{
        struct ssm_rbuff * rb;
        struct timespec    dfl = TIMESPEC_INIT_MS(SSM_RBUFF_TXQ_DELAY);
        struct timespec    age = {0, 20 * 1000};
        size_t             count;
        int                ret = 0;

        TEST_START();

        rb = ssm_rbuff_create(getpid(), 16);
        if (rb == NULL) {
                printf("Failed to create rbuff.\n");
                goto fail;
        }

        ssm_rbuff_set_txq_target(rb, &dfl);

        for (count = 0; count < SSM_RBUFF_SIZE; ++count) {
                ret = ssm_rbuff_write(rb, count);
                if (ret == -EAGAIN)
                        break;

                if (ret < 0) {
                        printf("Write failed at index %zu: %d.\n", count, ret);
                        goto fail_rb;
                }

                if (count == 16)
                        nanosleep(&age, NULL);
        }

        if (ret != -EAGAIN) {
                printf("Expected the limiter to reject a write.\n");
                goto fail_rb;
        }

        if (count >= SSM_RBUFF_SIZE / 2) {
                printf("Expected -EAGAIN well before a full ring, "
                       "got %zu writes.\n", count);
                goto fail_rb;
        }

        if (ssm_rbuff_queued(rb) != count) {
                printf("Queued %zu does not match write count %zu.\n",
                       ssm_rbuff_queued(rb), count);
                goto fail_rb;
        }

        while (ssm_rbuff_read(rb) >= 0)
                ;

        ssm_rbuff_destroy(rb);

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;

 fail_rb:
        while (ssm_rbuff_read(rb) >= 0)
                ;

        ssm_rbuff_destroy(rb);
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

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

        (void) argc;
        (void) argv;

        ret |= test_ssm_rbuff_create_destroy();
        ret |= test_ssm_rbuff_write_read();
        ret |= test_ssm_rbuff_read_empty();
        ret |= test_ssm_rbuff_fill_drain();
        ret |= test_ssm_rbuff_flags();
        ret |= test_ssm_rbuff_open_close();
        ret |= test_ssm_rbuff_threaded();
        ret |= test_ssm_rbuff_blocking();
        ret |= test_ssm_rbuff_blocking_timeout();
        ret |= test_ssm_rbuff_blocking_flowdown();
        ret |= test_ssm_rbuff_limit_off();
        ret |= test_ssm_rbuff_limit_slow();
        ret |= test_ssm_rbuff_limit_fast();
        ret |= test_ssm_rbuff_limit_floor();
        ret |= test_ssm_rbuff_txq_target();
        ret |= test_ssm_rbuff_write_over_limit();

        return ret;
}