Skip to content

Commit

Permalink
hide the bitcoind stopper inside the carrier
Browse files Browse the repository at this point in the history
this avoids having exiplict handling of the bitcoind stopper and it being all over the place
when the carrier (by the end of the test) the stopper will get dropped as well which will cause the mock server to shutdown
  • Loading branch information
mariocynicys committed Jun 28, 2024
1 parent c9f82bc commit a15d8f7
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 190 deletions.
69 changes: 34 additions & 35 deletions teos/src/api/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ mod test_helpers {

use crate::api::internal::InternalAPI;
use crate::protos::public_tower_services_server::PublicTowerServicesServer;
use crate::test_utils::{create_api_with_config, ApiConfig, BitcoindStopper};
use crate::test_utils::{create_api_with_config, ApiConfig};

pub(crate) enum RequestBody<'a> {
Jsonify(&'a str),
Expand All @@ -346,8 +346,8 @@ mod test_helpers {

pub(crate) async fn run_tower_in_background_with_config(
api_config: ApiConfig,
) -> (SocketAddr, Arc<InternalAPI>, BitcoindStopper) {
let (internal_rpc_api, bitcoind_stopper) = create_api_with_config(api_config).await;
) -> (SocketAddr, Arc<InternalAPI>) {
let internal_rpc_api = create_api_with_config(api_config).await;
let cloned = internal_rpc_api.clone();

let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
Expand All @@ -361,14 +361,13 @@ mod test_helpers {
.unwrap();
});

(addr, cloned, bitcoind_stopper)
(addr, cloned)
}

pub(crate) async fn run_tower_in_background() -> (SocketAddr, BitcoindStopper) {
let (sock_addr, _, bitcoind_stopper) =
run_tower_in_background_with_config(ApiConfig::default()).await;
pub(crate) async fn run_tower_in_background() -> SocketAddr {
let (sock_addr, _) = run_tower_in_background_with_config(ApiConfig::default()).await;

(sock_addr, bitcoind_stopper)
sock_addr
}

pub(crate) async fn check_api_error(
Expand Down Expand Up @@ -447,7 +446,7 @@ mod tests_failures {

#[tokio::test]
async fn test_no_json_request_body() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let (api_error, status) =
check_api_error(Endpoint::Register, RequestBody::Body(""), server_addr).await;
assert!(api_error.error.contains("EOF while parsing"));
Expand All @@ -457,7 +456,7 @@ mod tests_failures {

#[tokio::test]
async fn test_wrong_json_request_body() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let (api_error, status) = check_api_error(
Endpoint::Register,
RequestBody::DoNotJsonify(""),
Expand All @@ -471,7 +470,7 @@ mod tests_failures {

#[tokio::test]
async fn test_empty_json_request_body() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let (api_error, status) = check_api_error(
Endpoint::Register,
RequestBody::Jsonify(r#"{}"#),
Expand All @@ -485,7 +484,7 @@ mod tests_failures {

#[tokio::test]
async fn test_empty_field() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let (api_error, status) = check_api_error(
Endpoint::Register,
RequestBody::Jsonify(r#"{"user_id": ""}"#),
Expand All @@ -499,7 +498,7 @@ mod tests_failures {

#[tokio::test]
async fn test_wrong_field_hex_encoding_odd() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let (api_error, status) = check_api_error(
Endpoint::Register,
RequestBody::Jsonify(r#"{"user_id": "a"}"#),
Expand All @@ -513,7 +512,7 @@ mod tests_failures {

#[tokio::test]
async fn test_wrong_hex_encoding_character() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let (api_error, status) =
check_api_error(Endpoint::Register,
RequestBody::Jsonify(r#"{"user_id": "022fa2900ed7fc07b4e8ca3ea081e846245b0497944644aa78ea0b994ac22074dZ"}"#),
Expand All @@ -527,7 +526,7 @@ mod tests_failures {

#[tokio::test]
async fn test_wrong_field_size() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let (api_error, status) = check_api_error(
Endpoint::Register,
RequestBody::Jsonify(r#"{"user_id": "aa"}"#),
Expand All @@ -542,7 +541,7 @@ mod tests_failures {

#[tokio::test]
async fn test_wrong_field_type() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let (api_error, status) = check_api_error(
Endpoint::Register,
RequestBody::DoNotJsonify(r#"{"user_id": 1}"#),
Expand All @@ -557,7 +556,7 @@ mod tests_failures {
#[tokio::test]
async fn test_request_missing_field() {
// We'll use a different endpoint here since we need a json object with more than one field
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let (api_error, status) = check_api_error(
Endpoint::AddAppointment,
RequestBody::Jsonify(r#"{"signature": "aa"}"#),
Expand All @@ -573,7 +572,7 @@ mod tests_failures {

#[tokio::test]
async fn test_empty_request_body() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let grpc_conn = PublicTowerServicesClient::connect(format!(
"http://{}:{}",
server_addr.ip(),
Expand All @@ -593,7 +592,7 @@ mod tests_failures {

#[tokio::test]
async fn test_payload_too_large() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let grpc_conn = PublicTowerServicesClient::connect(format!(
"http://{}:{}",
server_addr.ip(),
Expand All @@ -614,7 +613,7 @@ mod tests_failures {

#[tokio::test]
async fn test_wrong_endpoint() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let grpc_conn = PublicTowerServicesClient::connect(format!(
"http://{}:{}",
server_addr.ip(),
Expand All @@ -634,7 +633,7 @@ mod tests_failures {

#[tokio::test]
async fn test_wrong_method() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let grpc_conn = PublicTowerServicesClient::connect(format!(
"http://{}:{}",
server_addr.ip(),
Expand Down Expand Up @@ -671,7 +670,7 @@ mod tests_methods {

#[tokio::test]
async fn test_register() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let response =
request_to_api::<common_msgs::RegisterRequest, common_msgs::RegisterResponse>(
Endpoint::Register,
Expand All @@ -686,7 +685,7 @@ mod tests_methods {

#[tokio::test]
async fn test_register_max_slots() {
let (server_addr, _, _s) =
let (server_addr, _) =
run_tower_in_background_with_config(ApiConfig::new(u32::MAX, DURATION)).await;
let user_id = get_random_user_id();

Expand Down Expand Up @@ -723,7 +722,7 @@ mod tests_methods {

#[tokio::test]
async fn test_register_service_unavailable() {
let (server_addr, _, _s) = run_tower_in_background_with_config(
let (server_addr, _) = run_tower_in_background_with_config(
ApiConfig::new(SLOTS, DURATION).bitcoind_unreachable(),
)
.await;
Expand Down Expand Up @@ -751,7 +750,7 @@ mod tests_methods {

#[tokio::test]
async fn test_add_appointment() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;

// Register first
let (user_sk, user_pk) = cryptography::get_random_keypair();
Expand Down Expand Up @@ -790,7 +789,7 @@ mod tests_methods {

#[tokio::test]
async fn test_add_appointment_non_registered() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let (user_sk, _s) = cryptography::get_random_keypair();
let appointment = generate_dummy_appointment(None).inner;
let signature = cryptography::sign(&appointment.to_vec(), &user_sk).unwrap();
Expand Down Expand Up @@ -818,7 +817,7 @@ mod tests_methods {
#[tokio::test]
async fn test_add_appointment_already_triggered() {
// Get the InternalAPI so we can mess with the inner state
let (server_addr, internal_api, _s) =
let (server_addr, internal_api) =
run_tower_in_background_with_config(ApiConfig::new(u32::MAX, DURATION)).await;

// Register
Expand Down Expand Up @@ -869,7 +868,7 @@ mod tests_methods {

#[tokio::test]
async fn test_add_appointment_service_unavailable() {
let (server_addr, _, _s) = run_tower_in_background_with_config(
let (server_addr, _) = run_tower_in_background_with_config(
ApiConfig::new(SLOTS, DURATION).bitcoind_unreachable(),
)
.await;
Expand Down Expand Up @@ -899,7 +898,7 @@ mod tests_methods {

#[tokio::test]
async fn test_get_appointment() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;

// Register first
let (user_sk, user_pk) = cryptography::get_random_keypair();
Expand Down Expand Up @@ -954,7 +953,7 @@ mod tests_methods {

#[tokio::test]
async fn test_get_appointment_non_registered() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;

// User is not registered
let (user_sk, _) = cryptography::get_random_keypair();
Expand Down Expand Up @@ -987,7 +986,7 @@ mod tests_methods {

#[tokio::test]
async fn test_get_appointment_not_found() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;

// Register first
let (user_sk, user_pk) = cryptography::get_random_keypair();
Expand Down Expand Up @@ -1030,7 +1029,7 @@ mod tests_methods {

#[tokio::test]
async fn test_get_appointment_service_unavailable() {
let (server_addr, _, _s) = run_tower_in_background_with_config(
let (server_addr, _) = run_tower_in_background_with_config(
ApiConfig::new(SLOTS, DURATION).bitcoind_unreachable(),
)
.await;
Expand Down Expand Up @@ -1065,7 +1064,7 @@ mod tests_methods {

#[tokio::test]
async fn test_get_subscription_info() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;

// Register first
let (user_sk, user_pk) = cryptography::get_random_keypair();
Expand Down Expand Up @@ -1101,7 +1100,7 @@ mod tests_methods {

#[tokio::test]
async fn test_get_subscription_info_non_registered() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;

// User is not registered
let (user_sk, _) = cryptography::get_random_keypair();
Expand Down Expand Up @@ -1129,7 +1128,7 @@ mod tests_methods {
#[tokio::test]
async fn test_get_subscription_info_service_unavailable() {
let (user_sk, _) = cryptography::get_random_keypair();
let (server_addr, _, _s) = run_tower_in_background_with_config(
let (server_addr, _) = run_tower_in_background_with_config(
ApiConfig::new(SLOTS, DURATION).bitcoind_unreachable(),
)
.await;
Expand Down
Loading

0 comments on commit a15d8f7

Please sign in to comment.