pub struct TestWrapper<B, Req, Res> {
pub backend: B,
pub worker: Worker<Context>,
/* private fields */
}
Expand description
A generic backend wrapper that polls and executes jobs
Fields§
§backend: B
The inner backend
worker: Worker<Context>
The inner worker
Implementations§
Source§impl<B, Req, Res, Ctx> TestWrapper<B, Request<Req, Ctx>, Res>
A test wrapper to allow you to test without requiring a worker.
Important for testing backends and jobs
impl<B, Req, Res, Ctx> TestWrapper<B, Request<Req, Ctx>, Res>
A test wrapper to allow you to test without requiring a worker. Important for testing backends and jobs
§Example
#[cfg(tests)]
mod tests {
use crate::{
error::Error, memory::MemoryStorage, mq::MessageQueue, service_fn::service_fn,
};
use super::*;
async fn is_even(req: usize) -> Result<(), Error> {
if req % 2 == 0 {
Ok(())
} else {
Err(Error::Abort("Not an even number".to_string()))
}
}
#[tokio::test]
async fn test_accepts_even() {
let backend = MemoryStorage::new();
let (mut tester, poller) = TestWrapper::new_with_service(backend, service_fn(is_even));
tokio::spawn(poller);
tester.enqueue(42usize).await.unwrap();
assert_eq!(tester.size().await.unwrap(), 1);
let (_, resp) = tester.execute_next().await.unwrap();
assert_eq!(resp, Ok("()".to_string()));
}
}
Sourcepub fn new_with_service<Svc>(
backend: B,
service: Svc,
) -> (Self, BoxFuture<'static, ()>)where
Svc::Future: Send,
Svc: Send + Sync + Service<Request<Req, Ctx>, Response = Res> + 'static,
Req: Send + Sync + 'static,
Svc::Response: Send + Sync + 'static,
Svc::Error: Send + Sync + Error,
Ctx: Send + Sync + 'static,
B: Backend<Request<Req, Ctx>> + 'static,
B::Layer: Layer<Svc> + Layer<TestEmitService<Svc>>,
<B::Layer as Layer<Svc>>::Service: Service<Request<Req, Ctx>, Response = Res> + Send + Sync,
B::Stream: Unpin + Send,
Res: Send,
<<B::Layer as Layer<Svc>>::Service as Service<Request<Req, Ctx>>>::Future: Send,
<<B::Layer as Layer<Svc>>::Service as Service<Request<Req, Ctx>>>::Error: Send + Sync + Error,
<B::Layer as Layer<TestEmitService<Svc>>>::Service: Service<Request<Req, Ctx>, Response = Res> + Send + Sync,
<<B::Layer as Layer<TestEmitService<Svc>>>::Service as Service<Request<Req, Ctx>>>::Future: Send,
<<B::Layer as Layer<TestEmitService<Svc>>>::Service as Service<Request<Req, Ctx>>>::Error: Send + Sync + Error,
pub fn new_with_service<Svc>(
backend: B,
service: Svc,
) -> (Self, BoxFuture<'static, ()>)where
Svc::Future: Send,
Svc: Send + Sync + Service<Request<Req, Ctx>, Response = Res> + 'static,
Req: Send + Sync + 'static,
Svc::Response: Send + Sync + 'static,
Svc::Error: Send + Sync + Error,
Ctx: Send + Sync + 'static,
B: Backend<Request<Req, Ctx>> + 'static,
B::Layer: Layer<Svc> + Layer<TestEmitService<Svc>>,
<B::Layer as Layer<Svc>>::Service: Service<Request<Req, Ctx>, Response = Res> + Send + Sync,
B::Stream: Unpin + Send,
Res: Send,
<<B::Layer as Layer<Svc>>::Service as Service<Request<Req, Ctx>>>::Future: Send,
<<B::Layer as Layer<Svc>>::Service as Service<Request<Req, Ctx>>>::Error: Send + Sync + Error,
<B::Layer as Layer<TestEmitService<Svc>>>::Service: Service<Request<Req, Ctx>, Response = Res> + Send + Sync,
<<B::Layer as Layer<TestEmitService<Svc>>>::Service as Service<Request<Req, Ctx>>>::Future: Send,
<<B::Layer as Layer<TestEmitService<Svc>>>::Service as Service<Request<Req, Ctx>>>::Error: Send + Sync + Error,
Build a new instance provided a custom service
Sourcepub async fn execute_next(&mut self) -> Option<(TaskId, Result<String, String>)>
pub async fn execute_next(&mut self) -> Option<(TaskId, Result<String, String>)>
Gets the current state of results
Sourcepub fn try_execute_next(
&mut self,
) -> Result<Option<(TaskId, Result<String, String>)>, TryRecvError>
pub fn try_execute_next( &mut self, ) -> Result<Option<(TaskId, Result<String, String>)>, TryRecvError>
Gets the current state of results
Trait Implementations§
Source§impl<B, Req, Res, Ctx> Deref for TestWrapper<B, Request<Req, Ctx>, Res>
impl<B, Req, Res, Ctx> Deref for TestWrapper<B, Request<Req, Ctx>, Res>
Auto Trait Implementations§
impl<B, Req, Res> Freeze for TestWrapper<B, Req, Res>where
B: Freeze,
impl<B, Req, Res> !RefUnwindSafe for TestWrapper<B, Req, Res>
impl<B, Req, Res> Send for TestWrapper<B, Req, Res>
impl<B, Req, Res> Sync for TestWrapper<B, Req, Res>
impl<B, Req, Res> Unpin for TestWrapper<B, Req, Res>
impl<B, Req, Res> !UnwindSafe for TestWrapper<B, Req, Res>
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
Mutably borrows from an owned value. Read more
Source§impl<R> Rng for R
impl<R> Rng for R
Source§fn random<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
fn random<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
Return a random value via the
StandardUniform
distribution. Read moreSource§fn random_iter<T>(self) -> Iter<StandardUniform, Self, T>
fn random_iter<T>(self) -> Iter<StandardUniform, Self, T>
Source§fn random_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
fn random_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
Generate a random value in the given range. Read more
Source§fn random_bool(&mut self, p: f64) -> bool
fn random_bool(&mut self, p: f64) -> bool
Return a bool with a probability
p
of being true. Read moreSource§fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool
fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool
Return a bool with a probability of
numerator/denominator
of being
true. Read moreSource§fn sample<T, D>(&mut self, distr: D) -> Twhere
D: Distribution<T>,
fn sample<T, D>(&mut self, distr: D) -> Twhere
D: Distribution<T>,
Sample a new value, using the given distribution. Read more
Source§fn sample_iter<T, D>(self, distr: D) -> Iter<D, Self, T>where
D: Distribution<T>,
Self: Sized,
fn sample_iter<T, D>(self, distr: D) -> Iter<D, Self, T>where
D: Distribution<T>,
Self: Sized,
Create an iterator that generates values using the given distribution. Read more
Source§fn gen<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
fn gen<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
👎Deprecated since 0.9.0: Renamed to
random
to avoid conflict with the new gen
keyword in Rust 2024.Alias for
Rng::random
.Source§fn gen_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
fn gen_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
👎Deprecated since 0.9.0: Renamed to
random_range
Alias for
Rng::random_range
.Source§impl<R> TryRngCore for R
impl<R> TryRngCore for R
Source§type Error = Infallible
type Error = Infallible
The type returned in the event of a RNG error.
Source§fn try_next_u32(&mut self) -> Result<u32, <R as TryRngCore>::Error>
fn try_next_u32(&mut self) -> Result<u32, <R as TryRngCore>::Error>
Return the next random
u32
.Source§fn try_next_u64(&mut self) -> Result<u64, <R as TryRngCore>::Error>
fn try_next_u64(&mut self) -> Result<u64, <R as TryRngCore>::Error>
Return the next random
u64
.Source§fn try_fill_bytes(
&mut self,
dst: &mut [u8],
) -> Result<(), <R as TryRngCore>::Error>
fn try_fill_bytes( &mut self, dst: &mut [u8], ) -> Result<(), <R as TryRngCore>::Error>
Fill
dest
entirely with random data.Source§fn unwrap_mut(&mut self) -> UnwrapMut<'_, Self>
fn unwrap_mut(&mut self) -> UnwrapMut<'_, Self>
Wrap RNG with the
UnwrapMut
wrapper.Source§fn read_adapter(&mut self) -> RngReadAdapter<'_, Self>where
Self: Sized,
fn read_adapter(&mut self) -> RngReadAdapter<'_, Self>where
Self: Sized,
Convert an
RngCore
to a RngReadAdapter
.