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");
assert!(outlet.wait_for_consumers(Duration::from_secs(5)));
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;
}
};
assert!(outlet.wait_for_consumers(Duration::from_secs(5)));
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;
}
};
assert!(outlet.wait_for_consumers(Duration::from_secs(5)));
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:?}");
}
#[test]
fn a_retarget_moves_the_probes_to_another_outlet() {
use labstream_net::clock::{self, ClockState};
use std::sync::{Arc, Mutex};
let make = |name: &str| {
let mut i = labstream_net::StreamInfo::new(
name,
"ClockTest",
1,
labstream_wire::Format::Float32,
100.0,
);
i.source_id = format!("{name}_src");
labstream_net::Outlet::new(i)
};
let (a, b) = match (make("RetargetA"), make("RetargetB")) {
(Ok(a), Ok(b)) => (a, b),
_ => {
eprintln!("no local network; skipping");
return;
}
};
let port = |o: &labstream_net::Outlet| -> std::net::SocketAddr {
format!("127.0.0.1:{}", o.info().v4service_port)
.parse()
.unwrap()
};
let state = Arc::new(ClockState::default());
clock::start(port(&a), Arc::clone(&state), Arc::new(Mutex::new(7)));
assert!(
state.wait(Duration::from_secs(5)).is_some(),
"outlet A answers"
);
state.retarget(port(&b));
state.reset_offset();
drop(a);
assert!(
state.wait(Duration::from_secs(5)).is_some(),
"outlet B answers after the retarget"
);
state.stop();
drop(b);
}
#[test]
fn the_first_offset_arrives_quickly() {
let (_outlet, inlet) = match pair("ClkLiveFast") {
Some(p) => p,
None => {
eprintln!("no local network; skipping");
return;
}
};
let t = std::time::Instant::now();
let got = inlet.wait_for_time_correction(Duration::from_secs(5));
let took = t.elapsed();
println!("first offset after {took:?}");
assert!(got.is_some(), "an offset arrives");
assert!(
took < Duration::from_millis(250),
"the first offset took {took:?}"
);
}