diff --git a/crates/tap-agent/src/agent/sender_account.rs b/crates/tap-agent/src/agent/sender_account.rs index cf1a135e8..76c82497e 100644 --- a/crates/tap-agent/src/agent/sender_account.rs +++ b/crates/tap-agent/src/agent/sender_account.rs @@ -1133,8 +1133,7 @@ pub mod tests { const RETRY_DURATION: Duration = Duration::from_millis(1000); lazy_static! { - static ref MOCK_EMPTY_ESCROW_SUBGRAPH: Mutex<Option<Arc<(MockServer, MockGuard)>>> = - Mutex::new(None); + static ref MOCK_EMPTY_ESCROW_SUBGRAPH: OnceCell<Arc<(MockServer, MockGuard)>> = OnceCell::new(); static ref MOCK_ESCROW_SUBGRAPH: OnceCell<Arc<(MockServer, MockGuard)>> = OnceCell::new(); } @@ -1164,20 +1163,25 @@ pub mod tests { } - async fn mock_escrow_subgraph_empty_response() -> (MockServer, MockGuard) { +async fn mock_escrow_subgraph_empty_response() -> Arc<(MockServer, MockGuard)> { + MOCK_EMPTY_ESCROW_SUBGRAPH + .get_or_try_init(|| async{ let mock_ecrow_subgraph_server: MockServer = MockServer::start().await; - let _mock_ecrow_subgraph = mock_ecrow_subgraph_server - .register_as_scoped( - Mock::given(method("POST")) - .and(body_string_contains("TapTransactions")) - .respond_with(ResponseTemplate::new(200).set_body_json(json!({ "data": { - "transactions": [], - } - }))), - ) - .await; - (mock_ecrow_subgraph_server, _mock_ecrow_subgraph) - } + let _mock_ecrow_subgraph = mock_ecrow_subgraph_server + .register_as_scoped( + Mock::given(method("POST")) + .and(body_string_contains("TapTransactions")) + .respond_with(ResponseTemplate::new(200).set_body_json(json!({ "data": { + "transactions": [], + } + }))), + ) + .await; + Ok::<_, anyhow::Error>(Arc::new((mock_ecrow_subgraph_server, _mock_ecrow_subgraph))) + }).await.unwrap().clone() + + +} #[allow(clippy::too_many_arguments)] async fn create_sender_account( @@ -1355,8 +1359,8 @@ pub mod tests { #[sqlx::test(migrations = "../../migrations")] async fn test_rav_marked_as_last_closing_sender_allocation(pgpool: PgPool) { - // Start a TAP aggregator server. - let (handle, aggregator_endpoint) = run_server( + // Start a TAP aggregator server. + let (handle, aggregator_endpoint) = run_server( 0, SIGNER.0.clone(), vec![SIGNER.1].into_iter().collect(), @@ -1388,9 +1392,8 @@ pub mod tests { ) .await; - let (mock_escrow_subgraph_server, _escrow_subgraph_guard) = - mock_escrow_subgraph_empty_response().await; - + let server_ref = mock_escrow_subgraph_empty_response().await; + let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = &*server_ref; let receipt = create_received_receipt(&ALLOCATION_ID_0, &SIGNER.0, 1, 1, TRIGGER_VALUE - 100); store_receipt(&pgpool, receipt.signed_receipt()) diff --git a/crates/tap-agent/src/agent/sender_accounts_manager.rs b/crates/tap-agent/src/agent/sender_accounts_manager.rs index a5b7e891e..96d7e38aa 100644 --- a/crates/tap-agent/src/agent/sender_accounts_manager.rs +++ b/crates/tap-agent/src/agent/sender_accounts_manager.rs @@ -611,7 +611,10 @@ mod tests { use sqlx::{postgres::PgListener, PgPool}; use test_assets::{flush_messages, TAP_SENDER as SENDER}; use thegraph_core::alloy::hex::ToHexExt; - use tokio::sync::{mpsc, mpsc::error::TryRecvError, watch, Notify}; + use tokio::sync::{ + mpsc::{self, error::TryRecvError}, + watch, Notify, OnceCell, + }; use super::{ new_receipts_watcher, SenderAccountsManager, SenderAccountsManagerArgs, @@ -633,8 +636,8 @@ mod tests { use std::str::FromStr; + use crate::lazy_static; use tap_aggregator::server::run_server; - use wiremock::matchers::{body_string_contains, method}; use wiremock::{Mock, MockGuard, MockServer, ResponseTemplate}; @@ -646,21 +649,32 @@ mod tests { const ESCROW_POLLING_INTERVAL: Duration = Duration::from_secs(30); const TAP_SENDER_TIMEOUT: Duration = Duration::from_secs(63); - async fn mock_escrow_subgraph_empty_response() -> (MockServer, MockGuard) { - let mock_ecrow_subgraph_server: MockServer = MockServer::start().await; - let _mock_ecrow_subgraph = mock_ecrow_subgraph_server - .register_as_scoped( - Mock::given(method("POST")) - //.and(body_string_contains("TapTransactions")) - .respond_with(ResponseTemplate::new(200).set_body_json(json!({ "data": { - "transactions": [], - } - }))), - ) - .await; - (mock_ecrow_subgraph_server, _mock_ecrow_subgraph) + lazy_static! { + static ref MOCK_EMPTY_ESCROW_SUBGRAPH: OnceCell<Arc<(MockServer, MockGuard)>> = + OnceCell::new(); + } + async fn mock_escrow_subgraph_empty_response() -> Arc<(MockServer, MockGuard)> { + MOCK_EMPTY_ESCROW_SUBGRAPH + .get_or_try_init(|| async { + let mock_ecrow_subgraph_server: MockServer = MockServer::start().await; + let _mock_ecrow_subgraph = mock_ecrow_subgraph_server + .register_as_scoped( + Mock::given(method("POST")) + .and(body_string_contains("TapTransactions")) + .respond_with(ResponseTemplate::new(200).set_body_json( + json!({ "data": { + "transactions": [], + } + }), + )), + ) + .await; + Ok::<_, anyhow::Error>(Arc::new((mock_ecrow_subgraph_server, _mock_ecrow_subgraph))) + }) + .await + .unwrap() + .clone() } - async fn get_subgraph_client(url: Option<&str>) -> &'static SubgraphClient { Box::leak(Box::new( SubgraphClient::new( @@ -877,8 +891,8 @@ mod tests { ) .await; - let (mock_escrow_subgraph_server, _escrow_subgraph_guard) = - mock_escrow_subgraph_empty_response().await; + let server_ref = mock_escrow_subgraph_empty_response().await; + let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = &*server_ref; let (prefix, notify, (actor, join_handle)) = create_sender_accounts_manager( pgpool.clone(),