Carl Lerche ef6d203b47
Create tower-test and include tower-mock. (#237)
tower-mock is deleted in favor of having a single test crate. This crate
also includes a new macro: `assert_request_eq!`.
2019-04-07 20:42:18 -07:00

66 lines
1.5 KiB
Rust

extern crate futures;
extern crate tower_service;
#[macro_use]
extern crate tower_test;
use tower_service::Service;
use tower_test::mock;
use futures::Future;
#[test]
fn single_request_ready() {
let (mut mock, mut handle) = new_mock();
// No pending requests
with_task(|| {
assert!(handle.poll_request().unwrap().is_not_ready());
});
// Issue a request
assert!(mock.poll_ready().unwrap().is_ready());
let mut response = mock.call("hello?".into());
// Get the request from the handle
let send_response = assert_request_eq!(handle, "hello?");
// Response is not ready
with_task(|| {
assert!(response.poll().unwrap().is_not_ready());
});
// Send the response
send_response.send_response("yes?".into());
assert_eq!(response.wait().unwrap().as_str(), "yes?");
}
#[test]
#[should_panic]
fn backpressure() {
let (mut mock, mut handle) = new_mock();
handle.allow(0);
// Make sure the mock cannot accept more requests
with_task(|| {
assert!(mock.poll_ready().unwrap().is_not_ready());
});
// Try to send a request
mock.call("hello?".into());
}
type Mock = mock::Mock<String, String>;
type Handle = mock::Handle<String, String>;
fn new_mock() -> (Mock, Handle) {
mock::pair()
}
// Helper to run some code within context of a task
fn with_task<F: FnOnce() -> U, U>(f: F) -> U {
use futures::future::{lazy, Future};
lazy(|| Ok::<_, ()>(f())).wait().unwrap()
}