salsa/tests/parallel/race.rs
Niko Matsakis b0171dbc11 remove fork_mut and adopt new strategy
Required simplifying the various tests.
2018-10-31 12:01:36 -04:00

34 lines
882 B
Rust

use crate::setup::{Input, ParDatabase, ParDatabaseImpl};
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() {
let db = ParDatabaseImpl::default();
db.query(Input).set('a', 100);
db.query(Input).set('b', 010);
db.query(Input).set('c', 001);
let thread1 = std::thread::spawn({
let db = db.fork();
move || {
let v = db.sum("abc");
v
}
});
let thread2 = std::thread::spawn(move || {
db.query(Input).set('a', 1000);
db.sum("a")
});
// If the 1st thread runs first, you get 111, otherwise you get
// 1011.
let value1 = thread1.join().unwrap();
assert!(value1 == 111 || value1 == 1011, "illegal result {}", value1);
assert_eq!(thread2.join().unwrap(), 1000);
}