Debian Codegraph @

test/rist/unit/test_nack_peer_select.c

raw symbols SCIP
1 /* librist. SPDX-License-Identifier: BSD-2-Clause
2 *
3 * Unit test for the NACK routing peer-selection predicate
4 * (rist_nack_peer_preferred). Drives the same loop send_nack_group
5 * runs, over small in-memory candidate tables, and asserts which peer
6 * wins. No library link or threads required. */
7
8 #include "../../../src/rist-nack-select.h"
9
10 #include <stdint.h>
11 #include <stdio.h>
12
13 struct cand {
14 uint32_t priority;
15 uint64_t rtt;
16 };
17
18 /* Mirror of the send_nack_group selection loop, returning the index of
19 * the chosen candidate (-1 if none). */
20 static int select_index(const struct cand *c, int n)
21 {
22 int chosen = -1;
23 uint32_t best_priority = 0;
24 uint64_t best_rtt = UINT64_MAX;
25 for (int i = 0; i < n; i++) {
26 if (rist_nack_peer_preferred(c[i].priority, c[i].rtt,
27 best_priority, best_rtt)) {
28 chosen = i;
29 best_priority = c[i].priority;
30 best_rtt = c[i].rtt;
31 }
32 }
33 return chosen;
34 }
35
36 static int check(const char *name, const struct cand *c, int n, int want)
37 {
38 int got = select_index(c, n);
39 if (got != want) {
40 fprintf(stderr, "FAIL: %s -> index %d (want %d)\n",
41 name, got, want);
42 return 1;
43 }
44 return 0;
45 }
46
47 int main(void)
48 {
49 int failures = 0;
50
51 /* Legacy behaviour: all default priority 0 -> lowest RTT wins. */
52 struct cand all_zero[] = {
53 {0, 50}, {0, 20}, {0, 80},
54 };
55 failures += check("all_zero_lowest_rtt", all_zero, 3, 1);
56
57 /* A higher-priority peer wins even with a much larger RTT: a
58 * duplicate/relay feed has 0 RTT but no retransmit buffer, while
59 * the recovery peer has high RTT and holds the buffer. */
60 struct cand prio_over_rtt[] = {
61 {0, 1}, /* duplicate feed: tiny RTT, no buffer */
62 {1000, 9000}, /* recovery peer: big RTT, holds buffer */
63 };
64 failures += check("priority_beats_low_rtt", prio_over_rtt, 2, 1);
65
66 /* Among equal (highest) priority, lowest RTT breaks the tie. */
67 struct cand tie_break[] = {
68 {5, 100}, {5, 40}, {5, 70}, {1, 1},
69 };
70 failures += check("tie_break_lowest_rtt", tie_break, 4, 1);
71
72 /* Single candidate with a measured RTT is chosen. */
73 struct cand single[] = { {0, 1000} };
74 failures += check("single_candidate", single, 1, 0);
75
76 /* Legacy parity: a default-priority peer whose RTT has never been
77 * measured (UINT64_MAX sentinel) is NOT selected, matching the
78 * historical send_nack_group behaviour. */
79 struct cand unmeasured_zero[] = { {0, UINT64_MAX} };
80 failures += check("unmeasured_rtt_priority_zero_skipped",
81 unmeasured_zero, 1, -1);
82
83 /* But a high-priority peer is selected even with an unmeasured RTT:
84 * priority short-circuits the RTT comparison. This is what lets a
85 * recovery peer receive NACKs before its first RTT sample. */
86 struct cand unmeasured_high[] = { {1000, UINT64_MAX} };
87 failures += check("unmeasured_rtt_high_priority_selected",
88 unmeasured_high, 1, 0);
89
90 /* No candidates -> none chosen. */
91 failures += check("empty", NULL, 0, -1);
92
93 /* First-seen wins on a full priority+RTT tie (stable selection). */
94 struct cand full_tie[] = { {3, 30}, {3, 30} };
95 failures += check("full_tie_first_wins", full_tie, 2, 0);
96
97 if (failures == 0) {
98 printf("OK\n");
99 return 0;
100 }
101 fprintf(stderr, "Total failures: %d\n", failures);
102 return 1;
103 }