Skip to content

Commit

Permalink
Allow tests to run in parallel (#56)
Browse files Browse the repository at this point in the history
* Prevents developers from having to set RUST_TEST_THREADS environment variable
* Dramatically improves test time, from 100s to 20s on my system
  • Loading branch information
wmedrano committed Jan 29, 2017
1 parent 71da2f5 commit 36f394d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 49 deletions.
27 changes: 17 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,30 @@ $ jackd -r -ddummy -r44100 -p1024 & # Start the dummy JACK server
Testing expects there to be an available JACK server running at a sample rate of
44.1kHz and a buffer size of 1024 samples.

### Running the tests

```bash
$ cargo test
```

If you want test coverage as well, try `cargo kcov`.

```bash
$ cargo install cargo-kcov
$ cargo kcov
```

#### Possible Issues

If the tests are failing, a possible gotcha may be timing issues.

1. Rust runs tests in parallel, it may be possible that the JACK server is not keeping up. Set the environment variable `RUST_TEST_THREADS` to 1.
2. Increase the value used by `sleep_on_test`.
2. Increase the value used by `sleep_on_test` in `client_impls.rs`.

Another case is that libjack may be broken on your setup.
Try switching between libjack and libjack2 (they have the same API and libjack2
isn't necessarily newer than libjack), or using a different version.

### Running the tests
Another case is that libjack may be broken on your setup. Try switching between
libjack and libjack2 (they have the same API and libjack2 isn't necessarily
newer than libjack), or using a different version.

```bash
$ RUST_TEST_THREADS=1
$ cargo test
```

## "C" & Rust API differences
* String lengths in the "C" API include the `NULL` character while these Rust
Expand Down
53 changes: 27 additions & 26 deletions src/test/test_client_cback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fn client_cback_has_proper_default_callbacks() {
fn client_cback_calls_thread_init() {
let ac = active_test_client("client_cback_calls_thread_init");
let counter = ac.deactivate().unwrap().1;
// IDK why this isn't 1.
// IDK why this isn't 1, even with a single thread.
assert!(*counter.thread_init_count.lock().unwrap() > 0);
}

Expand All @@ -122,18 +122,27 @@ fn client_cback_calls_buffer_size() {
ac.set_buffer_size(third).unwrap();
ac.set_buffer_size(initial).unwrap();
let counter = ac.deactivate().unwrap().1;
assert_eq!(*counter.buffer_size_change_history.lock().unwrap(),
vec![initial, second, third, initial]);
let history = counter.buffer_size_change_history.lock().unwrap();
let mut history_iter = history.iter().cloned();
assert_eq!(history_iter.find(|&s| s == initial), Some(initial));
assert_eq!(history_iter.find(|&s| s == second), Some(second));
assert_eq!(history_iter.find(|&s| s == third), Some(third));
assert_eq!(history_iter.find(|&s| s == initial), Some(initial));
}

#[test]
fn client_cback_calls_after_client_registered() {
let ac = active_test_client("client_cback_cacr");
let _other_client = open_test_client("client_cback_cacr_other");
let counter = ac.deactivate().unwrap().1;
assert_eq!(*counter.registered_client_history.lock().unwrap(),
vec!["client_cback_cacr_other"]);
assert!(counter.unregistered_client_history.lock().unwrap().is_empty());
assert!(counter.registered_client_history
.lock()
.unwrap()
.contains(&"client_cback_cacr_other".to_string()));
assert!(!counter.unregistered_client_history
.lock()
.unwrap()
.contains(&"client_cback_cacr_other".to_string()));
}

#[test]
Expand All @@ -142,20 +151,14 @@ fn client_cback_calls_after_client_unregistered() {
let other_client = open_test_client("client_cback_cacu_other");
drop(other_client);
let counter = ac.deactivate().unwrap().1;
assert_eq!(*counter.registered_client_history.lock().unwrap(),
vec!["client_cback_cacu_other"],
"wrong clients detected as registered");
assert_eq!(*counter.unregistered_client_history.lock().unwrap(),
vec!["client_cback_cacu_other"],
"wrong clients detected as unregistered");
}

#[test]
fn client_cback_doesnt_call_port_registered_when_no_ports() {
let ac = active_test_client("client_cback_dcprwnp");
let counter = ac.deactivate().unwrap().1;
assert!(counter.port_register_history.lock().unwrap().is_empty());
assert!(counter.port_unregister_history.lock().unwrap().is_empty());
assert!(counter.registered_client_history
.lock()
.unwrap()
.contains(&"client_cback_cacu_other".to_string()));
assert!(counter.unregistered_client_history
.lock()
.unwrap()
.contains(&"client_cback_cacu_other".to_string()));
}

#[test]
Expand Down Expand Up @@ -190,10 +193,8 @@ fn client_cback_calls_port_unregistered() {
_pa.unregister().unwrap();
_pb.unregister().unwrap();
let counter = ac.deactivate().unwrap().1;
assert_eq!(counter.port_register_history.lock().unwrap().len(),
2,
"Did not detect port registrations.");
assert_eq!(counter.port_unregister_history.lock().unwrap().len(),
2,
"Did not detect port deregistrations.");
assert!(counter.port_register_history.lock().unwrap().len() >= 2,
"Did not detect port registrations.");
assert!(counter.port_unregister_history.lock().unwrap().len() >= 2,
"Did not detect port deregistrations.");
}
30 changes: 17 additions & 13 deletions src/test/test_client_port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,12 @@ fn client_port_can_get_existing_ports() {
"system:playback_1".to_string(),
"system:capture_1".to_string(),
"system:capture_2".to_string()];
let exp: HashSet<String> = known_ports.into_iter().map(|x| x.clone()).collect();
let exp: HashSet<String> = known_ports.into_iter().cloned().collect();
let got: HashSet<String> = port_getter.ports(None, None, PortFlags::empty())
.into_iter()
.collect();
assert_eq!(got, exp);
let intersection: HashSet<String> = exp.intersection(&got).cloned().collect();
assert_eq!(exp, intersection);
}

#[test]
Expand All @@ -296,7 +297,7 @@ fn client_port_can_get_port_by_name_pattern() {
// retrieve
use std::collections::HashSet;
let known_ports = ["system:playback_2".to_string(), "system:capture_2".to_string()];
let exp: HashSet<String> = known_ports.into_iter().map(|x| x.clone()).collect();
let exp: HashSet<String> = known_ports.into_iter().cloned().collect();
let got: HashSet<String> = client.ports(Some("2"), None, PortFlags::empty())
.into_iter()
.collect();
Expand All @@ -305,17 +306,20 @@ fn client_port_can_get_port_by_name_pattern() {

#[test]
fn client_port_can_get_port_by_type_pattern() {
let client = open_test_client("client_port_cgpbnp");
let cname = "client_port_cgpbtp";
let pname = "midip";
let full_name = format!("{}:{}", cname, pname);
let client = open_test_client(cname);

// register port with more unique type name, like midi
let _p = client.register_port("midip", MidiInSpec::default());
// register port with type name, like midi
let _p = client.register_port(pname, MidiInSpec::default());
use std::{thread, time};
thread::sleep(time::Duration::from_millis(400));

// retrieve
use std::collections::HashSet;
let known_ports = ["client_port_cgpbnp:midip".to_string()];
let exp: HashSet<String> = known_ports.into_iter().map(|x| x.clone()).collect();
let got: HashSet<String> = client.ports(None, Some("midi"), PortFlags::empty())
.into_iter()
.collect();
assert_eq!(got, exp);
let ports = client.ports(None, Some("midi"), PortFlags::empty());
assert!(ports.contains(&full_name),
"{:?} does not contain {}",
&ports,
&full_name);
}

0 comments on commit 36f394d

Please sign in to comment.