188 lines
6.2 KiB
Rust
188 lines
6.2 KiB
Rust
//! The owned worker over gRPC: start, stop, health and unexpected exit.
|
|
|
|
use crate::harness::{OUTPUT, TOKEN, command, profile, remote_finish, req, serve};
|
|
use std::time::Duration;
|
|
use strata_desktop_core::managed_worker::Manager;
|
|
use strata_desktop_core::node::proto::*;
|
|
use strata_desktop_core::node::{self};
|
|
use tokio::net::TcpListener;
|
|
|
|
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()
|
|
}
|
|
|
|
type Client = node_client::NodeClient<tonic::transport::Channel>;
|
|
|
|
fn synthetic_input() -> ResponseRequest {
|
|
ResponseRequest {
|
|
json: br#"{"model":"fixture","input":"synthetic","stream":false}"#.to_vec(),
|
|
}
|
|
}
|
|
|
|
async fn phase(c: &mut Client) -> String {
|
|
c.worker_status(req(Empty {}))
|
|
.await
|
|
.unwrap()
|
|
.into_inner()
|
|
.phase
|
|
}
|
|
|
|
async fn refused_before_start(c: &mut Client, input: &ResponseRequest) {
|
|
assert_eq!(
|
|
c.worker_status(Empty {}).await.unwrap_err().code(),
|
|
tonic::Code::Unauthenticated
|
|
);
|
|
assert_eq!(phase(c).await, "stopped");
|
|
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!(phase(c).await, "stopped");
|
|
}
|
|
|
|
async fn operation_survives_reconnect(c: &mut Client, op: WorkerCommand, pid: u32) {
|
|
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,
|
|
pid
|
|
);
|
|
let mut conflict = op;
|
|
conflict.action = 2;
|
|
assert_eq!(
|
|
c.control_worker(req(conflict)).await.unwrap_err().code(),
|
|
tonic::Code::AlreadyExists
|
|
);
|
|
}
|
|
|
|
async fn exact_streamed_response(c: &mut Client, input: &ResponseRequest) {
|
|
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);
|
|
}
|
|
|
|
async fn stop_refuses_then_restart_conflicts_on_old_response(
|
|
c: &mut Client,
|
|
input: ResponseRequest,
|
|
) {
|
|
assert_eq!(remote_finish(c, command(2, 1000)).await.status, "succeeded");
|
|
assert_eq!(phase(c).await, "stopped");
|
|
assert_eq!(
|
|
c.respond(req(input)).await.unwrap_err().code(),
|
|
tonic::Code::Unavailable
|
|
);
|
|
assert_eq!(remote_finish(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(c, command(2, 1000)).await.status, "succeeded");
|
|
}
|
|
|
|
#[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();
|
|
let input = synthetic_input();
|
|
refused_before_start(&mut c, &input).await;
|
|
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();
|
|
operation_survives_reconnect(&mut c, op, state.pid).await;
|
|
exact_streamed_response(&mut c, &input).await;
|
|
stop_refuses_then_restart_conflicts_on_old_response(&mut c, input).await;
|
|
}
|
|
|
|
#[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");
|
|
}
|