2024-07-24 10:36:07 +00:00
|
|
|
//! Test for cycle recover spread across two threads.
|
|
|
|
//! See `../cycles.rs` for a complete listing of cycle tests,
|
|
|
|
//! both intra and cross thread.
|
|
|
|
|
|
|
|
use salsa::Cancelled;
|
2024-07-27 12:29:41 +00:00
|
|
|
use salsa::DatabaseImpl;
|
2024-07-24 10:36:07 +00:00
|
|
|
use salsa::Setter;
|
|
|
|
|
|
|
|
use crate::setup::Knobs;
|
2024-07-27 12:29:41 +00:00
|
|
|
use crate::setup::KnobsDatabase;
|
2024-07-24 10:36:07 +00:00
|
|
|
|
|
|
|
#[salsa::input]
|
|
|
|
struct MyInput {
|
|
|
|
field: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::tracked]
|
2024-07-27 12:29:41 +00:00
|
|
|
fn a1(db: &dyn KnobsDatabase, input: MyInput) -> MyInput {
|
2024-07-24 10:36:07 +00:00
|
|
|
db.signal(1);
|
|
|
|
db.wait_for(2);
|
|
|
|
dummy(db, input)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::tracked]
|
2024-07-27 12:29:41 +00:00
|
|
|
fn dummy(_db: &dyn KnobsDatabase, _input: MyInput) -> MyInput {
|
2024-07-24 10:36:07 +00:00
|
|
|
panic!("should never get here!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancellation signalling test
|
|
|
|
//
|
|
|
|
// The pattern is as follows.
|
|
|
|
//
|
|
|
|
// Thread A Thread B
|
|
|
|
// -------- --------
|
|
|
|
// a1
|
|
|
|
// | wait for stage 1
|
|
|
|
// signal stage 1 set input, triggers cancellation
|
|
|
|
// wait for stage 2 (blocks) triggering cancellation sends stage 2
|
|
|
|
// |
|
|
|
|
// (unblocked)
|
|
|
|
// dummy
|
|
|
|
// panics
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn execute() {
|
2024-07-27 19:57:47 +00:00
|
|
|
let mut db = <DatabaseImpl<Knobs>>::default();
|
2024-07-24 10:36:07 +00:00
|
|
|
db.knobs().signal_on_will_block.store(3);
|
|
|
|
|
2024-07-27 19:57:47 +00:00
|
|
|
let input = MyInput::new(&db, 1);
|
2024-07-24 10:36:07 +00:00
|
|
|
|
|
|
|
let thread_a = std::thread::spawn({
|
|
|
|
let db = db.clone();
|
2024-07-27 19:57:47 +00:00
|
|
|
move || a1(&db, input)
|
2024-07-24 10:36:07 +00:00
|
|
|
});
|
|
|
|
|
2024-07-27 19:57:47 +00:00
|
|
|
input.set_field(&mut db).to(2);
|
2024-07-24 10:36:07 +00:00
|
|
|
|
|
|
|
// Assert thread A *should* was cancelled
|
|
|
|
let cancelled = thread_a
|
|
|
|
.join()
|
|
|
|
.unwrap_err()
|
|
|
|
.downcast::<Cancelled>()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// and inspect the output
|
|
|
|
expect_test::expect![[r#"
|
|
|
|
PendingWrite
|
|
|
|
"#]]
|
|
|
|
.assert_debug_eq(&cancelled);
|
|
|
|
}
|