Skip to content

Commit

Permalink
Improve test coverage (RustAudio#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
wmedrano authored Dec 3, 2017
1 parent 5b93251 commit 263e65c
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/client/async_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
10 changes: 5 additions & 5 deletions src/client/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down Expand Up @@ -610,7 +610,7 @@ pub struct CycleTimes {
}

#[derive(Debug)]
struct DebugInfo {
struct ClientInfo {
name: String,
sample_rate: usize,
buffer_size: u32,
Expand All @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion src/client/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
16 changes: 7 additions & 9 deletions src/port/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,12 @@ pub type UnownedPort = Port<Unowned>;
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.
Expand All @@ -324,31 +324,29 @@ 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<String>,
}

impl DebugInfo {
fn new<PS: PortSpec>(p: &Port<PS>) -> DebugInfo {
impl PortInfo {
fn new<PS: PortSpec>(p: &Port<PS>) -> 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(),
}
}
}

impl<PS: PortSpec> fmt::Debug for Port<PS> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{:?}", DebugInfo::new(self))
write!(f, "{:?}", PortInfo::new(self))
}
}
39 changes: 34 additions & 5 deletions src/test/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);
}
}
7 changes: 7 additions & 0 deletions src/test/test_client_cback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub struct Counter {
pub port_register_history: Vec<JackPortId>,
pub port_unregister_history: Vec<JackPortId>,
pub xruns_count: usize,
pub last_frame_time: JackFrames,
pub frames_since_cycle_start: JackFrames,
}

impl NotificationHandler for Counter {
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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]
Expand Down
12 changes: 9 additions & 3 deletions src/test/test_client_port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
24 changes: 20 additions & 4 deletions src/test/test_port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,35 @@ 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]
#[should_panic]
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);
}
}

0 comments on commit 263e65c

Please sign in to comment.