salsa/tests/parallel/parallel_cycle_none_recover.rs

84 lines
2.1 KiB
Rust
Raw Normal View History

2022-08-09 10:33:46 +00:00
//! Test a cycle where no queries recover that occurs across threads.
//! See the `../cycles.rs` for a complete listing of cycle tests,
//! both intra and cross thread.
use crate::setup::Database;
2022-08-09 10:06:39 +00:00
use crate::setup::Knobs;
use expect_test::expect;
use salsa::ParallelDatabase;
2022-08-09 10:33:46 +00:00
pub(crate) trait Db: salsa::DbWithJar<Jar> + Knobs {}
impl<T: salsa::DbWithJar<Jar> + Knobs> Db for T {}
#[salsa::jar(db = Db)]
pub(crate) struct Jar(MyInput, a, b);
2022-08-09 10:06:39 +00:00
#[salsa::input(jar = Jar)]
pub(crate) struct MyInput {
2022-08-09 10:33:46 +00:00
field: i32,
2022-08-09 10:06:39 +00:00
}
#[salsa::tracked(jar = Jar)]
pub(crate) fn a(db: &dyn Db, input: MyInput) -> i32 {
2022-08-09 10:33:46 +00:00
// Wait to create the cycle until both threads have entered
2022-08-09 10:06:39 +00:00
db.signal(1);
db.wait_for(2);
b(db, input)
}
#[salsa::tracked(jar = Jar)]
pub(crate) fn b(db: &dyn Db, input: MyInput) -> i32 {
2022-08-09 10:33:46 +00:00
// Wait to create the cycle until both threads have entered
2022-08-09 10:06:39 +00:00
db.wait_for(1);
db.signal(2);
2022-08-09 10:33:46 +00:00
// Wait for thread A to block on this thread
2022-08-09 10:06:39 +00:00
db.wait_for(3);
2022-08-09 10:33:46 +00:00
// Now try to execute A
2022-08-09 10:06:39 +00:00
a(db, input)
}
#[test]
fn execute() {
let db = Database::default();
2022-08-09 10:06:39 +00:00
db.knobs().signal_on_will_block.set(3);
let input = MyInput::new(&db, -1);
2022-08-09 10:06:39 +00:00
2022-08-09 10:33:46 +00:00
let thread_a = std::thread::spawn({
2022-08-09 10:06:39 +00:00
let db = db.snapshot();
move || a(&*db, input)
});
let thread_b = std::thread::spawn({
let db = db.snapshot();
move || b(&*db, input)
});
2022-08-09 10:33:46 +00:00
// We expect B to panic because it detects a cycle (it is the one that calls A, ultimately).
// Right now, it panics with a string.
2022-08-09 10:06:39 +00:00
let err_b = thread_b.join().unwrap_err();
if let Some(c) = err_b.downcast_ref::<salsa::Cycle>() {
let expected = expect![[r#"
[
2022-08-22 07:23:58 +00:00
"a(0)",
"b(0)",
2022-08-09 10:06:39 +00:00
]
"#]];
expected.assert_debug_eq(&c.all_participants(&db));
} else {
panic!("b failed in an unexpected way: {:?}", err_b);
}
2022-08-09 10:33:46 +00:00
// We expect A to propagate a panic, which causes us to use the sentinel
// type `Canceled`.
assert!(thread_a
.join()
.unwrap_err()
.downcast_ref::<salsa::Cancelled>()
.is_some());
}