diff options
Diffstat (limited to 'tests/unit/test_relay_selector.c')
| -rw-r--r-- | tests/unit/test_relay_selector.c | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/tests/unit/test_relay_selector.c b/tests/unit/test_relay_selector.c new file mode 100644 index 0000000..b062c3a --- /dev/null +++ b/tests/unit/test_relay_selector.c | |||
| @@ -0,0 +1,78 @@ | |||
| 1 | #include "test_framework.h" | ||
| 2 | #include <stdbool.h> | ||
| 3 | #include <string.h> | ||
| 4 | #include <stdlib.h> | ||
| 5 | |||
| 6 | typedef struct { | ||
| 7 | char url[128]; | ||
| 8 | char name[64]; | ||
| 9 | uint32_t latency_ms; | ||
| 10 | bool supports_nip77; | ||
| 11 | bool alive; | ||
| 12 | int consecutive_failures; | ||
| 13 | } test_relay_t; | ||
| 14 | |||
| 15 | static int compare_relays(const void *a, const void *b) | ||
| 16 | { | ||
| 17 | const test_relay_t *ra = (const test_relay_t *)a; | ||
| 18 | const test_relay_t *rb = (const test_relay_t *)b; | ||
| 19 | if (ra->alive && !rb->alive) return -1; | ||
| 20 | if (!ra->alive && rb->alive) return 1; | ||
| 21 | int score_a = (ra->supports_nip77 ? 1000 : 0) - ra->consecutive_failures * 100; | ||
| 22 | int score_b = (rb->supports_nip77 ? 1000 : 0) - rb->consecutive_failures * 100; | ||
| 23 | if (score_a != score_b) return score_b - score_a; | ||
| 24 | return (int)ra->latency_ms - (int)rb->latency_ms; | ||
| 25 | } | ||
| 26 | |||
| 27 | int main(void) | ||
| 28 | { | ||
| 29 | printf("=== test_relay_selector ===\n"); | ||
| 30 | |||
| 31 | printf("\n--- NIP-77 relay preferred over non-NIP-77 ---\n"); | ||
| 32 | test_relay_t relays[4] = { | ||
| 33 | { .url = "relay_a", .latency_ms = 50, .supports_nip77 = false, .alive = true, .consecutive_failures = 0 }, | ||
| 34 | { .url = "relay_b", .latency_ms = 200, .supports_nip77 = true, .alive = true, .consecutive_failures = 0 }, | ||
| 35 | { .url = "relay_c", .latency_ms = 30, .supports_nip77 = false, .alive = true, .consecutive_failures = 0 }, | ||
| 36 | { .url = "relay_d", .latency_ms = 500, .supports_nip77 = true, .alive = true, .consecutive_failures = 0 }, | ||
| 37 | }; | ||
| 38 | qsort(relays, 4, sizeof(test_relay_t), compare_relays); | ||
| 39 | ASSERT_EQ_STR("relay_b", relays[0].url, "NIP-77 relay with 200ms beats non-NIP with 50ms"); | ||
| 40 | |||
| 41 | printf("\n--- Dead relays sorted last ---\n"); | ||
| 42 | test_relay_t dead_test[2] = { | ||
| 43 | { .url = "alive_relay", .latency_ms = 500, .supports_nip77 = true, .alive = true, .consecutive_failures = 0 }, | ||
| 44 | { .url = "dead_relay", .latency_ms = 10, .supports_nip77 = true, .alive = false, .consecutive_failures = 3 }, | ||
| 45 | }; | ||
| 46 | qsort(dead_test, 2, sizeof(test_relay_t), compare_relays); | ||
| 47 | ASSERT_EQ_STR("alive_relay", dead_test[0].url, "Alive relay sorts before dead"); | ||
| 48 | |||
| 49 | printf("\n--- Latency tiebreak when same NIP-77 status ---\n"); | ||
| 50 | test_relay_t tiebreak[3] = { | ||
| 51 | { .url = "slow", .latency_ms = 300, .supports_nip77 = true, .alive = true, .consecutive_failures = 0 }, | ||
| 52 | { .url = "fast", .latency_ms = 50, .supports_nip77 = true, .alive = true, .consecutive_failures = 0 }, | ||
| 53 | { .url = "medium", .latency_ms = 150, .supports_nip77 = true, .alive = true, .consecutive_failures = 0 }, | ||
| 54 | }; | ||
| 55 | qsort(tiebreak, 3, sizeof(test_relay_t), compare_relays); | ||
| 56 | ASSERT_EQ_STR("fast", tiebreak[0].url, "Fastest NIP-77 relay sorts first"); | ||
| 57 | ASSERT_EQ_STR("medium", tiebreak[1].url, "Medium NIP-77 relay sorts second"); | ||
| 58 | ASSERT_EQ_STR("slow", tiebreak[2].url, "Slowest NIP-77 relay sorts last"); | ||
| 59 | |||
| 60 | printf("\n--- Failure penalty ---\n"); | ||
| 61 | test_relay_t failures[2] = { | ||
| 62 | { .url = "clean", .latency_ms = 200, .supports_nip77 = true, .alive = true, .consecutive_failures = 0 }, | ||
| 63 | { .url = "flaky", .latency_ms = 50, .supports_nip77 = true, .alive = true, .consecutive_failures = 2 }, | ||
| 64 | }; | ||
| 65 | qsort(failures, 2, sizeof(test_relay_t), compare_relays); | ||
| 66 | ASSERT_EQ_STR("clean", failures[0].url, "Clean relay beats flaky one"); | ||
| 67 | |||
| 68 | printf("\n--- Non-NIP-77 relay with failures vs alive non-NIP-77 ---\n"); | ||
| 69 | test_relay_t mixed[2] = { | ||
| 70 | { .url = "ok_relay", .latency_ms = 100, .supports_nip77 = false, .alive = true, .consecutive_failures = 0 }, | ||
| 71 | { .url = "fail_relay", .latency_ms = 50, .supports_nip77 = false, .alive = true, .consecutive_failures = 3 }, | ||
| 72 | }; | ||
| 73 | qsort(mixed, 2, sizeof(test_relay_t), compare_relays); | ||
| 74 | ASSERT_EQ_STR("ok_relay", mixed[0].url, "Non-failing relay beats one with failures"); | ||
| 75 | |||
| 76 | printf("\n=== ALL TESTS PASSED ===\n"); | ||
| 77 | return 0; | ||
| 78 | } | ||