From 8be95856abd4c5a1929ee711d2b5c152c1675fa5 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Wed, 12 May 2021 13:30:12 -0400 Subject: [PATCH] test: Add a test with multiple threads using a process clock --- server/src/db/process_clock.rs | 42 +++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/server/src/db/process_clock.rs b/server/src/db/process_clock.rs index df7fb0c521..3e173cfbb1 100644 --- a/server/src/db/process_clock.rs +++ b/server/src/db/process_clock.rs @@ -73,7 +73,7 @@ mod tests { use super::*; use crate::query_tests::utils::TestDb; use entry::test_helpers::lp_to_entry; - use std::sync::Arc; + use std::{sync::Arc, thread, time::Duration}; #[test] fn process_clock_defaults_to_current_time_in_ns() { @@ -172,4 +172,44 @@ mod tests { ClockValue::try_from(later + 2).unwrap() ); } + + #[test] + fn process_clock_multithreaded_access_always_increments() { + let pc = Arc::new(ProcessClock::new()); + + let handles: Vec<_> = (0..10) + .map(|thread_num| { + let pc = Arc::clone(&pc); + thread::spawn(move || { + let mut pc_val_before = pc.next(); + for iteration in 0..10 { + let pc_val_after = pc.next(); + + // This might be useful for debugging if this test fails + println!( + "thread {} in iteration {} testing {:?} < {:?}", + thread_num, iteration, pc_val_before, pc_val_after + ); + + // Process clock should always increase + assert!( + pc_val_before < pc_val_after, + "expected {:?} to be less than {:?}", + pc_val_before, + pc_val_after + ); + + pc_val_before = pc_val_after; + + // encourage yielding + thread::sleep(Duration::from_millis(1)); + } + }) + }) + .collect(); + + for h in handles { + h.join().unwrap(); + } + } }