Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for durability changes #734

Merged
merged 1 commit into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/function/backdate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ where
// Careful: if the value became less durable than it
// used to be, that is a "breaking change" that our
// consumers must be aware of. Becoming *more* durable
// is not. See the test `constant_to_non_constant`.
// is not. See the test `durable_to_less_durable`.
if revisions.durability >= old_memo.revisions.durability
&& C::should_backdate_value(old_value, value)
{
Expand Down
47 changes: 47 additions & 0 deletions tests/durability.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! Tests that code using the builder's durability methods compiles.

use salsa::{Database, Durability, Setter};
use test_log::test;

#[salsa::input]
struct N {
value: u32,
}

#[salsa::tracked]
fn add3(db: &dyn Database, a: N, b: N, c: N) -> u32 {
add(db, a, b) + c.value(db)
}

#[salsa::tracked]
fn add(db: &dyn Database, a: N, b: N) -> u32 {
a.value(db) + b.value(db)
}

#[test]
fn durable_to_less_durable() {
let mut db = salsa::DatabaseImpl::new();

let a = N::builder(11).value_durability(Durability::HIGH).new(&db);
let b = N::builder(22).value_durability(Durability::HIGH).new(&db);
let c = N::builder(33).value_durability(Durability::HIGH).new(&db);

// Here, `add3` invokes `add(a, b)`, which yields 33.
assert_eq!(add3(&db, a, b, c), 66);

a.set_value(&mut db).with_durability(Durability::LOW).to(11);

// Here, `add3` invokes `add`, which *still* yields 33, but which
// is no longer of high durability. Since value didn't change, we might
// preserve `add3` unchanged, not noticing that it is no longer
// of high durability.

assert_eq!(add3(&db, a, b, c), 66);

// In that case, we would not get the correct result here, when
// 'a' changes *again*.

a.set_value(&mut db).to(22);

assert_eq!(add3(&db, a, b, c), 77);
}