fixt/
rng.rs

1//! Seedable random number generator to be used in all fixturator randomness
2//!
3//! In tests, when an unpredictable value causes a test failure, it's important to
4//! be able to re-run the test with the same values. This module provides a RNG
5//! whose seed will be set automatically and printed to stdout before each test run.
6//! To use a previous seed, just set the FIXT_SEED environment variable to the value
7//! of a previous run'
8
9use parking_lot::Mutex;
10use rand::rngs::StdRng;
11use rand::RngCore;
12use rand::SeedableRng;
13use std::sync::Arc;
14
15lazy_static::lazy_static! {
16    /// The singleton global RNG for test randomness
17    static ref FIXT_RNG: FixtRng = {
18        let seed: u64 = match std::env::var("FIXT_SEED") {
19            Ok(seed_str) => {
20                seed_str.parse().expect("Expected integer for FIXT_SEED")
21            }
22            Err(std::env::VarError::NotPresent) => { rand::random() },
23            Err(std::env::VarError::NotUnicode(v)) => { panic!("Invalid FIXT_SEED value: {v:?}") },
24        };
25        println!("Fixturator seed: {seed}");
26        FixtRng(Arc::new(
27            Mutex::new(StdRng::seed_from_u64(seed))
28        ))
29    };
30}
31
32/// A seedable RNG which uses an Arc and a Mutex to allow easy cloneability and thread safety.
33/// A singleton global instance is created in this module. See module-level docs for more info.
34#[derive(Clone)]
35pub struct FixtRng(Arc<Mutex<StdRng>>);
36
37impl RngCore for FixtRng {
38    fn next_u32(&mut self) -> u32 {
39        self.0.lock().next_u32()
40    }
41
42    fn next_u64(&mut self) -> u64 {
43        self.0.lock().next_u64()
44    }
45
46    fn fill_bytes(&mut self, dest: &mut [u8]) {
47        self.0.lock().fill_bytes(dest)
48    }
49}
50
51/// Access the seeded random number generator. This should be used in all places where
52/// tests produce random values.
53pub fn rng() -> FixtRng {
54    FIXT_RNG.clone()
55}