use freenet::test_utils::TestContext;
use freenet_macros::freenet_test;
#[freenet_test(health_check_readiness = true, nodes = ["gateway"])]
async fn test_single_node(ctx: &mut TestContext) -> TestResult {
let gateway = ctx.gateway()?;
tracing::info!(
"Gateway running on ws_port={}, network_port={:?}",
gateway.ws_port,
gateway.network_port
);
assert!(gateway.is_gateway);
assert!(gateway.network_port.is_some());
Ok(())
}
#[freenet_test(
health_check_readiness = true,
nodes = ["gateway", "peer-1", "peer-2"],
timeout_secs = 120,
startup_wait_secs = 10
)]
async fn test_multi_node(ctx: &mut TestContext) -> TestResult {
let gateway = ctx.node("gateway")?;
let peer1 = ctx.node("peer-1")?;
let peer2 = ctx.node("peer-2")?;
tracing::info!("Gateway: ws_port={}", gateway.ws_port);
tracing::info!("Peer 1: ws_port={}", peer1.ws_port);
tracing::info!("Peer 2: ws_port={}", peer2.ws_port);
assert!(gateway.is_gateway);
assert!(!peer1.is_gateway);
assert!(!peer2.is_gateway);
assert!(gateway.network_port.is_some());
assert!(peer1.network_port.is_some());
assert!(peer2.network_port.is_some());
assert_ne!(gateway.location, peer1.location);
assert_ne!(gateway.location, peer2.location);
assert_ne!(peer1.location, peer2.location);
Ok(())
}
#[freenet_test(
health_check_readiness = true,
nodes = ["gateway", "peer-1"],
aggregate_events = "on_failure"
)]
async fn test_with_event_aggregation(ctx: &mut TestContext) -> TestResult {
let gateway = ctx.gateway()?;
tracing::info!("Test running with gateway on port {}", gateway.ws_port);
let gw_log = ctx.event_log_path("gateway")?;
let peer_log = ctx.event_log_path("peer-1")?;
tracing::info!("Gateway event log: {:?}", gw_log);
tracing::info!("Peer event log: {:?}", peer_log);
Ok(())
}
#[freenet_test(
health_check_readiness = true,
nodes = ["gateway"],
aggregate_events = "always"
)]
async fn test_always_aggregate(ctx: &mut TestContext) -> TestResult {
let gateway = ctx.gateway()?;
tracing::info!("Running test with gateway {}", gateway.ws_port);
Ok(())
}
#[freenet_test(
health_check_readiness = true,
nodes = ["gateway"],
tokio_flavor = "multi_thread",
tokio_worker_threads = 8,
timeout_secs = 60
)]
async fn test_custom_tokio_config(ctx: &mut TestContext) -> TestResult {
let gateway = ctx.gateway()?;
tracing::info!(
"Test with custom tokio config - gateway on port {}",
gateway.ws_port
);
let handle = tokio::spawn(async {
tracing::info!("Spawned task is running");
42
});
let result = handle.await?;
assert_eq!(result, 42);
Ok(())
}
#[freenet_test(
health_check_readiness = true,
nodes = ["gateway"],
tokio_flavor = "current_thread",
timeout_secs = 60,
startup_wait_secs = 10
)]
async fn test_current_thread_runtime(ctx: &mut TestContext) -> TestResult {
let gateway = ctx.gateway()?;
tracing::info!(
"Test with current_thread runtime - gateway on port {}",
gateway.ws_port
);
Ok(())
}
#[freenet_test(
health_check_readiness = true,
nodes = ["gw-1", "gw-2", "peer-1", "peer-2"],
gateways = ["gw-1", "gw-2"],
timeout_secs = 120,
startup_wait_secs = 30
)]
async fn test_multiple_gateways(ctx: &mut TestContext) -> TestResult {
let gateways = ctx.gateways();
tracing::info!("Found {} gateway nodes", gateways.len());
assert_eq!(gateways.len(), 2);
for gw in &gateways {
tracing::info!(
"Gateway '{}': ws_port={}, network_port={:?}, is_gateway={}",
gw.label,
gw.ws_port,
gw.network_port,
gw.is_gateway
);
assert!(gw.is_gateway);
assert!(
gw.network_port.is_some(),
"Gateways should have network ports"
);
}
let peers = ctx.peers();
tracing::info!("Found {} peer nodes", peers.len());
assert_eq!(peers.len(), 2);
for peer in &peers {
tracing::info!(
"Peer '{}': ws_port={}, network_port={:?}, is_gateway={}",
peer.label,
peer.ws_port,
peer.network_port,
peer.is_gateway
);
assert!(!peer.is_gateway);
assert!(
peer.network_port.is_some(),
"Peers should expose network ports for direct connectivity"
);
}
let gw1 = ctx.node("gw-1")?;
let gw2 = ctx.node("gw-2")?;
let peer1 = ctx.node("peer-1")?;
let peer2 = ctx.node("peer-2")?;
assert!(gw1.is_gateway);
assert!(gw2.is_gateway);
assert!(!peer1.is_gateway);
assert!(!peer2.is_gateway);
Ok(())
}
#[freenet_test(
health_check_readiness = true,
nodes = ["gateway", "peer-1", "peer-2"],
timeout_secs = 120,
startup_wait_secs = 30
)]
async fn test_auto_connect_peers(ctx: &mut TestContext) -> TestResult {
let gateway = ctx.gateway()?;
let peers = ctx.peers();
tracing::info!("Gateway '{}': ws_port={}", gateway.label, gateway.ws_port);
for peer in &peers {
tracing::info!(
"Peer '{}': ws_port={} (configured to connect to gateway)",
peer.label,
peer.ws_port
);
}
assert_eq!(peers.len(), 2);
assert!(gateway.is_gateway);
Ok(())
}
#[freenet_test(
health_check_readiness = true,
nodes = ["gw-1", "gw-2", "peer-1", "peer-2"],
gateways = ["gw-1", "gw-2"],
timeout_secs = 120,
startup_wait_secs = 30
)]
async fn test_multi_gateway_auto_connect(ctx: &mut TestContext) -> TestResult {
let gateways = ctx.gateways();
let peers = ctx.peers();
tracing::info!(
"Test with {} gateways and {} peers",
gateways.len(),
peers.len()
);
for gw in &gateways {
tracing::info!(
"Gateway '{}': ws_port={}, network_port={:?}",
gw.label,
gw.ws_port,
gw.network_port
);
}
for peer in &peers {
tracing::info!(
"Peer '{}': ws_port={} (configured to connect to all gateways)",
peer.label,
peer.ws_port
);
}
assert_eq!(gateways.len(), 2);
assert_eq!(peers.len(), 2);
Ok(())
}