use std::net::{SocketAddr, ToSocketAddrs};
use std::panic::{AssertUnwindSafe, catch_unwind, resume_unwind};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use crate::events::{Direction, RecordedEvent, Toward, Transport};
#[cfg(unix)]
use crate::fabric as peer;
use crate::faults::ListenerBehavior;
use crate::packet::Packet;
use crate::readiness::{Deadline, readiness};
use crate::scope::SimShared;
#[cfg(windows)]
use crate::win_net as peer;
use peer::{Conn, Listener, Registries, UdpEndpoint};
pub enum TesterAction<P> {
Nothing,
Send(P),
SendTo(SocketAddr, P),
SendAll(Vec<P>),
Close,
Multiple(Vec<TesterAction<P>>),
Reset,
Quiesce(Duration),
SetListenerBehavior(ListenerBehavior),
RaiseSocketError(std::io::Error),
IcmpPortUnreachable,
QuiesceLink(Duration, Direction),
SetRecvWindow(Option<usize>),
}
type OnMessage<P, S, C> = Box<dyn FnMut(&mut S, &mut C, P, SocketAddr) -> TesterAction<P> + Send>;
type OnFilter<P, S, C> = Box<dyn FnMut(&mut S, &mut C, P, SocketAddr) -> Option<P> + Send>;
type OnPeer<P, S, C> = Box<dyn FnMut(&mut S, &mut C, SocketAddr) -> TesterAction<P> + Send>;
type OnTick<P, S> = Box<dyn FnMut(&mut S) -> TesterAction<P> + Send>;
type OnConnTick<P, S, C> = Box<dyn FnMut(&mut S, &mut C, SocketAddr) -> TesterAction<P> + Send>;
type NewConn<C> = Box<dyn FnMut(SocketAddr) -> C + Send>;
type OnElapsed<S> = Box<dyn FnMut(&S, Duration) -> bool + Send>;
type OnConns<S, C> = Box<dyn FnMut(&S, &[(SocketAddr, C)]) -> bool + Send>;
enum Finish<S, C> {
State(OnElapsed<S>),
Conns(OnConns<S, C>),
}
enum Handler<P, S, C> {
Act(OnMessage<P, S, C>),
Filter(OnFilter<P, S, C>),
}
enum Tick<P, S, C> {
Once(OnTick<P, S>),
PerConn(OnConnTick<P, S, C>),
}
struct Cyclic<P, S, C> {
period: Duration,
phase: Duration,
action: Tick<P, S, C>,
next: Instant,
}
enum TesterTransport {
Tcp(Arc<Listener>),
Udp(UdpEndpoint),
}
struct Inner<P, S, C> {
transport: TesterTransport,
regs: Arc<Registries>,
state: S,
new_conn: NewConn<C>,
conns: Vec<(SocketAddr, C)>,
handlers: Vec<Handler<P, S, C>>,
on_peer: Vec<OnPeer<P, S, C>>,
cyclic: Vec<Cyclic<P, S, C>>,
finish: Vec<Finish<S, C>>,
deadline: Option<Duration>,
poll: bool,
}
const UNTIL_POLL: Duration = Duration::from_millis(1);
pub struct Tester<P: Packet, S = (), C = ()> {
addr: SocketAddr,
inner: Mutex<Inner<P, S, C>>,
recorder: Option<Recorder<P>>,
}
pub fn connect_tester<P: Packet>(addr: impl ToSocketAddrs) -> Tester<P> {
let addr = resolve(addr);
Tester::new(addr, TesterTransport::Tcp(peer::listen_at(addr)))
}
pub fn udp_tester<P: Packet>(addr: impl ToSocketAddrs) -> Tester<P> {
let addr = resolve(addr);
Tester::new(
addr,
TesterTransport::Udp(UdpEndpoint::bind(peer::registries_here(), addr)),
)
}
fn resolve(addr: impl ToSocketAddrs) -> SocketAddr {
addr.to_socket_addrs()
.expect("tester address must parse")
.next()
.expect("tester address resolved to nothing")
}
impl<P: Packet> Tester<P> {
fn new(addr: SocketAddr, transport: TesterTransport) -> Self {
Tester {
addr,
inner: Mutex::new(Inner {
transport,
regs: peer::registries_here(),
state: (),
new_conn: Box::new(|_| ()),
conns: Vec::new(),
handlers: Vec::new(),
on_peer: Vec::new(),
cyclic: Vec::new(),
finish: Vec::new(),
deadline: None,
poll: false,
}),
recorder: None,
}
}
}
impl<P: Packet, C: Send + 'static> Tester<P, (), C> {
pub fn with_state<T: Send + 'static>(self, state: T) -> Tester<P, T, C> {
let inner = self.inner.into_inner().unwrap();
Tester {
addr: self.addr,
recorder: self.recorder,
inner: Mutex::new(Inner {
transport: inner.transport,
regs: inner.regs,
state,
new_conn: inner.new_conn,
conns: inner.conns,
handlers: inner
.handlers
.into_iter()
.map(|handler| match handler {
Handler::Act(mut f) => {
Handler::Act(Box::new(move |_: &mut T, c, m, a| f(&mut (), c, m, a)))
}
Handler::Filter(mut f) => {
Handler::Filter(Box::new(move |_: &mut T, c, m, a| f(&mut (), c, m, a)))
}
})
.collect(),
on_peer: inner
.on_peer
.into_iter()
.map(|mut f| {
Box::new(move |_: &mut T, c: &mut C, a| f(&mut (), c, a)) as OnPeer<P, T, C>
})
.collect(),
cyclic: inner
.cyclic
.into_iter()
.map(|c| Cyclic {
period: c.period,
phase: c.phase,
next: c.next,
action: match c.action {
Tick::Once(mut f) => Tick::Once(Box::new(move |_: &mut T| f(&mut ()))),
Tick::PerConn(mut f) => {
Tick::PerConn(Box::new(move |_: &mut T, conn: &mut C, a| {
f(&mut (), conn, a)
}))
}
},
})
.collect(),
finish: inner
.finish
.into_iter()
.map(|finish| match finish {
Finish::State(mut f) => Finish::State(Box::new(move |_: &T, e| f(&(), e))),
Finish::Conns(mut f) => {
Finish::Conns(Box::new(move |_: &T, conns| f(&(), conns)))
}
})
.collect(),
deadline: inner.deadline,
poll: inner.poll,
}),
}
}
}
impl<P: Packet, S: Send + 'static> Tester<P, S, ()> {
pub fn with_conn_state<D: Send + 'static>(
self,
new: impl FnMut(SocketAddr) -> D + Send + 'static,
) -> Tester<P, S, D> {
let inner = self.inner.into_inner().unwrap();
Tester {
addr: self.addr,
recorder: self.recorder,
inner: Mutex::new(Inner {
transport: inner.transport,
regs: inner.regs,
state: inner.state,
new_conn: Box::new(new),
conns: Vec::new(),
handlers: inner
.handlers
.into_iter()
.map(|handler| match handler {
Handler::Act(mut f) => {
Handler::Act(Box::new(move |s, _: &mut D, m, a| f(s, &mut (), m, a)))
}
Handler::Filter(mut f) => {
Handler::Filter(Box::new(move |s, _: &mut D, m, a| f(s, &mut (), m, a)))
}
})
.collect(),
on_peer: inner
.on_peer
.into_iter()
.map(|mut f| {
Box::new(move |s: &mut S, _: &mut D, a| f(s, &mut (), a)) as OnPeer<P, S, D>
})
.collect(),
cyclic: inner
.cyclic
.into_iter()
.map(|c| Cyclic {
period: c.period,
phase: c.phase,
next: c.next,
action: match c.action {
Tick::Once(f) => Tick::Once(f),
Tick::PerConn(mut f) => {
Tick::PerConn(Box::new(move |s: &mut S, _: &mut D, a| {
f(s, &mut (), a)
}))
}
},
})
.collect(),
finish: inner
.finish
.into_iter()
.map(|finish| match finish {
Finish::State(f) => Finish::State(f),
Finish::Conns(mut f) => {
Finish::Conns(Box::new(move |s: &S, conns: &[(SocketAddr, D)]| {
let peers: Vec<(SocketAddr, ())> =
conns.iter().map(|&(a, _)| (a, ())).collect();
f(s, &peers)
}))
}
})
.collect(),
deadline: inner.deadline,
poll: inner.poll,
}),
}
}
}
impl<P: Packet, S: Send + 'static, C: Send + 'static> Tester<P, S, C> {
fn setup(&mut self) -> &mut Inner<P, S, C> {
self.inner.get_mut().unwrap()
}
pub fn then_action(
mut self,
mut action: impl FnMut(P, SocketAddr) -> TesterAction<P> + Send + 'static,
) -> Self {
self.setup()
.handlers
.push(Handler::Act(Box::new(move |_, _, m, from| action(m, from))));
self
}
pub fn then_stateful_action(
mut self,
mut action: impl FnMut(&mut S, P, SocketAddr) -> TesterAction<P> + Send + 'static,
) -> Self {
self.setup()
.handlers
.push(Handler::Act(Box::new(move |s, _, m, from| {
action(s, m, from)
})));
self
}
pub fn then_conn_action(
mut self,
mut action: impl FnMut(&mut C, &mut S, P, SocketAddr) -> TesterAction<P> + Send + 'static,
) -> Self {
self.setup()
.handlers
.push(Handler::Act(Box::new(move |s, c, m, from| {
action(c, s, m, from)
})));
self
}
pub fn then_test(
mut self,
mut test: impl FnMut(P, SocketAddr) -> Option<P> + Send + 'static,
) -> Self {
self.setup()
.handlers
.push(Handler::Filter(Box::new(move |_, _, m, from| {
test(m, from)
})));
self
}
pub fn then_stateful_test(
mut self,
mut test: impl FnMut(&mut S, P, SocketAddr) -> Option<P> + Send + 'static,
) -> Self {
self.setup()
.handlers
.push(Handler::Filter(Box::new(move |s, _, m, from| {
test(s, m, from)
})));
self
}
pub fn then_conn_test(
mut self,
mut test: impl FnMut(&mut C, &mut S, P, SocketAddr) -> Option<P> + Send + 'static,
) -> Self {
self.setup()
.handlers
.push(Handler::Filter(Box::new(move |s, c, m, from| {
test(c, s, m, from)
})));
self
}
pub fn then_edit_state(
mut self,
mut edit: impl FnMut(&mut S, SocketAddr) + Send + 'static,
) -> Self {
self.setup()
.handlers
.push(Handler::Filter(Box::new(move |state, _, m, from| {
edit(state, from);
Some(m)
})));
self
}
pub fn on_connect(
mut self,
mut action: impl FnMut(&mut S, SocketAddr) -> TesterAction<P> + Send + 'static,
) -> Self {
self.setup()
.on_peer
.push(Box::new(move |s, _, from| action(s, from)));
self
}
pub fn on_conn_connect(
mut self,
mut action: impl FnMut(&mut C, &mut S, SocketAddr) -> TesterAction<P> + Send + 'static,
) -> Self {
self.setup()
.on_peer
.push(Box::new(move |s, c, from| action(c, s, from)));
self
}
pub fn with_cyclic_action(
self,
period: Duration,
action: impl FnMut() -> TesterAction<P> + Send + 'static,
) -> Self {
self.with_cyclic_action_at(period, period, action)
}
pub fn with_stateful_cyclic_action(
self,
period: Duration,
action: impl FnMut(&mut S) -> TesterAction<P> + Send + 'static,
) -> Self {
self.with_stateful_cyclic_action_at(period, period, action)
}
pub fn with_cyclic_action_at(
self,
period: Duration,
phase: Duration,
mut action: impl FnMut() -> TesterAction<P> + Send + 'static,
) -> Self {
self.with_stateful_cyclic_action_at(period, phase, move |_| action())
}
pub fn with_stateful_cyclic_action_at(
mut self,
period: Duration,
phase: Duration,
action: impl FnMut(&mut S) -> TesterAction<P> + Send + 'static,
) -> Self {
self.push_cyclic(period, phase, Tick::Once(Box::new(action)));
self
}
pub fn with_conn_cyclic_action(
self,
period: Duration,
action: impl FnMut(&mut C, &mut S, SocketAddr) -> TesterAction<P> + Send + 'static,
) -> Self {
self.with_conn_cyclic_action_at(period, period, action)
}
pub fn with_conn_cyclic_action_at(
mut self,
period: Duration,
phase: Duration,
mut action: impl FnMut(&mut C, &mut S, SocketAddr) -> TesterAction<P> + Send + 'static,
) -> Self {
self.push_cyclic(
period,
phase,
Tick::PerConn(Box::new(move |s, c, from| action(c, s, from))),
);
self
}
fn push_cyclic(&mut self, period: Duration, phase: Duration, action: Tick<P, S, C>) {
assert!(
!period.is_zero(),
"a cyclic action's period must not be zero"
);
self.setup().cyclic.push(Cyclic {
period,
phase,
action,
next: Instant::now(),
});
}
pub fn until(mut self, mut condition: impl FnMut(Duration) -> bool + Send + 'static) -> Self {
let inner = self.setup();
inner.poll = true;
inner.finish.push(Finish::State(Box::new(move |_, elapsed| {
condition(elapsed)
})));
self
}
pub fn until_state(mut self, mut condition: impl FnMut(&S) -> bool + Send + 'static) -> Self {
self.setup()
.finish
.push(Finish::State(Box::new(move |state, _| condition(state))));
self
}
pub fn until_conns(
mut self,
condition: impl FnMut(&S, &[(SocketAddr, C)]) -> bool + Send + 'static,
) -> Self {
self.setup().finish.push(Finish::Conns(Box::new(condition)));
self
}
pub fn until_after(mut self, duration: Duration) -> Self {
let inner = self.setup();
inner.deadline = Some(inner.deadline.map_or(duration, |d| d.min(duration)));
self
}
pub fn recording(mut self) -> Self {
self.recorder = Some(Recorder::default());
self
}
pub fn addr(&self) -> SocketAddr {
self.addr
}
pub fn recorder(&self) -> Recorder<P> {
self.recorder
.clone()
.expect("call .recording() on the tester to record what it receives")
}
pub fn recorded(&self) -> Vec<(SocketAddr, P)> {
self.recorder().snapshot()
}
pub fn inspect<R>(&self, read: impl FnOnce(&S) -> R) -> R {
let inner = match self.inner.try_lock() {
Ok(inner) => inner,
Err(std::sync::TryLockError::Poisoned(e)) => e.into_inner(),
Err(std::sync::TryLockError::WouldBlock) => {
panic!("Tester::inspect called while the tester is running")
}
};
read(&inner.state)
}
pub fn inspect_conns<R>(&self, read: impl FnOnce(&[(SocketAddr, C)]) -> R) -> R {
let inner = match self.inner.try_lock() {
Ok(inner) => inner,
Err(std::sync::TryLockError::Poisoned(e)) => e.into_inner(),
Err(std::sync::TryLockError::WouldBlock) => {
panic!("Tester::inspect_conns called while the tester is running")
}
};
read(&inner.conns)
}
pub fn into_state(self) -> S {
self.inner
.into_inner()
.unwrap_or_else(|e| e.into_inner())
.state
}
pub fn run(&self) {
self.run_until(&AtomicBool::new(false), Instant::now());
}
}
pub struct Recorder<P> {
received: Arc<Mutex<Vec<(SocketAddr, P)>>>,
}
impl<P> Clone for Recorder<P> {
fn clone(&self) -> Self {
Recorder {
received: self.received.clone(),
}
}
}
impl<P> Default for Recorder<P> {
fn default() -> Self {
Recorder {
received: Arc::default(),
}
}
}
impl<P: Clone> Recorder<P> {
pub fn snapshot(&self) -> Vec<(SocketAddr, P)> {
self.received.lock().unwrap().clone()
}
pub fn len(&self) -> usize {
self.received.lock().unwrap().len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
fn push(&self, from: SocketAddr, message: &P) {
self.received.lock().unwrap().push((from, message.clone()));
}
}
struct Log<'a> {
shared: &'a SimShared,
tester: SocketAddr,
}
impl Log<'_> {
fn received(&self, transport: Transport, peer: SocketAddr, len: usize) {
self.shared.record(RecordedEvent::Received {
transport,
tester: self.tester,
peer,
len,
});
}
fn sent(&self, transport: Transport, peer: SocketAddr, len: usize) {
self.shared.record(RecordedEvent::Sent {
transport,
tester: self.tester,
peer,
len,
});
}
fn suppressed(&self, peer: SocketAddr, toward: Toward, len: usize) {
self.shared.record(RecordedEvent::Suppressed {
tester: self.tester,
peer,
toward,
len,
});
}
fn quiesced(&self, peer: SocketAddr, span: Duration) {
self.shared.record(RecordedEvent::Quiesced {
tester: self.tester,
peer,
span,
});
}
}
struct Peer {
conn: Arc<Conn>,
buf: Vec<u8>,
outbox: Vec<u8>,
gone: bool,
closed: bool,
closing: bool,
reset: bool,
silent_until: Option<Instant>,
}
impl Peer {
fn pull(&mut self, log: &Log) {
if self.gone {
return;
}
let (tester, peer) = (log.tester, self.conn.client);
let mut chunk = [0u8; 8192]; loop {
match self.conn.read_from_peer(&mut chunk) {
Ok(0) => {
self.gone = true;
log.shared
.record(RecordedEvent::PeerClosed { tester, peer });
break;
}
Ok(n) => self.buf.extend_from_slice(&chunk[..n]),
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => break,
Err(e) => {
self.gone = true;
if e.kind() == std::io::ErrorKind::ConnectionReset && !self.reset {
self.closed = true;
self.outbox.clear();
log.shared.record(RecordedEvent::PeerReset { tester, peer });
}
break;
}
}
}
}
fn open(&self) -> bool {
!self.gone && !self.closed && !self.closing
}
fn silent(&self) -> bool {
self.silent_until
.is_some_and(|until| Instant::now() < until)
}
fn send(&mut self, bytes: &[u8], log: &Log) {
if !self.open() {
return;
}
if self.silent() {
log.suppressed(self.conn.client, Toward::CodeUnderTest, bytes.len());
return;
}
self.outbox.extend_from_slice(bytes);
self.flush(log);
}
fn flush(&mut self, log: &Log) {
if self.closed {
return;
}
if !self.outbox.is_empty() {
let n = self.conn.write_from_peer(&self.outbox);
if n > 0 {
log.sent(Transport::Tcp, self.conn.client, n);
self.outbox.drain(..n);
} else if !self.conn.peer_can_write(self.outbox.len()) {
return;
} else {
self.outbox.clear();
}
}
if self.closing && self.outbox.is_empty() {
self.closing = false;
self.close_now(log);
}
}
fn flushable(&self) -> bool {
!self.closed && !self.outbox.is_empty() && self.conn.peer_can_write(self.outbox.len())
}
fn quiesce(&mut self, until: Instant, span: Duration, log: &Log) {
self.silent_until = Some(until);
log.quiesced(self.conn.client, span);
}
fn reset(&mut self, log: &Log) {
if !self.closed {
self.closed = true;
self.closing = false;
self.reset = true;
self.outbox.clear();
self.conn.reset_peer();
log.shared.record(RecordedEvent::Reset {
tester: log.tester,
peer: self.conn.client,
});
}
}
fn close(&mut self, log: &Log) {
if self.closed || self.closing {
return;
}
self.closing = true;
self.flush(log);
}
fn close_now(&mut self, log: &Log) {
if !self.closed {
self.closed = true;
self.closing = false;
self.outbox.clear();
self.conn.close_peer();
log.shared.record(RecordedEvent::Closed {
tester: log.tester,
peer: self.conn.client,
});
}
}
}
#[derive(Clone, Copy)]
enum Origin {
Tcp(usize),
Udp(SocketAddr),
Everyone,
}
struct Peers<'a> {
transport: &'a TesterTransport,
log: Log<'a>,
tcp: Vec<Peer>,
udp: Vec<SocketAddr>,
udp_silent: crate::HashMap<SocketAddr, Instant>,
}
impl Peers<'_> {
fn apply<P: Packet>(&mut self, action: TesterAction<P>, origin: Origin) {
match action {
TesterAction::Nothing => {}
TesterAction::Send(p) => self.send(&p.to_bytes(), origin),
TesterAction::SendAll(ps) => {
for p in ps {
self.send(&p.to_bytes(), origin);
}
}
TesterAction::SendTo(to, p) => self.send_to(to, &p.to_bytes()),
TesterAction::Close => match origin {
Origin::Tcp(i) => self.tcp[i].close(&self.log),
Origin::Everyone => self.tcp.iter_mut().for_each(|p| p.close(&self.log)),
Origin::Udp(_) => {}
},
TesterAction::Multiple(actions) => {
for a in actions {
self.apply(a, origin);
}
}
TesterAction::Reset => match origin {
Origin::Tcp(i) => self.tcp[i].reset(&self.log),
Origin::Everyone => self.tcp.iter_mut().for_each(|p| p.reset(&self.log)),
Origin::Udp(_) => {}
},
TesterAction::SetListenerBehavior(behavior) => {
if matches!(self.transport, TesterTransport::Tcp(_)) {
crate::faults::set_listener_behavior_on(
self.log.shared,
self.log.tester,
behavior,
);
}
}
TesterAction::RaiseSocketError(error) => {
let errno = crate::faults::socket_errno(&error);
let addrs = self.sut_addrs(origin);
self.log.shared.raise_socket_error(&addrs, errno);
}
TesterAction::IcmpPortUnreachable => {
if matches!(self.transport, TesterTransport::Udp(_)) {
let addrs = self.sut_addrs(origin);
self.log.shared.inject_icmp(&addrs, self.log.tester);
}
}
TesterAction::QuiesceLink(span, direction) => {
let addrs = self.sut_addrs(origin);
self.log.shared.quiesce(&addrs, span, direction);
}
TesterAction::SetRecvWindow(window) => {
for addr in self.sut_addrs(origin) {
self.log
.shared
.update_tcp_policy(addr, |p| p.recv_window = window);
}
self.log.shared.kick();
}
TesterAction::Quiesce(span) => {
let until = Instant::now() + span;
match origin {
Origin::Tcp(i) => self.tcp[i].quiesce(until, span, &self.log),
Origin::Udp(from) => {
self.udp_silent.insert(from, until);
self.log.quiesced(from, span);
}
Origin::Everyone => {
for peer in self.tcp.iter_mut().filter(|p| p.open()) {
peer.quiesce(until, span, &self.log);
}
for &from in &self.udp {
self.udp_silent.insert(from, until);
self.log.quiesced(from, span);
}
}
}
}
}
}
fn sut_addrs(&self, origin: Origin) -> Vec<SocketAddr> {
match origin {
Origin::Tcp(i) => vec![self.tcp[i].conn.client],
Origin::Udp(from) => vec![from],
Origin::Everyone => self
.tcp
.iter()
.filter(|p| p.open())
.map(|p| p.conn.client)
.chain(self.udp.iter().copied())
.collect(),
}
}
fn udp_silent(&self, peer: SocketAddr) -> bool {
self.udp_silent
.get(&peer)
.is_some_and(|&until| Instant::now() < until)
}
fn send_udp(&self, ep: &UdpEndpoint, to: SocketAddr, bytes: &[u8]) {
if self.udp_silent(to) {
self.log.suppressed(to, Toward::CodeUnderTest, bytes.len());
} else {
self.log.sent(Transport::Udp, to, bytes.len());
ep.send_to(to, bytes);
}
}
fn send(&mut self, bytes: &[u8], origin: Origin) {
match (origin, self.transport) {
(Origin::Tcp(i), _) => self.tcp[i].send(bytes, &self.log),
(Origin::Udp(to), TesterTransport::Udp(ep)) => self.send_udp(ep, to, bytes),
(Origin::Everyone, TesterTransport::Tcp(_)) => {
let log = &self.log;
self.tcp.iter_mut().for_each(|peer| peer.send(bytes, log));
}
(Origin::Everyone, TesterTransport::Udp(ep)) => {
for &to in &self.udp {
self.send_udp(ep, to, bytes);
}
}
(Origin::Udp(_), TesterTransport::Tcp(_)) => {
unreachable!("UDP origin on a TCP tester")
}
}
}
fn send_to(&mut self, to: SocketAddr, bytes: &[u8]) {
match self.transport {
TesterTransport::Udp(ep) => self.send_udp(ep, to, bytes),
TesterTransport::Tcp(_) => self
.tcp
.iter_mut()
.find(|peer| peer.conn.client == to && peer.open())
.unwrap_or_else(|| panic!("tester has no open connection from {to}"))
.send(bytes, &self.log),
}
}
}
impl<P: Packet, S, C> Inner<P, S, C> {
fn run(
&mut self,
addr: SocketAddr,
recorder: Option<&Recorder<P>>,
stop: &AtomicBool,
start: Instant,
) {
#[cfg(windows)]
let wake_origin = (Instant::now(), Deadline::after(Duration::ZERO));
for c in &mut self.cyclic {
c.next = start + c.phase;
}
self.conns.clear();
let as_fast_as_possible = snare_interpose::virtual_now().is_none()
&& snare_interpose::now(snare_interpose::ClockKind::Monotonic).is_some();
let Inner {
transport,
regs,
state,
new_conn,
conns,
handlers,
on_peer,
cyclic,
finish,
deadline,
poll,
} = self;
let mut peers = Peers {
transport,
log: Log {
shared: ®s.shared,
tester: addr,
},
tcp: Vec::new(),
udp: Vec::new(),
udp_silent: crate::HashMap::default(),
};
let mut deliver =
|peers: &mut Peers, state: &mut S, conn: &mut C, message: P, from, origin| {
if let Some(r) = recorder {
r.push(from, &message);
}
let last = handlers.len().saturating_sub(1);
let mut message = Some(message);
for (i, handler) in handlers.iter_mut().enumerate() {
let Some(m) = message.take() else {
return;
};
match handler {
Handler::Filter(test) => message = test(state, conn, m, from),
Handler::Act(act) => {
let m = if i == last {
m
} else {
message.insert(m).clone()
};
let action = act(state, conn, m, from);
peers.apply(action, origin);
}
}
}
};
loop {
match peers.transport {
TesterTransport::Tcp(listener) => {
while let Some(conn) = listener.try_accept() {
let from = conn.client;
peers.log.shared.record(RecordedEvent::Accepted {
tester: addr,
peer: from,
});
peers.tcp.push(Peer {
conn,
buf: Vec::new(),
outbox: Vec::new(),
gone: false,
closed: false,
closing: false,
reset: false,
silent_until: None,
});
let origin = Origin::Tcp(peers.tcp.len() - 1);
conns.push((from, new_conn(from)));
let conn = &mut conns.last_mut().expect("just pushed").1;
for handler in on_peer.iter_mut() {
let action = handler(state, conn, from);
peers.apply(action, origin);
}
}
for (i, (_, conn)) in conns.iter_mut().enumerate() {
peers.tcp[i].flush(&peers.log);
peers.tcp[i].pull(&peers.log);
let from = peers.tcp[i].conn.client;
loop {
let before = peers.tcp[i].buf.len();
let Some(message) = P::parse(&mut peers.tcp[i].buf) else {
break;
};
let len = before.saturating_sub(peers.tcp[i].buf.len());
if peers.tcp[i].silent() {
peers.log.suppressed(from, Toward::Tester, len);
continue;
}
peers.log.received(Transport::Tcp, from, len);
deliver(&mut peers, state, conn, message, from, Origin::Tcp(i));
}
}
}
TesterTransport::Udp(ep) => {
while let Some((from, mut datagram)) = ep.try_recv() {
if peers.udp_silent(from) {
peers.log.suppressed(from, Toward::Tester, datagram.len());
continue;
}
let index = match peers.udp.iter().position(|&p| p == from) {
Some(index) => index,
None => {
peers.udp.push(from);
conns.push((from, new_conn(from)));
let conn = &mut conns.last_mut().expect("just pushed").1;
for handler in on_peer.iter_mut() {
let action = handler(state, conn, from);
peers.apply(action, Origin::Udp(from));
}
peers.udp.len() - 1
}
};
loop {
let before = datagram.len();
let Some(message) = P::parse(&mut datagram) else {
break;
};
let len = before.saturating_sub(datagram.len());
peers.log.received(Transport::Udp, from, len);
let conn = &mut conns[index].1;
deliver(&mut peers, state, conn, message, from, Origin::Udp(from));
}
}
}
}
let now = Instant::now();
let cyclic_until = deadline.map_or(now, |span| now.min(start + span));
for c in cyclic.iter_mut() {
if cyclic_until >= c.next {
match &mut c.action {
Tick::Once(action) => {
let action = action(state);
peers.apply(action, Origin::Everyone);
}
Tick::PerConn(action) => {
for (i, (from, conn)) in conns.iter_mut().enumerate() {
let origin = match peers.transport {
TesterTransport::Tcp(_) if peers.tcp[i].open() => {
Origin::Tcp(i)
}
TesterTransport::Tcp(_) => continue,
TesterTransport::Udp(_) => Origin::Udp(*from),
};
let action = action(state, conn, *from);
peers.apply(action, origin);
}
}
}
c.next += c.period;
}
}
let elapsed = start.elapsed();
let finished = finish.iter_mut().any(|f| match f {
Finish::State(f) => f(state, elapsed),
Finish::Conns(f) => f(state, conns),
});
if stop.load(Ordering::Acquire) || finished {
break;
}
if let Some(span) = deadline.filter(|span| elapsed >= *span) {
if cyclic.iter().any(|c| c.next <= start + span) {
continue;
}
break;
}
let now = Instant::now();
let wake_at = cyclic
.iter()
.map(|c| c.next)
.chain(deadline.map(|d| start + d))
.chain((*poll || as_fast_as_possible).then_some(now + UNTIL_POLL))
.min();
let tcp = &peers.tcp;
let pending = || {
stop.load(Ordering::Acquire)
|| match peers.transport {
TesterTransport::Tcp(listener) => {
listener.has_pending()
|| tcp
.iter()
.any(|p| (!p.gone && p.conn.peer_readable()) || p.flushable())
}
TesterTransport::Udp(ep) => ep.has_pending(),
}
};
#[cfg(unix)]
let deadline = wake_at.map(|at| Deadline::after(at.saturating_duration_since(now)));
#[cfg(windows)]
let deadline = wake_at.map(|at| {
wake_origin
.1
.later(at.saturating_duration_since(wake_origin.0))
});
let woke =
snare_interpose::real(|| readiness().wait_until("tester", deadline, pending));
if as_fast_as_possible
&& !woke
&& let Some(at) = wake_at
{
std::thread::sleep(at.saturating_duration_since(Instant::now()));
}
}
let Peers { tcp, log, .. } = &mut peers;
for peer in tcp {
peer.flush(log);
peer.close_now(log);
}
if let TesterTransport::Tcp(listener) = peers.transport {
peer::unlisten(regs, listener);
}
}
}
impl<P: Packet, S: Send + 'static, C: Send + 'static> Tester<P, S, C> {
fn run_until(&self, stop: &AtomicBool, start: Instant) {
let mut inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
inner.run(self.addr, self.recorder.as_ref(), stop, start);
}
fn has_finish(&self) -> bool {
let inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
inner.deadline.is_some() || !inner.finish.is_empty()
}
}
#[doc(hidden)]
pub trait RunTester: Sync {
fn has_finish(&self) -> bool;
fn run_until(&self, stop: &AtomicBool, start: Instant);
}
impl<P: Packet, S: Send + 'static, C: Send + 'static> RunTester for Tester<P, S, C> {
fn has_finish(&self) -> bool {
Tester::has_finish(self)
}
fn run_until(&self, stop: &AtomicBool, start: Instant) {
Tester::run_until(self, stop, start);
}
}
#[doc(hidden)]
pub fn run_testers(testers: &[&dyn RunTester]) {
let finishing = testers.iter().filter(|t| t.has_finish()).count();
assert!(
finishing > 0,
"run_testers!: no tester has a finish condition (until, until_state or until_after), so \
the run would never end"
);
let start = Instant::now();
let stop = AtomicBool::new(false);
let remaining = AtomicUsize::new(finishing);
let domain = snare_interpose::domain_key();
let halt = || {
stop.store(true, Ordering::Release);
snare_interpose::real(|| readiness().bump(domain));
};
let mut panic = None;
std::thread::scope(|scope| {
let handles: Vec<_> = testers
.iter()
.map(|tester| {
let (stop, remaining, halt) = (&stop, &remaining, &halt);
scope.spawn(move || {
let result = catch_unwind(AssertUnwindSafe(|| tester.run_until(stop, start)));
if result.is_err()
|| (tester.has_finish() && remaining.fetch_sub(1, Ordering::AcqRel) == 1)
{
halt();
}
result
})
})
.collect();
for handle in handles {
if let Err(payload) = handle.join().expect("tester thread") {
panic.get_or_insert(payload);
}
}
});
if let Some(payload) = panic {
resume_unwind(payload);
}
}
#[macro_export]
macro_rules! run_testers {
($($tester:expr),+ $(,)?) => {
$crate::__run_testers(&[$( &$tester as &dyn $crate::__RunTester ),+])
};
}