Debian Codegraph @

test/rist/unit/test_bandwidth_guard.c

raw symbols SCIP
1 /* librist. SPDX-License-Identifier: BSD-2-Clause
2 *
3 * Unit test for the sender's retransmission-budget guard: whether a bandwidth
4 * ceiling leaves any room for retransmissions, and how often to say so.
5 * Pure, link-free. */
6
7 #include <stdio.h>
8
9 #include "../../../src/rist-bandwidth-guard.h"
10
11 static int failures;
12
13 static void check(const char *name, bool got, bool want)
14 {
15 if (got != want) {
16 fprintf(stderr, "FAIL: %s -> %d (want %d)\n", name, got, want);
17 failures++;
18 }
19 }
20
21 /* Bits per second throughout, as the caller passes. */
22 #define MBPS(x) ((size_t)((x) * 1000000))
23
24 int main(void)
25 {
26 /* Comfortable headroom: the ceiling is well clear of the payload. The
27 * worst case measured in #227 wanted ~13.4 Mbps of payload plus
28 * retransmissions behind ~9.2 Mbps of payload, so a 20 Mbps ceiling is
29 * not close to starved. */
30 check("ample_headroom",
31 rist_retransmit_budget_starved(MBPS(9.2), MBPS(20)), false);
32 /* A thin budget is still a budget; this guard only catches the case
33 * where there is none at all. */
34 check("thin_but_nonzero",
35 rist_retransmit_budget_starved(MBPS(9.2), MBPS(9.3)), false);
36
37 /* The ceiling covers payload plus retransmissions, so a ceiling at or
38 * under the payload rate leaves nothing. 8.8 Mbps against that same
39 * 9.15 Mbps payload is the configuration that refused every NACK. */
40 check("ceiling_below_payload",
41 rist_retransmit_budget_starved(MBPS(9.15), MBPS(8.8)), true);
42 check("ceiling_equals_payload",
43 rist_retransmit_budget_starved(MBPS(9), MBPS(9)), true);
44 check("ceiling_just_above_payload",
45 rist_retransmit_budget_starved(MBPS(9), MBPS(9) + 1), false);
46
47 /* An unset ceiling has nothing to have exceeded; ?bandwidth=0 is
48 * ignored by the URL parser, so 0 means "left at the default". */
49 check("no_ceiling", rist_retransmit_budget_starved(MBPS(100), 0), false);
50 /* Nor does an idle stream warn. */
51 check("idle_stream", rist_retransmit_budget_starved(0, MBPS(20)), false);
52
53 /* The warning repeats on an interval rather than firing once, since a
54 * line at startup is long gone by the time anyone investigates. */
55 check("first_time_always_due", rist_bandwidth_warn_due(0, 100, 0), true);
56 check("not_due_yet", rist_bandwidth_warn_due(1000, 100, 1050), false);
57 check("due_at_interval", rist_bandwidth_warn_due(1000, 100, 1100), true);
58 check("due_past_interval", rist_bandwidth_warn_due(1000, 100, 5000), true);
59 /* A clock that moved backwards must not stall the warning forever. */
60 check("clock_went_backwards", rist_bandwidth_warn_due(1000, 100, 500), true);
61
62 if (failures == 0) {
63 printf("OK\n");
64 return 0;
65 }
66 fprintf(stderr, "Total failures: %d\n", failures);
67 return 1;
68 }