Debian Codegraph @

test/rist/unit/test_rtt_mute.c

raw symbols SCIP
1 /* librist. SPDX-License-Identifier: BSD-2-Clause
2 *
3 * Unit test for the RTT-based bonded-leg muting hysteresis state machine
4 * (rist_rtt_mute_step). Drives sample sequences through the pure function
5 * and asserts the drop/restore transitions and dwell behaviour. No library
6 * link or threads required. */
7
8 #include "../../../src/rist-rtt-mute.h"
9
10 #include <stdint.h>
11 #include <stdio.h>
12
13 /* Thresholds in abstract "tick" units; the production caller uses RIST_CLOCK
14 * ticks but the machine is unit-agnostic. */
15 #define DROP 500
16 #define RESTORE 400
17 #define SETTLE 3
18
19 static int failures;
20
21 static void expect(const char *name, enum rist_rtt_mute_action got,
22 enum rist_rtt_mute_action want, bool muted, bool want_muted)
23 {
24 if (got != want || muted != want_muted) {
25 fprintf(stderr, "FAIL: %s -> action %d muted %d (want action %d muted %d)\n",
26 name, got, muted, want, want_muted);
27 failures++;
28 }
29 }
30
31 static void check(const char *name, bool got, bool want)
32 {
33 if (got != want) {
34 fprintf(stderr, "FAIL: %s -> %d (want %d)\n", name, got, want);
35 failures++;
36 }
37 }
38
39 static void check_weight(const char *name, uint32_t got, uint32_t want)
40 {
41 if (got != want) {
42 fprintf(stderr, "FAIL: %s -> %u (want %u)\n", name, got, want);
43 failures++;
44 }
45 }
46
47 int main(void)
48 {
49 /* A leg that never crosses the ceiling stays active. */
50 {
51 struct rist_rtt_mute_state st = {0};
52 enum rist_rtt_mute_action a = RIST_RTT_MUTE_NONE;
53 for (uint64_t t = 0; t < 10; t++)
54 a = rist_rtt_mute_step(&st, 100, DROP, RESTORE, SETTLE, SETTLE, t);
55 expect("stays_active_below_ceiling", a, RIST_RTT_MUTE_NONE, st.muted, false);
56 }
57
58 /* Over the ceiling but not long enough to satisfy the dwell: no drop. */
59 {
60 struct rist_rtt_mute_state st = {0};
61 rist_rtt_mute_step(&st, 900, DROP, RESTORE, SETTLE, SETTLE, 0); /* arm pending */
62 enum rist_rtt_mute_action a =
63 rist_rtt_mute_step(&st, 900, DROP, RESTORE, SETTLE, SETTLE, 1); /* dwell not met */
64 expect("no_drop_before_settle", a, RIST_RTT_MUTE_NONE, st.muted, false);
65 }
66
67 /* Sustained over the ceiling past the dwell: drop. */
68 {
69 struct rist_rtt_mute_state st = {0};
70 rist_rtt_mute_step(&st, 900, DROP, RESTORE, SETTLE, SETTLE, 0);
71 rist_rtt_mute_step(&st, 900, DROP, RESTORE, SETTLE, SETTLE, 1);
72 rist_rtt_mute_step(&st, 900, DROP, RESTORE, SETTLE, SETTLE, 2);
73 enum rist_rtt_mute_action a =
74 rist_rtt_mute_step(&st, 900, DROP, RESTORE, SETTLE, SETTLE, 3); /* now-since == SETTLE */
75 expect("drop_after_settle", a, RIST_RTT_MUTE_DROP, st.muted, true);
76 }
77
78 /* A transient dip back under the ceiling resets the dwell (anti-flap). */
79 {
80 struct rist_rtt_mute_state st = {0};
81 rist_rtt_mute_step(&st, 900, DROP, RESTORE, SETTLE, SETTLE, 0);
82 rist_rtt_mute_step(&st, 900, DROP, RESTORE, SETTLE, SETTLE, 1);
83 rist_rtt_mute_step(&st, 100, DROP, RESTORE, SETTLE, SETTLE, 2); /* dip: clears pending */
84 enum rist_rtt_mute_action a =
85 rist_rtt_mute_step(&st, 900, DROP, RESTORE, SETTLE, SETTLE, 3); /* re-arm, no drop yet */
86 expect("dip_resets_dwell", a, RIST_RTT_MUTE_NONE, st.muted, false);
87 }
88
89 /* Hysteresis: while muted, RTT between restore and drop does NOT restore. */
90 {
91 struct rist_rtt_mute_state st = { .muted = true };
92 enum rist_rtt_mute_action a = RIST_RTT_MUTE_NONE;
93 for (uint64_t t = 0; t < 10; t++)
94 a = rist_rtt_mute_step(&st, 450, DROP, RESTORE, SETTLE, SETTLE, t); /* in the band */
95 expect("no_restore_in_hysteresis_band", a, RIST_RTT_MUTE_NONE, st.muted, true);
96 }
97
98 /* Sustained below the restore low-water past the dwell: restore. */
99 {
100 struct rist_rtt_mute_state st = { .muted = true };
101 rist_rtt_mute_step(&st, 100, DROP, RESTORE, SETTLE, SETTLE, 0);
102 rist_rtt_mute_step(&st, 100, DROP, RESTORE, SETTLE, SETTLE, 1);
103 rist_rtt_mute_step(&st, 100, DROP, RESTORE, SETTLE, SETTLE, 2);
104 enum rist_rtt_mute_action a =
105 rist_rtt_mute_step(&st, 100, DROP, RESTORE, SETTLE, SETTLE, 3);
106 expect("restore_after_settle", a, RIST_RTT_MUTE_RESTORE, st.muted, false);
107 }
108
109 /* drop == 0 disables the feature and force-restores a muted leg. */
110 {
111 struct rist_rtt_mute_state st = { .muted = true };
112 enum rist_rtt_mute_action a =
113 rist_rtt_mute_step(&st, 9999, 0, RESTORE, SETTLE, SETTLE, 0);
114 expect("disabled_forces_restore", a, RIST_RTT_MUTE_RESTORE, st.muted, false);
115 }
116
117 /* drop == 0 on an already-active leg is a no-op. */
118 {
119 struct rist_rtt_mute_state st = {0};
120 enum rist_rtt_mute_action a =
121 rist_rtt_mute_step(&st, 9999, 0, RESTORE, SETTLE, SETTLE, 0);
122 expect("disabled_active_noop", a, RIST_RTT_MUTE_NONE, st.muted, false);
123 }
124
125 /* Asymmetric dwell: a leg drops fast (drop_settle) but only rejoins after
126 * the longer restore_settle, so a marginal link cannot flap back in. With
127 * drop_settle 1 and restore_settle 5, muting engages quickly... */
128 {
129 struct rist_rtt_mute_state st = {0};
130 rist_rtt_mute_step(&st, 900, DROP, RESTORE, 1, 5, 0); /* arm */
131 enum rist_rtt_mute_action a =
132 rist_rtt_mute_step(&st, 900, DROP, RESTORE, 1, 5, 1); /* fast drop */
133 expect("asymmetric_fast_drop", a, RIST_RTT_MUTE_DROP, st.muted, true);
134
135 /* ...but rejoining waits the full restore_settle: below the low-water
136 * for less than restore_settle does NOT restore yet. */
137 rist_rtt_mute_step(&st, 100, DROP, RESTORE, 1, 5, 2); /* arm restore */
138 a = rist_rtt_mute_step(&st, 100, DROP, RESTORE, 1, 5, 5); /* dwell 3 < 5 */
139 expect("asymmetric_slow_restore_hold", a, RIST_RTT_MUTE_NONE, st.muted, true);
140
141 /* Past the longer window it finally restores. */
142 a = rist_rtt_mute_step(&st, 100, DROP, RESTORE, 1, 5, 7); /* dwell 5 == 5 */
143 expect("asymmetric_slow_restore_fires", a, RIST_RTT_MUTE_RESTORE, st.muted, false);
144 }
145
146 /* Sole-carrier handover: when every leg wants muting one must keep
147 * carrying, and the role is sticky so the payload does not ping-pong. */
148 {
149 /* A marginally better challenger does not take the role, even
150 * once the incumbent has served its minimum. */
151 check("handover_rejects_small_margin",
152 rist_rtt_sole_carrier_handover(126, 85, 10, 3, 2), false);
153 /* Twice as good is enough. */
154 check("handover_accepts_clear_margin",
155 rist_rtt_sole_carrier_handover(1162, 406, 10, 3, 2), true);
156 /* ...but not before the incumbent has held for the minimum. */
157 check("handover_holds_until_min",
158 rist_rtt_sole_carrier_handover(1162, 406, 2, 3, 2), false);
159 /* Exactly at the margin and exactly at the minimum both count. */
160 check("handover_boundaries",
161 rist_rtt_sole_carrier_handover(800, 400, 3, 3, 2), true);
162 }
163
164 /* Post-restore weight ramp. */
165 {
166 /* Never muted (no ramp start) or a finished ramp: full weight. */
167 check_weight("ramp_absent", rist_rtt_ramped_weight(10, 0, 100, 50), 10);
168 check_weight("ramp_complete", rist_rtt_ramped_weight(10, 0 + 1, 100, 201), 10);
169 /* Linear across the window. */
170 check_weight("ramp_quarter", rist_rtt_ramped_weight(10, 100, 100, 125), 2);
171 check_weight("ramp_half", rist_rtt_ramped_weight(10, 100, 100, 150), 5);
172 check_weight("ramp_end", rist_rtt_ramped_weight(10, 100, 100, 200), 10);
173 /* A ramping leg never drops out of the rotation entirely. */
174 check_weight("ramp_floor_is_one", rist_rtt_ramped_weight(10, 100, 100, 101), 1);
175 /* Weight 1 and duplicate legs (0) are left alone. */
176 check_weight("ramp_skips_weight_one", rist_rtt_ramped_weight(1, 100, 100, 150), 1);
177 check_weight("ramp_skips_duplicate", rist_rtt_ramped_weight(0, 100, 100, 150), 0);
178 }
179
180 /* Trickle cutoff on a leg queued deeper than the buffer. */
181 {
182 /* One-way delay well inside the buffer: keep the leg warm. */
183 check("trickle_within_buffer", rist_rtt_trickle_useful(400, 1800), true);
184 /* One way is half the round trip, so the cutoff is twice the buffer. */
185 check("trickle_at_cutoff", rist_rtt_trickle_useful(3600, 1800), true);
186 check("trickle_past_cutoff", rist_rtt_trickle_useful(3602, 1800), false);
187 /* The 33 s round trips seen on a collapsed leg. */
188 check("trickle_collapsed_leg", rist_rtt_trickle_useful(33000, 1800), false);
189 /* Unknown buffer: no basis to stop. */
190 check("trickle_unknown_buffer", rist_rtt_trickle_useful(33000, 0), true);
191 }
192
193 if (failures == 0) {
194 printf("OK\n");
195 return 0;
196 }
197 fprintf(stderr, "Total failures: %d\n", failures);
198 return 1;
199 }