2019-01-12 10:11:59 +00:00
|
|
|
use crate::setup::{InputQuery, ParDatabase, ParDatabaseImpl};
|
2018-10-13 09:24:34 +00:00
|
|
|
use salsa::{Database, ParallelDatabase};
|
|
|
|
|
|
|
|
/// 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-12 10:11:59 +00:00
|
|
|
db.query_mut(InputQuery).set('a', 100);
|
|
|
|
db.query_mut(InputQuery).set('b', 010);
|
|
|
|
db.query_mut(InputQuery).set('c', 001);
|
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();
|
2018-10-31 16:01:36 +00:00
|
|
|
move || {
|
|
|
|
let v = db.sum("abc");
|
|
|
|
v
|
|
|
|
}
|
2018-10-13 09:24:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let thread2 = std::thread::spawn(move || {
|
2019-01-12 10:11:59 +00:00
|
|
|
db.query_mut(InputQuery).set('a', 1000);
|
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
|
|
|
|
// cancelation, you get back usize::max.
|
2018-10-13 09:24:34 +00:00
|
|
|
let value1 = thread1.join().unwrap();
|
2019-01-04 14:06:38 +00:00
|
|
|
assert!(
|
|
|
|
value1 == 111 || value1 == 1011 || value1 == std::usize::MAX,
|
|
|
|
"illegal result {}",
|
|
|
|
value1
|
|
|
|
);
|
2018-10-13 09:24:34 +00:00
|
|
|
|
|
|
|
assert_eq!(thread2.join().unwrap(), 1000);
|
|
|
|
}
|