add more tests, fix event/command response race condition
All checks were successful
Build and test / check (pull_request) Successful in 1m56s
Build and test / build (pull_request) Successful in 1m57s
Build and test / docs (pull_request) Successful in 2m30s
Build and test / test (pull_request) Successful in 4m0s
Build and test / check (push) Successful in 1m55s
Build and test / build (push) Successful in 1m56s
Build and test / docs (push) Successful in 3m2s
Build and test / test (push) Successful in 3m59s
All checks were successful
Build and test / check (pull_request) Successful in 1m56s
Build and test / build (pull_request) Successful in 1m57s
Build and test / docs (pull_request) Successful in 2m30s
Build and test / test (pull_request) Successful in 4m0s
Build and test / check (push) Successful in 1m55s
Build and test / build (push) Successful in 1m56s
Build and test / docs (push) Successful in 3m2s
Build and test / test (push) Successful in 3m59s
This commit is contained in:
100
tests/integration.rs
Normal file
100
tests/integration.rs
Normal file
@@ -0,0 +1,100 @@
|
||||
use mpvipc::{Error, Mpv, MpvExt};
|
||||
use std::path::Path;
|
||||
use tokio::{
|
||||
process::{Child, Command},
|
||||
time::{sleep, timeout, Duration},
|
||||
};
|
||||
|
||||
#[cfg(target_family = "unix")]
|
||||
async fn spawn_headless_mpv() -> Result<(Child, Mpv), Error> {
|
||||
let socket_path_str = format!("/tmp/mpv-ipc-{}", uuid::Uuid::new_v4());
|
||||
let socket_path = Path::new(&socket_path_str);
|
||||
|
||||
let process_handle = Command::new("mpv")
|
||||
.arg("--no-config")
|
||||
.arg("--idle")
|
||||
.arg("--no-video")
|
||||
.arg("--no-audio")
|
||||
.arg(format!(
|
||||
"--input-ipc-server={}",
|
||||
&socket_path.to_str().unwrap()
|
||||
))
|
||||
.spawn()
|
||||
.expect("Failed to start mpv");
|
||||
|
||||
if timeout(Duration::from_millis(500), async {
|
||||
while !&socket_path.exists() {
|
||||
sleep(Duration::from_millis(10)).await;
|
||||
}
|
||||
})
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
panic!("Failed to create mpv socket at {:?}", &socket_path);
|
||||
}
|
||||
|
||||
let mpv = Mpv::connect(socket_path.to_str().unwrap()).await.unwrap();
|
||||
Ok((process_handle, mpv))
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[cfg(target_family = "unix")]
|
||||
async fn test_get_mpv_version() {
|
||||
let (mut proc, mpv) = spawn_headless_mpv().await.unwrap();
|
||||
let version: String = mpv.get_property("mpv-version").await.unwrap();
|
||||
assert!(version.starts_with("mpv"));
|
||||
|
||||
mpv.kill().await.unwrap();
|
||||
proc.kill().await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[cfg(target_family = "unix")]
|
||||
async fn test_set_property() {
|
||||
let (mut proc, mpv) = spawn_headless_mpv().await.unwrap();
|
||||
mpv.set_property("pause", true).await.unwrap();
|
||||
let paused: bool = mpv.get_property("pause").await.unwrap();
|
||||
assert!(paused);
|
||||
|
||||
mpv.kill().await.unwrap();
|
||||
proc.kill().await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[cfg(target_family = "unix")]
|
||||
async fn test_events() {
|
||||
use futures::stream::StreamExt;
|
||||
|
||||
let (mut proc, mpv) = spawn_headless_mpv().await.unwrap();
|
||||
|
||||
mpv.observe_property(1337, "pause").await.unwrap();
|
||||
|
||||
let mut events = mpv.get_event_stream().await;
|
||||
let event_checking_thread = tokio::spawn(async move {
|
||||
loop {
|
||||
let event = events.next().await.unwrap().unwrap();
|
||||
if let mpvipc::Event::PropertyChange { id, property } = event {
|
||||
if id == 1337 {
|
||||
assert_eq!(property, mpvipc::Property::Pause(true));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
tokio::time::sleep(Duration::from_millis(10)).await;
|
||||
|
||||
mpv.set_property("pause", true).await.unwrap();
|
||||
|
||||
if let Err(_) = tokio::time::timeout(
|
||||
tokio::time::Duration::from_millis(500),
|
||||
event_checking_thread,
|
||||
)
|
||||
.await
|
||||
{
|
||||
panic!("Event checking thread timed out");
|
||||
}
|
||||
|
||||
mpv.kill().await.unwrap();
|
||||
proc.kill().await.unwrap();
|
||||
}
|
Reference in New Issue
Block a user