Skip to content

Commit 7cd154c

Browse files
committed
test: add rstest and fixtures
1 parent 387d660 commit 7cd154c

File tree

10 files changed

+506
-202
lines changed

10 files changed

+506
-202
lines changed

Cargo.lock

Lines changed: 243 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/monitor/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ env_logger = { version = "0.11.0", default-features = false }
2525
test-log = { version = "0.2.12", default-features = false }
2626
wiremock.workspace = true
2727
test-assets = { path = "../test-assets" }
28+
rstest = "0.24.0"
2829
test-with = "0.14.6"

crates/monitor/src/client/monitor.rs

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pub async fn check_deployment_status(
6262

6363
#[cfg(test)]
6464
mod tests {
65+
use axum::error_handling::future;
6566
use reqwest::Url;
6667
use serde_json::json;
6768
use thegraph_core::deployment_id;
@@ -72,8 +73,14 @@ mod tests {
7273

7374
use super::*;
7475

75-
#[tokio::test]
76-
async fn test_parses_synced_and_healthy_response() {
76+
struct MonitorMock {
77+
mock_server: MockServer,
78+
status_url: Url,
79+
deployment: DeploymentId,
80+
}
81+
82+
#[rstest::fixture]
83+
async fn monitor_mock() -> MonitorMock {
7784
let mock_server = MockServer::start().await;
7885
let status_url: Url = mock_server
7986
.uri()
@@ -82,7 +89,16 @@ mod tests {
8289
.join("/status")
8390
.unwrap();
8491
let deployment = deployment_id!("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
92+
MonitorMock {
93+
mock_server,
94+
status_url,
95+
deployment,
96+
}
97+
}
8598

99+
#[rstest::rstest]
100+
#[tokio::test]
101+
async fn test_parses_synced_and_healthy_response(#[future(awt)] monitor_mock: MonitorMock) {
86102
Mock::given(method("POST"))
87103
.and(path("/status"))
88104
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
@@ -95,10 +111,10 @@ mod tests {
95111
]
96112
}
97113
})))
98-
.mount(&mock_server)
114+
.mount(&monitor_mock.mock_server)
99115
.await;
100116

101-
let status = monitor_deployment_status(deployment, status_url)
117+
let status = monitor_deployment_status(monitor_mock.deployment, monitor_mock.status_url)
102118
.await
103119
.unwrap();
104120

@@ -111,17 +127,9 @@ mod tests {
111127
);
112128
}
113129

130+
#[rstest::rstest]
114131
#[tokio::test]
115-
async fn test_parses_not_synced_and_healthy_response() {
116-
let mock_server = MockServer::start().await;
117-
let status_url: Url = mock_server
118-
.uri()
119-
.parse::<Url>()
120-
.unwrap()
121-
.join("/status")
122-
.unwrap();
123-
let deployment = deployment_id!("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
124-
132+
async fn test_parses_not_synced_and_healthy_response(#[future(awt)] monitor_mock: MonitorMock) {
125133
Mock::given(method("POST"))
126134
.and(path("/status"))
127135
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
@@ -134,10 +142,10 @@ mod tests {
134142
]
135143
}
136144
})))
137-
.mount(&mock_server)
145+
.mount(&monitor_mock.mock_server)
138146
.await;
139147

140-
let status = monitor_deployment_status(deployment, status_url)
148+
let status = monitor_deployment_status(monitor_mock.deployment, monitor_mock.status_url)
141149
.await
142150
.unwrap();
143151

@@ -150,17 +158,9 @@ mod tests {
150158
);
151159
}
152160

161+
#[rstest::rstest]
153162
#[tokio::test]
154-
async fn test_parses_synced_and_unhealthy_response() {
155-
let mock_server = MockServer::start().await;
156-
let status_url: Url = mock_server
157-
.uri()
158-
.parse::<Url>()
159-
.unwrap()
160-
.join("/status")
161-
.unwrap();
162-
let deployment = deployment_id!("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
163-
163+
async fn test_parses_synced_and_unhealthy_response(#[future(awt)] monitor_mock: MonitorMock) {
164164
Mock::given(method("POST"))
165165
.and(path("/status"))
166166
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
@@ -173,10 +173,10 @@ mod tests {
173173
]
174174
}
175175
})))
176-
.mount(&mock_server)
176+
.mount(&monitor_mock.mock_server)
177177
.await;
178178

