Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add established_tcp_stream_count{,_on} #188

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,10 @@ impl Tcp {
rx
}

pub(crate) fn stream_count(&self) -> usize {
self.sockets.len()
}

pub(crate) fn accept(&mut self, addr: SocketAddr) -> Option<(Syn, SocketAddr)> {
self.binds[&addr.port()].deque.pop_front()
}
Expand Down
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,13 @@ pub fn partition(a: impl ToIpAddrs, b: impl ToIpAddrs) {
pub fn repair(a: impl ToIpAddrs, b: impl ToIpAddrs) {
World::current(|world| world.repair_many(a, b))
}

/// Return the number of established tcp streams on the current host.
pub fn established_tcp_stream_count() -> usize {
World::current(|world| world.est_tcp_streams())
}

/// Return the number of established tcp streams on the given host.
pub fn established_tcp_stream_count_on(addr: impl ToIpAddr) -> usize {
World::current(|world| world.est_tcp_streams_on(addr))
}
12 changes: 12 additions & 0 deletions src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,18 @@ impl World {
});
}

pub(crate) fn est_tcp_streams(&mut self) -> usize {
self.current_host().tcp.stream_count()
}

pub(crate) fn est_tcp_streams_on(&mut self, addr: impl ToIpAddr) -> usize {
self.hosts
.get(&self.dns.lookup(addr))
.unwrap()
.tcp
.stream_count()
}

/// Register a new host with the simulation.
pub(crate) fn register(
&mut self,
Expand Down
7 changes: 7 additions & 0 deletions tests/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,15 @@ fn hangup() -> Result {
sim.client("client", async move {
let s = TcpStream::connect(("server", PORT)).await?;

assert_eq!(1, turmoil::established_tcp_stream_count());
assert_eq!(1, turmoil::established_tcp_stream_count_on("server"));

drop(s);
assert_eq!(0, turmoil::established_tcp_stream_count());
assert_eq!(1, turmoil::established_tcp_stream_count_on("server")); // server sleeps in its loop

wait.notified().await;
assert_eq!(0, turmoil::established_tcp_stream_count_on("server"));

Ok(())
});
Expand Down