use labstream_net::{Inlet, Outlet, StreamInfo};
use labstream_wire::{Format, Sample, Value};
use std::time::Duration;
fn stream(name: &str) -> StreamInfo {
let mut i = StreamInfo::new(name, "ClockTest", 2, Format::Float32, 100.0);
i.source_id = format!("{name}_src");
i
}
fn pair(name: &str) -> Option<(Outlet, Inlet)> {
let outlet = Outlet::new(stream(name)).ok()?;
let info = outlet.info().clone();
let inlet = Inlet::open(&info, Duration::from_secs(5)).ok()?;
Some((outlet, inlet))
}
#[test]
fn the_offset_between_two_local_clocks_is_small() {
let (_outlet, inlet) = match pair("ClkLive1") {
Some(p) => p,
None => {
eprintln!("no local network; skipping");
return;
}
};
let got = inlet.wait_for_time_correction(Duration::from_secs(8));
let offset = match got {
Some(v) => v,
None => {
eprintln!("no measurement inside the timeout; skipping");
return;
}
};
let rtt = inlet.time_uncertainty().unwrap_or(f64::MAX);
println!("offset {offset:+.9} s, round-trip {:.6} ms", rtt * 1000.0);
assert!(
offset.abs() < 1.0,
"the offset is {offset} s. A value near the age of the machine means \
the clock does not share its origin with liblsl."
);
assert!(
offset.abs() <= rtt / 2.0 + 0.001,
"the offset {offset} passed the bound from its round-trip time {rtt}"
);
}
#[test]
fn clock_sync_moves_a_timestamp_by_the_measured_offset() {
let (outlet, mut inlet) = match pair("ClkLive2") {
Some(p) => p,
None => {
eprintln!("no local network; skipping");
return;
}
};
if inlet
.wait_for_time_correction(Duration::from_secs(8))
.is_none()
{
eprintln!("no measurement inside the timeout; skipping");
return;
}
let offset = inlet.time_correction().expect("an offset");
let sample = Sample {
timestamp: 1000.0,
values: vec![Value::F32(1.0), Value::F32(2.0)],
};
outlet.push(&sample);
inlet.set_postprocessing(labstream_time::flags::CLOCKSYNC);
let got = inlet
.pull(Duration::from_secs(5))
.expect("a pull")
.expect("a sample");
let moved = got.timestamp - 1000.0;
println!("offset {offset:+.9}, the timestamp moved {moved:+.9}");
assert!(
(moved - offset).abs() < 1e-6,
"the stage moved the timestamp by {moved} and the offset is {offset}"
);
}
#[test]
fn no_stage_leaves_a_timestamp_alone() {
let (outlet, mut inlet) = match pair("ClkLive3") {
Some(p) => p,
None => {
eprintln!("no local network; skipping");
return;
}
};
let sample = Sample {
timestamp: 1234.5,
values: vec![Value::F32(1.0), Value::F32(2.0)],
};
outlet.push(&sample);
let got = inlet
.pull(Duration::from_secs(5))
.expect("a pull")
.expect("a sample");
assert_eq!(got.timestamp.to_bits(), 1234.5f64.to_bits());
}
#[test]
fn a_deduced_timestamp_is_rebuilt_from_the_rate() {
let srate = 100.0;
let mut info = StreamInfo::new("DeducedTest", "T", 2, Format::Float32, srate);
info.source_id = "deduced_src".into();
let outlet = match Outlet::new(info) {
Ok(o) => o,
Err(_) => {
eprintln!("no local network; skipping");
return;
}
};
let mut inlet = match Inlet::open(outlet.info(), Duration::from_secs(5)) {
Ok(i) => i,
Err(_) => {
eprintln!("could not open; skipping");
return;
}
};
let first = 5000.0;
outlet.push(&Sample {
timestamp: first,
values: vec![Value::F32(0.0), Value::F32(0.0)],
});
for k in 1..5 {
outlet.push(&Sample {
timestamp: labstream_wire::DEDUCED_TIMESTAMP,
values: vec![Value::F32(k as f32), Value::F32(0.0)],
});
}
let mut got = Vec::new();
for _ in 0..5 {
match inlet.pull(Duration::from_secs(3)) {
Ok(Some(s)) => got.push(s.timestamp),
_ => break,
}
}
assert_eq!(got.len(), 5, "not every sample arrived");
for (k, t) in got.iter().enumerate() {
let want = first + k as f64 / srate;
assert!(
(t - want).abs() < 1e-9,
"sample {k} carries {t} and the rate gives {want}"
);
}
println!("rebuilt {got:?}");
}