Skip to content

Commit

Permalink
test: Dump simulator RNG seeds
Browse files Browse the repository at this point in the history
Into the directory given in the `DUMP_SIMULATION_SEEDS` environment
variable. Also, export them as artifacts from the CI runs.

Fixes #1645
  • Loading branch information
larseggert committed Oct 10, 2024
1 parent 5f8d876 commit cbce1e5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
18 changes: 16 additions & 2 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ on:
env:
CARGO_TERM_COLOR: always
RUST_BACKTRACE: 1
DUMP_SIMULATION_SEEDS: /tmp/simulation-seeds

concurrency:
group: ${{ github.workflow }}-${{ github.ref_name }}
Expand Down Expand Up @@ -68,12 +69,14 @@ jobs:
cargo +${{ matrix.rust-toolchain }} check $BUILD_TYPE --all-targets --features ci
- name: Run tests and determine coverage
env:
RUST_LOG: trace
run: |
# shellcheck disable=SC2086
if [ "${{ matrix.rust-toolchain }}" == "stable" ]; then
RUST_LOG=trace cargo +${{ matrix.rust-toolchain }} llvm-cov nextest $BUILD_TYPE --features ci --no-fail-fast --lcov --output-path lcov.info
cargo +${{ matrix.rust-toolchain }} llvm-cov nextest $BUILD_TYPE --features ci --no-fail-fast --lcov --output-path lcov.info
else
RUST_LOG=trace cargo +${{ matrix.rust-toolchain }} nextest run $BUILD_TYPE --features ci --no-fail-fast
cargo +${{ matrix.rust-toolchain }} nextest run $BUILD_TYPE --features ci --no-fail-fast
fi
- name: Run client/server transfer
Expand Down Expand Up @@ -121,6 +124,17 @@ jobs:
RUSTFLAGS="-Z sanitizer=$sanitizer" RUSTDOCFLAGS="-Z sanitizer=$sanitizer" cargo +nightly nextest run -Z build-std --features ci --target "$TARGET"
done
- name: Print simulation seeds
if: env.DUMP_SIMULATION_SEEDS
run: ls -1 ${{ env.DUMP_SIMULATION_SEEDS }}

- name: Save simulation seeds artifact
uses: actions/upload-artifact@50769540e7f4bd5e21e526ee35c689e35e0d6874 # v4.4.0
with:
name: simulation-seeds
path: /tmp/simulation-seeds
compression-level: 9

bench:
needs: [check]
if: >
Expand Down
33 changes: 20 additions & 13 deletions test-fixture/src/sim/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ use std::{
cell::RefCell,
cmp::min,
fmt::Debug,
fs::write,
ops::{Deref, DerefMut},
rc::Rc,
time::{Duration, Instant},
};

use neqo_common::{qdebug, qinfo, qtrace, Datagram, Encoder};
use neqo_common::{qdebug, qerror, qinfo, qtrace, Datagram, Encoder};
use neqo_transport::Output;
use rng::Random;
use NodeState::{Active, Idle, Waiting};
Expand Down Expand Up @@ -64,11 +65,7 @@ macro_rules! simulate {
let f: Box<dyn FnOnce(&_) -> _> = Box::new($v);
nodes.push(Box::new(f(&fixture)));
)*
let mut sim = Simulator::new(stringify!($n), nodes);
if let Ok(seed) = std::env::var("SIMULATION_SEED") {
sim.seed_str(seed);
}
sim.run();
Simulator::new(stringify!($n), nodes).run();
}
};
}
Expand Down Expand Up @@ -151,23 +148,33 @@ impl Simulator {
.into_iter()
.chain(it.map(|node| NodeHolder { node, state: Idle }))
.collect::<Vec<_>>();
Self {
let mut sim = Self {
name,
nodes,
rng: Rc::default(),
};
// Seed from the `SIMULATION_SEED` environment variable, if set.
if let Ok(seed) = std::env::var("SIMULATION_SEED") {
sim.seed_str(seed);
}
}

pub fn seed(&mut self, seed: [u8; 32]) {
self.rng = Rc::new(RefCell::new(Random::new(&seed)));
// Dump the seed to a file to the directory in the `DUMP_SIMULATION_SEEDS` environment
// variable, if set.
if let Ok(dir) = std::env::var("DUMP_SIMULATION_SEEDS") {
let seed_str = sim.rng.borrow().seed_str();
let path = format!("{dir}/{seed_str}");
if write(&path, sim.rng.borrow().seed_str()).is_err() {
qerror!("Failed to write seed to {path}");
}
}
sim
}

/// Seed from a hex string.
/// # Panics
/// When the provided string is not 32 bytes of hex (64 characters).
pub fn seed_str(&mut self, seed: impl AsRef<str>) {
let seed = Encoder::from_hex(seed);
self.seed(<[u8; 32]>::try_from(seed.as_ref()).unwrap());
let seed = <[u8; 32]>::try_from(Encoder::from_hex(seed).as_ref()).unwrap();
self.rng = Rc::new(RefCell::new(Random::new(&seed)));
}

fn next_time(&self, now: Instant) -> Instant {
Expand Down

0 comments on commit cbce1e5

Please sign in to comment.