From 59bf336da0faa3c24dbd600a15ac49ba53c72214 Mon Sep 17 00:00:00 2001 From: Boquan Fang Date: Thu, 27 Feb 2025 23:50:00 +0000 Subject: [PATCH] build(deps): bach and rand updates --- quic/s2n-quic-platform/Cargo.toml | 4 +-- quic/s2n-quic-platform/src/io/testing.rs | 35 ++++++++++--------- .../s2n-quic-platform/src/io/testing/model.rs | 9 +++-- quic/s2n-quic-platform/src/io/testing/time.rs | 2 +- quic/s2n-quic-sim/Cargo.toml | 3 +- quic/s2n-quic-sim/src/run.rs | 2 +- quic/s2n-quic-sim/src/run/endpoint.rs | 3 +- quic/s2n-quic-sim/src/run/range.rs | 6 ++-- quic/s2n-quic/src/tests/issue_1427.rs | 4 +-- 9 files changed, 37 insertions(+), 31 deletions(-) diff --git a/quic/s2n-quic-platform/Cargo.toml b/quic/s2n-quic-platform/Cargo.toml index 5d219b03a9..699b167ae7 100644 --- a/quic/s2n-quic-platform/Cargo.toml +++ b/quic/s2n-quic-platform/Cargo.toml @@ -20,7 +20,7 @@ tokio-runtime = ["futures", "tokio"] xdp = ["s2n-quic-xdp"] [dependencies] -bach = { version = "0.0.6", optional = true } +bach = { version = "0.0.10", optional = true } bolero-generator = { version = "0.13", default-features = false, optional = true } cfg-if = "1" futures = { version = "0.3", default-features = false, features = ["async-await"], optional = true } @@ -36,7 +36,7 @@ turmoil = { version = "0.6.0", optional = true } libc = "0.2" [dev-dependencies] -bach = { version = "0.0.6" } +bach = { version = "0.0.10" } bolero = "0.13" bolero-generator = "0.13" futures = { version = "0.3", features = ["std"] } diff --git a/quic/s2n-quic-platform/src/io/testing.rs b/quic/s2n-quic-platform/src/io/testing.rs index ddce97af4f..74c0d691a1 100644 --- a/quic/s2n-quic-platform/src/io/testing.rs +++ b/quic/s2n-quic-platform/src/io/testing.rs @@ -1,7 +1,7 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -use bach::time::scheduler; +use bach::{environment::Macrostep, time::scheduler}; use core::task::Poll; use s2n_quic_core::{ endpoint::Endpoint, inet::SocketAddress, io::event_loop::EventLoop, path::mtu, @@ -42,7 +42,7 @@ pub mod rand { #[inline] fn gen_bool(&mut self) -> bool { - gen() + produce::().any() } #[inline] @@ -52,7 +52,7 @@ pub mod rand { #[inline] fn gen_range(&mut self, range: core::ops::Range) -> u64 { - gen_range(range) + range.any() } } } @@ -90,7 +90,7 @@ impl Executor { } pub fn enter O, O>(&mut self, f: F) -> O { - self.executor.environment().enter(f) + bach::environment::Environment::enter(self.executor.environment(), f) } pub fn run(&mut self) { @@ -100,7 +100,7 @@ impl Executor { pub fn close(&mut self) { // close the environment, which notifies all of the tasks that we're shutting down self.executor.environment().close(|| {}); - while self.executor.macrostep() > 0 {} + while self.executor.macrostep().tasks > 0 {} // then close the actual executor self.executor.close() @@ -123,10 +123,6 @@ struct Env { } impl Env { - fn enter O, O>(&self, f: F) -> O { - self.handle.enter(|| self.time.enter(|| self.rand.enter(f))) - } - fn close(&mut self, f: F) { let handle = &mut self.handle; let rand = &mut self.rand; @@ -144,11 +140,15 @@ impl Env { } } -impl bach::executor::Environment for Env { - fn run(&mut self, tasks: Tasks) -> Poll<()> +impl bach::environment::Environment for Env { + fn enter O, O>(&mut self, f: F) -> O { + self.handle.enter(|| self.time.enter(|| self.rand.enter(f))) + } + + fn run(&mut self, tasks: Tasks) -> Poll<()> where - Tasks: Iterator + Send, - F: 'static + FnOnce() -> Poll<()> + Send, + Tasks: IntoIterator, + R: bach::environment::Runnable, { let mut is_ready = true; @@ -165,7 +165,7 @@ impl bach::executor::Environment for Env { time.enter(|| { rand.enter(|| { for task in tasks { - is_ready &= task().is_ready(); + is_ready &= task.run().is_ready(); } network.execute(buffers); }) @@ -179,11 +179,11 @@ impl bach::executor::Environment for Env { } } - fn on_macrostep(&mut self, count: usize) { + fn on_macrostep(&mut self, macrostep: Macrostep) -> Macrostep { // only advance time after a stall - if count > 0 { + if macrostep.tasks > 0 { self.stalled_iterations = 0; - return; + return macrostep; } self.stalled_iterations += 1; @@ -206,6 +206,7 @@ impl bach::executor::Environment for Env { break; } } + macrostep } fn close(&mut self, close: F) diff --git a/quic/s2n-quic-platform/src/io/testing/model.rs b/quic/s2n-quic-platform/src/io/testing/model.rs index f86c3eb8b3..27ed076179 100644 --- a/quic/s2n-quic-platform/src/io/testing/model.rs +++ b/quic/s2n-quic-platform/src/io/testing/model.rs @@ -1,7 +1,10 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -use super::network::{Buffers, Network, Packet}; +use super::{ + network::{Buffers, Network, Packet}, + rand::Any, +}; use core::time::Duration; use s2n_quic_core::{havoc, path::MaxMtu}; use std::{ @@ -241,7 +244,7 @@ impl Network for Model { #[inline] fn gen_rate(rate: u64) -> bool { // ensure the rate isn't 0 before actually generating a random number - rate > 0 && super::rand::gen::() < rate + rate > 0 && super::rand::produce::().any() < rate } let mut transmit = |packet: Cow| { @@ -360,7 +363,7 @@ impl Network for Model { } fn gen_jitter(max_jitter: Duration) -> Duration { - let micros = super::rand::gen_range(0..max_jitter.as_micros() as u64); + let micros = Any::any(&(0..max_jitter.as_micros() as u64)); let micros = micros as f64; // even though we're generated micros, we round to the nearest millisecond // so packets can be grouped together diff --git a/quic/s2n-quic-platform/src/io/testing/time.rs b/quic/s2n-quic-platform/src/io/testing/time.rs index 9f257c06d0..31610e3152 100644 --- a/quic/s2n-quic-platform/src/io/testing/time.rs +++ b/quic/s2n-quic-platform/src/io/testing/time.rs @@ -11,7 +11,7 @@ use core::{ use s2n_quic_core::time::{clock, Timestamp}; pub fn now() -> Timestamp { - unsafe { Timestamp::from_duration(time::now()) } + unsafe { Timestamp::from_duration(time::Instant::now().elapsed_since_start()) } } pub fn delay(duration: Duration) -> Timer { diff --git a/quic/s2n-quic-sim/Cargo.toml b/quic/s2n-quic-sim/Cargo.toml index 03b41d4f0c..a1a11fd56e 100644 --- a/quic/s2n-quic-sim/Cargo.toml +++ b/quic/s2n-quic-sim/Cargo.toml @@ -13,11 +13,12 @@ publish = false [dependencies] anyhow = "1" bytes = "1" +bolero-generator = "0.13.0" humantime = "2" indicatif = { version = "0.17", features = ["rayon"] } once_cell = "1" prost = "0.13" -rand = "0.8" +rand = "0.9" rayon = "1" s2n-quic = { path = "../s2n-quic", features = ["unstable-provider-io-testing", "provider-event-tracing"] } s2n-quic-core = { path = "../s2n-quic-core", features = ["testing"] } diff --git a/quic/s2n-quic-sim/src/run.rs b/quic/s2n-quic-sim/src/run.rs index ab83f7268c..d089cba2d2 100644 --- a/quic/s2n-quic-sim/src/run.rs +++ b/quic/s2n-quic-sim/src/run.rs @@ -108,7 +108,7 @@ impl Run { .while_some() .for_each(|_| { use ::rand::prelude::*; - let seed = thread_rng().gen(); + let seed = rand::rng().random(); test(seed); }); } else { diff --git a/quic/s2n-quic-sim/src/run/endpoint.rs b/quic/s2n-quic-sim/src/run/endpoint.rs index c42f29b812..f3cedf699a 100644 --- a/quic/s2n-quic-sim/src/run/endpoint.rs +++ b/quic/s2n-quic-sim/src/run/endpoint.rs @@ -60,7 +60,8 @@ pub fn client( total_delay += delay.gen_duration(); // pick a random server to connect to - let server_addr = *rand::one_of(servers); + let server_addr_idx = rand::Any::any(&(0..servers.len())); + let server_addr = servers[server_addr_idx]; let delay = total_delay; let client = client.clone(); diff --git a/quic/s2n-quic-sim/src/run/range.rs b/quic/s2n-quic-sim/src/run/range.rs index 55b6db56a0..80e9962c03 100644 --- a/quic/s2n-quic-sim/src/run/range.rs +++ b/quic/s2n-quic-sim/src/run/range.rs @@ -47,14 +47,14 @@ impl fmt::Display for CliRange { impl CliRange where - T: Copy + PartialOrd + ::rand::distributions::uniform::SampleUniform, + T: Copy + PartialOrd + ::bolero_generator::bounded::BoundedValue, { pub fn gen(&self) -> T { if self.start == self.end { return self.start; } - rand::gen_range(self.start..self.end) + rand::Any::any(&(self.start..self.end)) } } @@ -67,7 +67,7 @@ impl CliRange { return Duration::from_nanos(start as _); } - let nanos = rand::gen_range(start..end); + let nanos = rand::Any::any(&(start..end)); Duration::from_nanos(nanos as _) } } diff --git a/quic/s2n-quic/src/tests/issue_1427.rs b/quic/s2n-quic/src/tests/issue_1427.rs index 45025d394e..51afe44cf7 100644 --- a/quic/s2n-quic/src/tests/issue_1427.rs +++ b/quic/s2n-quic/src/tests/issue_1427.rs @@ -1,7 +1,7 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 -use super::*; +use super::{rand::Any, *}; use tokio::io::{AsyncReadExt, AsyncWriteExt}; /// Ensures tokio `AsyncRead` implementation functions properly @@ -31,7 +31,7 @@ fn tokio_read_exact_test() { while read_len < LEN { let max_len = buf.len().min(LEN - read_len); // generate a random amount of bytes to read - let len = rand::gen_range(1..=max_len); + let len = Any::any(&(1..=max_len)); let buf = &mut buf[0..len]; recv.read_exact(buf).await.unwrap();