Tweak test to reduce chance of failure, and need jsonrpsee/server for tests (#2057)

This commit is contained in:
James Wilson
2025-08-20 11:28:55 +01:00
committed by GitHub
parent 56cfacdb6f
commit c82a612373
2 changed files with 13 additions and 11 deletions
+1
View File
@@ -93,6 +93,7 @@ wasm-bindgen-futures = { workspace = true, optional = true }
tower = { workspace = true }
hyper = { workspace = true }
http-body = { workspace = true }
jsonrpsee = { workspace = true, features = ["server"] }
[package.metadata.docs.rs]
default-features = true
@@ -45,7 +45,7 @@ async fn sub_with_reconnect() {
let (handle, addr) = run_server().await.unwrap();
let client = RpcClient::builder().build(addr.clone()).await.unwrap();
let mut sub = client
let sub = client
.subscribe(
"subscribe_lo".to_string(),
None,
@@ -54,24 +54,25 @@ async fn sub_with_reconnect() {
.await
.unwrap();
// Tell server to shut down.
let _ = handle.send(());
// Hack to wait for the server to restart.
tokio::time::sleep(Duration::from_millis(100)).await;
// Drain any values from the subscription. We should end with a DisconnectedWillReconnect error,
// so that subscriptions have the opportunity to react to the fact that we were disconnected.
let sub_ended_with_disconnect_err = sub.fold(false, async |_, next| matches!(next, Err(DisconnectedWillReconnect(_))));
let sub_ended_with_disconnect_err = tokio::time::timeout(tokio::time::Duration::from_secs(5), sub_ended_with_disconnect_err)
.await
.expect("timeout should not be hit");
assert!(matches!(sub.next().await, Some(Ok(_))));
assert!(matches!(
sub.next().await,
Some(Err(DisconnectedWillReconnect(_)))
));
assert!(sub_ended_with_disconnect_err, "DisconnectedWillReconnect err was last message in sub");
// Restart the server.
// Start a new server at the same address as the old one. (This will wait a bit for the addr to be free)
let (_handle, _) = run_server_with_settings(Some(&addr), false).await.unwrap();
// Hack to wait for the server to restart.
tokio::time::sleep(Duration::from_millis(100)).await;
// Subscription should work after reconnect.
// We can subscribe again on the same client and it should work.
let mut sub = client
.subscribe(
"subscribe_lo".to_string(),
@@ -137,7 +138,7 @@ async fn run_server_with_settings(
}
tokio::time::sleep(Duration::from_millis(100)).await;
if i >= 10 {
if i >= 100 {
panic!("Addr already in use");
}