2021-05-17 16:59:28 +00:00
|
|
|
use std::panic::AssertUnwindSafe;
|
|
|
|
|
2019-01-27 21:06:14 +00:00
|
|
|
use crate::setup::{ParDatabase, ParDatabaseImpl};
|
2021-05-25 13:08:23 +00:00
|
|
|
use salsa::{Cancelled, ParallelDatabase};
|
2018-10-13 09:24:34 +00:00
|
|
|
|
|
|
|
/// Test where a read and a set are racing with one another.
|
|
|
|
/// Should be atomic.
|
|
|
|
#[test]
|
|
|
|
fn in_par_get_set_race() {
|
2018-11-01 08:30:54 +00:00
|
|
|
let mut db = ParDatabaseImpl::default();
|
2018-10-13 09:24:34 +00:00
|
|
|
|
2019-01-27 21:06:14 +00:00
|
|
|
db.set_input('a', 100);
|
2021-06-03 11:12:38 +00:00
|
|
|
db.set_input('b', 10);
|
|
|
|
db.set_input('c', 1);
|
2018-10-13 09:24:34 +00:00
|
|
|
|
2018-10-31 16:01:36 +00:00
|
|
|
let thread1 = std::thread::spawn({
|
2018-11-01 00:05:31 +00:00
|
|
|
let db = db.snapshot();
|
2021-06-03 11:12:38 +00:00
|
|
|
move || Cancelled::catch(AssertUnwindSafe(|| db.sum("abc")))
|
2018-10-13 09:24:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let thread2 = std::thread::spawn(move || {
|
2022-06-03 09:49:23 +00:00
|
|
|
let value = db.remove_input('a') * 10;
|
|
|
|
db.set_input('a', value);
|
2018-10-31 16:01:36 +00:00
|
|
|
db.sum("a")
|
2018-10-13 09:24:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// If the 1st thread runs first, you get 111, otherwise you get
|
2019-01-04 14:06:38 +00:00
|
|
|
// 1011; if they run concurrently and the 1st thread observes the
|
2021-05-25 13:08:23 +00:00
|
|
|
// cancellation, it'll unwind.
|
2021-05-17 16:59:28 +00:00
|
|
|
let result1 = thread1.join().unwrap();
|
|
|
|
if let Ok(value1) = result1 {
|
|
|
|
assert!(value1 == 111 || value1 == 1011, "illegal result {}", value1);
|
|
|
|
}
|
2018-10-13 09:24:34 +00:00
|
|
|
|
2021-05-25 13:08:23 +00:00
|
|
|
// thread2 can not observe a cancellation because it performs a
|
2021-05-17 16:59:28 +00:00
|
|
|
// database write before running any other queries.
|
2018-10-13 09:24:34 +00:00
|
|
|
assert_eq!(thread2.join().unwrap(), 1000);
|
|
|
|
}
|