diff --git a/crates/thing-core/src/port.rs b/crates/thing-core/src/port.rs index c897d5e..9687fba 100644 --- a/crates/thing-core/src/port.rs +++ b/crates/thing-core/src/port.rs @@ -96,7 +96,8 @@ pub trait StatePort { fn unsubscribe(&mut self, id: SubscriptionId); - /// Every change since the last drain that some live interest matches, in mutation order, + /// Every change since the last drain that some interest matched when it happened, in mutation + /// order (unsubscribing does not recall changes already queued), /// each once. fn drain(&mut self) -> Vec; } diff --git a/crates/thing-testkit/src/fixture.rs b/crates/thing-testkit/src/fixture.rs index ff83ad9..8ffc052 100644 --- a/crates/thing-testkit/src/fixture.rs +++ b/crates/thing-testkit/src/fixture.rs @@ -83,8 +83,17 @@ impl Fixture { /// The committed store, decoded through the contract. pub fn load(self) -> FixtureSet { - let cache = store::open(&Self::dir().join(self.file_name())); - FixtureSet::new(cache.get_all().unwrap(), cache.get_all().unwrap(), cache.get_all().unwrap()) + // Opening a store creates a lock file beside it, so open a copy: the fixtures directory + // is never written by a load. + let work = std::env::temp_dir().join(format!("thing-testkit-load-{}-{}", std::process::id(), self.file_name())); + std::fs::create_dir_all(&work).unwrap(); + let copy = work.join(self.file_name()); + std::fs::copy(Self::dir().join(self.file_name()), ©).unwrap(); + let cache = store::open(©); + let set = FixtureSet::new(cache.get_all().unwrap(), cache.get_all().unwrap(), cache.get_all().unwrap()); + drop(cache); + let _ = std::fs::remove_dir_all(&work); + set } } diff --git a/crates/thing-testkit/src/mem_state.rs b/crates/thing-testkit/src/mem_state.rs index 96ec2cb..b649c4e 100644 --- a/crates/thing-testkit/src/mem_state.rs +++ b/crates/thing-testkit/src/mem_state.rs @@ -208,6 +208,8 @@ impl StatePort for MemState { Some(value) => Lookup::Present(Entry { address: address.clone(), record: value.into_record() }), None => Lookup::Absent, } + // A schema outside the four contract arms reads Absent. Cut 4 replaces this with the provider-state + // answer once thing2:question:provider-state-value is ruled. }, else Lookup::Absent) } diff --git a/crates/thing-testkit/tests/fixtures.rs b/crates/thing-testkit/tests/fixtures.rs index c559c50..4702291 100644 --- a/crates/thing-testkit/tests/fixtures.rs +++ b/crates/thing-testkit/tests/fixtures.rs @@ -161,3 +161,25 @@ fn a_state_holds_every_fixture_document_under_its_publisher() { }; assert!(matches!(state.read(&at), Lookup::Present(_))); } + +#[test] +fn loading_a_fixture_writes_nothing_in_the_fixtures_directory() { + let listing = || { + let mut names: Vec<_> = std::fs::read_dir(Fixture::dir()) + .unwrap() + .map(|entry| entry.unwrap().file_name().into_string().unwrap()) + .collect(); + names.sort(); + names + }; + let before = listing(); + let mut permissions = std::fs::metadata(Fixture::dir()).unwrap().permissions(); + let writable = permissions.clone(); + permissions.set_readonly(true); + std::fs::set_permissions(Fixture::dir(), permissions).unwrap(); + for fixture in Fixture::ALL { + load(fixture); + } + std::fs::set_permissions(Fixture::dir(), writable).unwrap(); + assert_eq!(listing(), before); +} diff --git a/crates/thing-testkit/tests/golden.rs b/crates/thing-testkit/tests/golden.rs index 4930d57..367cddd 100644 --- a/crates/thing-testkit/tests/golden.rs +++ b/crates/thing-testkit/tests/golden.rs @@ -57,3 +57,30 @@ fn read_returns_the_committed_bytes() { std::fs::write(dir.join("r.bin"), b"abc").unwrap(); assert_eq!(golden::read(&dir, "r.bin"), b"abc"); } + +/// Re-runs this binary with THING_BLESS=1 so the writer is observed without touching this +/// process's environment. +#[test] +fn blessing_writes_the_file_and_the_text() { + let dir = scratch("bless"); + let status = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "bless_child", "--test-threads=1"]) + .env("THING_BLESS", "1") + .env("THING_BLESS_CHILD_DIR", &dir) + .status() + .unwrap(); + assert!(status.success()); + assert_eq!(std::fs::read(dir.join("b.bin")).unwrap(), b"blessed"); + assert_eq!(std::fs::read(dir.join("t.bin")).unwrap(), b"tb"); + assert_eq!(std::fs::read(dir.join("t.txt")).unwrap(), b"tt"); +} + +/// The child half of the test above; does nothing in a normal run. +#[test] +fn bless_child() { + if let Ok(dir) = std::env::var("THING_BLESS_CHILD_DIR") { + let dir = PathBuf::from(dir); + golden::check(&dir, "b.bin", b"blessed"); + golden::check_with_text(&dir, "t", b"tb", "tt"); + } +} diff --git a/crates/thing-testkit/tests/mem_state.rs b/crates/thing-testkit/tests/mem_state.rs index 1a0c5d9..44651bd 100644 --- a/crates/thing-testkit/tests/mem_state.rs +++ b/crates/thing-testkit/tests/mem_state.rs @@ -265,3 +265,41 @@ fn unsubscribing_ends_only_that_interest() { state.put(&provider_a, &panel); assert!(state.drain().is_empty()); } + +#[test] +fn unsubscribing_keeps_changes_already_queued() { + let one = surface("provider.a", "one"); + let mut state = MemState::new(); + let id = state.subscribe(Interest::Record(surface_at("provider.a", &one))); + state.put(&provider("provider.a"), &one); + state.unsubscribe(id); + assert_eq!(state.drain().len(), 1, "a change queued while the interest was live is still drained"); +} + +#[test] +fn a_list_mixes_refused_and_present_in_key_order() { + let a = surface("provider.a", "a"); + let c = surface("provider.a", "c"); + let b_at = address("provider.a", SURFACE, "thing:surface:provider.a.b"); + let mut state = MemState::new(); + state.put(&provider("provider.a"), &c); + state.put_raw(b_at.clone(), unknown_arm()); + state.put(&provider("provider.a"), &a); + let kinds: Vec<(&str, String)> = state + .list(&prefix("provider.a", SURFACE, "thing:")) + .into_iter() + .map(|lookup| match lookup { + Lookup::Present(entry) => ("present", entry.address.key.as_str().to_string()), + Lookup::Refused(refusal) => ("refused", refusal.address.key.as_str().to_string()), + Lookup::Absent => panic!("a list holds no absence"), + }) + .collect(); + assert_eq!( + kinds, + [ + ("present", "thing:surface:provider.a.a".to_string()), + ("refused", b_at.key.as_str().to_string()), + ("present", "thing:surface:provider.a.c".to_string()), + ] + ); +}