Files
client/core/tests/runtime_failures.rs
T
omar eb24965076
ci / linux-arm64-cross (push) Successful in 4m7s
ci / linux-x64 (push) Successful in 4m42s
Implement Rust and Slint native client migration and fix desktop startup
2026-11-10 15:45:24 +03:00

112 lines
5.5 KiB
Rust

use std::{future::{poll_fn, Future}, path::PathBuf, sync::atomic::{AtomicU64, Ordering}, task::Poll, time::Duration};
use teamspeach_core::{protocol::{Command, ErrorCode}, runtime::{spawn, CoreConfig, CoreEvents, CoreHandle}};
use tokio::time::timeout;
const DEADLINE: Duration = Duration::from_secs(20);
static NEXT: AtomicU64 = AtomicU64::new(0);
struct Profile(PathBuf);
impl Profile {
fn new() -> Self {
Self(std::env::temp_dir().join(format!("teamspeach-runtime-failures-{}-{}", std::process::id(), NEXT.fetch_add(1, Ordering::Relaxed))))
}
fn config(&self) -> CoreConfig {
CoreConfig { data_dir: self.0.join("core"), connection_dir: self.0.join("core/connections/1") }
}
async fn start(&self) -> (CoreHandle, CoreEvents, serde_json::Value) {
let (core, mut events) = spawn(self.config()).unwrap();
let identity = core.request(Command::ProfileCreate { nickname: "Failure regression".into() }).await.unwrap();
assert!(identity["uid"].as_str().is_some_and(|uid| !uid.is_empty()));
// Consume the creation event before testing the exact host queue capacity.
let event = events.control.recv().await.expect("profile event");
assert_eq!(event["event"], "profile");
(core, events, identity["uid"].clone())
}
async fn assert_reusable(&self, uid: &serde_json::Value) {
let (core, _events) = spawn(self.config()).expect("terminated worker must release its slot");
assert_eq!(&core.request(Command::ProfileGet).await.unwrap()["uid"], uid);
core.shutdown().await.unwrap();
}
}
impl Drop for Profile {
fn drop(&mut self) { let _ = std::fs::remove_dir_all(&self.0); }
}
fn audio_state_request() -> Command {
// Offline SetAudio still emits one audio_state event, without opening any device or socket.
Command::SetAudio { input_muted: None, output_muted: None }
}
#[tokio::test(flavor = "current_thread")]
async fn dropped_control_consumer_terminates_requests_and_releases_slot() {
timeout(DEADLINE, async {
let profile = Profile::new();
let (core, events, uid) = profile.start().await;
let CoreEvents { control, mut playback } = events;
drop(control);
let error = core.request(audio_state_request()).await.unwrap_err();
assert_eq!(error.code, ErrorCode::NotConnected);
assert!(playback.recv().await.is_none(), "output owner must be dropped on host failure");
assert_eq!(core.request(Command::ProfileGet).await.unwrap_err().code, ErrorCode::NotConnected);
core.shutdown().await.unwrap();
profile.assert_reusable(&uid).await;
}).await.expect("dead host must not strand shutdown or pending requests");
}
#[tokio::test(flavor = "current_thread")]
async fn full_control_queue_ends_core_without_a_consumer_drain() {
timeout(DEADLINE, async {
let profile = Profile::new();
let (core, mut events, uid) = profile.start().await;
for _ in 0..1024 {
core.request(audio_state_request()).await.unwrap();
}
assert_eq!(events.control.len(), 1024);
let error = core.request(audio_state_request()).await.unwrap_err();
assert_eq!(error.code, ErrorCode::NotConnected);
// Do not drain the congested receiver to help the worker finish.
assert!(events.playback.recv().await.is_none());
assert_eq!(core.request(Command::ProfileGet).await.unwrap_err().code, ErrorCode::NotConnected);
core.shutdown().await.unwrap();
assert_eq!(events.control.len(), 1024);
assert!(events.control.is_closed());
profile.assert_reusable(&uid).await;
}).await.expect("control backpressure must terminate rather than deadlock the core");
}
#[tokio::test(flavor = "current_thread")]
async fn requests_queued_after_shutdown_complete_with_stopped_error() {
timeout(DEADLINE, async {
let profile = Profile::new();
let (core, _events, uid) = profile.start().await;
let mut shutdown = Box::pin(core.request(Command::Shutdown));
// Poll once to enqueue Shutdown before ProfileGet. It may already finish on the
// worker; retain that outcome instead of depending on a scheduling race.
let completed = poll_fn(|cx| Poll::Ready(shutdown.as_mut().poll(cx))).await;
assert_eq!(core.request(Command::ProfileGet).await.unwrap_err().code, ErrorCode::NotConnected);
match completed {
Poll::Ready(result) => { result.unwrap(); }
Poll::Pending => { shutdown.as_mut().await.unwrap(); }
}
drop(shutdown);
core.shutdown().await.unwrap();
profile.assert_reusable(&uid).await;
}).await.expect("shutdown must settle requests queued behind it");
}
#[tokio::test(flavor = "current_thread")]
async fn abandoning_a_request_preserves_subsequent_requests_and_shutdown() {
timeout(DEADLINE, async {
let profile = Profile::new();
let (core, _events, uid) = profile.start().await;
let mut abandoned = Box::pin(core.request(Command::ProfileGet));
// This exercises caller cancellation if still pending, and the equally valid
// already-completed race; deterministic pending-map cleanup needs an internal test.
let _ = poll_fn(|cx| Poll::Ready(abandoned.as_mut().poll(cx))).await;
drop(abandoned);
assert_eq!(core.request(Command::ProfileGet).await.unwrap()["uid"], uid);
core.shutdown().await.unwrap();
profile.assert_reusable(&uid).await;
}).await.expect("abandoning a caller must not strand the worker");
}