use labstream_net::{Inlet, Outlet, StreamInfo};
use labstream_wire::{Format, Sample, Value};
use std::time::Duration;
fn stream(name: &str, format: Format) -> StreamInfo {
let mut i = StreamInfo::new(name, "Test", 1, format, 100.0);
i.source_id = format!("{name}_src");
i
}
#[test]
fn a_stream_of_strings_cannot_be_blocking() {
let err = match Outlet::new_blocking(stream("SyncStr", Format::String)) {
Ok(_) => panic!("a stream of strings was accepted in the blocking mode"),
Err(e) => e,
};
assert!(
err.to_string().contains("string"),
"the message must name the reason, got {err}"
);
}
#[test]
fn a_blocking_outlet_reports_its_consumers() {
let outlet = Outlet::new_blocking(stream("SyncCount", Format::Float32)).expect("an outlet");
assert_eq!(outlet.consumer_count(), 0);
let _inlet = Inlet::open(outlet.info(), Duration::from_secs(5)).expect("an inlet");
assert!(
outlet.wait_for_consumers(Duration::from_secs(5)),
"a blocking outlet must count the consumers of its writer"
);
assert_eq!(outlet.consumer_count(), 1);
}
#[test]
fn a_blocking_outlet_loses_nothing_to_a_slow_consumer() {
let outlet = Outlet::new_blocking(stream("SyncKeep", Format::Float32)).expect("an outlet");
let mut inlet =
Inlet::open_with_buffer(outlet.info(), Duration::from_secs(5), 1000).expect("an inlet");
assert!(outlet.wait_for_consumers(Duration::from_secs(5)));
std::thread::sleep(Duration::from_millis(200));
const COUNT: usize = 200;
let sender = std::thread::spawn(move || {
for k in 0..COUNT {
outlet.push(&Sample {
timestamp: 1.0 + k as f64,
values: vec![Value::F32(k as f32)],
});
}
outlet
});
let mut seen = Vec::new();
for _ in 0..COUNT {
std::thread::sleep(Duration::from_millis(1));
match inlet.pull(Duration::from_secs(5)) {
Ok(Some(s)) => seen.push(s.values[0].clone()),
Ok(None) => break,
Err(e) => panic!("pull failed: {e}"),
}
}
let _outlet = sender.join().expect("the sender");
assert_eq!(seen.len(), COUNT, "a blocking outlet must lose nothing");
let want: Vec<Value> = (0..COUNT).map(|k| Value::F32(k as f32)).collect();
assert_eq!(seen, want, "the samples must arrive in order and complete");
}