test: Add a test with multiple threads using a process clock

pull/24376/head
Carol (Nichols || Goulding) 2021-05-12 13:30:12 -04:00
parent cecb4afc58
commit 8be95856ab
1 changed files with 41 additions and 1 deletions

View File

@ -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();
}
}
}