cultcache-rs: pin the reported-stamp validation (RFC 3339 only) in update tests

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Libby 2026-11-13 19:59:56 +00:00
commit c00b4ad6ed

View file

@ -655,3 +655,81 @@ fn the_byte_exact_writers_keep_the_callers_stamp() {
assert_eq!(redb.push_all(std::slice::from_ref(&held), PushAllOptions::default()).unwrap(), vec![held.stored_at.clone()]);
assert_eq!(redb.pull_all().unwrap(), vec![held]);
}
/// A single-file store that reports whatever stamp it is told to, as a custom store might.
struct ReportingStore {
inner: SingleFileMessagePackBackingStore,
report: String,
}
impl CacheBackingStore for ReportingStore {
fn pull_all(&self) -> anyhow::Result<Vec<CultCacheEnvelope>> {
self.inner.pull_all()
}
fn push(&mut self, entry: &CultCacheEnvelope) -> anyhow::Result<String> {
self.inner.push(entry)?;
Ok(self.report.clone())
}
fn delete(&mut self, entry: &CultCacheEnvelope) -> anyhow::Result<()> {
self.inner.delete(entry)
}
fn push_all(&mut self, entries: &[CultCacheEnvelope], options: PushAllOptions) -> anyhow::Result<Vec<String>> {
self.inner.push_all(entries, options)?;
Ok(entries.iter().map(|_| self.report.clone()).collect())
}
fn read(&self, key: &str) -> anyhow::Result<Option<CultStoredRecord>> {
self.inner.read(key)
}
fn commit_if(&mut self, entry: &CultCacheEnvelope, expected: Option<&str>) -> anyhow::Result<CultCommitIf> {
Ok(match self.inner.commit_if(entry, expected)? {
CultCommitIf::Committed { .. } => CultCommitIf::Committed { stored_at: self.report.clone() },
CultCommitIf::Mismatch => CultCommitIf::Mismatch,
})
}
}
#[test]
fn the_cache_holds_a_reported_stamp_only_when_it_parses_as_rfc3339() {
type Write = fn(&mut CultCache);
let writes: [(&str, Write); 4] = [
("put", |cache| drop(cache.put("c", &counter(1)).unwrap())),
("put_raw_envelope", |cache| {
let entry = envelope_of(cache, "c", &counter(1), "2020-01-01T00:00:00Z");
cache.put_raw_envelope(entry).unwrap()
}),
("put_prepared_batch", |cache| {
let entry = envelope_of(cache, "c", &counter(1), "2020-01-01T00:00:00Z");
cache.put_prepared_batch(vec![entry]).unwrap()
}),
("update", |cache| drop(cache.update::<Counter, _>("c", |_| Some(counter(1))).unwrap())),
];
for (report, parses) in [
("2030-01-01T00:00:00Z", true),
("2030-01-01T00:00:00.123456+00:00", true),
("not a stamp", false),
("", false),
("2030-01-01", false),
("2030-01-01 00:00:00", false),
("2030-01-01T00:00:00", false),
("1893456000", false),
] {
for (name, write) in writes {
let dir = tempfile::tempdir().unwrap();
let path = path_in(&dir);
let mut cache = CultCache::new();
cache.register_entry_type::<Counter>().unwrap();
cache
.add_generic_backing_store(ReportingStore { inner: SingleFileMessagePackBackingStore::new(&path), report: report.to_string() })
.unwrap();
cache.pull_all_backing_stores().unwrap();
write(&mut cache);
let held = cache.get_envelope::<Counter>("c").unwrap().unwrap().stored_at;
if parses {
assert_eq!(held, report, "{name}");
} else {
assert_ne!(held, report, "{name} held a stamp that is not RFC 3339");
assert!(chrono::DateTime::parse_from_rfc3339(&held).is_ok(), "{name}: {held}");
}
}
}
}