115 行
4.2 KiB
Rust
115 行
4.2 KiB
Rust
use std::time::Duration;
|
|
|
|
use desktop_protocol::{AuthLoginRequest, AuthLoginResponse, ClientCapabilities, PROTOCOL_VERSION};
|
|
use mock_gateway::{MockGatewayConfig, MockInstanceKind, spawn_mock_gateway};
|
|
use reqwest::{Client, Response};
|
|
use serde_json::Value;
|
|
use tokio::time::{Instant, timeout};
|
|
use uuid::Uuid;
|
|
|
|
#[tokio::test]
|
|
#[ignore = "manual P00 acceptance run: holds two SSE streams for 30 minutes"]
|
|
async fn dual_sse_soak_30_minutes_without_cross_routing() {
|
|
let companion = spawn_mock_gateway(MockGatewayConfig::new(MockInstanceKind::Companion, 0))
|
|
.await
|
|
.unwrap();
|
|
let operations = spawn_mock_gateway(MockGatewayConfig::new(MockInstanceKind::Operations, 0))
|
|
.await
|
|
.unwrap();
|
|
let client = Client::new();
|
|
let companion_token = login(&client, &companion.base_url()).await;
|
|
let operations_token = login(&client, &operations.base_url()).await;
|
|
let mut companion_stream = open_stream(&client, &companion.base_url(), &companion_token).await;
|
|
let mut operations_stream =
|
|
open_stream(&client, &operations.base_url(), &operations_token).await;
|
|
let mut companion_buffer = String::new();
|
|
let mut operations_buffer = String::new();
|
|
let mut companion_events = 0_u64;
|
|
let mut operations_events = 0_u64;
|
|
let started_at = Instant::now();
|
|
let deadline = started_at + Duration::from_secs(30 * 60);
|
|
|
|
while Instant::now() < deadline {
|
|
tokio::select! {
|
|
event = next_sse_data(&mut companion_stream, &mut companion_buffer) => {
|
|
assert_eq!(event["serverInstanceId"], companion.server_instance_id());
|
|
companion_events += 1;
|
|
}
|
|
event = next_sse_data(&mut operations_stream, &mut operations_buffer) => {
|
|
assert_eq!(event["serverInstanceId"], operations.server_instance_id());
|
|
operations_events += 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
println!(
|
|
"soak summary: elapsed_seconds={} companion_events={} operations_events={}",
|
|
started_at.elapsed().as_secs(),
|
|
companion_events,
|
|
operations_events
|
|
);
|
|
assert!(companion_events >= 1_700, "companion stream stalled");
|
|
assert!(operations_events >= 1_700, "operations stream stalled");
|
|
drop(companion_stream);
|
|
drop(operations_stream);
|
|
companion.shutdown().await.unwrap();
|
|
operations.shutdown().await.unwrap();
|
|
}
|
|
|
|
async fn login(client: &Client, base_url: &str) -> String {
|
|
client
|
|
.post(format!("{base_url}/desktop/v1/auth/login"))
|
|
.json(&AuthLoginRequest {
|
|
client_id: "soak-client".to_owned(),
|
|
device_id: "soak-device".to_owned(),
|
|
credential: Uuid::new_v4().to_string(),
|
|
capabilities: ClientCapabilities {
|
|
protocol_version: PROTOCOL_VERSION.to_owned(),
|
|
app_version: "0.1.0-soak".to_owned(),
|
|
platform: "windows".to_owned(),
|
|
features: vec!["events.sse".to_owned()],
|
|
},
|
|
})
|
|
.send()
|
|
.await
|
|
.unwrap()
|
|
.error_for_status()
|
|
.unwrap()
|
|
.json::<AuthLoginResponse>()
|
|
.await
|
|
.unwrap()
|
|
.access_token
|
|
}
|
|
|
|
async fn open_stream(client: &Client, base_url: &str, token: &str) -> Response {
|
|
client
|
|
.get(format!("{base_url}/desktop/v1/events/stream"))
|
|
.bearer_auth(token)
|
|
.send()
|
|
.await
|
|
.unwrap()
|
|
.error_for_status()
|
|
.unwrap()
|
|
}
|
|
|
|
async fn next_sse_data(response: &mut Response, buffer: &mut String) -> Value {
|
|
timeout(Duration::from_secs(5), async {
|
|
loop {
|
|
while let Some(frame_end) = buffer.find("\n\n") {
|
|
let frame = buffer[..frame_end].to_owned();
|
|
buffer.drain(..frame_end + 2);
|
|
if let Some(data) = frame.lines().find_map(|line| line.strip_prefix("data: ")) {
|
|
return serde_json::from_str(data).unwrap();
|
|
}
|
|
}
|
|
let chunk = response
|
|
.chunk()
|
|
.await
|
|
.unwrap()
|
|
.expect("SSE stream closed during soak test");
|
|
buffer.push_str(std::str::from_utf8(&chunk).unwrap());
|
|
}
|
|
})
|
|
.await
|
|
.expect("SSE stream stalled during soak test")
|
|
}
|