From ddb0af95098cf04a307d5ea6e93d77a00643c5d7 Mon Sep 17 00:00:00 2001 From: Andrew Hlynskyi Date: Thu, 16 Mar 2023 15:08:56 +0200 Subject: [PATCH] test: use random SEED numbers This is needed to omit occurrences of the same seed in a sequence of following seeds due to the reason of that two initial seed are very close if based on unix epoch seconds. --- cli/src/tests/helpers/mod.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/cli/src/tests/helpers/mod.rs b/cli/src/tests/helpers/mod.rs index def0ea3e..54df8809 100644 --- a/cli/src/tests/helpers/mod.rs +++ b/cli/src/tests/helpers/mod.rs @@ -6,7 +6,8 @@ pub(super) mod random; pub(super) mod scope_sequence; use lazy_static::lazy_static; -use std::{env, time, usize}; +use rand::Rng; +use std::env; lazy_static! { pub static ref LOG_ENABLED: bool = env::var("TREE_SITTER_LOG").is_ok(); @@ -16,11 +17,7 @@ lazy_static! { } lazy_static! { - pub static ref START_SEED: usize = - int_env_var("TREE_SITTER_SEED").unwrap_or_else(|| time::SystemTime::now() - .duration_since(time::UNIX_EPOCH) - .unwrap() - .as_secs() as usize,); + pub static ref START_SEED: usize = new_seed(); pub static ref EDIT_COUNT: usize = int_env_var("TREE_SITTER_EDITS").unwrap_or(3); pub static ref ITERATION_COUNT: usize = int_env_var("TREE_SITTER_ITERATIONS").unwrap_or(10); } @@ -28,3 +25,10 @@ lazy_static! { fn int_env_var(name: &'static str) -> Option { env::var(name).ok().and_then(|e| e.parse().ok()) } + +pub(crate) fn new_seed() -> usize { + int_env_var("TREE_SITTER_SEED").unwrap_or_else(|| { + let mut rng = rand::thread_rng(); + rng.gen::() + }) +}