Add a check method to the Transcript.

This commit is contained in:
Henry de Valence 2020-06-22 23:06:52 -07:00
parent 5c9cd52baf
commit e8d42264e8
2 changed files with 35 additions and 2 deletions

View File

@ -1,12 +1,12 @@
//! A [`Service`](tower::Service) implementation based on a fixed transcript.
use color_eyre::eyre::{eyre, Report};
use color_eyre::eyre::{ensure, eyre, Report};
use futures::future::{ready, Ready};
use std::{
fmt::Debug,
task::{Context, Poll},
};
use tower::Service;
use tower::{Service, ServiceExt};
pub struct Transcript<R, S, I>
where
@ -24,6 +24,32 @@ where
}
}
impl<R, S, I> Transcript<R, S, I>
where
I: Iterator<Item = (R, S)>,
R: Debug,
S: Debug + Eq,
{
pub async fn check<C>(mut self, mut to_check: C) -> Result<(), Report>
where
C: Service<R, Response = S>,
C::Error: Debug,
{
while let Some((req, expected_rsp)) = self.messages.next() {
// These unwraps could propagate errors with the correct
// bound on C::Error
let rsp = to_check.ready_and().await.unwrap().call(req).await.unwrap();
ensure!(
rsp == expected_rsp,
"Expected {:?}, got {:?}",
expected_rsp,
rsp
);
}
Ok(())
}
}
impl<R, S, I> Service<R> for Transcript<R, S, I>
where
R: Debug + Eq,

View File

@ -32,3 +32,10 @@ async fn transcript_errors_wrong_request() {
);
assert!(svc.ready_and().await.unwrap().call("bad").await.is_err());
}
#[tokio::test]
async fn self_check() {
let t1 = Transcript::from(TRANSCRIPT_DATA.iter().cloned());
let t2 = Transcript::from(TRANSCRIPT_DATA.iter().cloned());
assert!(t1.check(t2).await.is_ok());
}