179-
let status = monitor_deployment_status(deployment, status_url)
179+
let status = monitor_deployment_status(monitor_mock.deployment, monitor_mock.status_url)
180180
.await
181181
.unwrap();
182182

@@ -189,17 +189,9 @@ mod tests {
189189
);
190190
}
191191

192+
#[rstest::rstest]
192193
#[tokio::test]
193-
async fn test_parses_synced_and_failed_response() {
194-
let mock_server = MockServer::start().await;
195-
let status_url: Url = mock_server
196-
.uri()
197-
.parse::<Url>()
198-
.unwrap()
199-
.join("/status")
200-
.unwrap();
201-
let deployment = deployment_id!("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
202-
194+
async fn test_parses_synced_and_failed_response(#[future(awt)] monitor_mock: MonitorMock) {
203195
Mock::given(method("POST"))
204196
.and(path("/status"))
205197
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
@@ -212,10 +204,10 @@ mod tests {
212204
]
213205
}
214206
})))
215-
.mount(&mock_server)
207+
.mount(&monitor_mock.mock_server)
216208
.await;
217209

218-
let status = monitor_deployment_status(deployment, status_url)
210+
let status = monitor_deployment_status(monitor_mock.deployment, monitor_mock.status_url)
219211
.await
220212
.unwrap();
221213

crates/service/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ tower-service = "0.3.3"
7171
tokio-test = "0.4.4"
7272
wiremock.workspace = true
7373
insta = "1.41.1"
74+
async-std = { version = "1.12.0", features = ["attributes"] }
7475

7576
[build-dependencies]
7677
build-info-build = { version = "0.0.39", default-features = false }

