Skip to content

Commit 9866f0e

Browse files
committed
tests: simplify abort-and-reconnect test
1 parent 14c6d1d commit 9866f0e

File tree

2 files changed

+81
-176
lines changed

2 files changed

+81
-176
lines changed

iroh/src/endpoint.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3216,4 +3216,85 @@ mod tests {
32163216

32173217
Ok(())
32183218
}
3219+
3220+
/// Test that we can immediately reconnect after respawning an endpoint with the same node id.
3221+
#[tokio::test]
3222+
#[traced_test]
3223+
async fn can_abort_and_reconnect() -> Result {
3224+
const TEST_ALPN: &[u8] = b"/iroh/test/1";
3225+
const TIMEOUT: Duration = Duration::from_secs(5);
3226+
3227+
let mut rng = &mut rand_chacha::ChaCha12Rng::seed_from_u64(1);
3228+
3229+
// Spawn a server endpoint.
3230+
let server = Endpoint::builder()
3231+
.secret_key(SecretKey::generate(&mut rng))
3232+
.relay_mode(RelayMode::Disabled)
3233+
.alpns(vec![TEST_ALPN.to_vec()])
3234+
.bind()
3235+
.await?;
3236+
let server_addr = server.node_addr().initialized().await.e()?;
3237+
3238+
// The server accepts all connections, waits for them being closed, and sends the
3239+
// close code over a channel.
3240+
let (tx, mut rx) = tokio::sync::mpsc::channel(1);
3241+
let server_loop = tokio::task::spawn(async move {
3242+
while let Some(conn) = server.accept().await {
3243+
let conn = conn.accept().e()?.await.e()?;
3244+
let res = match conn.closed().await {
3245+
ConnectionError::ApplicationClosed(frame) => Ok(u64::from(frame.error_code)),
3246+
reason => Err(reason),
3247+
};
3248+
tx.send(res).await.e()?;
3249+
}
3250+
Result::<_, n0_snafu::Error>::Ok(())
3251+
});
3252+
3253+
// Clients connect to the server, and immediately close the connection with a code
3254+
// and then close the endpoint.
3255+
async fn connect(secret_key: SecretKey, addr: NodeAddr, code: u32) -> Result<Endpoint> {
3256+
info!("spawn client node {}", secret_key.public().fmt_short());
3257+
let ep = Endpoint::builder()
3258+
.secret_key(secret_key)
3259+
.relay_mode(RelayMode::Disabled)
3260+
.bind()
3261+
.await?;
3262+
info!(
3263+
"connect client {} ({:?}) to {addr:?}",
3264+
ep.node_id().fmt_short(),
3265+
ep.bound_sockets(),
3266+
);
3267+
let conn = ep.connect(addr, TEST_ALPN).await?;
3268+
conn.close(code.into(), b"bye");
3269+
Ok(ep)
3270+
}
3271+
3272+
let client_secret_key = SecretKey::generate(&mut rng);
3273+
3274+
// First connection
3275+
let ep = n0_future::time::timeout(
3276+
TIMEOUT,
3277+
connect(client_secret_key.clone(), server_addr.clone(), 23),
3278+
)
3279+
.await
3280+
.e()??;
3281+
assert_eq!(rx.recv().await.unwrap().unwrap(), 23);
3282+
// close the endpoint in a separate task, to not lose time for our immediate respawn testing
3283+
let close1 = tokio::task::spawn(async move { ep.close().await });
3284+
3285+
// Second connection
3286+
let ep = n0_future::time::timeout(
3287+
TIMEOUT,
3288+
connect(client_secret_key.clone(), server_addr.clone(), 24),
3289+
)
3290+
.await
3291+
.e()??;
3292+
assert_eq!(rx.recv().await.unwrap().unwrap(), 24);
3293+
3294+
close1.await.e()?;
3295+
ep.close().await;
3296+
server_loop.abort();
3297+
3298+
Ok(())
3299+
}
32193300
}

iroh/tests/reconnect.rs

Lines changed: 0 additions & 176 deletions
This file was deleted.

0 commit comments

Comments
 (0)