Skip to content

Commit 2680baa

Browse files
committed
Add test for durability changes
1 parent 5c37082 commit 2680baa

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/function/backdate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ where
1919
// Careful: if the value became less durable than it
2020
// used to be, that is a "breaking change" that our
2121
// consumers must be aware of. Becoming *more* durable
22-
// is not. See the test `constant_to_non_constant`.
22+
// is not. See the test `durable_to_less_durable`.
2323
if revisions.durability >= old_memo.revisions.durability
2424
&& C::should_backdate_value(old_value, value)
2525
{

tests/durability.rs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//! Tests that code using the builder's durability methods compiles.
2+
3+
use salsa::{Database, Durability, Setter};
4+
use test_log::test;
5+
6+
#[salsa::input]
7+
struct N {
8+
value: u32,
9+
}
10+
11+
#[salsa::tracked]
12+
fn add3(db: &dyn Database, a: N, b: N, c: N) -> u32 {
13+
add(db, a, b) + c.value(db)
14+
}
15+
16+
#[salsa::tracked]
17+
fn add(db: &dyn Database, a: N, b: N) -> u32 {
18+
a.value(db) + b.value(db)
19+
}
20+
21+
#[test]
22+
fn durable_to_less_durable() {
23+
let mut db = salsa::DatabaseImpl::new();
24+
25+
let a = N::builder(11).value_durability(Durability::HIGH).new(&db);
26+
let b = N::builder(22).value_durability(Durability::HIGH).new(&db);
27+
let c = N::builder(33).value_durability(Durability::HIGH).new(&db);
28+
29+
// Here, `add3` invokes `add(a, b)`, which yields 33.
30+
assert_eq!(add3(&db, a, b, c), 66);
31+
32+
a.set_value(&mut db).with_durability(Durability::LOW).to(11);
33+
34+
// Here, `add3` invokes `add`, which *still* yields 33, but which
35+
// is no longer of high durability. Since value didn't change, we might
36+
// preserve `add3` unchanged, not noticing that it is no longer
37+
// of high durability.
38+
39+
assert_eq!(add3(&db, a, b, c), 66);
40+
41+
// In that case, we would not get the correct result here, when
42+
// 'a' changes *again*.
43+
44+
a.set_value(&mut db).to(22);
45+
46+
assert_eq!(add3(&db, a, b, c), 77);
47+
}

0 commit comments

Comments
 (0)