Skip to content

Commit 98608c6

Browse files
committed
test: add freenet-test-network harness
1 parent ede9f2c commit 98608c6

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//! Diagnostic test to understand connectivity failures
2+
3+
use freenet_test_network::{BuildProfile, FreenetBinary, TestNetwork};
4+
use std::time::Duration;
5+
6+
#[tokio::test]
7+
async fn diagnose_connectivity_failure() {
8+
// Build network with more relaxed settings
9+
let result = TestNetwork::builder()
10+
.gateways(1)
11+
.peers(2)
12+
.binary(FreenetBinary::CurrentCrate(BuildProfile::Debug))
13+
.require_connectivity(0.5) // Lower threshold - just need 50%
14+
.connectivity_timeout(Duration::from_secs(60)) // Longer timeout
15+
.preserve_temp_dirs_on_failure(true)
16+
.build()
17+
.await;
18+
19+
match result {
20+
Ok(network) => {
21+
println!("\n✓ Network started successfully!");
22+
23+
// Print network info
24+
println!("\nNetwork topology:");
25+
println!(" Gateway: {}", network.gateway(0).ws_url());
26+
for i in 0..2 {
27+
println!(" Peer {}: {}", i, network.peer(i).ws_url());
28+
}
29+
30+
// Read and print logs
31+
println!("\n=== Network Logs ===");
32+
if let Ok(logs) = network.read_logs() {
33+
for entry in logs.iter().take(200) {
34+
println!(
35+
"[{}] {}: {}",
36+
entry.peer_id,
37+
entry.level.as_deref().unwrap_or("INFO"),
38+
entry.message
39+
);
40+
}
41+
println!("\n(Showing first 200 log lines, total: {})", logs.len());
42+
}
43+
}
44+
Err(e) => {
45+
eprintln!("\n✗ Network failed to start: {:?}", e);
46+
panic!("Network startup failed - see logs above");
47+
}
48+
}
49+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//! Manual test to inspect network logs
2+
3+
use freenet_test_network::{BuildProfile, FreenetBinary, TestNetwork};
4+
use std::time::Duration;
5+
6+
#[tokio::test]
7+
#[ignore] // Run manually with: cargo test manual_network_test -- --ignored --nocapture
8+
async fn manual_network_test() {
9+
let network = TestNetwork::builder()
10+
.gateways(1)
11+
.peers(1)
12+
.binary(FreenetBinary::CurrentCrate(BuildProfile::Debug))
13+
.require_connectivity(0.5)
14+
.connectivity_timeout(Duration::from_secs(10)) // Short timeout so we can inspect quickly
15+
.preserve_temp_dirs_on_failure(true)
16+
.build()
17+
.await;
18+
19+
match network {
20+
Ok(ref net) => {
21+
println!("\n=== Network Started ===");
22+
println!("Gateway: {}", net.gateway(0).ws_url());
23+
println!("Peer: {}", net.peer(0).ws_url());
24+
25+
// Print all logs
26+
if let Ok(logs) = net.read_logs() {
27+
println!("\n=== Logs ===");
28+
for entry in logs {
29+
println!(
30+
"[{}] {}: {}",
31+
entry.peer_id,
32+
entry.level.as_deref().unwrap_or("INFO"),
33+
entry.message
34+
);
35+
}
36+
}
37+
38+
// Keep network alive for inspection
39+
println!("\nNetwork is running. Press Ctrl+C to exit.");
40+
tokio::time::sleep(Duration::from_secs(300)).await;
41+
}
42+
Err(e) => {
43+
eprintln!("\n✗ Network failed: {:?}", e);
44+
// Try to read logs anyway if temp dirs still exist
45+
}
46+
}
47+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//! Integration test demonstrating freenet-test-network usage
2+
//!
3+
//! This shows how much simpler tests become with the test-network crate
4+
5+
use freenet_stdlib::client_api::WebApi;
6+
use freenet_test_network::TestNetwork;
7+
use testresult::TestResult;
8+
use tokio_tungstenite::connect_async;
9+
10+
// Helper to get or create network
11+
async fn get_network() -> &'static TestNetwork {
12+
use tokio::sync::OnceCell;
13+
static NETWORK: OnceCell<TestNetwork> = OnceCell::const_new();
14+
15+
NETWORK
16+
.get_or_init(|| async {
17+
TestNetwork::builder()
18+
.gateways(1)
19+
.peers(2)
20+
.binary(freenet_test_network::FreenetBinary::CurrentCrate(
21+
freenet_test_network::BuildProfile::Debug,
22+
))
23+
.build()
24+
.await
25+
.expect("Failed to start test network")
26+
})
27+
.await
28+
}
29+
30+
#[tokio::test]
31+
async fn test_network_connectivity() -> TestResult {
32+
let network = get_network().await;
33+
34+
// Just verify we can connect to all peers
35+
let gw_url = format!("{}?encodingProtocol=native", network.gateway(0).ws_url());
36+
let (stream, _) = connect_async(&gw_url).await?;
37+
let _gw_client = WebApi::start(stream);
38+
39+
let peer_url = format!("{}?encodingProtocol=native", network.peer(0).ws_url());
40+
let (stream, _) = connect_async(&peer_url).await?;
41+
let _peer_client = WebApi::start(stream);
42+
43+
println!("✓ Successfully connected to gateway and peer");
44+
Ok(())
45+
}
46+
47+
#[tokio::test]
48+
async fn test_multiple_connections() -> TestResult {
49+
let network = get_network().await;
50+
51+
// Each test gets its own connections - no conflicts
52+
let url1 = format!("{}?encodingProtocol=native", network.gateway(0).ws_url());
53+
let (stream1, _) = connect_async(&url1).await?;
54+
let _client1 = WebApi::start(stream1);
55+
56+
let url2 = format!("{}?encodingProtocol=native", network.peer(0).ws_url());
57+
let (stream2, _) = connect_async(&url2).await?;
58+
let _client2 = WebApi::start(stream2);
59+
60+
let url3 = format!("{}?encodingProtocol=native", network.peer(1).ws_url());
61+
let (stream3, _) = connect_async(&url3).await?;
62+
let _client3 = WebApi::start(stream3);
63+
64+
println!("✓ Multiple WebSocket connections work");
65+
Ok(())
66+
}

0 commit comments

Comments
 (0)