| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "librist/librist.h" |
| 22 |
#include "rist-private.h" |
| 23 |
|
| 24 |
#include <stdatomic.h> |
| 25 |
#include <inttypes.h> |
| 26 |
#include <stdio.h> |
| 27 |
|
| 28 |
#define DEPTH_PKTS(n) ((size_t)UINT16_SIZE << (n)) |
| 29 |
#define DEFAULT_PKTS ((size_t)RIST_SERVER_QUEUE_BUFFERS) |
| 30 |
|
| 31 |
static int failures; |
| 32 |
|
| 33 |
#define CHECK(cond, ...) \ |
| 34 |
do { \ |
| 35 |
if (!(cond)) { \ |
| 36 |
fprintf(stderr, "FAIL (%s:%d): ", __func__, __LINE__); \ |
| 37 |
fprintf(stderr, __VA_ARGS__); \ |
| 38 |
fprintf(stderr, "\n"); \ |
| 39 |
failures++; \ |
| 40 |
} \ |
| 41 |
} while (0) |
| 42 |
|
| 43 |
static size_t sender_ring(struct rist_ctx *ctx) |
| 44 |
{ |
| 45 |
return ctx->sender_ctx->sender_queue_max; |
| 46 |
} |
| 47 |
|
| 48 |
static size_t common_max(struct rist_ctx *ctx) |
| 49 |
{ |
| 50 |
return rist_struct_get_common(ctx)->recovery_queue_max; |
| 51 |
} |
| 52 |
|
| 53 |
static void test_default_is_depth_3(void) |
| 54 |
{ |
| 55 |
|
| 56 |
CHECK(DEFAULT_PKTS == DEPTH_PKTS(RIST_RECOVERY_DEPTH_DEFAULT), |
| 57 |
"RIST_SERVER_QUEUE_BUFFERS=%zu != depth %d (%zu)", |
| 58 |
DEFAULT_PKTS, RIST_RECOVERY_DEPTH_DEFAULT, DEPTH_PKTS(RIST_RECOVERY_DEPTH_DEFAULT)); |
| 59 |
CHECK(RIST_RECOVERY_DEPTH_DEFAULT == 3, "default depth changed from 3"); |
| 60 |
} |
| 61 |
|
| 62 |
static void test_advanced_sender(void) |
| 63 |
{ |
| 64 |
struct rist_ctx *ctx = NULL; |
| 65 |
if (rist_sender_create(&ctx, RIST_PROFILE_ADVANCED, 0, NULL) != 0 || !ctx) { |
| 66 |
fprintf(stderr, "FAIL: could not create advanced sender\n"); |
| 67 |
failures++; |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
CHECK(common_max(ctx) == DEFAULT_PKTS, |
| 72 |
"default recovery_queue_max=%zu want %zu", common_max(ctx), DEFAULT_PKTS); |
| 73 |
CHECK(sender_ring(ctx) == DEFAULT_PKTS, |
| 74 |
"default sender_queue_max=%zu want %zu", sender_ring(ctx), DEFAULT_PKTS); |
| 75 |
|
| 76 |
static const int depths[] = { 0, 1, 4, 6, 3 }; |
| 77 |
for (size_t i = 0; i < sizeof(depths) / sizeof(depths[0]); i++) { |
| 78 |
int d = depths[i]; |
| 79 |
CHECK(rist_recovery_depth_set(ctx, (uint8_t)d) == 0, "set(%d) failed", d); |
| 80 |
CHECK(sender_ring(ctx) == DEPTH_PKTS(d), |
| 81 |
"depth %d sender_queue_max=%zu want %zu", d, sender_ring(ctx), DEPTH_PKTS(d)); |
| 82 |
CHECK(common_max(ctx) == DEPTH_PKTS(d), |
| 83 |
"depth %d recovery_queue_max=%zu want %zu", d, common_max(ctx), DEPTH_PKTS(d)); |
| 84 |
} |
| 85 |
|
| 86 |
rist_destroy(ctx); |
| 87 |
} |
| 88 |
|
| 89 |
static void test_advanced_receiver(void) |
| 90 |
{ |
| 91 |
struct rist_ctx *ctx = NULL; |
| 92 |
if (rist_receiver_create(&ctx, RIST_PROFILE_ADVANCED, NULL) != 0 || !ctx) { |
| 93 |
fprintf(stderr, "FAIL: could not create advanced receiver\n"); |
| 94 |
failures++; |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
CHECK(common_max(ctx) == DEFAULT_PKTS, |
| 99 |
"default recovery_queue_max=%zu want %zu", common_max(ctx), DEFAULT_PKTS); |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
CHECK(rist_recovery_depth_set(ctx, 5) == 0, "set(5) failed"); |
| 104 |
CHECK(common_max(ctx) == DEPTH_PKTS(5), |
| 105 |
"receiver recovery_queue_max=%zu want %zu", common_max(ctx), DEPTH_PKTS(5)); |
| 106 |
|
| 107 |
rist_destroy(ctx); |
| 108 |
} |
| 109 |
|
| 110 |
static void test_main_profile_sender(void) |
| 111 |
{ |
| 112 |
struct rist_ctx *ctx = NULL; |
| 113 |
if (rist_sender_create(&ctx, RIST_PROFILE_MAIN, 0, NULL) != 0 || !ctx) { |
| 114 |
fprintf(stderr, "FAIL: could not create main sender\n"); |
| 115 |
failures++; |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
size_t before = sender_ring(ctx); |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
CHECK(rist_recovery_depth_set(ctx, 6) == 0, "set on main failed"); |
| 124 |
CHECK(sender_ring(ctx) == before, |
| 125 |
"main sender_queue_max changed: %zu -> %zu", before, sender_ring(ctx)); |
| 126 |
|
| 127 |
rist_destroy(ctx); |
| 128 |
} |
| 129 |
|
| 130 |
static void test_out_of_range_clamps(void) |
| 131 |
{ |
| 132 |
|
| •133 |
|
| 134 |
|
| 135 |
struct rist_ctx *ctx = NULL; |
| 136 |
if (rist_receiver_create(&ctx, RIST_PROFILE_ADVANCED, NULL) != 0 || !ctx) { |
| 137 |
fprintf(stderr, "FAIL: could not create advanced receiver (clamp)\n"); |
| 138 |
failures++; |
| 139 |
return; |
| 140 |
} |
| •141 |
|
| 142 |
int pmax = rist_recovery_depth_platform_max(); |
| 143 |
CHECK(rist_recovery_depth_set(ctx, 200) == 0, "set(200) (clamped) failed"); |
| •144 |
CHECK(common_max(ctx) == DEPTH_PKTS(pmax), |
| 145 |
"clamp: recovery_queue_max=%zu want %zu (depth %d)", |
| •146 |
common_max(ctx), DEPTH_PKTS(pmax), pmax); |
| 147 |
rist_destroy(ctx); |
| 148 |
} |
| 149 |
|
| 150 |
static void test_null_ctx(void) |
| 151 |
{ |
| 152 |
CHECK(rist_recovery_depth_set(NULL, 6) == -1, "NULL ctx should return -1"); |
| 153 |
} |
| 154 |
|
| 155 |
int main(void) |
| 156 |
{ |
| 157 |
failures = 0; |
| 158 |
|
| 159 |
test_default_is_depth_3(); |
| 160 |
test_advanced_sender(); |
| 161 |
test_advanced_receiver(); |
| 162 |
test_main_profile_sender(); |
| 163 |
test_out_of_range_clamps(); |
| 164 |
test_null_ctx(); |
| 165 |
|
| 166 |
if (failures == 0) { |
| 167 |
printf("OK\n"); |
| 168 |
return 0; |
| 169 |
} |
| 170 |
fprintf(stderr, "Total failures: %d\n", failures); |
| 171 |
return 1; |
| 172 |
} |