Skip to main content

QueueFake

Struct QueueFake 

Source
pub struct QueueFake { /* private fields */ }
Expand description

A queue that records jobs instead of running them, for tests.

Swap it in for the app’s queue with override_value(Arc::new(fake.queue())) (the module’s dyn Dispatcher then records too), or, in an app without QueueModule, with override_value(fake.dispatcher()). Then assert on what was dispatched:

use navree_contracts::{BoxError, Job, JobContext};
use navree_queue::QueueFake;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct SendInvoice {
    invoice: u32,
}

impl Job for SendInvoice {
    const NAME: &'static str = "billing.send_invoice";

    async fn handle(self, _ctx: JobContext) -> Result<(), BoxError> {
        Ok(())
    }
}

let fake = QueueFake::new();
fake.queue().dispatch(SendInvoice { invoice: 7 }).await.unwrap();

fake.assert_dispatched::<SendInvoice>(|job| job.invoice == 7);
assert_eq!(fake.dispatched::<SendInvoice>().len(), 1);

Clones share the record. Jobs dispatched in a unit of work are recorded when it commits. The fake queue never runs jobs: work and run_pending return empty stats and nothing ever fails.

Batches are recorded apart from single jobs (batches, assert_batched): a batch’s jobs are not in dispatched. find_batch and batches return a recorded batch as a driver stores it when it is dispatched (every job pending), and cancel_batch cancels it.

Implementations§

Source§

impl QueueFake

Source

pub fn batches(&self) -> Vec<RecordedBatch>

Every recorded batch, in dispatch order.

Source

pub fn assert_batched(&self, pred: impl Fn(&RecordedBatch) -> bool)

Asserts a batch matching pred was dispatched.

§Panics

When none was, naming the batches that were.

Source

pub fn assert_nothing_batched(&self)

Asserts no batch was dispatched.

§Panics

When one was.

Source

pub fn new() -> Self

An empty fake.

Source

pub fn queue(&self) -> Queue

A Queue whose dispatches are recorded by this fake. It accepts any job type, registered or not.

Source

pub fn dispatcher(&self) -> Arc<dyn Dispatcher> ⓘ

This fake as a dyn Dispatcher.

Source

pub fn envelopes(&self) -> Vec<JobEnvelope>

Every recorded job, in dispatch order.

Source

pub fn dispatched<J: Job>(&self) -> Vec<J>

The recorded jobs of type J, in dispatch order.

§Panics

When a recorded payload of type J does not deserialize as J.

Source

pub fn assert_dispatched<J: Job>(&self, pred: impl Fn(&J) -> bool)

Asserts a job of type J matching pred was dispatched.

§Panics

When none was, naming the job type and what was dispatched.

Source

pub fn assert_dispatched_times<J: Job>(&self, times: usize)

Asserts exactly times jobs of type J were dispatched.

§Panics

When the count differs.

Source

pub fn assert_not_dispatched<J: Job>(&self)

Asserts no job of type J was dispatched.

§Panics

When one was.

Source

pub fn assert_nothing_dispatched(&self)

Asserts nothing was dispatched.

§Panics

When something was.

Source

pub fn clear(&self)

Forgets every recorded job and batch.

Trait Implementations§

Source§

impl Clone for QueueFake

Source§

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
Source§

impl Debug for QueueFake

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for QueueFake

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Dispatcher for QueueFake

Source§

fn dispatch_envelope( &self, env: JobEnvelope, ) -> BoxFuture<'_, Result<String, BoxError>>

Queues a serialized job and returns its id.

Auto Trait Implementations§

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
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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<T> ValueFor<Option<T>> for T

Source§

fn into_value_for(self) -> Option<T>

The value as the column’s type.
Source§

impl<T> ValueFor<T> for T

Source§

fn into_value_for(self) -> T

The value as the column’s type.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more