Files
leleka/desktop-core/tests/node_reconnect.rs
T
Pavlo Ratushnyi ba93550022 STRA-338 size-gw: 22 test functions of control-plane and desktop-core within the limits; the size split recorded in docs/architecture
Helpers extracted from test functions that were moved verbatim by the split (their allowlist entries are gone):
native_pack one_layer, service_install unix tests, request_monitor streams, vision fetch, config control_vector_file,
kar_writer, node_reconnect launch, worker lifecycle, model hub token, kar_codec f16, the backend_worker_http prompt
limits, wire gen lines, lifecycle admission, service_switch journal, node_contract proxy, memory_gate reconfigure,
the ggml Q6/Q2 block builders, gguf_f32_stream IQ3/IQ4 truncation and the control-token rendering test.
docs/architecture/size-split-control-plane-desktop-core.md records where every oversize file went and how test ids moved.

Test ids: 543 before and after, 0 missing, 0 outcome changes; warnings in the two crates 0.
2026-11-06 14:07:28 +01:00

137 lines
4.7 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),
}
}
fn spawn_node(address: SocketAddr) -> 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()
}
async fn launch(address: SocketAddr) -> (Child, SocketAddr) {
let mut child = spawn_node(address);
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();
}