From 00172efb19b0cb6a30dba7035e79061d9769e7ab Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Sun, 7 Aug 2022 00:32:56 -0400 Subject: [PATCH] add red-herring tests which modify distinct inputs --- salsa-2022-tests/tests/hello_world.rs | 25 +++++++++++++++++ .../tests/tracked_fn_read_own_entity.rs | 28 ++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/salsa-2022-tests/tests/hello_world.rs b/salsa-2022-tests/tests/hello_world.rs index 04e4a078..1c03a822 100644 --- a/salsa-2022-tests/tests/hello_world.rs +++ b/salsa-2022-tests/tests/hello_world.rs @@ -83,3 +83,28 @@ fn execute() { "final_result(MyInput(Id { value: 1 }))", ]"#]]); } + +/// Create and mutate a distinct input. No re-execution required. +#[test] +fn red_herring() { + let mut db = Database::default(); + + let input = MyInput::new(&mut db, 22); + assert_eq!(final_result(&db, input), 22); + db.assert_logs(expect![[r#" + [ + "final_result(MyInput(Id { value: 1 }))", + "intermediate_result(MyInput(Id { value: 1 }))", + ]"#]]); + + // Create a distinct input and mutate it. + // This will trigger a new revision in the database + // but shouldn't actually invalidate our existing ones. + let input2 = MyInput::new(&mut db, 44); + input2.set_field(&mut db, 66); + + // Re-run the query on the original input. Nothing re-executes! + assert_eq!(final_result(&db, input), 22); + db.assert_logs(expect![[r#" + []"#]]); +} diff --git a/salsa-2022-tests/tests/tracked_fn_read_own_entity.rs b/salsa-2022-tests/tests/tracked_fn_read_own_entity.rs index 57d0462f..7a339843 100644 --- a/salsa-2022-tests/tests/tracked_fn_read_own_entity.rs +++ b/salsa-2022-tests/tests/tracked_fn_read_own_entity.rs @@ -57,7 +57,7 @@ impl HasLogger for Database { } #[test] -fn execute() { +fn one_entity() { let mut db = Database::default(); let input = MyInput::new(&mut db, 22); @@ -85,3 +85,29 @@ fn execute() { "final_result(MyInput(Id { value: 1 }))", ]"#]]); } + +/// Create and mutate a distinct input. No re-execution required. +#[test] +fn red_herring() { + let mut db = Database::default(); + + let input = MyInput::new(&mut db, 22); + assert_eq!(final_result(&db, input), 22); + db.assert_logs(expect![[r#" + [ + "final_result(MyInput(Id { value: 1 }))", + "intermediate_result(MyInput(Id { value: 1 }))", + ]"#]]); + + // Create a distinct input and mutate it. + // This will trigger a new revision in the database + // but shouldn't actually invalidate our existing ones. + let input2 = MyInput::new(&mut db, 44); + input2.set_field(&mut db, 66); + + // Re-run the query on the original input. Nothing re-executes! + assert_eq!(final_result(&db, input), 22); + db.assert_logs(expect![[r#" + [ + ]"#]]); +}