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
impl QueueFake
Sourcepub fn batches(&self) -> Vec<RecordedBatch>
pub fn batches(&self) -> Vec<RecordedBatch>
Every recorded batch, in dispatch order.
Sourcepub fn assert_batched(&self, pred: impl Fn(&RecordedBatch) -> bool)
pub fn assert_batched(&self, pred: impl Fn(&RecordedBatch) -> bool)
Sourcepub fn assert_nothing_batched(&self)
pub fn assert_nothing_batched(&self)
Sourcepub fn queue(&self) -> Queue
pub fn queue(&self) -> Queue
A Queue whose dispatches are recorded by this fake. It accepts
any job type, registered or not.
Sourcepub fn dispatcher(&self) -> Arc<dyn Dispatcher> ⓘ
pub fn dispatcher(&self) -> Arc<dyn Dispatcher> ⓘ
This fake as a dyn Dispatcher.
Sourcepub fn envelopes(&self) -> Vec<JobEnvelope>
pub fn envelopes(&self) -> Vec<JobEnvelope>
Every recorded job, in dispatch order.
Sourcepub fn dispatched<J: Job>(&self) -> Vec<J>
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.
Sourcepub fn assert_dispatched<J: Job>(&self, pred: impl Fn(&J) -> bool)
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.
Sourcepub fn assert_dispatched_times<J: Job>(&self, times: usize)
pub fn assert_dispatched_times<J: Job>(&self, times: usize)
Sourcepub fn assert_not_dispatched<J: Job>(&self)
pub fn assert_not_dispatched<J: Job>(&self)
Sourcepub fn assert_nothing_dispatched(&self)
pub fn assert_nothing_dispatched(&self)
Trait Implementations§
Source§impl Dispatcher for QueueFake
impl Dispatcher for QueueFake
Source§fn dispatch_envelope(
&self,
env: JobEnvelope,
) -> BoxFuture<'_, Result<String, BoxError>>
fn dispatch_envelope( &self, env: JobEnvelope, ) -> BoxFuture<'_, Result<String, BoxError>>
Auto Trait Implementations§
impl Forget for QueueFake
impl Freeze for QueueFake
impl RefUnwindSafe for QueueFake
impl Send for QueueFake
impl Sync for QueueFake
impl Unpin for QueueFake
impl UnsafeUnpin for QueueFake
impl UnwindSafe for QueueFake
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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