tests.rs 906 B

123456789101112131415161718192021222324252627282930313233
  1. use super::ThingBuf;
  2. use crate::loom::{self, thread};
  3. use std::sync::Arc;
  4. #[test]
  5. fn linearizable() {
  6. const THREADS: usize = 4;
  7. fn thread(i: usize, q: Arc<ThingBuf<usize>>) {
  8. while q
  9. .push_ref()
  10. .map(|mut r| r.with_mut(|val| *val = i))
  11. .is_err()
  12. {}
  13. if let Some(mut r) = q.pop_ref() {
  14. r.with_mut(|val| *val = 0);
  15. }
  16. }
  17. loom::model(|| {
  18. let q = Arc::new(ThingBuf::new(THREADS));
  19. let q1 = q.clone();
  20. let q2 = q.clone();
  21. let q3 = q.clone();
  22. let t1 = thread::spawn(move || thread(1, q1));
  23. let t2 = thread::spawn(move || thread(2, q2));
  24. let t3 = thread::spawn(move || thread(3, q3));
  25. thread(4, q);
  26. t1.join().expect("thread 1 panicked!");
  27. t2.join().expect("thread 2 panicked!");
  28. t3.join().expect("thread 3 panicked!");
  29. })
  30. }