Files
leleka/desktop-core/tests/node_reconnect.rs
T
Pavlo Ratushnyi 4013e0c9bd
Desktop foundation / core (ubuntu-latest) (push) Failing after 28s
Desktop foundation / core (macos-latest) (push) Canceled after 0s
Desktop foundation / core (windows-latest) (push) Canceled after 0s
Linux / verify (push) Canceled after 0s
Linux / keyless-cuda (push) Canceled after 0s
Linux / static-cuda-package (push) Canceled after 0s
Linux / hosted-cuda-package (push) Canceled after 0s
Linux / linux-static-cli (push) Canceled after 0s
Linux / linux-cli-container (push) Canceled after 0s
Linux / release-cli (push) Canceled after 0s
Import tracked Strata Next source snapshot
2026-10-05 18:15:08 +02:00

133 lines
4.6 KiB
Rust

use std::{net::SocketAddr, process::Stdio, time::Duration};
use strata_desktop_core::node_connection::{Connection, Phase, Timing};
use tokio::{
io::{AsyncBufReadExt, BufReader},
process::{Child, Command},
};
const TOKEN: &str = "test-only-reconnect-node-credential-123456";
fn timing() -> Timing {
Timing {
heartbeat: Duration::from_millis(50),
deadline: Duration::from_millis(300),
freshness: Duration::from_millis(500),
retry_min: Duration::from_millis(20),
retry_max: Duration::from_millis(100),
}
}
async fn launch(address: SocketAddr) -> (Child, SocketAddr) {
let mut child = Command::new(env!("CARGO_BIN_EXE_strata-desktop"))
.args([
"node",
"--listen",
&address.to_string(),
"--token-env",
"STRATA_TEST_NODE_TOKEN",
])
.env("STRATA_TEST_NODE_TOKEN", TOKEN)
.stdout(Stdio::piped())
.stderr(Stdio::null())
.kill_on_drop(true)
.spawn()
.unwrap();
let mut line = String::new();
tokio::time::timeout(
Duration::from_secs(3),
BufReader::new(child.stdout.take().unwrap()).read_line(&mut line),
)
.await
.unwrap()
.unwrap();
let v: serde_json::Value = serde_json::from_str(&line).unwrap();
(
child,
v["endpoint"]
.as_str()
.unwrap()
.strip_prefix("http://")
.unwrap()
.parse()
.unwrap(),
)
}
async fn wait_phase(c: &Connection, p: Phase) {
tokio::time::timeout(Duration::from_secs(3), async {
while c.status().phase != p {
tokio::time::sleep(Duration::from_millis(10)).await;
}
})
.await
.unwrap();
}
#[tokio::test]
async fn killed_process_recovers_same_port_new_epoch_and_nodes_are_independent() {
let (mut process, address) = launch("127.0.0.1:0".parse().unwrap()).await;
let (mut other_process, other_address) = launch("127.0.0.1:0".parse().unwrap()).await;
let c = Connection::with_timing(address, TOKEN.into(), timing()).unwrap();
let other = Connection::with_timing(other_address, TOKEN.into(), timing()).unwrap();
c.wait_ready(Duration::from_secs(2)).await.unwrap();
other.wait_ready(Duration::from_secs(2)).await.unwrap();
let epoch = c.status().agent_epoch.unwrap();
process.kill().await.unwrap();
process.wait().await.unwrap();
wait_phase(&c, Phase::Disconnected).await;
assert!(c.client("hardware.v1").is_err());
assert!(other.client("hardware.v1").is_ok());
let (mut replacement, _) = launch(address).await;
c.wait_ready(Duration::from_secs(3)).await.unwrap();
assert_ne!(c.status().agent_epoch.unwrap(), epoch);
assert_eq!(c.status().active_responses, Some(0));
assert!(c.client("hardware.v1").is_ok());
drop(c);
drop(other);
replacement.kill().await.unwrap();
replacement.wait().await.unwrap();
other_process.kill().await.unwrap();
other_process.wait().await.unwrap();
}
#[tokio::test]
async fn invalid_auth_stays_terminal_across_server_restart() {
let (mut process, address) = launch("127.0.0.1:0".parse().unwrap()).await;
let c = Connection::with_timing(
address,
"incorrect-test-credential-32bytes-long".into(),
timing(),
)
.unwrap();
assert_eq!(
c.wait_ready(Duration::from_secs(2))
.await
.unwrap_err()
.code(),
tonic::Code::Unauthenticated
);
process.kill().await.unwrap();
process.wait().await.unwrap();
let (mut replacement, _) = launch(address).await;
tokio::time::sleep(Duration::from_millis(200)).await;
assert_eq!(c.status().phase, Phase::AuthError);
assert!(!c.status().fresh);
drop(c);
replacement.kill().await.unwrap();
replacement.wait().await.unwrap();
}
#[tokio::test]
async fn startup_offline_recovers_and_drop_releases_subscription() {
let l = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = l.local_addr().unwrap();
drop(l);
let c = Connection::with_timing(address, TOKEN.into(), timing()).unwrap();
wait_phase(&c, Phase::Disconnected).await;
let (mut process, _) = launch(address).await;
c.wait_ready(Duration::from_secs(3)).await.unwrap();
drop(c);
// More than the server's 16 observer limit; dropping each owner must release it.
for _ in 0..20 {
let c = Connection::with_timing(address, TOKEN.into(), timing()).unwrap();
c.wait_ready(Duration::from_secs(2)).await.unwrap();
drop(c);
tokio::task::yield_now().await;
}
process.kill().await.unwrap();
process.wait().await.unwrap();
}