Skip to content

Commit

Permalink
build(deps): bach and rand updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Boquan Fang committed Feb 28, 2025
1 parent 3094d96 commit 59bf336
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 31 deletions.
4 changes: 2 additions & 2 deletions quic/s2n-quic-platform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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"] }
Expand Down
35 changes: 18 additions & 17 deletions quic/s2n-quic-platform/src/io/testing.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -42,7 +42,7 @@ pub mod rand {

#[inline]
fn gen_bool(&mut self) -> bool {
gen()
produce::<bool>().any()
}

#[inline]
Expand All @@ -52,7 +52,7 @@ pub mod rand {

#[inline]
fn gen_range(&mut self, range: core::ops::Range<u64>) -> u64 {
gen_range(range)
range.any()
}
}
}
Expand Down Expand Up @@ -90,7 +90,7 @@ impl<N: Network> Executor<N> {
}

pub fn enter<F: FnOnce() -> 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) {
Expand All @@ -100,7 +100,7 @@ impl<N: Network> Executor<N> {
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()
Expand All @@ -123,10 +123,6 @@ struct Env<N> {
}

impl<N> Env<N> {
fn enter<F: FnOnce() -> O, O>(&self, f: F) -> O {
self.handle.enter(|| self.time.enter(|| self.rand.enter(f)))
}

fn close<F: FnOnce()>(&mut self, f: F) {
let handle = &mut self.handle;
let rand = &mut self.rand;
Expand All @@ -144,11 +140,15 @@ impl<N> Env<N> {
}
}

impl<N: Network> bach::executor::Environment for Env<N> {
fn run<Tasks, F>(&mut self, tasks: Tasks) -> Poll<()>
impl<N: Network> bach::environment::Environment for Env<N> {
fn enter<F: FnOnce() -> O, O>(&mut self, f: F) -> O {
self.handle.enter(|| self.time.enter(|| self.rand.enter(f)))
}

fn run<Tasks, R>(&mut self, tasks: Tasks) -> Poll<()>
where
Tasks: Iterator<Item = F> + Send,
F: 'static + FnOnce() -> Poll<()> + Send,
Tasks: IntoIterator<Item = R>,
R: bach::environment::Runnable,
{
let mut is_ready = true;

Expand All @@ -165,7 +165,7 @@ impl<N: Network> bach::executor::Environment for Env<N> {
time.enter(|| {
rand.enter(|| {
for task in tasks {
is_ready &= task().is_ready();
is_ready &= task.run().is_ready();
}
network.execute(buffers);
})
Expand All @@ -179,11 +179,11 @@ impl<N: Network> bach::executor::Environment for Env<N> {
}
}

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;
Expand All @@ -206,6 +206,7 @@ impl<N: Network> bach::executor::Environment for Env<N> {
break;
}
}
macrostep
}

fn close<F>(&mut self, close: F)
Expand Down
9 changes: 6 additions & 3 deletions quic/s2n-quic-platform/src/io/testing/model.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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::<u64>() < rate
rate > 0 && super::rand::produce::<u64>().any() < rate
}

let mut transmit = |packet: Cow<Packet>| {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion quic/s2n-quic-platform/src/io/testing/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion quic/s2n-quic-sim/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
2 changes: 1 addition & 1 deletion quic/s2n-quic-sim/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion quic/s2n-quic-sim/src/run/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions quic/s2n-quic-sim/src/run/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ impl<T: PartialEq + fmt::Display> fmt::Display for CliRange<T> {

impl<T> CliRange<T>
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))
}
}

Expand All @@ -67,7 +67,7 @@ impl CliRange<humantime::Duration> {
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 _)
}
}
Expand Down
4 changes: 2 additions & 2 deletions quic/s2n-quic/src/tests/issue_1427.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 59bf336

Please sign in to comment.