Skip to content

Commit

Permalink
Fix doctests and replace some confusing example requests
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed May 31, 2023
1 parent c7702fe commit ca23d02
Showing 1 changed file with 43 additions and 25 deletions.
68 changes: 43 additions & 25 deletions zebra-test/src/mock_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,14 +742,18 @@ impl<Request, Response, Error> ResponseSender<Request, Response, Error> {
///
/// # Panics
///
/// If one of the `respond*` methods isn't called, the caller will panic.
/// If one of the `respond*` methods isn't called, the [`MockService`] might panic with a
/// timeout error.
///
/// # Example
///
/// ```
/// # use zebra_test::mock_service::MockService;
/// # use tower::{Service, ServiceExt};
/// #
/// # #[derive(Debug, PartialEq, Eq)]
/// # struct Request;
/// #
/// # let reactor = tokio::runtime::Builder::new_current_thread()
/// # .enable_all()
/// # .build()
Expand All @@ -762,19 +766,19 @@ impl<Request, Response, Error> ResponseSender<Request, Response, Error> {
///
/// # let mut service = mock_service.clone();
/// # let task = tokio::spawn(async move {
/// # let first_call_result = (&mut service).oneshot(1).await;
/// # let second_call_result = service.oneshot(1).await;
/// # let first_call_result = (&mut service).oneshot(Request).await;
/// # let second_call_result = service.oneshot(Request).await;
/// #
/// # (first_call_result, second_call_result)
/// # });
/// #
/// mock_service
/// .expect_request(1)
/// .expect_request(Request)
/// .await
/// .respond("Received one".to_owned());
/// .respond("Received Request".to_owned());
///
/// mock_service
/// .expect_request(1)
/// .expect_request(Request)
/// .await
/// .respond(Err("Duplicate request"));
/// # });
Expand All @@ -793,14 +797,18 @@ impl<Request, Response, Error> ResponseSender<Request, Response, Error> {
///
/// # Panics
///
/// If one of the `respond*` methods isn't called, the caller will panic.
/// If one of the `respond*` methods isn't called, the [`MockService`] might panic with a
/// timeout error.
///
/// # Example
///
/// ```
/// # use zebra_test::mock_service::MockService;
/// # use tower::{Service, ServiceExt};
/// #
/// # #[derive(Debug, PartialEq, Eq)]
/// # struct Request;
/// #
/// # let reactor = tokio::runtime::Builder::new_current_thread()
/// # .enable_all()
/// # .build()
Expand All @@ -813,21 +821,21 @@ impl<Request, Response, Error> ResponseSender<Request, Response, Error> {
///
/// # let mut service = mock_service.clone();
/// # let task = tokio::spawn(async move {
/// # let first_call_result = (&mut service).oneshot(1).await;
/// # let second_call_result = service.oneshot(1).await;
/// # let first_call_result = (&mut service).oneshot(Request).await;
/// # let second_call_result = service.oneshot(Request).await;
/// #
/// # (first_call_result, second_call_result)
/// # });
/// #
/// mock_service
/// .expect_request(1)
/// .expect_request(Request)
/// .await
/// .respond_with(|req| format!("Received: {}", req));
/// .respond_with(|req| format!("Received: {req:?}"));
///
/// mock_service
/// .expect_request(1)
/// .expect_request(Request)
/// .await
/// .respond_with(|req| Err(format!("Duplicate request: {}", req)));
/// .respond_with(|req| Err(format!("Duplicate request: {req:?}")));
/// # });
/// ```
pub fn respond_with<F, R>(self, response_fn: F)
Expand All @@ -849,36 +857,41 @@ impl<Request, Response, Error> ResponseSender<Request, Response, Error> {
///
/// # Panics
///
/// If one of the `respond*` methods isn't called, the caller will panic.
/// If one of the `respond*` methods isn't called, the [`MockService`] might panic with a
/// timeout error.
///
/// # Example
///
/// ```
/// # use zebra_test::mock_service::MockService;
/// # use tower::{Service, ServiceExt};
/// #
/// # #[derive(Debug, PartialEq, Eq)]
/// # struct Request;
/// # struct Response;
/// #
/// # let reactor = tokio::runtime::Builder::new_current_thread()
/// # .enable_all()
/// # .build()
/// # .expect("Failed to build Tokio runtime");
/// #
/// # reactor.block_on(async {
/// // Mock a service with a `String` as the service `Error` type.
/// let mut mock_service: MockService<_, _, _, String> =
/// let mut mock_service: MockService<Request, Response, _, String> =
/// MockService::build().for_unit_tests();
///
/// # let mut service = mock_service.clone();
/// # let task = tokio::spawn(async move {
/// # let first_call_result = (&mut service).oneshot(1).await;
/// # let second_call_result = service.oneshot(1).await;
/// # let first_call_result = (&mut service).oneshot(Request).await;
/// # let second_call_result = service.oneshot(Request).await;
/// #
/// # (first_call_result, second_call_result)
/// # });
/// #
/// mock_service
/// .expect_request(1)
/// .expect_request(Request)
/// .await
/// .respond_error("Duplicate request");
/// .respond_error("Duplicate request".to_string());
/// # });
/// ```
pub fn respond_error(self, error: Error) {
Expand All @@ -897,36 +910,41 @@ impl<Request, Response, Error> ResponseSender<Request, Response, Error> {
///
/// # Panics
///
/// If one of the `respond*` methods isn't called, the caller will panic.
/// If one of the `respond*` methods isn't called, the [`MockService`] might panic with a
/// timeout error.
///
/// # Example
///
/// ```
/// # use zebra_test::mock_service::MockService;
/// # use tower::{Service, ServiceExt};
/// #
/// # #[derive(Debug, PartialEq, Eq)]
/// # struct Request;
/// # struct Response;
/// #
/// # let reactor = tokio::runtime::Builder::new_current_thread()
/// # .enable_all()
/// # .build()
/// # .expect("Failed to build Tokio runtime");
/// #
/// # reactor.block_on(async {
/// // Mock a service with a `String` as the service `Error` type.
/// let mut mock_service: MockService<_, _, _, String> =
/// let mut mock_service: MockService<Request, Response, _, String> =
/// MockService::build().for_unit_tests();
///
/// # let mut service = mock_service.clone();
/// # let task = tokio::spawn(async move {
/// # let first_call_result = (&mut service).oneshot(1).await;
/// # let second_call_result = service.oneshot(1).await;
/// # let first_call_result = (&mut service).oneshot(Request).await;
/// # let second_call_result = service.oneshot(Request).await;
/// #
/// # (first_call_result, second_call_result)
/// # });
/// #
/// mock_service
/// .expect_request(1)
/// .expect_request(Request)
/// .await
/// .respond_with_error(|req| format!("Duplicate request: {}", req));
/// .respond_with_error(|req| format!("Duplicate request: {req:?}"));
/// # });
/// ```
pub fn respond_with_error<F>(self, response_fn: F)
Expand Down

0 comments on commit ca23d02

Please sign in to comment.