From 77db8d41544921569f42d967f6d452b1a1a3b712 Mon Sep 17 00:00:00 2001 From: dd di cesare Date: Mon, 31 Jul 2023 16:39:08 +0200 Subject: [PATCH] [refactor] Simplifying and fixing multi-threading test * Using thread.scope in order to get rid of Arc and Barrier * Fixing assertion to include possible accepted values --- .../src/storage/atomic_expiring_value.rs | 35 ++++++------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/limitador/src/storage/atomic_expiring_value.rs b/limitador/src/storage/atomic_expiring_value.rs index e7b5c976..570fe5c2 100644 --- a/limitador/src/storage/atomic_expiring_value.rs +++ b/limitador/src/storage/atomic_expiring_value.rs @@ -85,7 +85,6 @@ impl Clone for AtomicExpiringValue { #[cfg(test)] mod tests { use super::*; - use std::sync::{Arc, Barrier}; use std::thread; use std::time::{Duration, SystemTime}; @@ -130,30 +129,16 @@ mod tests { #[test] fn test_overlapping_updates() { let now = SystemTime::now(); - let atomic_expiring_value = - Arc::new(AtomicExpiringValue::new(42, now + Duration::from_secs(10))); - - let barrier = Arc::new(Barrier::new(2)); - - let atomic_expiring_value_clone = Arc::clone(&atomic_expiring_value); - let barrier_clone = Arc::clone(&barrier); - let thread1 = thread::spawn(move || { - barrier_clone.wait(); - atomic_expiring_value_clone.update(2, 1, now + Duration::from_secs(11)); - }); - - let atomic_expiring_value_clone2 = Arc::clone(&atomic_expiring_value); - let barrier_clone2 = Arc::clone(&barrier); - let thread2 = thread::spawn(move || { - barrier_clone2.wait(); - atomic_expiring_value_clone2.update(1, 1, now); + let atomic_expiring_value = AtomicExpiringValue::new(42, now + Duration::from_secs(10)); + + thread::scope(|s| { + s.spawn(|| { + atomic_expiring_value.update(1, 1, now); + }); + s.spawn(|| { + atomic_expiring_value.update(2, 1, now + Duration::from_secs(11)); + }); }); - - assert_eq!(atomic_expiring_value.value.load(Ordering::SeqCst), 42); - - thread2.join().unwrap(); - thread1.join().unwrap(); - - assert_eq!(atomic_expiring_value.value.load(Ordering::SeqCst), 3); + assert!([2i64, 3i64].contains(&atomic_expiring_value.value.load(Ordering::SeqCst))); } }