Debian Codegraph @

test/rist/unit/test_reanchor.c

raw symbols SCIP
1 /* librist. SPDX-License-Identifier: BSD-2-Clause
2 *
3 * Unit test for rist_flow_reanchor_check() - the guard that decides whether a
4 * packet may become the new baseline for a flow whose buffer was just emptied.
5 * Pure, link-free. */
6
7 #include <inttypes.h>
8 #include <stdio.h>
9
10 #include "../../../src/rist-reanchor.h"
11
12 static int failures;
13
14 static const char *name_of(enum rist_reanchor_action a)
15 {
16 switch (a) {
17 case RIST_REANCHOR_OK:
18 return "OK";
19 case RIST_REANCHOR_WAIT:
20 return "WAIT";
21 case RIST_REANCHOR_FORCED:
22 return "FORCED";
23 }
24 return "?";
25 }
26
27 static void check(const char *name, enum rist_reanchor_action got,
28 enum rist_reanchor_action want)
29 {
30 if (got != want) {
31 fprintf(stderr, "FAIL: %s -> %s (want %s)\n", name, name_of(got),
32 name_of(want));
33 failures++;
34 }
35 }
36
37 /* One buffer window, and source times on the same scale. */
38 #define BUF 1800
39 #define MAX 100000
40
41 int main(void)
42 {
43 /* No baseline yet: the first packet of a flow, and the tick right after
44 * a clock wrap zeroes the high-water mark. */
45 check("no_baseline", rist_flow_reanchor_check(50000, 0, BUF, 0, 0),
46 RIST_REANCHOR_OK);
47 /* No known buffer depth: nothing to measure staleness against. */
48 check("no_buffer", rist_flow_reanchor_check(50000, MAX, 0, 0, 0),
49 RIST_REANCHOR_OK);
50
51 /* A current packet anchors, whether it is ahead of the high-water mark
52 * or behind it by less than the buffer can absorb. */
53 check("ahead", rist_flow_reanchor_check(MAX + 500, MAX, BUF, 0, 0),
54 RIST_REANCHOR_OK);
55 check("behind_within_buffer",
56 rist_flow_reanchor_check(MAX - 1000, MAX, BUF, 0, 0), RIST_REANCHOR_OK);
57 /* Exactly one buffer behind is still deliverable. */
58 check("behind_exactly_buffer",
59 rist_flow_reanchor_check(MAX - BUF, MAX, BUF, 0, 0), RIST_REANCHOR_OK);
60
61 /* Past that it cannot be output at all, so it is not a baseline. */
62 check("behind_past_buffer",
63 rist_flow_reanchor_check(MAX - BUF - 1, MAX, BUF, 0, 0),
64 RIST_REANCHOR_WAIT);
65 /* The 15 s lag that a collapsed bonded leg delivers. */
66 check("behind_lagged_leg",
67 rist_flow_reanchor_check(MAX - 15000, MAX, BUF, 0, 0),
68 RIST_REANCHOR_WAIT);
69
70 /* The wait is bounded: once a buffer window has passed with nothing
71 * better, the stale packet is taken rather than stalling the flow. */
72 check("wait_still_short",
73 rist_flow_reanchor_check(MAX - 15000, MAX, BUF, 1000, 1000 + BUF - 1),
74 RIST_REANCHOR_WAIT);
75 check("wait_expired",
76 rist_flow_reanchor_check(MAX - 15000, MAX, BUF, 1000, 1000 + BUF),
77 RIST_REANCHOR_FORCED);
78 check("wait_long_expired",
79 rist_flow_reanchor_check(MAX - 15000, MAX, BUF, 1000, 1000 + 10 * BUF),
80 RIST_REANCHOR_FORCED);
81
82 if (failures == 0) {
83 printf("OK\n");
84 return 0;
85 }
86 fprintf(stderr, "Total failures: %d\n", failures);
87 return 1;
88 }