Struct FaultyStore
pub struct FaultyStore<S: EventStore> { /* private fields */ }Expand description
Wraps a store so it can be made to fail on demand — the !Send flavour.
Rejects: a retry loop that branches on conflicting_position being
Some. An injected violation reports None, which is what a store with
no interactive transaction legitimately reports, so a loop written for an
in-process store never retries here — in the author’s own test suite, in
under a second, with no database.
One instance is one arming. Cloning a wrapper produces a second handle onto the same counters, so the total number of injected failures across every clone is at most the number armed; which clone receives which failure is deliberately unspecified.
Three inputs can be armed, and they are not interchangeable.
violate_next produces the DCB rejection signal,
fail_next_read a mid-stream read failure, and
contend_next a transient refusal,
AppendError::Busy: the busy store, which no in-process store in this
workspace can otherwise be.
Use SendFaultyStore where the inner store implements
SendEventStore and the wrapper has to
cross a thread boundary. Two types rather than one impl: trait_variant’s
blanket impl makes one type carrying both flavours error[E0119].
§Examples
use happenstance_core::{AppendError, Event, EventStore};
use happenstance_core::MemoryEventStore;
use happenstance_testkit::{FaultyStore, block_on};
let store = FaultyStore::new(MemoryEventStore::new()).violate_next(1);
let seat = Event::new("Seated", &b"{}"[..])?;
let refused = block_on(store.append(&[seat.clone()], None));
match refused {
Err(AppendError::ConditionViolated(violated)) => {
assert!(violated.conflicting_position.is_none());
}
other => panic!("expected a violation, got {other:?}"),
}
// The arming is spent, so the next append delegates.
assert!(block_on(store.append(&[seat], None)).is_ok());Implementations§
§impl<S: EventStore> FaultyStore<S>
impl<S: EventStore> FaultyStore<S>
pub fn new(inner: S) -> Self
pub fn new(inner: S) -> Self
Wraps inner with nothing armed.
Until something is armed the wrapper is transparent: it delegates every
call and is indistinguishable from inner, which the conformance suite
asserts rather than assumes.
pub fn violate_next(self, n: u32) -> Self
pub fn violate_next(self, n: u32) -> Self
Fails the next n appends with a violated condition naming no
conflicting event.
The inner store is never called for an armed append, so nothing is written, no position is allocated and the head does not move. Each armed call consumes one; when the count reaches zero the wrapper delegates again, so a fixture is never permanently poisoned.
n = 0 arms nothing and is legal — a caller writing
violate_next(attempts - 1) is not surprised.
pub fn fail_next_read(self, n: u32) -> Self
pub fn fail_next_read(self, n: u32) -> Self
Fails the next n reads at their first polled item.
The count is consumed where the stream is produced, not where it is
first polled, so a read whose stream is dropped undrained still spends
one. The failure itself arrives lazily, as
FaultyStoreError::Injected, because that is where a real store fails
— read is not async and has no call-time channel for one.
n = 0 arms nothing and is legal.
pub fn contend_next(self, n: u32) -> Self
pub fn contend_next(self, n: u32) -> Self
Refuses the next n appends as busy, through
AppendError::Busy.
See SendFaultyStore::contend_next for what this is for and why the
count is exact under a race; this flavour differs only in the bound.
n = 0 arms nothing and is legal.