From c5c4c732ced7a8d62d5b6f3a4d29074d7628c0d9 Mon Sep 17 00:00:00 2001 From: Simon Buttgereit Date: Tue, 26 Mar 2024 12:43:32 +0100 Subject: [PATCH 01/14] enable tokio io driver --- src/rt.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/rt.rs b/src/rt.rs index f87b4c3..1daf4dd 100644 --- a/src/rt.rs +++ b/src/rt.rs @@ -213,7 +213,12 @@ fn init() -> (Runtime, LocalSet) { #[cfg(tokio_unstable)] builder.unhandled_panic(tokio::runtime::UnhandledPanic::ShutdownRuntime); - let tokio = builder.enable_time().start_paused(true).build().unwrap(); + let tokio = builder + .enable_time() + .enable_io() + .start_paused(true) + .build() + .unwrap(); tokio.block_on(async { // Sleep to "round" `Instant::now()` to the closest `ms` From 36ae45e081f0ebd66561499cc62b9f62ac03d00c Mon Sep 17 00:00:00 2001 From: Simon Buttgereit Date: Tue, 26 Mar 2024 12:43:47 +0100 Subject: [PATCH 02/14] imports & format stuff --- src/builder.rs | 5 +---- src/sim.rs | 10 +++++----- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index c4ba8c8..342dd87 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -1,10 +1,7 @@ use crate::*; use rand::{RngCore, SeedableRng}; -use std::{ - ops::RangeInclusive, - time::{Duration, SystemTime}, -}; +use std::{ops::RangeInclusive, time::SystemTime}; /// A builder that can be used to configure the simulation. /// diff --git a/src/sim.rs b/src/sim.rs index 3178210..54666b9 100644 --- a/src/sim.rs +++ b/src/sim.rs @@ -414,9 +414,9 @@ mod test { }; use crate::{ - elapsed, hold, World, + elapsed, hold, net::{TcpListener, TcpStream}, - sim_elapsed, Builder, Result, + sim_elapsed, Builder, Result, World, }; #[test] @@ -561,16 +561,16 @@ mod test { } /// This is a regression test to ensure it is safe to call sim_elapsed - /// if current world of host is not set. + /// if current world of host is not set. #[test] fn sim_elapsed_time() -> Result { - // Safe to call outside of simution while there + // Safe to call outside of simution while there // is no current world set assert!(sim_elapsed().is_none()); let sim = Builder::new().build(); // Safe to call while there is no current host set - World::enter(&sim.world, || assert!(sim_elapsed().is_none())); + World::enter(&sim.world, || assert!(sim_elapsed().is_none())); Ok(()) } From 65953838a0849f0075b30acc88df1c88766062c3 Mon Sep 17 00:00:00 2001 From: Simon Buttgereit Date: Tue, 26 Mar 2024 16:08:40 +0100 Subject: [PATCH 03/14] add tokio_io feature --- Cargo.toml | 1 + src/rt.rs | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 4d02bb9..09abb03 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,3 +42,4 @@ tracing-subscriber = "0.3" [features] default = [] regex = ["dep:regex"] +tokio_io = [] diff --git a/src/rt.rs b/src/rt.rs index 1daf4dd..a23b9b8 100644 --- a/src/rt.rs +++ b/src/rt.rs @@ -213,6 +213,14 @@ fn init() -> (Runtime, LocalSet) { #[cfg(tokio_unstable)] builder.unhandled_panic(tokio::runtime::UnhandledPanic::ShutdownRuntime); + #[cfg(not(tokio_io))] + let tokio = builder + .enable_time() + .start_paused(true) + .build() + .unwrap(); + + #[cfg(tokio_io)] let tokio = builder .enable_time() .enable_io() From 485706a356767a52c5290f90a4f54b36d567e08b Mon Sep 17 00:00:00 2001 From: Simon Buttgereit Date: Thu, 28 Mar 2024 15:15:20 +0100 Subject: [PATCH 04/14] Revert last three commits --- Cargo.toml | 1 - src/builder.rs | 5 ++++- src/rt.rs | 15 +-------------- src/sim.rs | 10 +++++----- 4 files changed, 10 insertions(+), 21 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 09abb03..4d02bb9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,4 +42,3 @@ tracing-subscriber = "0.3" [features] default = [] regex = ["dep:regex"] -tokio_io = [] diff --git a/src/builder.rs b/src/builder.rs index 342dd87..c4ba8c8 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -1,7 +1,10 @@ use crate::*; use rand::{RngCore, SeedableRng}; -use std::{ops::RangeInclusive, time::SystemTime}; +use std::{ + ops::RangeInclusive, + time::{Duration, SystemTime}, +}; /// A builder that can be used to configure the simulation. /// diff --git a/src/rt.rs b/src/rt.rs index a23b9b8..f87b4c3 100644 --- a/src/rt.rs +++ b/src/rt.rs @@ -213,20 +213,7 @@ fn init() -> (Runtime, LocalSet) { #[cfg(tokio_unstable)] builder.unhandled_panic(tokio::runtime::UnhandledPanic::ShutdownRuntime); - #[cfg(not(tokio_io))] - let tokio = builder - .enable_time() - .start_paused(true) - .build() - .unwrap(); - - #[cfg(tokio_io)] - let tokio = builder - .enable_time() - .enable_io() - .start_paused(true) - .build() - .unwrap(); + let tokio = builder.enable_time().start_paused(true).build().unwrap(); tokio.block_on(async { // Sleep to "round" `Instant::now()` to the closest `ms` diff --git a/src/sim.rs b/src/sim.rs index 54666b9..3178210 100644 --- a/src/sim.rs +++ b/src/sim.rs @@ -414,9 +414,9 @@ mod test { }; use crate::{ - elapsed, hold, + elapsed, hold, World, net::{TcpListener, TcpStream}, - sim_elapsed, Builder, Result, World, + sim_elapsed, Builder, Result, }; #[test] @@ -561,16 +561,16 @@ mod test { } /// This is a regression test to ensure it is safe to call sim_elapsed - /// if current world of host is not set. + /// if current world of host is not set. #[test] fn sim_elapsed_time() -> Result { - // Safe to call outside of simution while there + // Safe to call outside of simution while there // is no current world set assert!(sim_elapsed().is_none()); let sim = Builder::new().build(); // Safe to call while there is no current host set - World::enter(&sim.world, || assert!(sim_elapsed().is_none())); + World::enter(&sim.world, || assert!(sim_elapsed().is_none())); Ok(()) } From 95d55ad476c83b8fd1bf6e1c882584f81de4495a Mon Sep 17 00:00:00 2001 From: Simon Buttgereit Date: Thu, 28 Mar 2024 15:06:45 +0100 Subject: [PATCH 05/14] extend builder to enable & disable tokio io --- src/builder.rs | 18 +++++++++++++----- src/config.rs | 4 ++++ src/rt.rs | 44 +++++++++++++++++++++++++++++++------------- src/sim.rs | 4 ++-- 4 files changed, 50 insertions(+), 20 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index c4ba8c8..57f88aa 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -1,10 +1,8 @@ -use crate::*; +use std::{ops::RangeInclusive, time::SystemTime}; use rand::{RngCore, SeedableRng}; -use std::{ - ops::RangeInclusive, - time::{Duration, SystemTime}, -}; + +use crate::*; /// A builder that can be used to configure the simulation. /// @@ -166,6 +164,16 @@ impl Builder { self } + pub fn enable_tokio_io(&mut self) -> &mut Self { + self.config.enable_tokio_io = true; + self + } + + pub fn disable_tokio_io(&mut self) -> &mut Self { + self.config.enable_tokio_io = false; + self + } + /// Build a simulation with the settings from the builder. /// /// This will use default rng with entropy from the device running. diff --git a/src/config.rs b/src/config.rs index 290a7ac..5d1a0ee 100644 --- a/src/config.rs +++ b/src/config.rs @@ -36,6 +36,9 @@ pub(crate) struct Config { /// Max size of the udp receive buffer pub(crate) udp_capacity: usize, + + /// Enables tokio IO driver + pub(crate) enable_tokio_io: bool, } /// Configures link behavior. @@ -87,6 +90,7 @@ impl Default for Config { ephemeral_ports: 49152..=65535, tcp_capacity: 64, udp_capacity: 64, + enable_tokio_io: false, } } } diff --git a/src/rt.rs b/src/rt.rs index f87b4c3..30c1d3c 100644 --- a/src/rt.rs +++ b/src/rt.rs @@ -1,13 +1,16 @@ use std::mem; +use std::pin::Pin; use std::sync::Arc; -use super::Result; use futures::Future; -use std::pin::Pin; use tokio::runtime::Runtime; use tokio::task::JoinHandle; use tokio::task::LocalSet; -use tokio::time::{sleep, Duration, Instant}; +use tokio::time::{Duration, Instant, sleep}; + +use crate::config::Config; + +use super::Result; // To support re-creation, we need to store a factory of the future that // represents the software. This is somewhat annoying in that it requires @@ -48,14 +51,17 @@ pub(crate) struct Rt<'a> { /// Optional handle to a host's software. When software finishes, the handle is /// consumed to check for error, which is propagated up to fail the simulation. handle: Option>, + + /// Configuration of simulation + sim_cfg: Config, } impl<'a> Rt<'a> { - pub(crate) fn client(nodename: Arc, client: F) -> Self + pub(crate) fn client(nodename: Arc, client: F, sim_cfg: Config) -> Self where F: Future + 'static, { - let (tokio, local) = init(); + let (tokio, local) = init(&sim_cfg); let handle = with(&tokio, &local, || tokio::task::spawn_local(client)); @@ -65,15 +71,16 @@ impl<'a> Rt<'a> { local, nodename, handle: Some(handle), + sim_cfg, } } - pub(crate) fn host(nodename: Arc, software: F) -> Self + pub(crate) fn host(nodename: Arc, software: F, sim_cfg: Config) -> Self where F: Fn() -> Fut + 'a, Fut: Future + 'static, { - let (tokio, local) = init(); + let (tokio, local) = init(&sim_cfg); let software: Software = Box::new(move || Box::pin(software())); let handle = with(&tokio, &local, || tokio::task::spawn_local(software())); @@ -84,11 +91,13 @@ impl<'a> Rt<'a> { local, nodename, handle: Some(handle), + sim_cfg, } } pub(crate) fn no_software() -> Self { - let (tokio, local) = init(); + let sim_cfg = Config::default(); + let (tokio, local) = init(&sim_cfg); Self { kind: Kind::NoSoftware, @@ -96,6 +105,7 @@ impl<'a> Rt<'a> { local, nodename: String::new().into(), handle: None, + sim_cfg, } } @@ -200,20 +210,28 @@ impl<'a> Rt<'a> { /// /// Both the [`Runtime`] and [`LocalSet`] are replaced with new instances. fn cancel_tasks(&mut self) { - let (tokio, local) = init(); + let (tokio, local) = init(&self.sim_cfg); _ = mem::replace(&mut self.tokio, tokio); drop(mem::replace(&mut self.local, local)); } } -fn init() -> (Runtime, LocalSet) { - let mut builder = tokio::runtime::Builder::new_current_thread(); +fn init(sim_cfg: &Config) -> (Runtime, LocalSet) { + let mut tokio_builder = tokio::runtime::Builder::new_current_thread(); #[cfg(tokio_unstable)] - builder.unhandled_panic(tokio::runtime::UnhandledPanic::ShutdownRuntime); + tokio_builder.unhandled_panic(tokio::runtime::UnhandledPanic::ShutdownRuntime); + + if sim_cfg.enable_tokio_io { + tokio_builder.enable_io(); + } - let tokio = builder.enable_time().start_paused(true).build().unwrap(); + let tokio = tokio_builder + .enable_time() + .start_paused(true) + .build() + .unwrap(); tokio.block_on(async { // Sleep to "round" `Instant::now()` to the closest `ms` diff --git a/src/sim.rs b/src/sim.rs index 3178210..5c2a121 100644 --- a/src/sim.rs +++ b/src/sim.rs @@ -87,7 +87,7 @@ impl<'a> Sim<'a> { world.register(addr, &nodename, HostTimer::new(self.elapsed), &self.config); } - let rt = World::enter(&self.world, || Rt::client(nodename, client)); + let rt = World::enter(&self.world, || Rt::client(nodename, client, self.config.clone())); self.rts.insert(addr, rt); } @@ -120,7 +120,7 @@ impl<'a> Sim<'a> { world.register(addr, &nodename, HostTimer::new(self.elapsed), &self.config); } - let rt = World::enter(&self.world, || Rt::host(nodename, host)); + let rt = World::enter(&self.world, || Rt::host(nodename, host, self.config.clone())); self.rts.insert(addr, rt); } From 3f97b64ca3ac479ede975030557f9b4d0211c81e Mon Sep 17 00:00:00 2001 From: Simon Buttgereit Date: Thu, 28 Mar 2024 15:18:18 +0100 Subject: [PATCH 06/14] some format stuff --- src/rt.rs | 2 +- src/sim.rs | 18 +++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/rt.rs b/src/rt.rs index 30c1d3c..1d32e4b 100644 --- a/src/rt.rs +++ b/src/rt.rs @@ -6,7 +6,7 @@ use futures::Future; use tokio::runtime::Runtime; use tokio::task::JoinHandle; use tokio::task::LocalSet; -use tokio::time::{Duration, Instant, sleep}; +use tokio::time::{sleep, Duration, Instant}; use crate::config::Config; diff --git a/src/sim.rs b/src/sim.rs index 5c2a121..5a5963d 100644 --- a/src/sim.rs +++ b/src/sim.rs @@ -87,7 +87,9 @@ impl<'a> Sim<'a> { world.register(addr, &nodename, HostTimer::new(self.elapsed), &self.config); } - let rt = World::enter(&self.world, || Rt::client(nodename, client, self.config.clone())); + let rt = World::enter(&self.world, || { + Rt::client(nodename, client, self.config.clone()) + }); self.rts.insert(addr, rt); } @@ -120,7 +122,9 @@ impl<'a> Sim<'a> { world.register(addr, &nodename, HostTimer::new(self.elapsed), &self.config); } - let rt = World::enter(&self.world, || Rt::host(nodename, host, self.config.clone())); + let rt = World::enter(&self.world, || { + Rt::host(nodename, host, self.config.clone()) + }); self.rts.insert(addr, rt); } @@ -414,9 +418,9 @@ mod test { }; use crate::{ - elapsed, hold, World, + elapsed, hold, net::{TcpListener, TcpStream}, - sim_elapsed, Builder, Result, + sim_elapsed, Builder, Result, World, }; #[test] @@ -561,16 +565,16 @@ mod test { } /// This is a regression test to ensure it is safe to call sim_elapsed - /// if current world of host is not set. + /// if current world of host is not set. #[test] fn sim_elapsed_time() -> Result { - // Safe to call outside of simution while there + // Safe to call outside of simution while there // is no current world set assert!(sim_elapsed().is_none()); let sim = Builder::new().build(); // Safe to call while there is no current host set - World::enter(&sim.world, || assert!(sim_elapsed().is_none())); + World::enter(&sim.world, || assert!(sim_elapsed().is_none())); Ok(()) } From 8ea6d85d2c42ba0c1cc49a3c619fe83f5127a08e Mon Sep 17 00:00:00 2001 From: Simon Buttgereit Date: Tue, 2 Apr 2024 12:09:37 +0200 Subject: [PATCH 07/14] add tests --- tests/tokio_io.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tests/tokio_io.rs diff --git a/tests/tokio_io.rs b/tests/tokio_io.rs new file mode 100644 index 0000000..5adf187 --- /dev/null +++ b/tests/tokio_io.rs @@ -0,0 +1,39 @@ +use tokio::fs::remove_file; +use tokio::net::UnixListener; +use tokio_test::assert_err; + +use turmoil::Builder; + +#[test] +fn test_tokio_with_io_enabled() -> turmoil::Result { + let mut sim = Builder::new().enable_tokio_io().build(); + // client, which would panic if tokio io wouldn't be enabled + sim.client("client", async move { + let path = "/tmp/test_socket1"; + // bind unix domain socket -> needs tokio io + let _ = UnixListener::bind(path).unwrap(); + // remove socket file + let _ = remove_file(path).await?; + Ok(()) + }); + + sim.run() +} +#[test] +fn test_tokio_with_io_disabled() -> () { + let mut sim = Builder::new().build(); + // client, which would panics (if not catched) since tokio is not + sim.client("client", async move { + let path = "/tmp/test_socket2"; + let result = std::panic::catch_unwind(|| { + let _ = UnixListener::bind(path); + }); + assert_err!(result); + + // remove socket file + tokio::fs::remove_file(path).await?; + Ok(()) + }); + + let _ = sim.run(); +} From 928d7be2efa32da5fb13428b190880b6980fa94e Mon Sep 17 00:00:00 2001 From: Simon Buttgereit Date: Tue, 2 Apr 2024 12:10:04 +0200 Subject: [PATCH 08/14] remove disable_tokio_io fn --- src/builder.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 57f88aa..1061ed8 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -169,11 +169,6 @@ impl Builder { self } - pub fn disable_tokio_io(&mut self) -> &mut Self { - self.config.enable_tokio_io = false; - self - } - /// Build a simulation with the settings from the builder. /// /// This will use default rng with entropy from the device running. From f791b4835479f255530991f91b66b7dda612232e Mon Sep 17 00:00:00 2001 From: Simon Buttgereit Date: Tue, 2 Apr 2024 12:12:50 +0200 Subject: [PATCH 09/14] test documentation --- tests/tokio_io.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/tokio_io.rs b/tests/tokio_io.rs index 5adf187..0002596 100644 --- a/tests/tokio_io.rs +++ b/tests/tokio_io.rs @@ -4,6 +4,7 @@ use tokio_test::assert_err; use turmoil::Builder; +/// test assumes IO operation (binding unix domain socket) will succeed #[test] fn test_tokio_with_io_enabled() -> turmoil::Result { let mut sim = Builder::new().enable_tokio_io().build(); @@ -19,10 +20,12 @@ fn test_tokio_with_io_enabled() -> turmoil::Result { sim.run() } + +/// test assumes IO operation (binding unix domain socket) will fail #[test] fn test_tokio_with_io_disabled() -> () { let mut sim = Builder::new().build(); - // client, which would panics (if not catched) since tokio is not + // client, which would panic (if not catched) since tokio is not sim.client("client", async move { let path = "/tmp/test_socket2"; let result = std::panic::catch_unwind(|| { From d0e58f3d3491ec2a9e1f4a301d1309a82a6b5221 Mon Sep 17 00:00:00 2001 From: Simon Buttgereit Date: Tue, 2 Apr 2024 12:14:34 +0200 Subject: [PATCH 10/14] some typos --- tests/tokio_io.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/tokio_io.rs b/tests/tokio_io.rs index 0002596..0dd0583 100644 --- a/tests/tokio_io.rs +++ b/tests/tokio_io.rs @@ -25,7 +25,7 @@ fn test_tokio_with_io_enabled() -> turmoil::Result { #[test] fn test_tokio_with_io_disabled() -> () { let mut sim = Builder::new().build(); - // client, which would panic (if not catched) since tokio is not + // client panics (panic is caught) since tokio IO is not enabled sim.client("client", async move { let path = "/tmp/test_socket2"; let result = std::panic::catch_unwind(|| { From 50890629b387df7ea92c88a21d20ac4b6963df3f Mon Sep 17 00:00:00 2001 From: Simon Buttgereit Date: Wed, 3 Apr 2024 08:26:27 +0200 Subject: [PATCH 11/14] config -> bool --- src/rt.rs | 29 +++++++++++++---------------- src/sim.rs | 23 ++++++++++++----------- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/rt.rs b/src/rt.rs index 1d32e4b..cdfd385 100644 --- a/src/rt.rs +++ b/src/rt.rs @@ -6,9 +6,7 @@ use futures::Future; use tokio::runtime::Runtime; use tokio::task::JoinHandle; use tokio::task::LocalSet; -use tokio::time::{sleep, Duration, Instant}; - -use crate::config::Config; +use tokio::time::{Duration, Instant, sleep}; use super::Result; @@ -53,15 +51,15 @@ pub(crate) struct Rt<'a> { handle: Option>, /// Configuration of simulation - sim_cfg: Config, + enable_io: bool, } impl<'a> Rt<'a> { - pub(crate) fn client(nodename: Arc, client: F, sim_cfg: Config) -> Self + pub(crate) fn client(nodename: Arc, client: F, enable_io: bool) -> Self where F: Future + 'static, { - let (tokio, local) = init(&sim_cfg); + let (tokio, local) = init(enable_io); let handle = with(&tokio, &local, || tokio::task::spawn_local(client)); @@ -71,16 +69,16 @@ impl<'a> Rt<'a> { local, nodename, handle: Some(handle), - sim_cfg, + enable_io, } } - pub(crate) fn host(nodename: Arc, software: F, sim_cfg: Config) -> Self + pub(crate) fn host(nodename: Arc, software: F, enable_io: bool) -> Self where F: Fn() -> Fut + 'a, Fut: Future + 'static, { - let (tokio, local) = init(&sim_cfg); + let (tokio, local) = init(enable_io); let software: Software = Box::new(move || Box::pin(software())); let handle = with(&tokio, &local, || tokio::task::spawn_local(software())); @@ -91,13 +89,12 @@ impl<'a> Rt<'a> { local, nodename, handle: Some(handle), - sim_cfg, + enable_io, } } pub(crate) fn no_software() -> Self { - let sim_cfg = Config::default(); - let (tokio, local) = init(&sim_cfg); + let (tokio, local) = init(false); Self { kind: Kind::NoSoftware, @@ -105,7 +102,7 @@ impl<'a> Rt<'a> { local, nodename: String::new().into(), handle: None, - sim_cfg, + enable_io: false, } } @@ -210,20 +207,20 @@ impl<'a> Rt<'a> { /// /// Both the [`Runtime`] and [`LocalSet`] are replaced with new instances. fn cancel_tasks(&mut self) { - let (tokio, local) = init(&self.sim_cfg); + let (tokio, local) = init(self.enable_io); _ = mem::replace(&mut self.tokio, tokio); drop(mem::replace(&mut self.local, local)); } } -fn init(sim_cfg: &Config) -> (Runtime, LocalSet) { +fn init(enable_io: bool) -> (Runtime, LocalSet) { let mut tokio_builder = tokio::runtime::Builder::new_current_thread(); #[cfg(tokio_unstable)] tokio_builder.unhandled_panic(tokio::runtime::UnhandledPanic::ShutdownRuntime); - if sim_cfg.enable_tokio_io { + if enable_io { tokio_builder.enable_io(); } diff --git a/src/sim.rs b/src/sim.rs index 5a5963d..9fd5441 100644 --- a/src/sim.rs +++ b/src/sim.rs @@ -1,16 +1,17 @@ -use crate::host::HostTimer; -use crate::{for_pairs, Config, LinksIter, Result, Rt, ToIpAddr, ToIpAddrs, World, TRACING_TARGET}; - -use indexmap::IndexMap; use std::cell::RefCell; use std::future::Future; use std::net::IpAddr; use std::ops::DerefMut; use std::sync::Arc; use std::time::UNIX_EPOCH; + +use indexmap::IndexMap; use tokio::time::Duration; use tracing::Level; +use crate::{Config, for_pairs, LinksIter, Result, Rt, ToIpAddr, ToIpAddrs, TRACING_TARGET, World}; +use crate::host::HostTimer; + /// A handle for interacting with the simulation. pub struct Sim<'a> { /// Simulation configuration @@ -88,7 +89,7 @@ impl<'a> Sim<'a> { } let rt = World::enter(&self.world, || { - Rt::client(nodename, client, self.config.clone()) + Rt::client(nodename, client, self.config.enable_tokio_io) }); self.rts.insert(addr, rt); @@ -123,7 +124,7 @@ impl<'a> Sim<'a> { } let rt = World::enter(&self.world, || { - Rt::host(nodename, host, self.config.clone()) + Rt::host(nodename, host, self.config.enable_tokio_io) }); self.rts.insert(addr, rt); @@ -404,13 +405,13 @@ mod test { net::{IpAddr, Ipv4Addr}, rc::Rc, sync::{ - atomic::{AtomicU64, Ordering}, Arc, + atomic::{AtomicU64, Ordering}, }, time::Duration, }; - use std::future; + use tokio::{ io::{AsyncReadExt, AsyncWriteExt}, sync::Semaphore, @@ -418,9 +419,9 @@ mod test { }; use crate::{ - elapsed, hold, - net::{TcpListener, TcpStream}, - sim_elapsed, Builder, Result, World, + Builder, elapsed, + hold, + net::{TcpListener, TcpStream}, Result, sim_elapsed, World, }; #[test] From e98e26c1ec2cd3267f6bf5a56c867d9341862c81 Mon Sep 17 00:00:00 2001 From: Simon Buttgereit Date: Wed, 3 Apr 2024 08:26:40 +0200 Subject: [PATCH 12/14] fix clippy for testing target --- tests/tokio_io.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/tokio_io.rs b/tests/tokio_io.rs index 0dd0583..aba41be 100644 --- a/tests/tokio_io.rs +++ b/tests/tokio_io.rs @@ -14,7 +14,7 @@ fn test_tokio_with_io_enabled() -> turmoil::Result { // bind unix domain socket -> needs tokio io let _ = UnixListener::bind(path).unwrap(); // remove socket file - let _ = remove_file(path).await?; + remove_file(path).await?; Ok(()) }); @@ -23,7 +23,7 @@ fn test_tokio_with_io_enabled() -> turmoil::Result { /// test assumes IO operation (binding unix domain socket) will fail #[test] -fn test_tokio_with_io_disabled() -> () { +fn test_tokio_with_io_disabled() -> turmoil::Result { let mut sim = Builder::new().build(); // client panics (panic is caught) since tokio IO is not enabled sim.client("client", async move { @@ -38,5 +38,5 @@ fn test_tokio_with_io_disabled() -> () { Ok(()) }); - let _ = sim.run(); + sim.run() } From 5fb7b1432f4c616233f0d5fcf78c4834ad46f31d Mon Sep 17 00:00:00 2001 From: Brett McChesney <39924297+mcches@users.noreply.github.com> Date: Wed, 3 Apr 2024 08:58:08 -0600 Subject: [PATCH 13/14] Update src/rt.rs --- src/rt.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rt.rs b/src/rt.rs index cdfd385..34fa028 100644 --- a/src/rt.rs +++ b/src/rt.rs @@ -50,7 +50,7 @@ pub(crate) struct Rt<'a> { /// consumed to check for error, which is propagated up to fail the simulation. handle: Option>, - /// Configuration of simulation + /// Whether io is enabled on this runtime. enable_io: bool, } From 0b305f79a736deaa1c6d305f77612b289d1f8120 Mon Sep 17 00:00:00 2001 From: Brett McChesney <39924297+mcches@users.noreply.github.com> Date: Wed, 3 Apr 2024 08:58:14 -0600 Subject: [PATCH 14/14] Update src/builder.rs --- src/builder.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/builder.rs b/src/builder.rs index 1061ed8..03a0a32 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -164,6 +164,7 @@ impl Builder { self } + /// Enables the tokio I/O driver. pub fn enable_tokio_io(&mut self) -> &mut Self { self.config.enable_tokio_io = true; self