#![cfg(unix)]
use std::net::UdpSocket;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use mio::{Events, Interest, Poll, Token};
use snare::Sim;
fn vsim() -> Sim {
Sim::builder().virtual_clock().build()
}
#[test]
fn reads_a_fixed_epoch_without_ticking() {
Sim::builder().fixed_epoch().build().run(|| {
let a = Instant::now();
let b = Instant::now();
assert_eq!(a, b);
let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
assert_eq!(now.as_secs(), 1_700_000_000);
});
}
#[test]
fn lone_sleep_skips_virtual_time() {
let real = Instant::now();
vsim().run(|| {
let start = Instant::now();
std::thread::sleep(Duration::from_secs(3600));
assert!(
start.elapsed() >= Duration::from_secs(3600),
"virtual hour elapsed"
);
});
assert!(real.elapsed() < Duration::from_secs(60));
}
#[test]
fn advance_time_steps_the_clock() {
let sim = Sim::builder().fixed_epoch().build();
sim.run(|| {
let start = Instant::now();
assert!(start.elapsed() < Duration::from_millis(1));
});
sim.advance_time(Duration::from_secs(5));
sim.run(|| {
let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
assert_eq!(now.as_secs(), 1_700_000_005);
});
}
#[test]
fn time_skips_across_threads_to_a_timer() {
vsim().run(|| {
let receiver = UdpSocket::bind("127.0.0.1:0").unwrap();
let addr = receiver.local_addr().unwrap();
let start = Instant::now();
let sender = std::thread::spawn(move || {
let s = UdpSocket::bind("127.0.0.1:0").unwrap();
std::thread::sleep(Duration::from_secs(600));
s.send_to(b"tick", addr).unwrap();
});
let mut buf = [0u8; 8];
let (n, _) = receiver.recv_from(&mut buf).unwrap();
assert_eq!(&buf[..n], b"tick");
sender.join().unwrap();
assert!(
start.elapsed() >= Duration::from_secs(600),
"clock advanced to the timer"
);
});
}
#[test]
fn run_testers_join_works_under_virtual_time() {
use snare::{Line, TesterAction, connect_tester, run_testers};
let real = Instant::now();
vsim().run(|| {
let server = connect_tester::<Line>("127.0.0.7:9710")
.then_action(|msg, _| TesterAction::Send(Line(format!("echo:{}", msg.0))))
.until_after(Duration::from_secs(5));
let client = std::thread::spawn(|| {
use std::io::{BufRead, BufReader, Write};
let mut s = std::net::TcpStream::connect("127.0.0.7:9710").unwrap();
s.write_all(b"hi\n").unwrap();
let mut line = String::new();
BufReader::new(s).read_line(&mut line).unwrap();
line
});
run_testers!(server);
assert_eq!(client.join().unwrap(), "echo:hi\n");
});
assert!(
real.elapsed() < Duration::from_secs(5),
"ran as-fast-as-possible, not for 5 real s"
);
}
#[test]
fn poll_timeout_skips_virtual_time() {
let real = Instant::now();
vsim().run(|| {
let mut poll = Poll::new().unwrap();
let mut events = Events::with_capacity(8);
let mut sock = mio::net::UdpSocket::bind("127.0.0.1:0".parse().unwrap()).unwrap();
poll.registry()
.register(&mut sock, Token(0), Interest::READABLE)
.unwrap();
let start = Instant::now();
poll.poll(&mut events, Some(Duration::from_secs(30)))
.unwrap();
assert_eq!(events.iter().count(), 0, "no fd became ready");
let advanced = start.elapsed();
assert!(
(Duration::from_millis(29_900)..=Duration::from_secs(31)).contains(&advanced),
"virtual time reached the timeout, got {advanced:?}"
);
});
assert!(
real.elapsed() < Duration::from_secs(30),
"ran as-fast-as-possible, not 30 real s"
);
}
#[test]
fn a_busy_poll_lets_virtual_time_reach_a_sleeper() {
let real = Instant::now();
Sim::new().run(|| {
let receiver = UdpSocket::bind("127.0.0.1:0").unwrap();
receiver.set_nonblocking(true).unwrap();
let addr = receiver.local_addr().unwrap();
let start = Instant::now();
let sender = std::thread::spawn(move || {
let s = UdpSocket::bind("127.0.0.1:0").unwrap();
std::thread::sleep(Duration::from_millis(10));
s.send_to(b"late", addr).unwrap();
});
let mut buf = [0u8; 8];
let n = loop {
match receiver.recv_from(&mut buf) {
Ok((n, _)) => break n,
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => continue,
Err(e) => panic!("{e}"),
}
};
assert_eq!(&buf[..n], b"late");
sender.join().unwrap();
assert!(
start.elapsed() >= Duration::from_millis(10),
"the sender's sleep elapsed"
);
});
assert!(
real.elapsed() < Duration::from_secs(30),
"the spin did not stall the clock"
);
}
#[test]
fn wall_clock_opts_out() {
Sim::builder().wall_clock().build().run(|| {
let real = std::time::Instant::now();
std::thread::sleep(Duration::from_millis(30));
assert!(
real.elapsed() >= Duration::from_millis(30),
"slept in real time"
);
let a = Instant::now();
let b = Instant::now();
assert!(b >= a);
});
}
#[test]
fn deadlock_without_a_timer_still_gives_up() {
vsim().run(|| {
let s = UdpSocket::bind("127.0.0.1:0").unwrap();
let mut buf = [0u8; 8];
assert!(s.recv_from(&mut buf).is_err());
});
}