Skip to content

Commit f0aad37

Browse files
committed
test(agent): use test isolated db
1 parent 202ddc1 commit f0aad37

File tree

6 files changed

+71
-94
lines changed

6 files changed

+71
-94
lines changed

crates/tap-agent/src/agent.rs

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ use indexer_config::{
3939
Config, EscrowSubgraphConfig, GraphNodeConfig, IndexerConfig, NetworkSubgraphConfig,
4040
SubgraphConfig, SubgraphsConfig, TapConfig,
4141
};
42-
use indexer_monitor::{
43-
empty_escrow_accounts_watcher, escrow_accounts_v1, escrow_accounts_v2, indexer_allocations,
44-
DeploymentDetails, SubgraphClient,
45-
};
42+
use indexer_monitor::{DeploymentDetails, SubgraphClient};
4643
use sender_account::SenderAccountConfig;
4744
use sender_accounts_manager_task::SenderAccountsManagerTask;
4845
use tokio::task::JoinHandle;
@@ -88,9 +85,7 @@ use crate::actor_migrate::TaskHandle;
8885
/// - JoinHandle for the system health monitoring task that triggers shutdown on critical failures
8986
pub async fn start_agent() -> (TaskHandle<SenderAccountsManagerMessage>, JoinHandle<()>) {
9087
let Config {
91-
indexer: IndexerConfig {
92-
indexer_address, ..
93-
},
88+
indexer: IndexerConfig { .. },
9489
graph_node:
9590
GraphNodeConfig {
9691
status_url: graph_node_status_endpoint,
@@ -106,9 +101,9 @@ pub async fn start_agent() -> (TaskHandle<SenderAccountsManagerMessage>, JoinHan
106101
query_url: network_query_url,
107102
query_auth_token: network_query_auth_token,
108103
deployment_id: network_deployment_id,
109-
syncing_interval_secs: network_sync_interval,
104+
..
110105
},
111-
recently_closed_allocation_buffer_secs: recently_closed_allocation_buffer,
106+
..
112107
},
113108
escrow:
114109
EscrowSubgraphConfig {
@@ -117,7 +112,7 @@ pub async fn start_agent() -> (TaskHandle<SenderAccountsManagerMessage>, JoinHan
117112
query_url: escrow_query_url,
118113
query_auth_token: escrow_query_auth_token,
119114
deployment_id: escrow_deployment_id,
120-
syncing_interval_secs: escrow_sync_interval,
115+
..
121116
},
122117
},
123118
},
@@ -151,14 +146,8 @@ pub async fn start_agent() -> (TaskHandle<SenderAccountsManagerMessage>, JoinHan
151146
.await,
152147
));
153148

154-
let _indexer_allocations = indexer_allocations(
155-
network_subgraph,
156-
*indexer_address,
157-
*network_sync_interval,
158-
*recently_closed_allocation_buffer,
159-
)
160-
.await
161-
.expect("Failed to initialize indexer_allocations watcher");
149+
// Note: indexer_allocations watcher is not needed here as SenderAccountsManagerTask
150+
// creates its own PostgreSQL notification listeners for receipt events
162151

