Skip to main content

FaultyStore

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>

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

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

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

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.

Trait Implementations§

§

impl<S: Clone + EventStore> Clone for FaultyStore<S>

§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl<S: Debug + EventStore> Debug for FaultyStore<S>

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
§

impl<S: EventStore> EventStore for FaultyStore<S>

§

type Error = FaultyStoreError<<S as EventStore>::Error>

How this adapter fails for its own reasons. Read more
§

fn read( &self, query: &Query, options: ReadOptions, ) -> impl Stream<Item = Result<SequencedEvent, Self::Error>>

Reads the events matching query, in the order options asks for. Read more
§

async fn append( &self, events: &[Event], condition: Option<&AppendCondition>, ) -> Result<SequencePosition, AppendError<Self::Error>>

Atomically appends events, rejecting the write if condition matches. Read more
§

async fn head(&self) -> Result<Option<SequencePosition>, Self::Error>

The highest position currently visible in the store, or None when it holds nothing. Read more
§

async fn contains_event_id(&self, id: EventId) -> Result<bool, Self::Error>

Whether this store holds an event with id. Read more

Auto Trait Implementations§

§

impl<S> Freeze for FaultyStore<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for FaultyStore<S>
where S: RefUnwindSafe,

§

impl<S> Send for FaultyStore<S>
where S: Send,

§

impl<S> Sync for FaultyStore<S>
where S: Sync,

§

impl<S> Unpin for FaultyStore<S>
where S: Unpin,

§

impl<S> UnsafeUnpin for FaultyStore<S>
where S: UnsafeUnpin,

§

impl<S> UnwindSafe for FaultyStore<S>
where S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V