diff --git a/src/client/async_client.rs b/src/client/async_client.rs index fc3bf258f..24452f22e 100644 --- a/src/client/async_client.rs +++ b/src/client/async_client.rs @@ -164,8 +164,8 @@ where if self.client.is_some() { j::jack_deactivate(self.as_ptr()); // result doesn't matter } - sleep_on_test(); + sleep_on_test(); // Drop the handler if self.handler.is_some() { drop(Box::from_raw(self.handler.unwrap())); diff --git a/src/client/base.rs b/src/client/base.rs index c42c7d6dd..4b08c6a05 100644 --- a/src/client/base.rs +++ b/src/client/base.rs @@ -494,7 +494,7 @@ impl Drop for Client { impl fmt::Debug for Client { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - write!(f, "{:?}", DebugInfo::new(self)) + write!(f, "{:?}", ClientInfo::new(self)) } } @@ -610,7 +610,7 @@ pub struct CycleTimes { } #[derive(Debug)] -struct DebugInfo { +struct ClientInfo { name: String, sample_rate: usize, buffer_size: u32, @@ -619,9 +619,9 @@ struct DebugInfo { frame_time: pt::JackFrames, } -impl DebugInfo { - fn new(c: &Client) -> DebugInfo { - DebugInfo { +impl ClientInfo { + fn new(c: &Client) -> ClientInfo { + ClientInfo { name: c.name().into(), sample_rate: c.sample_rate(), buffer_size: c.buffer_size(), diff --git a/src/client/common.rs b/src/client/common.rs index faabd99e5..a9fad24b2 100644 --- a/src/client/common.rs +++ b/src/client/common.rs @@ -26,6 +26,6 @@ pub fn sleep_on_test() { #[cfg(test)] { use std::{thread, time}; - thread::sleep(time::Duration::from_millis(300)); + thread::sleep(time::Duration::from_millis(150)); } } diff --git a/src/port/port.rs b/src/port/port.rs index 60bcf3da9..f741d5a7b 100644 --- a/src/port/port.rs +++ b/src/port/port.rs @@ -309,12 +309,12 @@ pub type UnownedPort = Port; unsafe impl PortSpec for Unowned { /// Panics on call since the `Unowned` spec can't be used to create ports. fn jack_port_type(&self) -> &str { - unreachable!() + "" } /// Panics on call since the `Unowned` spec can't be used to create ports. fn jack_flags(&self) -> PortFlags { - unreachable!() + PortFlags::empty() } /// Panics on call since the `Unowned` spec can't be used to create ports. @@ -324,24 +324,22 @@ unsafe impl PortSpec for Unowned { } #[derive(Debug)] -struct DebugInfo { +struct PortInfo { name: String, connections: usize, port_type: String, port_flags: PortFlags, - buffer_size: u64, aliases: Vec, } -impl DebugInfo { - fn new(p: &Port) -> DebugInfo { +impl PortInfo { + fn new(p: &Port) -> PortInfo { let s = p.spec(); - DebugInfo { + PortInfo { name: p.name().into(), connections: p.connected_count(), port_type: s.jack_port_type().to_string(), port_flags: s.jack_flags(), - buffer_size: s.jack_buffer_size(), aliases: p.aliases(), } } @@ -349,6 +347,6 @@ impl DebugInfo { impl fmt::Debug for Port { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - write!(f, "{:?}", DebugInfo::new(self)) + write!(f, "{:?}", PortInfo::new(self)) } } diff --git a/src/test/test_client.rs b/src/test/test_client.rs index e9e4dc11f..04454563e 100644 --- a/src/test/test_client.rs +++ b/src/test/test_client.rs @@ -53,6 +53,15 @@ fn client_can_set_buffer_size() { assert_eq!(c.buffer_size(), initial_size); } +#[test] +fn client_detects_bad_buffer_size() { + let (c, _) = open_test_client("client_detects_bad_buffer_size"); + let initial_size = c.buffer_size(); + assert_eq!(c.set_buffer_size(0), Err(JackErr::SetBufferSizeError)); + c.set_buffer_size(initial_size).unwrap(); + assert_eq!(c.buffer_size(), initial_size); +} + #[test] fn client_can_deactivate() { let (c, _) = open_test_client("client_can_deactivate"); @@ -74,17 +83,37 @@ fn client_knows_sample_rate() { assert_eq!(c.sample_rate(), 44100); } -// TODO - improve test #[test] fn client_knows_cpu_load() { let (c, _) = open_test_client("client_knows_cpu_load"); let _load = c.cpu_load(); } -// TODO - improve test #[test] fn client_can_estimate_frame_times() { - let (c, _) = open_test_client("client_knows_cpu_load"); - c.frames_to_time(44100); - c.time_to_frames(1000000); + let (c, _) = open_test_client("client_knows_frame_times"); + let current_frame_time = c.frame_time(); + let time = c.frames_to_time(44100); + let frames = c.time_to_frames(1000000); + assert!(current_frame_time > 0); + assert!(time > 0); + assert!(frames > 0); +} + +#[test] +fn client_debug_printing() { + let (c, _) = open_test_client("client_has_debug_string"); + let got = format!("{:?}", c); + let parts = [ + ("name", "\"client_has_debug_string\""), + ("sample_rate", "44100"), + ("buffer_size", "1024"), + ("cpu_usage", ""), + ("ports", "["), + ("frame_time", ""), + ]; + for &(k, v) in parts.iter() { + let p = format!("{}: {}", k, v); + assert!(got.contains(&p), "Expected {} to contain {}.", got, p); + } } diff --git a/src/test/test_client_cback.rs b/src/test/test_client_cback.rs index 101a45cb6..f5f6fa6e9 100644 --- a/src/test/test_client_cback.rs +++ b/src/test/test_client_cback.rs @@ -15,6 +15,8 @@ pub struct Counter { pub port_register_history: Vec, pub port_unregister_history: Vec, pub xruns_count: usize, + pub last_frame_time: JackFrames, + pub frames_since_cycle_start: JackFrames, } impl NotificationHandler for Counter { @@ -50,6 +52,9 @@ impl NotificationHandler for Counter { impl ProcessHandler for Counter { fn process(&mut self, _: &Client, ps: &ProcessScope) -> JackControl { self.frames_processed += ps.n_frames() as usize; + self.last_frame_time = ps.last_frame_time(); + self.frames_since_cycle_start = ps.frames_since_cycle_start(); + let _cycle_times = ps.cycle_times(); if self.induce_xruns { thread::sleep(time::Duration::from_millis(400)); } @@ -116,6 +121,8 @@ fn client_cback_calls_process() { let ac = active_test_client("client_cback_calls_process"); let counter = ac.deactivate().unwrap().2; assert!(counter.frames_processed > 0); + assert!(counter.last_frame_time > 0); + assert!(counter.frames_since_cycle_start > 0); } #[test] diff --git a/src/test/test_client_port.rs b/src/test/test_client_port.rs index 91a22f6f1..7195b5b45 100644 --- a/src/test/test_client_port.rs +++ b/src/test/test_client_port.rs @@ -91,9 +91,15 @@ fn client_port_can_get_port_by_id() { .flat_map(|i| c.port_by_id(i)) .map(|p| p.name().to_string()) .collect(); - assert!(registered_ports.contains( - &format!("{}:{}", client_name, port_name), - )); + let port_name = format!("{}:{}", client_name, port_name); + assert!(registered_ports.contains(&port_name)); + + // Port that doesn't exist + let nonexistant_port = c.port_by_id(1000000000); + assert!( + nonexistant_port.is_none(), + format!("Expected None but got: {:?}", nonexistant_port) + ); } #[test] diff --git a/src/test/test_port.rs b/src/test/test_port.rs index 8e3789cd8..876111bb4 100644 --- a/src/test/test_port.rs +++ b/src/test/test_port.rs @@ -150,15 +150,13 @@ fn port_can_unset_alias() { } #[test] -#[should_panic] fn port_unowned_no_port_type() { - Unowned::default().jack_port_type(); + assert_eq!("", Unowned::default().jack_port_type()); } #[test] -#[should_panic] fn port_unowned_no_port_flags() { - Unowned::default().jack_flags(); + assert_eq!(PortFlags::empty(), Unowned::default().jack_flags()); } #[test] @@ -166,3 +164,21 @@ fn port_unowned_no_port_flags() { fn port_unowned_no_port_size() { Unowned::default().jack_buffer_size(); } + +#[test] +fn port_debug_printing() { + let (_c, mut p) = open_client_with_port("port_has_debug_string", "debug_info"); + p.set_alias("this_port_alias").unwrap(); + let got = format!("{:?}", p); + let parts = [ + ("name", "\"port_has_debug_string:debug_info\""), + ("connections", "0"), + ("port_type", "\"32 bit float mono audio\""), + ("port_flags", "IS_INPUT"), + ("aliases", "[\"this_port_alias\"]"), + ]; + for &(k, v) in parts.iter() { + let p = format!("{}: {}", k, v); + assert!(got.contains(&p), "Expected {} to contain \"{}\".", got, p); + } +}