Skip to content

Commit

Permalink
Merge pull request #734 from Veykril/veykril/push-txytyxrsumuu
Browse files Browse the repository at this point in the history
Add test for durability changes
  • Loading branch information
Veykril authored Feb 26, 2025
2 parents 5c37082 + 2680baa commit 2fc72ba
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
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);
}

0 comments on commit 2fc72ba

Please sign in to comment.