Skip to content

Commit

Permalink
test: add use of restest crate to reduce boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
uriel-starkware committed Jul 10, 2024
1 parent 6dac4d9 commit ee5144a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/mempool_infra/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ workspace = true
async-trait.workspace = true
bincode.workspace = true
hyper.workspace = true
rstest.workspace = true
serde.workspace = true
thiserror.workspace = true
tokio.workspace = true
Expand Down
48 changes: 27 additions & 21 deletions crates/mempool_infra/tests/component_server_client_http_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use common::{
};
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request, Response, Server, StatusCode};
use rstest::rstest;
use serde::Serialize;
use starknet_mempool_infra::component_client::ComponentClientHttp;
use starknet_mempool_infra::component_definitions::{ComponentRequestHandler, ServerError};
Expand All @@ -27,6 +28,7 @@ const B_PORT: u16 = 10001;
const UNCONNECTED_SERVER_PORT: u16 = 10002;
const FAULTY_SERVER_REQ_DESER_PORT: u16 = 10003;
const FAULTY_SERVER_RES_DESER_PORT: u16 = 10004;
const MOCK_SERVER_ERROR: &str = "mock server error";

#[async_trait]
impl ComponentAClientTrait for ComponentClientHttp<ComponentARequest, ComponentAResponse> {
Expand Down Expand Up @@ -83,7 +85,7 @@ fn assert_error_contains_keywords(error: String, expected_error_contained_keywor
}
}

async fn spawn_faulty_server<T>(ip: IpAddr, port: u16, body: T)
async fn create_client_and_faulty_server<T>(port: u16, body: T) -> ComponentAClient
where
T: Serialize + Send + Sync + 'static + Clone,
{
Expand All @@ -98,7 +100,7 @@ where
.unwrap())
}

let socket = SocketAddr::new(ip, port);
let socket = SocketAddr::new(LOCAL_IP, port);
let make_svc = make_service_fn(|_conn| {
let body = body.clone();
async move { Ok::<_, hyper::Error>(service_fn(move |req| handler(req, body.clone()))) }
Expand All @@ -108,6 +110,8 @@ where

// Ensure the server starts running.
task::yield_now().await;

ComponentAClient::new(LOCAL_IP, port)
}

#[tokio::test]
Expand Down Expand Up @@ -151,28 +155,30 @@ async fn test_unconnected_server() {
let client = ComponentAClient::new(LOCAL_IP, UNCONNECTED_SERVER_PORT);

let expected_error_contained_keywords = vec!["Connection refused"];
verify_error(client.clone(), expected_error_contained_keywords).await;
}

#[tokio::test]
async fn test_faulty_server_request_deseralization_failure() {
let mock_server_error = "Mock server error";
let returned_server_error =
ServerError::RequestDeserializationFailure(mock_server_error.to_string());

spawn_faulty_server(LOCAL_IP, FAULTY_SERVER_REQ_DESER_PORT, returned_server_error).await;
let client = ComponentAClient::new(LOCAL_IP, FAULTY_SERVER_REQ_DESER_PORT);

let expected_error_contained_keywords =
vec![StatusCode::BAD_REQUEST.as_str(), mock_server_error];
verify_error(client, expected_error_contained_keywords).await;
}

#[rstest]
#[case::request_deserialization_failure(
create_client_and_faulty_server(
FAULTY_SERVER_REQ_DESER_PORT,
ServerError::RequestDeserializationFailure(MOCK_SERVER_ERROR.to_string())
).await,
vec![
StatusCode::BAD_REQUEST.as_str(),
"Could not deserialize client request",
MOCK_SERVER_ERROR
],
)]
#[case::response_deserialization_failure(
create_client_and_faulty_server(FAULTY_SERVER_RES_DESER_PORT, "arbitrary data").await,
vec!["Could not deserialize server response"],
)]
#[tokio::test]
async fn test_faulty_server_response_deseralization_failure() {
spawn_faulty_server(LOCAL_IP, FAULTY_SERVER_RES_DESER_PORT, "arbitrary data").await;
let client = ComponentAClient::new(LOCAL_IP, FAULTY_SERVER_RES_DESER_PORT);

let expected_error_contained_keywords = vec!["Could not deserialize server response"];
async fn test_faulty_server(
#[case] client: ComponentAClient,
#[case] expected_error_contained_keywords: Vec<&str>,
) {
verify_error(client, expected_error_contained_keywords).await;
}

0 comments on commit ee5144a

Please sign in to comment.