Add a Transcript Service for testing.
This commit is contained in:
parent
a9efb8715e
commit
5c9cd52baf
|
|
@ -2403,8 +2403,12 @@ dependencies = [
|
||||||
name = "zebra-test"
|
name = "zebra-test"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"color-eyre",
|
||||||
|
"futures",
|
||||||
"hex",
|
"hex",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
|
"tokio",
|
||||||
|
"tower",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
||||||
|
|
@ -10,3 +10,9 @@ edition = "2018"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
hex = "0.4.2"
|
hex = "0.4.2"
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
|
tower = "0.3.1"
|
||||||
|
futures = "0.3.5"
|
||||||
|
color-eyre = "0.5"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
tokio = { version = "0.2", features = ["full"] }
|
||||||
|
|
@ -1 +1,4 @@
|
||||||
|
//! Miscellaneous test code for Zebra.
|
||||||
|
|
||||||
|
pub mod transcript;
|
||||||
pub mod vectors;
|
pub mod vectors;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,55 @@
|
||||||
|
//! A [`Service`](tower::Service) implementation based on a fixed transcript.
|
||||||
|
|
||||||
|
use color_eyre::eyre::{eyre, Report};
|
||||||
|
use futures::future::{ready, Ready};
|
||||||
|
use std::{
|
||||||
|
fmt::Debug,
|
||||||
|
task::{Context, Poll},
|
||||||
|
};
|
||||||
|
use tower::Service;
|
||||||
|
|
||||||
|
pub struct Transcript<R, S, I>
|
||||||
|
where
|
||||||
|
I: Iterator<Item = (R, S)>,
|
||||||
|
{
|
||||||
|
messages: I,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R, S, I> From<I> for Transcript<R, S, I>
|
||||||
|
where
|
||||||
|
I: Iterator<Item = (R, S)>,
|
||||||
|
{
|
||||||
|
fn from(messages: I) -> Self {
|
||||||
|
Self { messages }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R, S, I> Service<R> for Transcript<R, S, I>
|
||||||
|
where
|
||||||
|
R: Debug + Eq,
|
||||||
|
I: Iterator<Item = (R, S)>,
|
||||||
|
{
|
||||||
|
type Response = S;
|
||||||
|
type Error = Report;
|
||||||
|
type Future = Ready<Result<S, Report>>;
|
||||||
|
|
||||||
|
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
|
Poll::Ready(Ok(()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call(&mut self, request: R) -> Self::Future {
|
||||||
|
if let Some((expected_request, response)) = self.messages.next() {
|
||||||
|
if request == expected_request {
|
||||||
|
ready(Ok(response))
|
||||||
|
} else {
|
||||||
|
ready(Err(eyre!(
|
||||||
|
"Expected {:?}, got {:?}",
|
||||||
|
expected_request,
|
||||||
|
request
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ready(Err(eyre!("Got request after transcript ended")))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
use tower::{Service, ServiceExt};
|
||||||
|
|
||||||
|
use zebra_test::transcript::Transcript;
|
||||||
|
|
||||||
|
const TRANSCRIPT_DATA: [(&'static str, &'static str); 4] = [
|
||||||
|
("req1", "rsp1"),
|
||||||
|
("req2", "rsp2"),
|
||||||
|
("req3", "rsp3"),
|
||||||
|
("req4", "rsp4"),
|
||||||
|
];
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn transcript_returns_responses_and_ends() {
|
||||||
|
let mut svc = Transcript::from(TRANSCRIPT_DATA.iter().cloned());
|
||||||
|
|
||||||
|
for (req, rsp) in TRANSCRIPT_DATA.iter() {
|
||||||
|
assert_eq!(
|
||||||
|
svc.ready_and().await.unwrap().call(req).await.unwrap(),
|
||||||
|
*rsp,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
assert!(svc.ready_and().await.unwrap().call("end").await.is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn transcript_errors_wrong_request() {
|
||||||
|
let mut svc = Transcript::from(TRANSCRIPT_DATA.iter().cloned());
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
svc.ready_and().await.unwrap().call("req1").await.unwrap(),
|
||||||
|
"rsp1",
|
||||||
|
);
|
||||||
|
assert!(svc.ready_and().await.unwrap().call("bad").await.is_err());
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue