From 7f885a232ca916ee29c0b711e163832b0fe25b63 Mon Sep 17 00:00:00 2001 From: Huan-Cheng Chang Date: Fri, 15 Nov 2024 15:50:47 +0000 Subject: [PATCH] test(jstzd): clean up after failed jstzd test --- crates/jstzd/src/task/jstzd.rs | 1 + crates/jstzd/tests/jstzd_test.rs | 32 +++++++++++++++++++------------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/crates/jstzd/src/task/jstzd.rs b/crates/jstzd/src/task/jstzd.rs index 14d77555e..d5794f24b 100644 --- a/crates/jstzd/src/task/jstzd.rs +++ b/crates/jstzd/src/task/jstzd.rs @@ -18,6 +18,7 @@ use serde::Serialize; use std::sync::Arc; use tokio::{net::TcpListener, sync::RwLock, task::JoinHandle}; +#[derive(Clone)] struct Jstzd { octez_node: Arc>, baker: Arc>, diff --git a/crates/jstzd/tests/jstzd_test.rs b/crates/jstzd/tests/jstzd_test.rs index 3aa89b611..538b89b3c 100644 --- a/crates/jstzd/tests/jstzd_test.rs +++ b/crates/jstzd/tests/jstzd_test.rs @@ -25,26 +25,32 @@ async fn jstzd_test() { let rpc_endpoint = Endpoint::localhost(unused_port()); let jstzd_port = unused_port(); let (mut jstzd, config) = create_jstzd_server(&rpc_endpoint, jstzd_port).await; + let mut copy = jstzd.clone(); - jstzd.run().await.unwrap(); - ensure_jstzd_components_are_up(&rpc_endpoint, jstzd_port).await; - let octez_client = OctezClient::new(config.octez_client_config().clone()); - check_bootstrap_contracts(&octez_client).await; + // wrapping the test like this so that we can still call `stop` at the end of the test + // to clean up everything should the test fail and panic + let res = tokio::spawn(async move { + copy.run().await.unwrap(); + ensure_jstzd_components_are_up(&rpc_endpoint, jstzd_port).await; + assert!(copy.health_check().await); - assert!(jstzd.health_check().await); + let octez_client = OctezClient::new(config.octez_client_config().clone()); + check_bootstrap_contracts(&octez_client).await; + fetch_config_test(config, jstzd_port).await; - fetch_config_test(config, jstzd_port).await; - - reqwest::Client::new() - .put(&format!("http://localhost:{}/shutdown", jstzd_port)) - .send() - .await - .unwrap(); + reqwest::Client::new() + .put(&format!("http://localhost:{}/shutdown", jstzd_port)) + .send() + .await + .unwrap(); - ensure_jstzd_components_are_down(&rpc_endpoint, jstzd_port).await; + ensure_jstzd_components_are_down(&rpc_endpoint, jstzd_port).await; + }) + .await; // stop should be idempotent and thus should be okay after jstzd is already stopped jstzd.stop().await.unwrap(); + assert!(res.is_ok()) } async fn create_jstzd_server(