salsa/tests/gc/volatile_tests.rs

73 lines
1.6 KiB
Rust
Raw Normal View History

2019-03-22 20:24:37 +00:00
use crate::db;
use salsa::{Database, SweepStrategy};
2019-03-25 18:40:32 +00:00
use std::sync::atomic::{AtomicUsize, Ordering};
2019-03-22 20:24:37 +00:00
use std::sync::Arc;
/// Query group for tests for how interned keys interact with GC.
#[salsa::query_group(Volatile)]
pub(crate) trait VolatileDatabase: Database {
2019-03-22 20:24:37 +00:00
#[salsa::input]
2019-03-25 18:40:32 +00:00
fn atomic_cell(&self) -> Arc<AtomicUsize>;
2019-03-22 20:24:37 +00:00
/// Underlying volatile query.
2019-03-25 18:40:32 +00:00
fn volatile(&self) -> usize;
2019-03-22 20:24:37 +00:00
/// This just executes the intern query and returns the result.
2019-03-25 18:40:32 +00:00
fn repeat1(&self) -> usize;
2019-03-22 20:24:37 +00:00
/// Same as `repeat_intern1`. =)
2019-03-25 18:40:32 +00:00
fn repeat2(&self) -> usize;
2019-03-22 20:24:37 +00:00
}
2019-03-25 18:40:32 +00:00
fn volatile(db: &impl VolatileDatabase) -> usize {
db.salsa_runtime().report_untracked_read();
2019-03-22 20:24:37 +00:00
db.atomic_cell().load(Ordering::SeqCst)
}
2019-03-25 18:40:32 +00:00
fn repeat1(db: &impl VolatileDatabase) -> usize {
2019-03-22 20:24:37 +00:00
db.volatile()
}
2019-03-25 18:40:32 +00:00
fn repeat2(db: &impl VolatileDatabase) -> usize {
2019-03-22 20:24:37 +00:00
db.volatile()
}
#[test]
fn consistency_no_gc() {
let mut db = db::DatabaseImpl::default();
2019-03-25 18:40:32 +00:00
let cell = Arc::new(AtomicUsize::new(22));
2019-03-22 20:24:37 +00:00
db.set_atomic_cell(cell.clone());
let v1 = db.repeat1();
cell.store(23, Ordering::SeqCst);
let v2 = db.repeat2();
assert_eq!(v1, v2);
}
#[test]
fn consistency_with_gc() {
let mut db = db::DatabaseImpl::default();
2019-03-25 18:40:32 +00:00
let cell = Arc::new(AtomicUsize::new(22));
2019-03-22 20:24:37 +00:00
db.set_atomic_cell(cell.clone());
let v1 = db.repeat1();
cell.store(23, Ordering::SeqCst);
db.query(VolatileQuery).sweep(
SweepStrategy::default()
.discard_everything()
.sweep_all_revisions(),
);
let v2 = db.repeat2();
assert_eq!(v1, v2);
}