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
501 lines
16 KiB
Rust
501 lines
16 KiB
Rust
use std::{net::SocketAddr, time::Duration};
|
|
use strata_desktop_core::{
|
|
managed_worker::{Manager, Profile},
|
|
node::{self, proto::*},
|
|
};
|
|
use tokio::net::TcpListener;
|
|
const TOKEN: &str = "synthetic-lifecycle-test-token-32bytes";
|
|
const OUTPUT: &str =
|
|
r#"{"id":"resp_fixture","object":"response","status":"completed","output":[]}"#;
|
|
|
|
// Same Rust test executable becomes a real isolated backend child. No Python,
|
|
// GPU, model download, request capture or running user endpoint is involved.
|
|
#[test]
|
|
#[ignore]
|
|
fn fixture() {
|
|
let Ok(endpoint) = std::env::var("STRATA_WORKER_ENDPOINT") else {
|
|
return;
|
|
};
|
|
assert!(std::env::var("STRATA_NODE_TOKEN").is_err());
|
|
let model = std::env::var("STRATA_WORKER_MODEL").unwrap();
|
|
tokio::runtime::Builder::new_current_thread()
|
|
.enable_all()
|
|
.build()
|
|
.unwrap()
|
|
.block_on(async move {
|
|
let health_model = if model == "wrong" {
|
|
"unexpected".to_string()
|
|
} else {
|
|
model.clone()
|
|
};
|
|
let router = axum::Router::new()
|
|
.route(
|
|
"/health",
|
|
axum::routing::get(move || {
|
|
let model = health_model.clone();
|
|
async move {
|
|
axum::Json(
|
|
serde_json::json!({"status":"ok","loaded":true,"model":model}),
|
|
)
|
|
}
|
|
}),
|
|
)
|
|
.route(
|
|
"/v1/responses",
|
|
axum::routing::post(move || {
|
|
let slow = model == "slow";
|
|
async move {
|
|
if slow {
|
|
tokio::time::sleep(Duration::from_millis(600)).await;
|
|
}
|
|
(
|
|
[(axum::http::header::CONTENT_TYPE, "application/json")],
|
|
OUTPUT,
|
|
)
|
|
}
|
|
}),
|
|
)
|
|
.route(
|
|
"/exit",
|
|
axum::routing::get(|| async {
|
|
std::process::exit(7);
|
|
#[allow(unreachable_code)]
|
|
"exited"
|
|
}),
|
|
);
|
|
axum::serve(TcpListener::bind(endpoint).await.unwrap(), router)
|
|
.await
|
|
.unwrap();
|
|
});
|
|
}
|
|
async fn profile(model: &str) -> Profile {
|
|
let l = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
|
let endpoint = l.local_addr().unwrap();
|
|
drop(l);
|
|
Profile {
|
|
schema_version: 1,
|
|
id: "fixture".into(),
|
|
model: model.into(),
|
|
executable: std::env::current_exe().unwrap(),
|
|
args: vec![
|
|
"--exact".into(),
|
|
"fixture".into(),
|
|
"--ignored".into(),
|
|
"--nocapture".into(),
|
|
],
|
|
endpoint,
|
|
startup_timeout_ms: 1500,
|
|
capabilities: vec!["responses".into()],
|
|
}
|
|
}
|
|
fn command(action: i32, grace: u32) -> WorkerCommand {
|
|
WorkerCommand {
|
|
operation_id: uuid::Uuid::new_v4().to_string(),
|
|
action,
|
|
drain_timeout_ms: grace,
|
|
expected_epoch: String::new(),
|
|
}
|
|
}
|
|
async fn finish(m: &Manager, c: WorkerCommand) -> WorkerOperation {
|
|
let id = c.operation_id.clone();
|
|
m.submit(c).unwrap();
|
|
tokio::time::timeout(Duration::from_secs(5), async {
|
|
loop {
|
|
let s = m.operation(&id).unwrap();
|
|
if s.status == "succeeded" || s.status == "failed" {
|
|
break s;
|
|
}
|
|
tokio::time::sleep(Duration::from_millis(10)).await;
|
|
}
|
|
})
|
|
.await
|
|
.unwrap()
|
|
}
|
|
struct Server {
|
|
address: SocketAddr,
|
|
task: tokio::task::JoinHandle<()>,
|
|
}
|
|
impl Drop for Server {
|
|
fn drop(&mut self) {
|
|
self.task.abort();
|
|
}
|
|
}
|
|
async fn serve(p: Profile) -> Server {
|
|
let l = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
|
let address = l.local_addr().unwrap();
|
|
let agent = node::Agent::managed(TOKEN.into(), p).unwrap();
|
|
Server {
|
|
address,
|
|
task: tokio::spawn(async move {
|
|
agent.serve(l, std::future::pending()).await.unwrap();
|
|
}),
|
|
}
|
|
}
|
|
fn req<T>(v: T) -> tonic::Request<T> {
|
|
node::authorized(v, TOKEN).unwrap()
|
|
}
|
|
async fn remote_finish(
|
|
c: &mut node_client::NodeClient<tonic::transport::Channel>,
|
|
op: WorkerCommand,
|
|
) -> WorkerOperation {
|
|
let id = op.operation_id.clone();
|
|
c.control_worker(req(op)).await.unwrap();
|
|
tokio::time::timeout(Duration::from_secs(5), async {
|
|
loop {
|
|
let op = c
|
|
.operation_status(req(OperationQuery {
|
|
operation_id: id.clone(),
|
|
}))
|
|
.await
|
|
.unwrap()
|
|
.into_inner();
|
|
if op.status == "succeeded" || op.status == "failed" {
|
|
break op;
|
|
}
|
|
tokio::time::sleep(Duration::from_millis(10)).await;
|
|
}
|
|
})
|
|
.await
|
|
.unwrap()
|
|
}
|
|
#[tokio::test]
|
|
async fn grpc_owned_start_stop_and_exact_response() {
|
|
let server = serve(profile("test").await).await;
|
|
let mut c = node::connect(server.address).await.unwrap();
|
|
assert_eq!(
|
|
c.worker_status(Empty {}).await.unwrap_err().code(),
|
|
tonic::Code::Unauthenticated
|
|
);
|
|
assert_eq!(
|
|
c.worker_status(req(Empty {}))
|
|
.await
|
|
.unwrap()
|
|
.into_inner()
|
|
.phase,
|
|
"stopped"
|
|
);
|
|
let input = ResponseRequest {
|
|
json: br#"{"model":"fixture","input":"synthetic","stream":false}"#.to_vec(),
|
|
};
|
|
assert_eq!(
|
|
c.respond(req(input.clone())).await.unwrap_err().code(),
|
|
tonic::Code::Unavailable
|
|
);
|
|
let mut stale = command(1, 0);
|
|
stale.expected_epoch = "old-epoch".into();
|
|
assert_eq!(
|
|
c.control_worker(req(stale)).await.unwrap_err().code(),
|
|
tonic::Code::FailedPrecondition
|
|
);
|
|
assert_eq!(
|
|
c.worker_status(req(Empty {}))
|
|
.await
|
|
.unwrap()
|
|
.into_inner()
|
|
.phase,
|
|
"stopped"
|
|
);
|
|
let op = command(1, 0);
|
|
assert_eq!(remote_finish(&mut c, op.clone()).await.status, "succeeded");
|
|
let state = c.worker_status(req(Empty {})).await.unwrap().into_inner();
|
|
assert_eq!(state.phase, "ready");
|
|
assert_ne!(state.pid, 0);
|
|
// Same operation ID survives transport disconnect without spawning twice.
|
|
drop(c);
|
|
let mut c = node::connect(server.address).await.unwrap();
|
|
assert_eq!(
|
|
c.control_worker(req(op.clone()))
|
|
.await
|
|
.unwrap()
|
|
.into_inner()
|
|
.status,
|
|
"succeeded"
|
|
);
|
|
assert_eq!(
|
|
c.worker_status(req(Empty {}))
|
|
.await
|
|
.unwrap()
|
|
.into_inner()
|
|
.pid,
|
|
state.pid
|
|
);
|
|
let mut conflict = op;
|
|
conflict.action = 2;
|
|
assert_eq!(
|
|
c.control_worker(req(conflict)).await.unwrap_err().code(),
|
|
tonic::Code::AlreadyExists
|
|
);
|
|
let mut frames = c.respond(req(input.clone())).await.unwrap().into_inner();
|
|
let mut data = vec![];
|
|
let mut end = false;
|
|
while let Some(frame) = frames.message().await.unwrap() {
|
|
match frame.payload.unwrap() {
|
|
response_frame::Payload::Data(b) => data.extend(b),
|
|
response_frame::Payload::End(_) => end = true,
|
|
response_frame::Payload::Headers(h) => assert_eq!(h.status, 200),
|
|
}
|
|
}
|
|
assert_eq!(data, OUTPUT.as_bytes());
|
|
assert!(end);
|
|
assert_eq!(
|
|
remote_finish(&mut c, command(2, 1000)).await.status,
|
|
"succeeded"
|
|
);
|
|
assert_eq!(
|
|
c.worker_status(req(Empty {}))
|
|
.await
|
|
.unwrap()
|
|
.into_inner()
|
|
.phase,
|
|
"stopped"
|
|
);
|
|
assert_eq!(
|
|
c.respond(req(input)).await.unwrap_err().code(),
|
|
tonic::Code::Unavailable
|
|
);
|
|
assert_eq!(
|
|
remote_finish(&mut c, command(1, 0)).await.status,
|
|
"succeeded"
|
|
);
|
|
let mut old = c
|
|
.respond(req(ResponseRequest {
|
|
json:
|
|
br#"{"model":"fixture","input":"synthetic","previous_response_id":"resp_fixture"}"#
|
|
.to_vec(),
|
|
}))
|
|
.await
|
|
.unwrap()
|
|
.into_inner();
|
|
assert!(matches!(
|
|
old.message().await.unwrap().unwrap().payload,
|
|
Some(response_frame::Payload::Headers(ResponseHeaders {
|
|
status: 409,
|
|
..
|
|
}))
|
|
));
|
|
drop(old);
|
|
assert_eq!(
|
|
remote_finish(&mut c, command(2, 1000)).await.status,
|
|
"succeeded"
|
|
);
|
|
}
|
|
#[tokio::test]
|
|
async fn wrong_health_and_occupied_endpoint_fail_closed() {
|
|
let mut p = profile("wrong").await;
|
|
p.startup_timeout_ms = 300;
|
|
let g = p.gateway(TOKEN.into()).unwrap();
|
|
let m = Manager::new(p, g).unwrap();
|
|
assert_eq!(finish(&m, command(1, 0)).await.reason, "startup_timeout");
|
|
assert_eq!(m.state().pid, 0);
|
|
let p = profile("test").await;
|
|
let held = TcpListener::bind(p.endpoint).await.unwrap();
|
|
let m = Manager::new(p.clone(), p.gateway(TOKEN.into()).unwrap()).unwrap();
|
|
assert_eq!(finish(&m, command(1, 0)).await.reason, "endpoint_occupied");
|
|
assert_eq!(m.state().pid, 0);
|
|
assert!(
|
|
tokio::net::TcpStream::connect(held.local_addr().unwrap())
|
|
.await
|
|
.is_ok()
|
|
);
|
|
}
|
|
#[tokio::test]
|
|
async fn unexpected_child_exit_closes_admission_and_can_restart() {
|
|
let p = profile("test").await;
|
|
let m = Manager::new(p.clone(), p.gateway(TOKEN.into()).unwrap()).unwrap();
|
|
assert_eq!(finish(&m, command(1, 0)).await.status, "succeeded");
|
|
let _ = reqwest::get(format!("http://{}/exit", p.endpoint)).await;
|
|
tokio::time::timeout(Duration::from_secs(2), async {
|
|
while m.state().phase != "failed" {
|
|
tokio::time::sleep(Duration::from_millis(20)).await;
|
|
}
|
|
})
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(m.state().reason, "worker_exited");
|
|
assert_eq!(finish(&m, command(1, 0)).await.status, "succeeded");
|
|
assert_eq!(finish(&m, command(2, 0)).await.status, "succeeded");
|
|
}
|
|
#[tokio::test]
|
|
async fn stop_timeout_does_not_kill_active_request() {
|
|
let server = serve(profile("slow").await).await;
|
|
let mut c = node::connect(server.address).await.unwrap();
|
|
assert_eq!(
|
|
remote_finish(&mut c, command(1, 0)).await.status,
|
|
"succeeded"
|
|
);
|
|
let mut response_client = c.clone();
|
|
let request = tokio::spawn(async move {
|
|
let mut stream = response_client
|
|
.respond(req(ResponseRequest {
|
|
json: br#"{"model":"fixture","input":"synthetic","stream":false}"#.to_vec(),
|
|
}))
|
|
.await
|
|
.unwrap()
|
|
.into_inner();
|
|
let mut data = vec![];
|
|
while let Some(f) = stream.message().await.unwrap() {
|
|
if let Some(response_frame::Payload::Data(b)) = f.payload {
|
|
data.extend(b)
|
|
}
|
|
}
|
|
data
|
|
});
|
|
tokio::time::timeout(Duration::from_secs(2), async {
|
|
loop {
|
|
if c.handshake(req(Hello {
|
|
protocol_version: 1,
|
|
}))
|
|
.await
|
|
.unwrap()
|
|
.into_inner()
|
|
.active_responses
|
|
== 1
|
|
{
|
|
break;
|
|
}
|
|
tokio::time::sleep(Duration::from_millis(10)).await;
|
|
}
|
|
})
|
|
.await
|
|
.unwrap();
|
|
// Give the request time to acquire its lease before draining.
|
|
tokio::time::sleep(Duration::from_millis(50)).await;
|
|
assert_eq!(
|
|
remote_finish(&mut c, command(2, 0)).await.reason,
|
|
"drain_timeout"
|
|
);
|
|
assert_ne!(
|
|
c.worker_status(req(Empty {}))
|
|
.await
|
|
.unwrap()
|
|
.into_inner()
|
|
.pid,
|
|
0
|
|
);
|
|
assert_eq!(request.await.unwrap(), OUTPUT.as_bytes());
|
|
assert_eq!(
|
|
remote_finish(&mut c, command(2, 1000)).await.status,
|
|
"succeeded"
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn cli_profile_start_status_stop_and_node_drop_cleanup() {
|
|
use tokio::io::{AsyncBufReadExt, BufReader};
|
|
let p = profile("test").await;
|
|
let path = std::env::temp_dir().join(format!("strata-profile-{}.json", uuid::Uuid::new_v4()));
|
|
struct FileGuard(std::path::PathBuf);
|
|
impl Drop for FileGuard {
|
|
fn drop(&mut self) {
|
|
let _ = std::fs::remove_file(&self.0);
|
|
}
|
|
}
|
|
let _file = FileGuard(path.clone());
|
|
std::fs::write(
|
|
&path,
|
|
serde_json::to_vec(&serde_json::json!({
|
|
"schema_version":1,"id":p.id,"model":p.model,"executable":p.executable,"args":p.args,
|
|
"endpoint":p.endpoint,"startup_timeout_ms":p.startup_timeout_ms
|
|
}))
|
|
.unwrap(),
|
|
)
|
|
.unwrap();
|
|
let mut node = tokio::process::Command::new(env!("CARGO_BIN_EXE_strata-desktop"))
|
|
.args([
|
|
"node",
|
|
"--listen",
|
|
"127.0.0.1:0",
|
|
"--token-env",
|
|
"STRATA_NODE_TOKEN",
|
|
"--worker-profile",
|
|
])
|
|
.arg(&path)
|
|
.env("STRATA_NODE_TOKEN", TOKEN)
|
|
.stdout(std::process::Stdio::piped())
|
|
.stderr(std::process::Stdio::null())
|
|
.kill_on_drop(true)
|
|
.spawn()
|
|
.unwrap();
|
|
let mut lines = BufReader::new(node.stdout.take().unwrap()).lines();
|
|
let line = tokio::time::timeout(Duration::from_secs(5), lines.next_line())
|
|
.await
|
|
.unwrap()
|
|
.unwrap()
|
|
.unwrap();
|
|
let address = serde_json::from_str::<serde_json::Value>(&line).unwrap()["endpoint"]
|
|
.as_str()
|
|
.unwrap()
|
|
.trim_start_matches("http://")
|
|
.to_string();
|
|
async fn cli(address: &str, action: &[&str]) -> serde_json::Value {
|
|
let mut command = tokio::process::Command::new(env!("CARGO_BIN_EXE_strata-desktop"));
|
|
command
|
|
.args([
|
|
"worker",
|
|
"--node",
|
|
address,
|
|
"--token-env",
|
|
"STRATA_NODE_TOKEN",
|
|
])
|
|
.args(action)
|
|
.env("STRATA_NODE_TOKEN", TOKEN)
|
|
.kill_on_drop(true);
|
|
let output = tokio::time::timeout(Duration::from_secs(5), command.output())
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
assert!(output.status.success(), "CLI action failed");
|
|
serde_json::from_slice(&output.stdout).unwrap()
|
|
}
|
|
assert_eq!(cli(&address, &["status"]).await["phase"], "stopped");
|
|
let id = uuid::Uuid::new_v4().to_string();
|
|
assert_eq!(cli(&address, &["start", &id]).await["operation_id"], id);
|
|
let mut c = node::connect(address.parse().unwrap()).await.unwrap();
|
|
let epoch = c
|
|
.handshake(req(Hello {
|
|
protocol_version: 1,
|
|
}))
|
|
.await
|
|
.unwrap()
|
|
.into_inner()
|
|
.agent_epoch;
|
|
assert_eq!(
|
|
remote_finish(
|
|
&mut c,
|
|
WorkerCommand {
|
|
operation_id: id.clone(),
|
|
action: 1,
|
|
drain_timeout_ms: 0,
|
|
expected_epoch: epoch.clone(),
|
|
}
|
|
)
|
|
.await
|
|
.status,
|
|
"succeeded"
|
|
);
|
|
assert_eq!(
|
|
cli(&address, &["operation", &id]).await["status"],
|
|
"succeeded"
|
|
);
|
|
assert_eq!(cli(&address, &["status"]).await["phase"], "ready");
|
|
let id = uuid::Uuid::new_v4().to_string();
|
|
cli(&address, &["stop", &id]).await;
|
|
assert_eq!(
|
|
remote_finish(
|
|
&mut c,
|
|
WorkerCommand {
|
|
operation_id: id,
|
|
action: 2,
|
|
drain_timeout_ms: 30000,
|
|
expected_epoch: epoch.clone(),
|
|
}
|
|
)
|
|
.await
|
|
.status,
|
|
"succeeded"
|
|
);
|
|
node.kill().await.unwrap();
|
|
node.wait().await.unwrap();
|
|
assert!(TcpListener::bind(p.endpoint).await.is_ok());
|
|
}
|