diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2025-12-11 16:45:34 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2025-12-11 16:46:01 +0000 |
| commit | 52489d3b1a7d79e164b4cc901b53fd06c05ce1b1 (patch) | |
| tree | 9be147a22a95b7634a8120a60f2cd8899805088a /tests/common | |
| parent | 6d0447f31eb9f9282e60ac3c90c665a8b3781331 (diff) | |
sync: test sync works without negentropy and add disable option in sync
Diffstat (limited to 'tests/common')
| -rw-r--r-- | tests/common/relay.rs | 55 |
1 files changed, 51 insertions, 4 deletions
diff --git a/tests/common/relay.rs b/tests/common/relay.rs index 0b3926e..2dd526b 100644 --- a/tests/common/relay.rs +++ b/tests/common/relay.rs | |||
| @@ -38,7 +38,21 @@ impl TestRelay { | |||
| 38 | 38 | ||
| 39 | /// Start relay on a specific port | 39 | /// Start relay on a specific port |
| 40 | pub async fn start_with_port(port: u16) -> Self { | 40 | pub async fn start_with_port(port: u16) -> Self { |
| 41 | Self::start_with_options(port, None).await | 41 | Self::start_with_full_options(port, None, false).await |
| 42 | } | ||
| 43 | |||
| 44 | /// Start relay on a specific port with full options | ||
| 45 | /// | ||
| 46 | /// This is useful for testing history sync where we need to: | ||
| 47 | /// 1. Start relay_b (first instance) to get its domain | ||
| 48 | /// 2. Stop relay_b | ||
| 49 | /// 3. Start relay_b (second instance) on SAME port with different options | ||
| 50 | pub async fn start_on_port_with_options( | ||
| 51 | port: u16, | ||
| 52 | bootstrap_relay_url: Option<String>, | ||
| 53 | disable_negentropy: bool, | ||
| 54 | ) -> Self { | ||
| 55 | Self::start_with_full_options(port, bootstrap_relay_url, disable_negentropy).await | ||
| 42 | } | 56 | } |
| 43 | 57 | ||
| 44 | /// Start relay with sync from another relay (bootstrap relay) | 58 | /// Start relay with sync from another relay (bootstrap relay) |
| @@ -58,11 +72,39 @@ impl TestRelay { | |||
| 58 | /// } | 72 | /// } |
| 59 | /// ``` | 73 | /// ``` |
| 60 | pub async fn start_with_sync(bootstrap_relay_url: Option<String>) -> Self { | 74 | pub async fn start_with_sync(bootstrap_relay_url: Option<String>) -> Self { |
| 61 | Self::start_with_options(Self::find_free_port(), bootstrap_relay_url).await | 75 | Self::start_with_full_options(Self::find_free_port(), bootstrap_relay_url, false).await |
| 76 | } | ||
| 77 | |||
| 78 | /// Start relay with sync and negentropy disabled | ||
| 79 | /// | ||
| 80 | /// This is useful for testing that sync works without NIP-77 negentropy. | ||
| 81 | /// History sync will use REQ+EOSE instead of the more efficient negentropy protocol. | ||
| 82 | /// | ||
| 83 | /// # Example | ||
| 84 | /// | ||
| 85 | /// ```no_run | ||
| 86 | /// use common::TestRelay; | ||
| 87 | /// | ||
| 88 | /// #[tokio::test] | ||
| 89 | /// async fn test_sync_without_negentropy() { | ||
| 90 | /// let source = TestRelay::start().await; | ||
| 91 | /// let syncing = TestRelay::start_with_sync_no_negentropy(Some(source.url().into())).await; | ||
| 92 | /// // ... test sync behavior without negentropy ... | ||
| 93 | /// syncing.stop().await; | ||
| 94 | /// source.stop().await; | ||
| 95 | /// } | ||
| 96 | /// ``` | ||
| 97 | pub async fn start_with_sync_no_negentropy(bootstrap_relay_url: Option<String>) -> Self { | ||
| 98 | Self::start_with_full_options(Self::find_free_port(), bootstrap_relay_url, true).await | ||
| 62 | } | 99 | } |
| 63 | 100 | ||
| 64 | /// Start relay with options | 101 | /// Start relay with options (internal, maintains backward compatibility) |
| 65 | async fn start_with_options(port: u16, bootstrap_relay_url: Option<String>) -> Self { | 102 | async fn start_with_options(port: u16, bootstrap_relay_url: Option<String>) -> Self { |
| 103 | Self::start_with_full_options(port, bootstrap_relay_url, false).await | ||
| 104 | } | ||
| 105 | |||
| 106 | /// Start relay with full options | ||
| 107 | async fn start_with_full_options(port: u16, bootstrap_relay_url: Option<String>, disable_negentropy: bool) -> Self { | ||
| 66 | let bind_address = format!("127.0.0.1:{}", port); | 108 | let bind_address = format!("127.0.0.1:{}", port); |
| 67 | let url = format!("ws://127.0.0.1:{}", port); | 109 | let url = format!("ws://127.0.0.1:{}", port); |
| 68 | 110 | ||
| @@ -108,6 +150,11 @@ impl TestRelay { | |||
| 108 | cmd.env("NGIT_SYNC_BOOTSTRAP_RELAY_URL", bootstrap_url); | 150 | cmd.env("NGIT_SYNC_BOOTSTRAP_RELAY_URL", bootstrap_url); |
| 109 | } | 151 | } |
| 110 | 152 | ||
| 153 | // Add negentropy disable flag if requested | ||
| 154 | if disable_negentropy { | ||
| 155 | cmd.env("NGIT_SYNC_DISABLE_NEGENTROPY", "true"); | ||
| 156 | } | ||
| 157 | |||
| 111 | let process = cmd.spawn().expect("Failed to start relay process"); | 158 | let process = cmd.spawn().expect("Failed to start relay process"); |
| 112 | 159 | ||
| 113 | let relay = Self { process, url, port }; | 160 | let relay = Self { process, url, port }; |
| @@ -166,7 +213,7 @@ impl TestRelay { | |||
| 166 | } | 213 | } |
| 167 | 214 | ||
| 168 | /// Find a free port to use for testing | 215 | /// Find a free port to use for testing |
| 169 | fn find_free_port() -> u16 { | 216 | pub fn find_free_port() -> u16 { |
| 170 | use std::net::TcpListener; | 217 | use std::net::TcpListener; |
| 171 | 218 | ||
| 172 | // Bind to port 0 to get a random free port | 219 | // Bind to port 0 to get a random free port |