Skip to content

Commit

Permalink
test: added oncecell to empty responses
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosvdr committed Jan 17, 2025
1 parent 014bf64 commit 7930988
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 38 deletions.
43 changes: 23 additions & 20 deletions crates/tap-agent/src/agent/sender_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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())
Expand Down
50 changes: 32 additions & 18 deletions crates/tap-agent/src/agent/sender_accounts_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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};

Expand All @@ -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(
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 7930988

Please sign in to comment.