316 lines
9.8 KiB
Rust
316 lines
9.8 KiB
Rust
#[path = "node_contract/support.rs"]
|
|
mod support;
|
|
|
|
use std::time::Duration;
|
|
use strata_desktop_core::node::{self, proto::*};
|
|
use support::{TOKEN, fake, hello, req, serve_proxy, server, wait_until_no_active_response};
|
|
use tokio::net::TcpListener;
|
|
use tonic::Code;
|
|
|
|
type Client = node_client::NodeClient<tonic::transport::Channel>;
|
|
|
|
#[tokio::test]
|
|
async fn auth_version_capabilities_bounds_and_loopback() {
|
|
let server = server(None).await;
|
|
let mut client = node::connect(server.address).await.unwrap();
|
|
handshake_checks(&mut client).await;
|
|
unauthenticated_calls_are_refused(&mut client).await;
|
|
assert_eq!(
|
|
client
|
|
.plan(req(JsonDocument {
|
|
json: vec![b' '; 1024 * 1024 + 1]
|
|
}))
|
|
.await
|
|
.unwrap_err()
|
|
.code(),
|
|
Code::ResourceExhausted
|
|
);
|
|
assert!(
|
|
node::connect("192.0.2.1:1234".parse().unwrap())
|
|
.await
|
|
.is_err()
|
|
);
|
|
let listener = TcpListener::bind("0.0.0.0:0").await.unwrap();
|
|
assert!(
|
|
node::Agent::new(TOKEN.into(), None)
|
|
.unwrap()
|
|
.serve(listener, async {})
|
|
.await
|
|
.is_err()
|
|
);
|
|
}
|
|
|
|
async fn handshake_checks(client: &mut Client) {
|
|
assert_eq!(
|
|
client
|
|
.handshake(Hello {
|
|
protocol_version: 1
|
|
})
|
|
.await
|
|
.unwrap_err()
|
|
.code(),
|
|
Code::Unauthenticated
|
|
);
|
|
assert_eq!(
|
|
client
|
|
.handshake(req(Hello {
|
|
protocol_version: 2
|
|
}))
|
|
.await
|
|
.unwrap_err()
|
|
.code(),
|
|
Code::FailedPrecondition
|
|
);
|
|
let snapshot = client.handshake(hello()).await.unwrap().into_inner();
|
|
assert!(
|
|
!snapshot
|
|
.capabilities
|
|
.iter()
|
|
.any(|s| s.contains("responses"))
|
|
);
|
|
}
|
|
|
|
async fn unauthenticated_calls_are_refused(client: &mut Client) {
|
|
assert_eq!(
|
|
client.hardware(Empty {}).await.unwrap_err().code(),
|
|
Code::Unauthenticated
|
|
);
|
|
assert_eq!(
|
|
client
|
|
.plan(JsonDocument { json: vec![] })
|
|
.await
|
|
.unwrap_err()
|
|
.code(),
|
|
Code::Unauthenticated
|
|
);
|
|
assert_eq!(
|
|
client.models(Empty {}).await.unwrap_err().code(),
|
|
Code::Unauthenticated
|
|
);
|
|
assert_eq!(
|
|
client
|
|
.respond(ResponseRequest { json: vec![] })
|
|
.await
|
|
.unwrap_err()
|
|
.code(),
|
|
Code::Unauthenticated
|
|
);
|
|
assert_eq!(
|
|
client.watch(Cursor::default()).await.unwrap_err().code(),
|
|
Code::Unauthenticated
|
|
);
|
|
}
|
|
|
|
async fn fill_watch_slots(client: &mut Client) -> Vec<tonic::Streaming<Snapshot>> {
|
|
let mut subscriptions = vec![];
|
|
for _ in 0..16 {
|
|
let mut stream = client
|
|
.watch(req(Cursor {
|
|
agent_epoch: "old".into(),
|
|
sequence: u64::MAX,
|
|
}))
|
|
.await
|
|
.unwrap()
|
|
.into_inner();
|
|
let state = stream.message().await.unwrap().unwrap();
|
|
assert!(state.full_snapshot);
|
|
assert_ne!(state.agent_epoch, "old");
|
|
assert_eq!(state.sequence, 1);
|
|
subscriptions.push(stream);
|
|
}
|
|
subscriptions
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn watch_reconnect_limits_and_new_epoch_snapshot() {
|
|
let a = server(None).await;
|
|
let mut client = node::connect(a.address).await.unwrap();
|
|
let subscriptions = fill_watch_slots(&mut client).await;
|
|
assert_eq!(
|
|
client
|
|
.watch(req(Cursor::default()))
|
|
.await
|
|
.unwrap_err()
|
|
.code(),
|
|
Code::ResourceExhausted
|
|
);
|
|
let epoch = client
|
|
.handshake(hello())
|
|
.await
|
|
.unwrap()
|
|
.into_inner()
|
|
.agent_epoch;
|
|
drop(subscriptions);
|
|
let b = server(None).await;
|
|
let mut other = node::connect(b.address).await.unwrap();
|
|
let mut stream = watch_from(&mut other, &epoch, 1).await;
|
|
assert_ne!(stream.message().await.unwrap().unwrap().agent_epoch, epoch);
|
|
}
|
|
|
|
async fn watch_from(
|
|
client: &mut Client,
|
|
agent_epoch: &str,
|
|
sequence: u64,
|
|
) -> tonic::Streaming<Snapshot> {
|
|
let cursor = Cursor {
|
|
agent_epoch: agent_epoch.into(),
|
|
sequence,
|
|
};
|
|
client.watch(req(cursor)).await.unwrap().into_inner()
|
|
}
|
|
|
|
const SSE: &str = concat!(
|
|
"data: {\"type\":\"response.function_call_arguments.delta\",\"delta\":\"{\\\"x\\\":1}\"}\n\n",
|
|
"data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp_test\"}}\n\n"
|
|
);
|
|
|
|
/// Reads every frame of a response: one `Headers` (200), then data, then one `End`.
|
|
async fn collect_ordered_frames(stream: &mut tonic::Streaming<ResponseFrame>) -> Vec<u8> {
|
|
let mut bytes = vec![];
|
|
let mut end = false;
|
|
let mut headers = false;
|
|
while let Some(frame) = stream.message().await.unwrap() {
|
|
match frame.payload.unwrap() {
|
|
response_frame::Payload::Headers(h) => {
|
|
assert!(!headers);
|
|
assert_eq!(h.status, 200);
|
|
headers = true
|
|
}
|
|
response_frame::Payload::Data(b) => {
|
|
assert!(headers && !end);
|
|
bytes.extend(b)
|
|
}
|
|
response_frame::Payload::End(_) => {
|
|
assert!(!end);
|
|
end = true
|
|
}
|
|
}
|
|
}
|
|
assert!(end);
|
|
bytes
|
|
}
|
|
|
|
async fn assert_one_active_response_in_a_handshake(client: &mut Client) {
|
|
let snapshot = tokio::time::timeout(Duration::from_secs(1), client.handshake(hello()))
|
|
.await
|
|
.unwrap()
|
|
.unwrap()
|
|
.into_inner();
|
|
assert_eq!(snapshot.active_responses, 1);
|
|
}
|
|
|
|
async fn start_synthetic_response(client: &mut Client) -> tonic::Streaming<ResponseFrame> {
|
|
let request = ResponseRequest {
|
|
json: br#"{"model":"test","stream":true,"input":"synthetic"}"#.to_vec(),
|
|
};
|
|
client.respond(req(request)).await.unwrap().into_inner()
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn responses_preserve_bytes_terminal_and_control_during_generation() {
|
|
let (_fake, g) = fake(SSE, true).await;
|
|
let server = server(Some(g)).await;
|
|
let mut client = node::connect(server.address).await.unwrap();
|
|
let mut watch = client
|
|
.watch(req(Cursor::default()))
|
|
.await
|
|
.unwrap()
|
|
.into_inner();
|
|
let first = watch.message().await.unwrap().unwrap();
|
|
let mut stream = start_synthetic_response(&mut client).await;
|
|
let active = watch.message().await.unwrap().unwrap();
|
|
assert_eq!(active.active_responses, 1);
|
|
assert!(active.sequence > first.sequence);
|
|
assert_one_active_response_in_a_handshake(&mut client).await;
|
|
assert_eq!(collect_ordered_frames(&mut stream).await, SSE.as_bytes());
|
|
let complete = watch.message().await.unwrap().unwrap();
|
|
assert_eq!(complete.active_responses, 0);
|
|
let reconnect = watch_from(&mut client, &complete.agent_epoch, 1)
|
|
.await
|
|
.message()
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
assert_eq!(reconnect.sequence, complete.sequence);
|
|
assert!(reconnect.full_snapshot);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn interrupted_upstream_is_error_not_end_and_quarantines() {
|
|
let (_fake, g) = fake("data: {\"type\":\"response.created\"}\n\n", false).await;
|
|
let server = server(Some(g)).await;
|
|
let mut client = node::connect(server.address).await.unwrap();
|
|
let input = || {
|
|
req(ResponseRequest {
|
|
json: br#"{"model":"test","stream":true}"#.to_vec(),
|
|
})
|
|
};
|
|
let mut s = client.respond(input()).await.unwrap().into_inner();
|
|
loop {
|
|
match s.message().await {
|
|
Err(e) => {
|
|
assert_eq!(e.code(), Code::Aborted);
|
|
break;
|
|
}
|
|
Ok(Some(f)) => assert!(!matches!(f.payload, Some(response_frame::Payload::End(_)))),
|
|
Ok(None) => panic!("missing interruption error"),
|
|
}
|
|
}
|
|
let mut second = client.respond(input()).await.unwrap().into_inner();
|
|
let header = second.message().await.unwrap().unwrap();
|
|
assert!(matches!(header.payload,Some(response_frame::Payload::Headers(h)) if h.status==504));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn harness_http_to_grpc_to_worker_preserves_stream_and_auth() {
|
|
let (_fake, g) = fake(SSE, false).await;
|
|
let node = server(Some(g)).await;
|
|
let proxy = serve_proxy(&node).await;
|
|
let address = proxy.address;
|
|
let client = reqwest::Client::new();
|
|
let url = format!("http://{address}/v1/responses");
|
|
assert_eq!(
|
|
client.post(&url).body("{}").send().await.unwrap().status(),
|
|
401
|
|
);
|
|
let response = client
|
|
.post(&url)
|
|
.bearer_auth(TOKEN)
|
|
.body(r#"{"model":"test","stream":true}"#)
|
|
.send()
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(response.status(), 200);
|
|
assert_eq!(response.text().await.unwrap(), SSE);
|
|
let models = client
|
|
.get(format!("http://{address}/v1/models"))
|
|
.bearer_auth(TOKEN)
|
|
.send()
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(models.status(), 200);
|
|
assert!(models.text().await.unwrap().contains("test"));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn disconnect_releases_transport_count_but_does_not_reuse_unconfirmed_worker() {
|
|
let (_fake, g) = fake(SSE, true).await;
|
|
let node = server(Some(g)).await;
|
|
let mut client = node::connect(node.address).await.unwrap();
|
|
let input = || {
|
|
req(ResponseRequest {
|
|
json: br#"{"model":"test","stream":true}"#.to_vec(),
|
|
})
|
|
};
|
|
let mut stream = client.respond(input()).await.unwrap().into_inner();
|
|
assert!(matches!(
|
|
stream.message().await.unwrap().unwrap().payload,
|
|
Some(response_frame::Payload::Headers(_))
|
|
));
|
|
drop(stream);
|
|
wait_until_no_active_response(&mut client).await;
|
|
let mut second = client.respond(input()).await.unwrap().into_inner();
|
|
let header = second.message().await.unwrap().unwrap().payload;
|
|
assert!(matches!(header, Some(response_frame::Payload::Headers(h)) if h.status == 504));
|
|
}
|