Skip to content

Commit e353599

Browse files
committed
test: more reliable tests
1 parent 2c7adf4 commit e353599

File tree

1 file changed

+42
-36
lines changed

1 file changed

+42
-36
lines changed

tests/latest_events/basic.rs

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::Arc;
33
use alloy::{
44
eips::BlockNumberOrTag,
55
network::Ethereum,
6-
primitives::FixedBytes,
6+
primitives::{Address, FixedBytes, U256, uint},
77
providers::{Provider, RootProvider, ext::AnvilApi},
88
rpc::types::Log,
99
sol_types::SolEvent,
@@ -13,6 +13,8 @@ use tokio_stream::StreamExt;
1313
use crate::common::{TestCounter, build_provider, deploy_counter, setup_scanner, spawn_anvil};
1414
use event_scanner::{event_filter::EventFilter, event_scanner::EventScannerMessage};
1515

16+
const ONE: U256 = uint!(1_U256);
17+
1618
async fn collect_events(
1719
stream: &mut tokio_stream::wrappers::ReceiverStream<EventScannerMessage>,
1820
) -> Vec<Log> {
@@ -26,11 +28,22 @@ async fn collect_events(
2628
}
2729
}
2830

29-
fn assert_ordering(logs: &[Log]) {
30-
for w in logs.windows(2) {
31-
let a = (&w[0].block_number.unwrap(), &w[0].transaction_index.unwrap());
32-
let b = (&w[1].block_number.unwrap(), &w[1].transaction_index.unwrap());
33-
assert!(a <= b, "events must be ordered ascending by (block, tx index)");
31+
fn assert_ordering(
32+
logs: Vec<Log>,
33+
expected_first_count: u64,
34+
expected_hashes: Vec<FixedBytes<32>>,
35+
expected_address: &Address,
36+
) {
37+
let mut expected_count = U256::from(expected_first_count);
38+
for (log, &expected_hash) in logs.iter().zip(expected_hashes.iter()) {
39+
let event = log.log_decode::<TestCounter::CountIncreased>().expect(
40+
format!("expected sig: 'TestCounter::CountIncreased', got: {:?}", log.topic0())
41+
.as_str(),
42+
);
43+
assert_eq!(&event.address(), expected_address);
44+
assert_eq!(event.transaction_hash.unwrap(), expected_hash);
45+
assert_eq!(expected_count, event.inner.newCount);
46+
expected_count += ONE;
3447
}
3548
}
3649

@@ -55,17 +68,11 @@ async fn scan_latest_exact_count_returns_last_events_in_order() -> anyhow::Resul
5568

5669
assert_eq!(logs.len(), 5, "should receive exactly 5 latest events");
5770

58-
// Ensure logs are in ascending block/tx order
59-
assert_ordering(&logs);
60-
6171
// Verify exact events (address, signature, tx hashes)
72+
let expected_first_count = 4;
6273
let expected_hashes = tx_hashes[3..8].to_vec();
63-
let sig = TestCounter::CountIncreased::SIGNATURE_HASH;
64-
for (log, expected_hash) in logs.iter().zip(expected_hashes.iter()) {
65-
assert_eq!(log.address(), *contract.address());
66-
assert_eq!(log.topics()[0], sig);
67-
assert_eq!(log.transaction_hash.unwrap(), *expected_hash);
68-
}
74+
75+
assert_ordering(logs, expected_first_count, expected_hashes, contract.address());
6976

7077
Ok(())
7178
}
@@ -91,12 +98,10 @@ async fn scan_latest_fewer_available_than_count_returns_all() -> anyhow::Result<
9198
assert_eq!(logs.len(), 3, "should receive only available events");
9299

93100
// Verify exact events
94-
let sig = TestCounter::CountIncreased::SIGNATURE_HASH;
95-
for (log, expected_hash) in logs.iter().zip(tx_hashes.iter()) {
96-
assert_eq!(log.address(), *contract.address());
97-
assert_eq!(log.topics()[0], sig);
98-
assert_eq!(log.transaction_hash.unwrap(), *expected_hash);
99-
}
101+
let expected_first_count = 1;
102+
103+
assert_ordering(logs, expected_first_count, tx_hashes, contract.address());
104+
100105
Ok(())
101106
}
102107

@@ -149,19 +154,17 @@ async fn scan_latest_respects_range_subset() -> anyhow::Result<()> {
149154

150155
// Expect last 4 emitted events exactly (the 2 empty blocks contain no events)
151156
assert_eq!(logs.len(), 2);
152-
let sig = TestCounter::CountIncreased::SIGNATURE_HASH;
157+
153158
let expected_hashes = tx_hashes[4..6].to_vec(); // counts 5..6
154-
for (log, expected_hash) in logs.iter().zip(expected_hashes.iter()) {
155-
assert_eq!(log.address(), *contract.address());
156-
assert_eq!(log.topics()[0], sig);
157-
assert_eq!(log.transaction_hash.unwrap(), *expected_hash);
158-
}
159+
let expected_first_count = 5;
160+
161+
assert_ordering(logs, expected_first_count, expected_hashes, contract.address());
159162

160163
Ok(())
161164
}
162165

163166
#[tokio::test]
164-
async fn scan_latest_multiple_listeners_receive_results() -> anyhow::Result<()> {
167+
async fn scan_latest_multiple_listeners_to_same_event_receive_same_results() -> anyhow::Result<()> {
165168
let setup = setup_scanner(None, None, None).await?;
166169
let contract = setup.contract;
167170
let mut client = setup.client;
@@ -174,22 +177,25 @@ async fn scan_latest_multiple_listeners_receive_results() -> anyhow::Result<()>
174177
let mut stream2 = client.create_event_stream(filter2);
175178

176179
// Produce 7 events
180+
let mut tx_hashes: Vec<FixedBytes<32>> = Vec::new();
177181
for _ in 0..7u8 {
178-
let _ = contract.increase().send().await?.get_receipt().await?;
182+
let receipt = contract.increase().send().await?.get_receipt().await?;
183+
tx_hashes.push(receipt.transaction_hash);
179184
}
180185

181186
client.scan_latest(5, BlockNumberOrTag::Earliest, BlockNumberOrTag::Latest).await?;
182187

183188
let logs1 = collect_events(&mut stream1).await;
184189
let logs2 = collect_events(&mut stream2).await;
185190

186-
assert_eq!(logs1.len(), 5);
187-
assert_eq!(logs2.len(), 5);
188-
assert_eq!(
189-
logs1.iter().map(|l| (l.block_number, l.transaction_index)).collect::<Vec<_>>(),
190-
logs2.iter().map(|l| (l.block_number, l.transaction_index)).collect::<Vec<_>>(),
191-
"both listeners should receive identical results"
192-
);
191+
assert_eq!(logs1, logs2);
192+
193+
// since logs are equal, asserting for one, asserts for both
194+
assert_eq!(5, logs1.len());
195+
196+
let expected_hashes = tx_hashes[2..7].to_vec();
197+
let expected_first_count = 3;
198+
assert_ordering(logs1, expected_first_count, expected_hashes, contract.address());
193199

194200
Ok(())
195201
}

0 commit comments

Comments
 (0)