163152
let escrow_subgraph = Box::leak(Box::new(
164153
SubgraphClient::new(
@@ -178,15 +167,6 @@ pub async fn start_agent() -> (TaskHandle<SenderAccountsManagerMessage>, JoinHan
178167
.await,
179168
));
180169

181-
let escrow_accounts_v1 = escrow_accounts_v1(
182-
escrow_subgraph,
183-
*indexer_address,
184-
*escrow_sync_interval,
185-
false,
186-
)
187-
.await
188-
.expect("Error creating escrow_accounts channel");
189-
190170
// Determine if we should check for Horizon contracts and potentially enable hybrid mode:
191171
// - If horizon.enabled = false: Pure legacy mode, no Horizon detection
192172
// - If horizon.enabled = true: Check if Horizon contracts are active in the network
@@ -217,30 +197,15 @@ pub async fn start_agent() -> (TaskHandle<SenderAccountsManagerMessage>, JoinHan
217197
false
218198
};
219199

220-
// Create V2 escrow accounts watcher only if Horizon is active
221-
// V2 escrow accounts are in the network subgraph, not a separate TAP v2 subgraph
222-
let escrow_accounts_v2 = if is_horizon_enabled {
223-
escrow_accounts_v2(
224-
network_subgraph,
225-
*indexer_address,
226-
*network_sync_interval,
227-
false,
228-
)
229-
.await
230-
.expect("Error creating escrow_accounts_v2 channel")
231-
} else {
232-
// Create a dummy watcher that never updates for consistency
233-
empty_escrow_accounts_watcher()
234-
};
235-
236-
// In both modes we need both watchers for the hybrid processing
237-
let (_escrow_accounts_v1_final, _escrow_accounts_v2_final) = if is_horizon_enabled {
200+
// Log the TAP Agent mode based on Horizon detection
201+
if is_horizon_enabled {
238202
tracing::info!("TAP Agent: Horizon migration mode - processing existing V1 receipts and new V2 receipts");
239-
(escrow_accounts_v1, escrow_accounts_v2)
240203
} else {
241204
tracing::info!("TAP Agent: Legacy mode - V1 receipts only");
242-
(escrow_accounts_v1, escrow_accounts_v2)
243-
};
205+
}
206+
207+
// Note: escrow_accounts watchers are not needed here as SenderAccountsManagerTask
208+
// handles escrow account monitoring through its own internal mechanisms
244209

245210
let config = Box::leak(Box::new({
246211
let mut config = SenderAccountConfig::from_config(&CONFIG);

crates/tap-agent/src/agent/test_tokio_migration.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ mod tests {
1919
test::{store_receipt, CreateReceipt},
2020
};
2121
use indexer_monitor::{DeploymentDetails, SubgraphClient};
22-
use sqlx::PgPool;
2322
use std::{collections::HashMap, time::Duration};
2423
use tap_core::tap_eip712_domain;
25-
use test_assets::{pgpool, ALLOCATION_ID_0, INDEXER_ADDRESS, TAP_SIGNER, VERIFIER_ADDRESS};
24+
use test_assets::{
25+
setup_shared_test_db, TestDatabase, ALLOCATION_ID_0, INDEXER_ADDRESS, TAP_SIGNER,
26+
VERIFIER_ADDRESS,
27+
};
2628
use thegraph_core::alloy::sol_types::Eip712Domain;
2729
use tokio::time::sleep;
2830
use tracing::{debug, info};
@@ -50,13 +52,12 @@ mod tests {
5052

5153
/// Helper to setup test environment
5254
async fn setup_test_env() -> (
53-
PgPool,
55+
TestDatabase,
5456
LifecycleManager,
5557
&'static SubgraphClient,
5658
&'static SubgraphClient,
5759
) {
58-
let pgpool_future = pgpool();
59-
let pgpool = pgpool_future.await;
60+
let test_db = setup_shared_test_db().await;
6061

6162
let lifecycle = LifecycleManager::new();
6263

@@ -79,13 +80,14 @@ mod tests {
7980
.await,
8081
));
8182

82-
(pgpool, lifecycle, escrow_subgraph, network_subgraph)
83+
(test_db, lifecycle, escrow_subgraph, network_subgraph)
8384
}
8485

8586
/// Test the basic infrastructure setup and receipt storage
8687
#[tokio::test]
8788
async fn test_basic_tokio_infrastructure() {
88-
let (pgpool, _lifecycle, _escrow_subgraph, _network_subgraph) = setup_test_env().await;
89+
let (test_db, _lifecycle, _escrow_subgraph, _network_subgraph) = setup_test_env().await;
90+
let pgpool = test_db.pool.clone();
8991

9092
// Test that we can create and store a receipt
9193
let receipt = Legacy::create_received_receipt(
@@ -116,7 +118,8 @@ mod tests {
116118
/// Test SenderAccountsManagerTask can be spawned
117119
#[tokio::test]
118120
async fn test_sender_accounts_manager_task_spawn() {
119-
let (pgpool, lifecycle, escrow_subgraph, network_subgraph) = setup_test_env().await;
121+
let (test_db, lifecycle, escrow_subgraph, network_subgraph) = setup_test_env().await;
122+
let pgpool = test_db.pool.clone();
120123
let config = create_test_config();
121124
let domain = create_test_eip712_domain();
122125

@@ -144,7 +147,8 @@ mod tests {
144147
/// Test PostgreSQL NOTIFY handling with tokio implementation
145148
#[tokio::test]
146149
async fn test_postgres_notify_handling_tokio() {
147-
let (pgpool, _lifecycle, _escrow_subgraph, _network_subgraph) = setup_test_env().await;
150+
let (test_db, _lifecycle, _escrow_subgraph, _network_subgraph) = setup_test_env().await;
151+
let pgpool = test_db.pool.clone();
148152

149153
// Start a separate PgListener to monitor notifications
150154
let mut listener = sqlx::postgres::PgListener::connect_with(&pgpool)
@@ -176,7 +180,7 @@ mod tests {
176180
/// Test that tasks can be created and managed
177181
#[tokio::test]
178182
async fn test_task_lifecycle_management() {
179-
let (_pgpool, _lifecycle, _escrow_subgraph, _network_subgraph) = setup_test_env().await;
183+
let (_test_db, _lifecycle, _escrow_subgraph, _network_subgraph) = setup_test_env().await;
180184

181185
// Test that we can track task lifecycle
182186
// This is a basic test of the lifecycle management infrastructure
@@ -194,7 +198,8 @@ mod tests {
194198
/// Test the "Missing allocation was not closed yet" regression scenario
195199
#[tokio::test]
196200
async fn test_missing_allocation_regression_basic() {
197-
let (pgpool, _lifecycle, _escrow_subgraph, _network_subgraph) = setup_test_env().await;
201+
let (test_db, _lifecycle, _escrow_subgraph, _network_subgraph) = setup_test_env().await;
202+
let pgpool = test_db.pool.clone();
198203

199204
// Create multiple receipts for the same allocation
200205
// This simulates the scenario that could trigger the "missing allocation" issue
@@ -231,7 +236,8 @@ mod tests {
231236
/// Test graceful shutdown behavior
232237
#[tokio::test]
233238
async fn test_graceful_shutdown_preparation() {
234-
let (pgpool, _lifecycle, _escrow_subgraph, _network_subgraph) = setup_test_env().await;
239+
let (test_db, _lifecycle, _escrow_subgraph, _network_subgraph) = setup_test_env().await;
240+
let pgpool = test_db.pool.clone();
235241

236242
// Create some test data
237243
let receipt =

crates/tap-agent/src/agent/test_tokio_regression.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ mod tests {
1919
test::{store_receipt, CreateReceipt},
2020
};
2121
use indexer_monitor::{DeploymentDetails, SubgraphClient};
22-
use sqlx::PgPool;
2322
use std::{collections::HashMap, time::Duration};
2423
use tap_core::tap_eip712_domain;
2524
use test_assets::{
26-
pgpool, ALLOCATION_ID_0, ALLOCATION_ID_1, INDEXER_ADDRESS, TAP_SIGNER, VERIFIER_ADDRESS,
25+
setup_shared_test_db, TestDatabase, ALLOCATION_ID_0, ALLOCATION_ID_1, INDEXER_ADDRESS,
26+
TAP_SIGNER, VERIFIER_ADDRESS,
2727
};
2828
use thegraph_core::alloy::{hex::ToHexExt, sol_types::Eip712Domain};
2929
use tokio::time::sleep;
@@ -52,13 +52,12 @@ mod tests {
5252

5353
/// Helper to setup test environment
5454
async fn setup_test_env() -> (
55-
PgPool,
55+
TestDatabase,
5656
LifecycleManager,
5757
&'static SubgraphClient,
5858
&'static SubgraphClient,
5959
) {
60-
let pgpool_future = pgpool();
61-
let pgpool = pgpool_future.await;
60+
let test_db = setup_shared_test_db().await;
6261

6362
let lifecycle = LifecycleManager::new();
6463

@@ -81,14 +80,15 @@ mod tests {
8180
.await,
8281
));
8382

84-
(pgpool, lifecycle, escrow_subgraph, network_subgraph)
83+
(test_db, lifecycle, escrow_subgraph, network_subgraph)
8584
}
8685

8786
/// Test that single allocation receipt processing works consistently
8887
/// This was a core functionality in ractor that must work in tokio
8988
#[tokio::test]
9089
async fn test_single_allocation_receipt_processing_regression() {
91-
let (pgpool, lifecycle, escrow_subgraph, network_subgraph) = setup_test_env().await;
90+
let (test_db, lifecycle, escrow_subgraph, network_subgraph) = setup_test_env().await;
91+
let pgpool = test_db.pool.clone();
9292
let config = create_test_config();
9393
let domain = create_test_eip712_domain();
9494

@@ -160,7 +160,8 @@ mod tests {
160160
/// This tests the scenario that could trigger "Missing allocation was not closed yet"
161161
#[tokio::test]
162162
async fn test_multiple_allocation_interleaved_receipts_regression() {
163-
let (pgpool, lifecycle, escrow_subgraph, network_subgraph) = setup_test_env().await;
163+
let (test_db, lifecycle, escrow_subgraph, network_subgraph) = setup_test_env().await;
164+
let pgpool = test_db.pool.clone();
164165
let config = create_test_config();
165166
let domain = create_test_eip712_domain();
166167

@@ -257,7 +258,8 @@ mod tests {
257258
/// This tests the system's ability to handle high-throughput scenarios
258259
#[tokio::test]
259260
async fn test_rapid_receipt_burst_regression() {
260-
let (pgpool, lifecycle, escrow_subgraph, network_subgraph) = setup_test_env().await;
261+
let (test_db, lifecycle, escrow_subgraph, network_subgraph) = setup_test_env().await;
262+
let pgpool = test_db.pool.clone();
261263
let config = create_test_config();
262264
let domain = create_test_eip712_domain();
263265

@@ -333,7 +335,8 @@ mod tests {
333335
/// This ensures the tokio implementation properly manages task lifecycles
334336
#[tokio::test]
335337
async fn test_task_lifecycle_regression() {
336-
let (pgpool, lifecycle, escrow_subgraph, network_subgraph) = setup_test_env().await;
338+
let (test_db, lifecycle, escrow_subgraph, network_subgraph) = setup_test_env().await;
339+
let pgpool = test_db.pool.clone();
337340
let config = create_test_config();
338341
let domain = create_test_eip712_domain();
339342

@@ -388,7 +391,8 @@ mod tests {
388391
/// This ensures the tokio implementation handles errors gracefully
389392
#[tokio::test]
390393
async fn test_error_resilience_regression() {
391-
let (pgpool, lifecycle, escrow_subgraph, network_subgraph) = setup_test_env().await;
394+
let (test_db, lifecycle, escrow_subgraph, network_subgraph) = setup_test_env().await;
395+
let pgpool = test_db.pool.clone();
392396
let config = create_test_config();
393397
let domain = create_test_eip712_domain();
394398

crates/tap-agent/tests/sender_account_manager_test.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ use indexer_tap_agent::{
1919
test::{store_receipt, CreateReceipt},
2020
};
2121
use serde_json::json;
22-
use sqlx::PgPool;
2322
use tap_core::tap_eip712_domain;
2423
use test_assets::{
25-
pgpool, ALLOCATION_ID_0, INDEXER_ADDRESS, TAP_SIGNER as SIGNER, VERIFIER_ADDRESS,
24+
setup_shared_test_db, TestDatabase, ALLOCATION_ID_0, INDEXER_ADDRESS, TAP_SIGNER as SIGNER,
25+
VERIFIER_ADDRESS,
2626
};
2727
use thegraph_core::alloy::{hex::ToHexExt, sol_types::Eip712Domain};
2828
use tokio::time::sleep;
@@ -57,14 +57,13 @@ fn create_test_config() -> &'static SenderAccountConfig {
5757

5858
/// Helper to setup test environment with mock subgraphs
5959
async fn setup_test_env_with_mocks() -> (
60-
PgPool,
60+
TestDatabase,
6161
MockServer,
6262
MockServer,
6363
&'static SubgraphClient,
6464
&'static SubgraphClient,
6565
) {
66-
let pgpool_future = pgpool();
67-
let pgpool = pgpool_future.await;
66+
let test_db = setup_shared_test_db().await;
6867

6968
// Setup mock network subgraph
7069
let mock_network_subgraph_server = MockServer::start().await;
@@ -121,7 +120,7 @@ async fn setup_test_env_with_mocks() -> (
121120
));
122121

123122
(
124-
pgpool,
123+
test_db,
125124
mock_network_subgraph_server,
126125
mock_escrow_subgraph_server,
127126
network_subgraph,
@@ -133,8 +132,9 @@ async fn setup_test_env_with_mocks() -> (
133132
/// This test verifies the full flow from receipt processing to RAV generation
134133
#[tokio::test]
135134
async fn tokio_sender_account_manager_layer_test() {
136-
let (pgpool, _mock_network, _mock_escrow, network_subgraph, escrow_subgraph) =
135+
let (test_db, _mock_network, _mock_escrow, network_subgraph, escrow_subgraph) =
137136
setup_test_env_with_mocks().await;
137+
let pgpool = test_db.pool.clone();
138138
let config = create_test_config();
139139
let domain = create_test_eip712_domain();
140140
let lifecycle = LifecycleManager::new();

crates/tap-agent/tests/sender_account_test.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ use indexer_tap_agent::{
1919
test::{store_receipt, CreateReceipt},
2020
};
2121
use serde_json::json;
22-
use sqlx::PgPool;
2322
use tap_core::tap_eip712_domain;
2423
use test_assets::{
25-
pgpool, ALLOCATION_ID_0, INDEXER_ADDRESS, TAP_SIGNER as SIGNER, VERIFIER_ADDRESS,
24+
setup_shared_test_db, TestDatabase, ALLOCATION_ID_0, INDEXER_ADDRESS, TAP_SIGNER as SIGNER,
25+
VERIFIER_ADDRESS,
2626
};
2727
use thegraph_core::alloy::{hex::ToHexExt, sol_types::Eip712Domain};
2828
use tokio::time::sleep;
@@ -57,14 +57,13 @@ fn create_test_config() -> &'static SenderAccountConfig {
5757

5858
/// Helper to setup test environment with mock subgraphs
5959
async fn setup_test_env() -> (
60-
PgPool,
60+
TestDatabase,
6161
MockServer,
6262
MockServer,
6363
&'static SubgraphClient,
6464
&'static SubgraphClient,
6565
) {
66-
let pgpool_future = pgpool();
67-
let pgpool = pgpool_future.await;
66+
let test_db = setup_shared_test_db().await;
6867

6968
// Setup mock network subgraph
7069
let mock_network_server = MockServer::start().await;
@@ -119,7 +118,7 @@ async fn setup_test_env() -> (
119118
));
120119

121120
(
122-
pgpool,
121+
test_db,
123122
mock_network_server,
124123
mock_escrow_server,
125124
network_subgraph,
@@ -131,8 +130,9 @@ async fn setup_test_env() -> (
131130
/// This test verifies allocation lifecycle management and RAV generation
132131
#[tokio::test]
133132
async fn tokio_sender_account_layer_test() {
134-
let (pgpool, _mock_network, _mock_escrow, network_subgraph, escrow_subgraph) =
133+
let (test_db, _mock_network, _mock_escrow, network_subgraph, escrow_subgraph) =
135134
setup_test_env().await;
135+
let pgpool = test_db.pool.clone();
136136
let config = create_test_config();
137137
let domain = create_test_eip712_domain();
138138
let lifecycle = LifecycleManager::new();

0 commit comments

Comments
 (0)