Skip to content

Commit 0644fe2

Browse files
committed
Make test more robust by sleeping to prevent event thread racing
1 parent 480f6d1 commit 0644fe2

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

tests/online.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use web_audio_api::context::{
99
use web_audio_api::node::AudioNode;
1010

1111
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
12+
use std::time::Duration;
1213
use web_audio_api::MAX_CHANNELS;
1314

1415
fn require_send_sync_static<T: Send + Sync + 'static>(_: T) {}
@@ -94,7 +95,7 @@ fn test_none_sink_id() {
9495
});
9596

9697
// give event thread some time to pick up events
97-
std::thread::sleep(std::time::Duration::from_millis(20));
98+
std::thread::sleep(Duration::from_millis(5));
9899
assert_eq!(state_changes.load(Ordering::Relaxed), 1); // started
99100

100101
// changing sink_id to 'none' again should make no changes
@@ -107,15 +108,24 @@ fn test_none_sink_id() {
107108

108109
context.suspend_sync();
109110
assert_eq!(context.state(), AudioContextState::Suspended);
111+
112+
// give event thread some time to pick up events
113+
std::thread::sleep(Duration::from_millis(5));
110114
assert_eq!(state_changes.load(Ordering::Relaxed), 2); // suspended
111115

112116
context.resume_sync();
113117
assert_eq!(context.state(), AudioContextState::Running);
118+
119+
// give event thread some time to pick up events
120+
std::thread::sleep(Duration::from_millis(5));
114121
assert_eq!(state_changes.load(Ordering::Relaxed), 3); // resumed
115122

116123
context.close_sync();
117124
assert_eq!(context.state(), AudioContextState::Closed);
118125
assert!(sink_stable.load(Ordering::SeqCst));
126+
127+
// give event thread some time to pick up events
128+
std::thread::sleep(Duration::from_millis(5));
119129
assert_eq!(state_changes.load(Ordering::Relaxed), 4); // closed
120130
}
121131

@@ -160,15 +170,15 @@ fn test_panner_node_drop_panic() {
160170
drop(panner);
161171

162172
// allow the audio render thread to boot and handle adding and dropping the panner
163-
std::thread::sleep(std::time::Duration::from_millis(200));
173+
std::thread::sleep(Duration::from_millis(200));
164174

165175
// creating a new panner node should not crash the render thread
166176
let mut _panner = context.create_panner();
167177

168178
// A crashed thread will not fail the test (only if the main thread panics).
169179
// Instead inspect if there is progression of time in the audio context.
170180
let time = context.current_time();
171-
std::thread::sleep(std::time::Duration::from_millis(200));
181+
std::thread::sleep(Duration::from_millis(200));
172182
assert!(context.current_time() >= time + 0.15);
173183
}
174184

@@ -187,7 +197,7 @@ fn test_audioparam_outlives_audionode() {
187197

188198
// Start the audio graph, and give some time to drop the gain node (it has no inputs connected
189199
// so dynamic lifetime will drop the node);
190-
std::thread::sleep(std::time::Duration::from_millis(200));
200+
std::thread::sleep(Duration::from_millis(200));
191201

192202
// We still have a handle to the param, so that should not be removed from the audio graph.
193203
// So by updating the value, the render thread should not crash.
@@ -196,7 +206,7 @@ fn test_audioparam_outlives_audionode() {
196206
// A crashed thread will not fail the test (only if the main thread panics).
197207
// Instead inspect if there is progression of time in the audio context.
198208
let time = context.current_time();
199-
std::thread::sleep(std::time::Duration::from_millis(200));
209+
std::thread::sleep(Duration::from_millis(200));
200210
assert!(context.current_time() >= time + 0.15);
201211
}
202212

@@ -214,7 +224,7 @@ fn test_closed() {
214224
drop(context);
215225

216226
// allow some time for the render thread to drop
217-
std::thread::sleep(std::time::Duration::from_millis(10));
227+
std::thread::sleep(Duration::from_millis(10));
218228

219229
node.disconnect(); // should not panic
220230
}

0 commit comments

Comments
 (0)