Skip to content

Commit b5d6355

Browse files
committed
test: add rstest and fixtures
test: revert test fix: tests that fixtures werent available fix: add tokyo to tests style: run cargo clippy
1 parent eeb9f61 commit b5d6355

File tree

10 files changed

+522
-155
lines changed

10 files changed

+522
-155
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: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,14 @@ mod tests {
7272

7373
use super::*;
7474

75-
#[tokio::test]
76-
async fn test_parses_synced_and_healthy_response() {
75+
struct MonitorMock {
76+
mock_server: MockServer,
77+
status_url: Url,
78+
deployment: DeploymentId,
79+
}
80+
81+
#[rstest::fixture]
82+
async fn monitor_mock() -> MonitorMock {
7783
let mock_server = MockServer::start().await;
7884
let status_url: Url = mock_server
7985
.uri()
@@ -82,7 +88,16 @@ mod tests {
8288
.join("/status")
8389
.unwrap();
8490
let deployment = deployment_id!("QmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
91+
MonitorMock {
92+
mock_server,
93+
status_url,
94+
deployment,
95+
}
96+
}
8597

98+
#[rstest::rstest]
99+
#[tokio::test]
100+
async fn test_parses_synced_and_healthy_response(#[future(awt)] monitor_mock: MonitorMock) {
86101
Mock::given(method("POST"))
87102
.and(path("/status"))
88103
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
@@ -95,10 +110,10 @@ mod tests {
95110
]
96111
}
97112
})))
98-
.mount(&mock_server)
113+
.mount(&monitor_mock.mock_server)
99114
.await;
100115

101-
let status = monitor_deployment_status(deployment, status_url)
116+
let status = monitor_deployment_status(monitor_mock.deployment, monitor_mock.status_url)
102117
.await
103118
.unwrap();
104119

@@ -111,17 +126,9 @@ mod tests {
111126
);
112127
}
113128

129+
#[rstest::rstest]
114130
#[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-
131+
async fn test_parses_not_synced_and_healthy_response(#[future(awt)] monitor_mock: MonitorMock) {
125132
Mock::given(method("POST"))
126133
.and(path("/status"))
127134
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
@@ -134,10 +141,10 @@ mod tests {
134141
]
135142
}
136143
})))
137-
.mount(&mock_server)
144+
.mount(&monitor_mock.mock_server)
138145
.await;
139146

140-
let status = monitor_deployment_status(deployment, status_url)
147+
let status = monitor_deployment_status(monitor_mock.deployment, monitor_mock.status_url)
141148
.await
142149
.unwrap();
143150

@@ -150,17 +157,9 @@ mod tests {
150157
);
151158
}
152159

160+
#[rstest::rstest]
153161
#[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-
162+
async fn test_parses_synced_and_unhealthy_response(#[future(awt)] monitor_mock: MonitorMock) {
164163
Mock::given(method("POST"))
165164
.and(path("/status"))
166165
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
@@ -173,10 +172,10 @@ mod tests {
173172
]
174173
}
175174
})))
176-
.mount(&mock_server)
175+
.mount(&monitor_mock.mock_server)
177176
.await;
178177

179-
let status = monitor_deployment_status(deployment, status_url)
178+
let status = monitor_deployment_status(monitor_mock.deployment, monitor_mock.status_url)
180179
.await
181180
.unwrap();
182181

@@ -189,17 +188,9 @@ mod tests {
189188
);
190189
}
191190

191+
#[rstest::rstest]
192192
#[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-
193+
async fn test_parses_synced_and_failed_response(#[future(awt)] monitor_mock: MonitorMock) {
203194
Mock::given(method("POST"))
204195
.and(path("/status"))
205196
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
@@ -212,10 +203,10 @@ mod tests {
212203
]
213204
}
214205
})))
215-
.mount(&mock_server)
206+
.mount(&monitor_mock.mock_server)
216207
.await;
217208

218-
let status = monitor_deployment_status(deployment, status_url)
209+
let status = monitor_deployment_status(monitor_mock.deployment, monitor_mock.status_url)
219210
.await
220211
.unwrap();
221212

crates/service/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ tokio-test = "0.4.4"
7474
wiremock.workspace = true
7575
insta = "1.41.1"
7676
test-log.workspace = true
77+
async-std = { version = "1.12.0", features = ["attributes"] }
7778