crates/tap-agent/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,5 @@ test-assets = { path = "../test-assets" }
6666
test-log = { version = "0.2.12", features = ["trace"] }
6767
bon = "3.3"
6868
rstest = "0.24.0"
69+
stdext = "0.3.3"
70+
async-std = { version = "1.12.0", features = ["attributes"] }

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

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,6 +1384,7 @@ pub mod tests {
13841384
#![allow(missing_docs)]
13851385
use std::{
13861386
collections::{HashMap, HashSet},
1387+
sync::Arc,
13871388
time::{Duration, SystemTime, UNIX_EPOCH},
13881389
};
13891390

@@ -1392,10 +1393,11 @@ pub mod tests {
13921393
use serde_json::json;
13931394
use sqlx::PgPool;
13941395
use test_assets::{
1395-
flush_messages, ALLOCATION_ID_0, ALLOCATION_ID_1, TAP_SENDER as SENDER,
1396+
flush_messages, pgpool, ALLOCATION_ID_0, ALLOCATION_ID_1, TAP_SENDER as SENDER,
13961397
TAP_SIGNER as SIGNER,
13971398
};
13981399
use thegraph_core::alloy::{hex::ToHexExt, primitives::U256};
1400+
use tokio::sync::Notify;
13991401
use wiremock::{
14001402
matchers::{body_string_contains, method},
14011403
Mock, MockServer, ResponseTemplate,
@@ -1480,6 +1482,22 @@ pub mod tests {
14801482
.await;
14811483
mock_escrow_subgraph_server
14821484
}
1485+
struct TestSenderAccount {
1486+
sender_account: ActorRef<SenderAccountMessage>,
1487+
notify: Arc<Notify>,
1488+
prefix: String,
1489+
}
1490+
1491+
#[rstest::fixture]
1492+
async fn basic_sender_account(#[future(awt)] pgpool: PgPool) -> TestSenderAccount {
1493+
let (sender_account, notify, prefix, _) =
1494+
create_sender_account().pgpool(pgpool.clone()).call().await;
1495+
TestSenderAccount {
1496+
sender_account,
1497+
notify,
1498+
prefix,
1499+
}
1500+
}
14831501

14841502
#[rstest::rstest]
14851503
#[sqlx::test(migrations = "../../migrations")]
@@ -1683,20 +1701,21 @@ pub mod tests {
16831701
.as_nanos() as u64
16841702
}
16851703

1686-
#[sqlx::test(migrations = "../../migrations")]
1687-
async fn test_update_receipt_fees_no_rav(pgpool: PgPool) {
1688-
let (sender_account, _, prefix, _) = create_sender_account().pgpool(pgpool).call().await;
1689-
1704+
#[rstest::rstest]
1705+
async fn test_update_receipt_fees_no_rav(
1706+
#[future(awt)] basic_sender_account: TestSenderAccount,
1707+
) {
16901708
// create a fake sender allocation
16911709
let (triggered_rav_request, _, _) = create_mock_sender_allocation(
1692-
prefix,
1710+
basic_sender_account.prefix,
16931711
SENDER.1,
16941712
ALLOCATION_ID_0,
1695-
sender_account.clone(),
1713+
basic_sender_account.sender_account.clone(),
16961714
)
16971715
.await;
16981716

1699-
sender_account
1717+
basic_sender_account
1718+
.sender_account
17001719
.cast(SenderAccountMessage::UpdateReceiptFees(
17011720
ALLOCATION_ID_0,
17021721
ReceiptFees::NewReceipt(TRIGGER_VALUE - 1, get_current_timestamp_u64_ns()),
@@ -1709,40 +1728,41 @@ pub mod tests {
17091728
assert_not_triggered!(&triggered_rav_request);
17101729
}
17111730

1712-
#[sqlx::test(migrations = "../../migrations")]
1713-
async fn test_update_receipt_fees_trigger_rav(pgpool: PgPool) {
1714-
let (sender_account, notify, prefix, _) =
1715-
create_sender_account().pgpool(pgpool).call().await;
1716-
1731+
#[rstest::rstest]
1732+
async fn test_update_receipt_fees_trigger_rav(
1733+
#[future(awt)] basic_sender_account: TestSenderAccount,
1734+
) {
17171735
// create a fake sender allocation
17181736
let (triggered_rav_request, _, _) = create_mock_sender_allocation(
1719-
prefix,
1737+
basic_sender_account.prefix,
17201738
SENDER.1,
17211739
ALLOCATION_ID_0,
1722-
sender_account.clone(),
1740+
basic_sender_account.sender_account.clone(),
17231741
)
17241742
.await;
17251743

1726-
sender_account
1744+
basic_sender_account
1745+
.sender_account
17271746
.cast(SenderAccountMessage::UpdateReceiptFees(
17281747
ALLOCATION_ID_0,
17291748
ReceiptFees::NewReceipt(TRIGGER_VALUE, get_current_timestamp_u64_ns()),
17301749
))
17311750
.unwrap();
17321751

1733-
flush_messages(&notify).await;
1752+
flush_messages(&basic_sender_account.notify).await;
17341753
assert_not_triggered!(&triggered_rav_request);
17351754

17361755
// wait for it to be outside buffer
17371756
tokio::time::sleep(BUFFER_DURATION).await;
17381757

1739-
sender_account
1758+
basic_sender_account
1759+
.sender_account
17401760
.cast(SenderAccountMessage::UpdateReceiptFees(
17411761
ALLOCATION_ID_0,
17421762
ReceiptFees::Retry,
17431763
))
17441764
.unwrap();
1745-
flush_messages(&notify).await;
1765+
flush_messages(&basic_sender_account.notify).await;
17461766

17471767
assert_triggered!(&triggered_rav_request);
17481768
}
@@ -1832,8 +1852,8 @@ pub mod tests {
18321852
}
18331853

18341854
/// Test that the deny status is correctly loaded from the DB at the start of the actor
1835-
#[sqlx::test(migrations = "../../migrations")]
1836-
async fn test_init_deny(pgpool: PgPool) {
1855+
#[rstest::rstest]
1856+
async fn test_init_deny(#[future(awt)] pgpool: PgPool) {
18371857
sqlx::query!(
18381858
r#"
18391859
INSERT INTO scalar_tap_denylist (sender_address)

0 commit comments

Comments
 (0)