Skip to content

Commit

Permalink
Create time and log tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
wmedrano committed Sep 14, 2024
1 parent e32d25d commit 5663356
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 16 deletions.
4 changes: 2 additions & 2 deletions .config/nextest.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[profile.default]
test-threads = 1
fail-fast = false
slow-timeout = { period = "30s", terminate-after = 4 }
retries = { backoff = "fixed", count = 2, delay = "500ms" }
slow-timeout = { period = "5s", terminate-after = 4 }
retries = { backoff = "fixed", count = 2, delay = "100ms" }
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ libc = "0.2"
log = { version = "0.4", optional = true}

[dev-dependencies]
approx = "0.5"
crossbeam-channel = "0.5"
ctor = "0.2"

Expand Down
5 changes: 3 additions & 2 deletions docs/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ enabled by default. The `log` crate provides a *facade* for logging; it provides
macros to perform logging, but another mechanism or crate is required to
actually perform the logging.

In the example below, we use the [`env_logger` crate]() to display logging for
info and error severity level messages.
In the example below, we use the [`env_logger`
crate](https://crates.io/crates/env_logger) to display logging for info and
error severity level messages.

```rust
env_logger::builder().filter(None, log::LevelFilter::Info).init();
Expand Down
10 changes: 0 additions & 10 deletions src/tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,3 @@ fn time_is_montonically_increasing() {
assert_ne!(client.frames_since_cycle_start(), frames0);
assert_ne!(client.frame_time(), frame_time0);
}

#[test]
fn frame_and_time_and_convertable() {
let (client, _) = crate::Client::new("frame times", crate::ClientOptions::empty()).unwrap();
let sample_rate = client.sample_rate();
assert_eq!(
(client.frames_to_time(1) - client.frames_to_time(0)) as f64,
(1_000_000.0 / sample_rate as f64).round()
);
}
13 changes: 13 additions & 0 deletions src/tests/log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[test]
fn can_set_logger() {
crate::set_logger(crate::LoggerType::Custom {
info: test_info_callback,
error: test_error_callback,
});
assert!(unsafe { crate::jack_sys::jack_info_callback } == Some(test_info_callback),);
assert!(unsafe { crate::jack_sys::jack_error_callback } == Some(test_error_callback),);
super::log_to_stdio(); // Revert to enable debugging in other tests.
}

unsafe extern "C" fn test_info_callback(_msg: *const libc::c_char) {}
unsafe extern "C" fn test_error_callback(_msg: *const libc::c_char) {}
5 changes: 3 additions & 2 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
mod client;
mod log;
mod time;

#[cfg(test)]
#[ctor::ctor]
fn init() {
fn log_to_stdio() {
crate::set_logger(crate::LoggerType::Stdio);
}
30 changes: 30 additions & 0 deletions src/tests/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use approx::assert_abs_diff_eq;

#[test]
fn frame_and_time_are_convertable() {
let (client, _) = crate::Client::new("", crate::ClientOptions::empty()).unwrap();
assert_eq!(client.time_to_frames(client.frames_to_time(0)), 0);
}

#[test]
fn one_frame_duration_is_inverse_of_sample_rate() {
let (client, _) = crate::Client::new("", crate::ClientOptions::empty()).unwrap();
let sample_rate = client.sample_rate();
assert_abs_diff_eq!(
(client.frames_to_time(sample_rate as _) - client.frames_to_time(0)) as f64,
1_000_000.0,
epsilon = 1_000_000.0 * 1e-4,
);
}

#[test]
fn one_second_is_sample_rate_frames() {
let (client, _) = crate::Client::new("", crate::ClientOptions::empty()).unwrap();
let t0 = client.time_to_frames(0);
let t1 = client.time_to_frames(1_000_000);
assert_abs_diff_eq!(
(t1 - t0) as f64,
client.sample_rate() as f64,
epsilon = client.sample_rate() as f64 * 1e-7
);
}

0 comments on commit 5663356

Please sign in to comment.