7879
[build-dependencies]
7980
build-info-build = { version = "0.0.40", 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 @@ wiremock-grpc = "0.0.3-alpha3"
6666
test-assets = { path = "../test-assets" }
6767
test-log.workspace = true
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: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,10 +1423,11 @@ pub mod tests {
14231423
use serde_json::json;
14241424
use sqlx::PgPool;
14251425
use test_assets::{
1426-
flush_messages, ALLOCATION_ID_0, ALLOCATION_ID_1, TAP_SENDER as SENDER,
1426+
flush_messages, pgpool, ALLOCATION_ID_0, ALLOCATION_ID_1, TAP_SENDER as SENDER,
14271427
TAP_SIGNER as SIGNER,
14281428
};
14291429
use thegraph_core::alloy::{hex::ToHexExt, primitives::U256};
1430+
use tokio::sync::mpsc;
14301431
use wiremock::{
14311432
matchers::{body_string_contains, method},
14321433
Mock, MockServer, ResponseTemplate,
@@ -1467,6 +1468,22 @@ pub mod tests {
14671468
.await;
14681469
mock_escrow_subgraph_server
14691470
}
1471+
struct TestSenderAccount {
1472+
sender_account: ActorRef<SenderAccountMessage>,
1473+
msg_receiver: mpsc::Receiver<SenderAccountMessage>,
1474+
prefix: String,
1475+
}
1476+
1477+
#[rstest::fixture]
1478+
async fn basic_sender_account(#[future(awt)] pgpool: PgPool) -> TestSenderAccount {
1479+
let (sender_account, msg_receiver, prefix, _) =
1480+
create_sender_account().pgpool(pgpool).call().await;
1481+
TestSenderAccount {
1482+
sender_account,
1483+
msg_receiver,
1484+
prefix,
1485+
}
1486+
}
14701487

14711488
#[rstest::rstest]
14721489
#[sqlx::test(migrations = "../../migrations")]
@@ -1688,20 +1705,22 @@ pub mod tests {
16881705
.as_nanos() as u64
16891706
}
16901707

1691-
#[sqlx::test(migrations = "../../migrations")]
1692-
async fn test_update_receipt_fees_no_rav(pgpool: PgPool) {
1693-
let (sender_account, _, prefix, _) = create_sender_account().pgpool(pgpool).call().await;
1694-
1708+
#[rstest::rstest]
1709+
#[tokio::test]
1710+
async fn test_update_receipt_fees_no_rav(
1711+
#[future(awt)] basic_sender_account: TestSenderAccount,
1712+
) {
16951713
// create a fake sender allocation
16961714
let (triggered_rav_request, _, _) = create_mock_sender_allocation(
1697-
prefix,
1715+
basic_sender_account.prefix,
16981716
SENDER.1,
16991717
ALLOCATION_ID_0,
1700-
sender_account.clone(),
1718+
basic_sender_account.sender_account.clone(),
17011719
)
17021720
.await;
17031721

1704-
sender_account
1722+
basic_sender_account
1723+
.sender_account
17051724
.cast(SenderAccountMessage::UpdateReceiptFees(
17061725
ALLOCATION_ID_0,
17071726
ReceiptFees::NewReceipt(TRIGGER_VALUE - 1, get_current_timestamp_u64_ns()),
@@ -1714,40 +1733,42 @@ pub mod tests {
17141733
assert_not_triggered!(&triggered_rav_request);
17151734
}
17161735

1717-
#[sqlx::test(migrations = "../../migrations")]
1718-
async fn test_update_receipt_fees_trigger_rav(pgpool: PgPool) {
1719-
let (sender_account, mut msg_receiver, prefix, _) =
1720-
create_sender_account().pgpool(pgpool).call().await;
1721-
1736+
#[rstest::rstest]
1737+
#[tokio::test]
1738+
async fn test_update_receipt_fees_trigger_rav(
1739+
#[future(awt)] mut basic_sender_account: TestSenderAccount,
1740+
) {
17221741
// create a fake sender allocation
17231742
let (triggered_rav_request, _, _) = create_mock_sender_allocation(
1724-
prefix,
1743+
basic_sender_account.prefix,
17251744
SENDER.1,
17261745
ALLOCATION_ID_0,
1727-
sender_account.clone(),
1746+
basic_sender_account.sender_account.clone(),
17281747
)
17291748
.await;
17301749

1731-
sender_account
1750+
basic_sender_account
1751+
.sender_account
17321752
.cast(SenderAccountMessage::UpdateReceiptFees(
17331753
ALLOCATION_ID_0,
17341754
ReceiptFees::NewReceipt(TRIGGER_VALUE, get_current_timestamp_u64_ns()),
17351755
))
17361756
.unwrap();
17371757

1738-
flush_messages(&mut msg_receiver).await;
1758+
flush_messages(&mut basic_sender_account.msg_receiver).await;
17391759
assert_not_triggered!(&triggered_rav_request);
17401760

17411761
// wait for it to be outside buffer
17421762
tokio::time::sleep(BUFFER_DURATION).await;
17431763

1744-
sender_account
1764+
basic_sender_account
1765+
.sender_account
17451766
.cast(SenderAccountMessage::UpdateReceiptFees(
17461767
ALLOCATION_ID_0,
17471768
ReceiptFees::Retry,
17481769
))
17491770
.unwrap();
1750-
flush_messages(&mut msg_receiver).await;
1771+
flush_messages(&mut basic_sender_account.msg_receiver).await;
17511772

17521773
assert_triggered!(&triggered_rav_request);
17531774
}
@@ -1837,8 +1858,9 @@ pub mod tests {
18371858
}
18381859

18391860
/// Test that the deny status is correctly loaded from the DB at the start of the actor
1840-
#[sqlx::test(migrations = "../../migrations")]
1841-
async fn test_init_deny(pgpool: PgPool) {
1861+
#[rstest::rstest]
1862+
#[tokio::test]
1863+
async fn test_init_deny(#[future(awt)] pgpool: PgPool) {
18421864
sqlx::query!(
18431865
r#"
18441866
INSERT INTO scalar_tap_denylist (sender_address)

0 commit comments

Comments
 (0)