pub struct TestWrapper<B, Req, Res> { /* private fields */ }Expand description
A generic backend wrapper that polls and executes jobs
Implementations§
Source§impl<B, Req, Res, Ctx> TestWrapper<B, Request<Req, Ctx>, Res>
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;
assert_eq!(resp, Ok("()".to_string()));
}
}Sourcepub fn new_with_service<S>(
backend: B,
service: S,
) -> (Self, BoxFuture<'static, ()>)where
S: Service<Request<Req, Ctx>, Response = Res> + Send + 'static,
B::Layer: Layer<S>,
<<B as Backend<Request<Req, Ctx>, Res>>::Layer as Layer<S>>::Service: Service<Request<Req, Ctx>> + Send + 'static,
<<<B as Backend<Request<Req, Ctx>, Res>>::Layer as Layer<S>>::Service as Service<Request<Req, Ctx>>>::Response: Send + Debug,
<<<B as Backend<Request<Req, Ctx>, Res>>::Layer as Layer<S>>::Service as Service<Request<Req, Ctx>>>::Error: Send + Into<BoxDynError> + Sync,
<<<B as Backend<Request<Req, Ctx>, Res>>::Layer as Layer<S>>::Service as Service<Request<Req, Ctx>>>::Future: Send + 'static,
pub fn new_with_service<S>(
backend: B,
service: S,
) -> (Self, BoxFuture<'static, ()>)where
S: Service<Request<Req, Ctx>, Response = Res> + Send + 'static,
B::Layer: Layer<S>,
<<B as Backend<Request<Req, Ctx>, Res>>::Layer as Layer<S>>::Service: Service<Request<Req, Ctx>> + Send + 'static,
<<<B as Backend<Request<Req, Ctx>, Res>>::Layer as Layer<S>>::Service as Service<Request<Req, Ctx>>>::Response: Send + Debug,
<<<B as Backend<Request<Req, Ctx>, Res>>::Layer as Layer<S>>::Service as Service<Request<Req, Ctx>>>::Error: Send + Into<BoxDynError> + Sync,
<<<B as Backend<Request<Req, Ctx>, Res>>::Layer as Layer<S>>::Service as Service<Request<Req, Ctx>>>::Future: Send + 'static,
Build a new instance provided a custom service
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