Skip to content

Commit 488a12a

Browse files
authored
Remove sleeps from tests. (#213)
* Remove sleeps. * Increase timeouts.
1 parent 8dcc07d commit 488a12a

File tree

8 files changed

+48
-47
lines changed

8 files changed

+48
-47
lines changed

.config/nextest.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[profile.default]
22
test-threads = 1
33
fail-fast = false
4-
slow-timeout = { period = "2s", terminate-after = 2 }
4+
slow-timeout = { period = "10s", terminate-after = 1 }
55
retries = { backoff = "fixed", count = 3, delay = "1s" }

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ run in single threaded mode.
4848
If the tests are failing, a possible gotcha may be timing issues.
4949

5050
1. If using `cargo test`, try `cargo nextest`. The `cargo nextest`
51-
configuration is set up to run single threaded and to retry flaky tests up
52-
to 3 times.
53-
1. Increase the value used by `sleep_on_test` in `client/common.rs`.
51+
configuration is set up to run single threaded and to retry flaky tests.
5452

5553
Another case is that libjack may be broken on your setup. Try using libjack2 or
5654
pipewire-jack.

src/client/client_impl.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt::Debug;
33
use std::sync::Arc;
44
use std::{ffi, fmt, ptr};
55

6-
use crate::client::common::{sleep_on_test, CREATE_OR_DESTROY_CLIENT_MUTEX};
6+
use crate::client::common::CREATE_OR_DESTROY_CLIENT_MUTEX;
77
use crate::jack_enums::CodeOrMessage;
88
use crate::jack_utils::collect_strs;
99
use crate::properties::PropertyChangeHandler;
@@ -61,18 +61,15 @@ impl Client {
6161
}
6262

6363
crate::logging::maybe_init_logging();
64-
sleep_on_test();
6564
let mut status_bits = 0;
6665
let client = unsafe {
6766
let client_name = ffi::CString::new(client_name).unwrap();
6867
j::jack_client_open(client_name.as_ptr(), options.bits(), &mut status_bits)
6968
};
70-
sleep_on_test();
7169
let status = ClientStatus::from_bits(status_bits).unwrap_or_else(ClientStatus::empty);
7270
if client.is_null() {
7371
Err(Error::ClientError(status))
7472
} else {
75-
sleep_on_test();
7673
Ok((Client(client, Arc::default(), None), status))
7774
}
7875
}
@@ -673,12 +670,10 @@ impl Client {
673670
impl Drop for Client {
674671
fn drop(&mut self) {
675672
let _m = CREATE_OR_DESTROY_CLIENT_MUTEX.lock().ok();
676-
debug_assert!(!self.raw().is_null()); // Rep invariant
677-
// Close the client
678-
sleep_on_test();
679-
let _res = unsafe { j::jack_client_close(self.raw()) }; // best effort: close the client
680-
sleep_on_test();
681-
//assert_eq!(res, 0); //do not assert here. connection could be broken
673+
// Rep invariant.
674+
debug_assert!(!self.raw().is_null());
675+
// Best effort close client.
676+
let _res = unsafe { j::jack_client_close(self.raw()) };
682677
self.0 = ptr::null_mut();
683678
}
684679
}

src/client/common.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,3 @@ lazy_static! {
1818
lazy_static! {
1919
pub static ref CREATE_OR_DESTROY_CLIENT_MUTEX: Mutex<()> = Mutex::new(());
2020
}
21-
22-
#[inline(always)]
23-
pub fn sleep_on_test() {
24-
#[cfg(test)]
25-
{
26-
use std::{thread, time};
27-
thread::sleep(time::Duration::from_millis(100));
28-
}
29-
}

src/tests/client.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::tests::DEFAULT_TEST_CLIENT;
2+
13
#[test]
24
fn client_can_open() {
35
let (client, status) =
@@ -7,34 +9,34 @@ fn client_can_open() {
79
assert_ne!(client.sample_rate(), 0);
810
assert_ne!(client.buffer_size(), 0);
911
assert_ne!(client.uuid_string(), "");
12+
assert_ne!(client.uuid(), 0);
1013
let cpu_load = client.cpu_load();
1114
assert!(cpu_load > 0.0, "client.cpu_load() = {}", cpu_load);
1215
}
1316

1417
#[test]
1518
fn time_is_montonically_increasing() {
16-
let (client, _) = crate::Client::new("", crate::ClientOptions::empty()).unwrap();
17-
18-
let t0 = client.time();
19-
let frames0 = client.frames_since_cycle_start();
20-
let frame_time0 = client.frame_time();
19+
let t0 = DEFAULT_TEST_CLIENT.time();
20+
let frames0 = DEFAULT_TEST_CLIENT.frames_since_cycle_start();
21+
let frame_time0 = DEFAULT_TEST_CLIENT.frame_time();
2122

2223
std::thread::sleep(std::time::Duration::from_millis(50));
23-
assert_ne!(client.time(), t0);
24-
assert_ne!(client.frames_since_cycle_start(), frames0);
25-
assert_ne!(client.frame_time(), frame_time0);
24+
assert_ne!(DEFAULT_TEST_CLIENT.time(), t0);
25+
assert_ne!(DEFAULT_TEST_CLIENT.frames_since_cycle_start(), frames0);
26+
assert_ne!(DEFAULT_TEST_CLIENT.frame_time(), frame_time0);
2627
}
2728

2829
#[test]
2930
fn maybe_client_can_set_buffer_size() {
30-
let (client, _) = crate::Client::new("", crate::ClientOptions::empty()).unwrap();
31-
let initial_buffer_size = client.buffer_size();
32-
if let Err(err) = client.set_buffer_size(initial_buffer_size * 2) {
31+
let initial_buffer_size = DEFAULT_TEST_CLIENT.buffer_size();
32+
if let Err(err) = DEFAULT_TEST_CLIENT.set_buffer_size(initial_buffer_size * 2) {
3333
eprintln!("client does not support setting buffer size: {err}");
3434
return;
3535
}
36-
assert_eq!(client.buffer_size(), 2 * initial_buffer_size);
37-
client.set_buffer_size(initial_buffer_size).unwrap();
36+
assert_eq!(DEFAULT_TEST_CLIENT.buffer_size(), 2 * initial_buffer_size);
37+
DEFAULT_TEST_CLIENT
38+
.set_buffer_size(initial_buffer_size)
39+
.unwrap();
3840
}
3941

4042
#[test]
@@ -76,12 +78,11 @@ fn uuid_can_map_to_client_name() {
7678

7779
#[test]
7880
fn nonexistant_uuid_to_client_name_returns_none() {
79-
let (client1, _) = crate::Client::new("", crate::ClientOptions::default()).unwrap();
80-
let (client2, _) =
81+
let (client, _) =
8182
crate::Client::new("dropped-client", crate::ClientOptions::default()).unwrap();
82-
let uuid_string = client2.uuid_string();
83-
let uuid = client2.uuid();
84-
drop(client2);
85-
assert_eq!(client1.name_by_uuid_str(&uuid_string), None);
86-
assert_eq!(client1.name_by_uuid(uuid), None);
83+
let uuid_string = client.uuid_string();
84+
let uuid = client.uuid();
85+
drop(client);
86+
assert_eq!(DEFAULT_TEST_CLIENT.name_by_uuid_str(&uuid_string), None);
87+
assert_eq!(DEFAULT_TEST_CLIENT.name_by_uuid(uuid), None);
8788
}

src/tests/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1+
use std::sync::LazyLock;
2+
3+
use crate::{Client, ClientOptions};
4+
15
mod client;
26
mod log;
37
mod processing;
48
mod ringbuffer;
59
mod time;
610
mod transport;
711

12+
pub static DEFAULT_TEST_CLIENT: LazyLock<Client> = LazyLock::new(|| {
13+
Client::new("default-test-client", ClientOptions::default())
14+
.unwrap()
15+
.0
16+
});
17+
818
#[ctor::ctor]
919
fn log_to_stdio() {
1020
crate::set_logger(crate::LoggerType::Stdio);

src/tests/processing.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,20 @@ fn panic_in_buffer_size_handler_propagates_as_error_in_deactivate() {
3434

3535
#[test]
3636
fn quitting_stops_calling_process() {
37+
eprintln!("Creating client.");
3738
let (client, _) = crate::Client::new("", crate::ClientOptions::default()).unwrap();
3839
let mut calls = 0;
3940
let (send, recv) = std::sync::mpsc::sync_channel(2);
41+
eprintln!("Creating callback.");
4042
let process_handler = crate::contrib::ClosureProcessHandler::new(move |_, _| {
4143
send.try_send(true).unwrap();
4244
calls += 1;
4345
assert_eq!(calls, 1);
4446
crate::Control::Quit
4547
});
48+
eprintln!("Activating client.");
4649
let ac = client.activate_async((), process_handler).unwrap();
50+
eprintln!("Waiting for async response.");
4751
assert!(recv
4852
.recv_timeout(std::time::Duration::from_secs(1))
4953
.unwrap());
@@ -85,12 +89,14 @@ fn buffer_size_is_called_before_process() {
8589
|state, _, _| {
8690
assert_eq!(*state, "initializing");
8791
*state = "processing";
92+
// Give the processing thread some time to run, in case it wants to.
93+
std::thread::sleep(std::time::Duration::from_secs(3));
8894
crate::Control::Continue
8995
},
9096
);
9197
let ac = client.activate_async((), process_handler).unwrap();
9298
assert!(recv
93-
.recv_timeout(std::time::Duration::from_secs(1))
99+
.recv_timeout(std::time::Duration::from_secs(5))
94100
.unwrap());
95101
assert_eq!(ac.deactivate().unwrap().2.state, "processing");
96102
}

src/tests/time.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use approx::assert_abs_diff_eq;
22

33
#[test]
44
fn frame_and_time_are_convertable() {
5-
let (client, _) = crate::Client::new("", crate::ClientOptions::empty()).unwrap();
5+
let (client, _) = crate::Client::new("", crate::ClientOptions::default()).unwrap();
66
assert_eq!(client.time_to_frames(client.frames_to_time(0)), 0);
77
}
88

@@ -13,7 +13,7 @@ fn one_frame_duration_is_inverse_of_sample_rate() {
1313
assert_abs_diff_eq!(
1414
(client.frames_to_time(sample_rate as _) - client.frames_to_time(0)) as f64,
1515
1_000_000.0,
16-
epsilon = 1_000_000.0 * 1e-4,
16+
epsilon = 1_000_000.0 * 1e-3,
1717
);
1818
}
1919

@@ -25,6 +25,6 @@ fn one_second_is_sample_rate_frames() {
2525
assert_abs_diff_eq!(
2626
(t1 - t0) as f64,
2727
client.sample_rate() as f64,
28-
epsilon = client.sample_rate() as f64 * 1e-4
28+
epsilon = client.sample_rate() as f64 * 1e-3
2929
);
3030
}

0 commit comments

Comments
 (0)