Skip to content

Commit 41ca025

Browse files
committed
Add tests for AudioRenderCapacity
1 parent 3fc010d commit 41ca025

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

src/capacity.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,67 @@ impl AudioRenderCapacity {
195195
self.context.clear_event_handler(EventType::RenderCapacity);
196196
}
197197
}
198+
199+
#[cfg(test)]
200+
mod tests {
201+
use super::*;
202+
use crate::context::{AudioContext, AudioContextOptions};
203+
204+
#[test]
205+
fn test_same_instance() {
206+
let options = AudioContextOptions {
207+
sink_id: "none".into(),
208+
..AudioContextOptions::default()
209+
};
210+
let context = AudioContext::new(options);
211+
212+
let rc1 = context.render_capacity();
213+
let rc2 = context.render_capacity();
214+
let rc3 = rc2.clone();
215+
216+
// assert all items are actually the same instance
217+
assert!(Arc::ptr_eq(&rc1.stop_send, &rc2.stop_send));
218+
assert!(Arc::ptr_eq(&rc1.stop_send, &rc3.stop_send));
219+
}
220+
221+
#[test]
222+
fn test_stop_when_not_running() {
223+
let options = AudioContextOptions {
224+
sink_id: "none".into(),
225+
..AudioContextOptions::default()
226+
};
227+
let context = AudioContext::new(options);
228+
229+
let rc = context.render_capacity();
230+
rc.stop();
231+
}
232+
233+
#[test]
234+
fn test_render_capacity() {
235+
let options = AudioContextOptions {
236+
sink_id: "none".into(),
237+
..AudioContextOptions::default()
238+
};
239+
let context = AudioContext::new(options);
240+
241+
let rc = context.render_capacity();
242+
let (send, recv) = crossbeam_channel::bounded(1);
243+
rc.set_onupdate(move |e| send.send(e).unwrap());
244+
rc.start(AudioRenderCapacityOptions {
245+
update_interval: 0.05,
246+
});
247+
let event = recv.recv().unwrap();
248+
249+
assert!(event.timestamp >= 0.);
250+
assert!(event.average_load >= 0.);
251+
assert!(event.peak_load >= 0.);
252+
assert!(event.underrun_ratio >= 0.);
253+
254+
assert!(event.timestamp.is_finite());
255+
assert!(event.average_load.is_finite());
256+
assert!(event.peak_load.is_finite());
257+
assert!(event.underrun_ratio.is_finite());
258+
259+
assert_eq!(event.event.type_, "AudioRenderCapacityEvent");
260+
}
261+
}

0 commit comments

Comments
 (0)