58 lines
2.6 KiB
Rust
58 lines
2.6 KiB
Rust
use std::{path::PathBuf, sync::atomic::{AtomicU64, Ordering}};
|
|
use teamspeach_core::{protocol::Command, runtime::{spawn, CoreConfig}};
|
|
|
|
static NEXT: AtomicU64 = AtomicU64::new(0);
|
|
struct Profile(PathBuf);
|
|
impl Profile {
|
|
fn new() -> Self {
|
|
Self(std::env::temp_dir().join(format!("teamspeach-embedded-{}-{}", std::process::id(), NEXT.fetch_add(1, Ordering::Relaxed))))
|
|
}
|
|
fn config(&self, slot: usize) -> CoreConfig {
|
|
CoreConfig { data_dir: self.0.join("core"), connection_dir: self.0.join("core/connections").join(slot.to_string()) }
|
|
}
|
|
}
|
|
impl Drop for Profile { fn drop(&mut self) { let _ = std::fs::remove_dir_all(&self.0); } }
|
|
|
|
#[tokio::test(flavor = "current_thread")]
|
|
async fn embedded_identity_survives_repeated_worker_lifetimes() {
|
|
let profile = Profile::new();
|
|
let (first, _events) = spawn(profile.config(1)).unwrap();
|
|
let created = first.request(Command::ProfileCreate { nickname: "Native owner".into() }).await.unwrap();
|
|
let uid = created["uid"].clone();
|
|
assert!(uid.as_str().is_some());
|
|
for _ in 0..100 {
|
|
let (other, _events) = spawn(profile.config(2)).unwrap();
|
|
let state = other.request(Command::ProfileGet).await.unwrap();
|
|
assert_eq!(state["uid"], uid);
|
|
other.shutdown().await.unwrap();
|
|
}
|
|
assert_eq!(first.request(Command::ProfileGet).await.unwrap()["uid"], uid);
|
|
first.shutdown().await.unwrap();
|
|
let (again, _events) = spawn(profile.config(1)).unwrap();
|
|
assert_eq!(again.request(Command::ProfileGet).await.unwrap()["uid"], uid);
|
|
again.shutdown().await.unwrap();
|
|
}
|
|
|
|
#[tokio::test(flavor = "current_thread")]
|
|
async fn native_requests_validate_before_session_dispatch() {
|
|
use teamspeach_core::protocol::{ErrorCode, TextTarget};
|
|
let profile = Profile::new();
|
|
let (core, _events) = spawn(profile.config(1)).unwrap();
|
|
let error = core.request(Command::SendText { target: TextTarget::Client, client_id: None,
|
|
expected_client_uid: None, message: "private".into() }).await.unwrap_err();
|
|
assert_eq!(error.code, ErrorCode::BadRequest);
|
|
let error = core.request(Command::StreamSignal { stream_id: "s1".into(), client_id: 1, json: "[]".into() }).await.unwrap_err();
|
|
assert_eq!(error.code, ErrorCode::BadRequest);
|
|
core.shutdown().await.unwrap();
|
|
}
|
|
|
|
#[tokio::test(flavor = "current_thread")]
|
|
async fn duplicate_connection_slot_is_refused_and_released_after_shutdown() {
|
|
let profile = Profile::new();
|
|
let (core, _events) = spawn(profile.config(1)).unwrap();
|
|
assert!(spawn(profile.config(1)).is_err());
|
|
core.shutdown().await.unwrap();
|
|
let (next, _events) = spawn(profile.config(1)).unwrap();
|
|
next.shutdown().await.unwrap();
|
|
}
|