Skip to content

Commit 14d3e9f

Browse files
committed
ref: make count assertion return err + test
1 parent 04f6c2e commit 14d3e9f

File tree

5 files changed

+64
-13
lines changed

5 files changed

+64
-13
lines changed

src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ pub enum ScannerError {
2424

2525
#[error("Block number {0} exceeds latest block {1}")]
2626
BlockExceedsLatest(u64, u64),
27+
28+
#[error("Event count must be greater than 0")]
29+
InvalidEventCount,
2730
}
2831

2932
impl From<RobustProviderError> for ScannerError {

src/event_scanner/scanner/latest.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,16 @@ impl EventScannerBuilder<LatestEvents> {
3030
///
3131
/// # Errors
3232
///
33-
/// Returns an error if the provider connection fails.
33+
/// Returns an error if:
34+
/// * The provider connection fails
35+
/// * The event count is zero
3436
pub async fn connect<N: Network>(
3537
self,
3638
provider: impl IntoRobustProvider<N>,
3739
) -> Result<EventScanner<LatestEvents, N>, ScannerError> {
40+
if self.config.count == 0 {
41+
return Err(ScannerError::InvalidEventCount);
42+
}
3843
self.build(provider).await
3944
}
4045
}
@@ -76,6 +81,12 @@ impl<N: Network> EventScanner<LatestEvents, N> {
7681

7782
#[cfg(test)]
7883
mod tests {
84+
use alloy::{
85+
network::Ethereum,
86+
providers::{RootProvider, mock::Asserter},
87+
rpc::client::RpcClient,
88+
};
89+
7990
use super::*;
8091

8192
#[test]
@@ -124,4 +135,15 @@ mod tests {
124135
assert_eq!(builder.config.block_confirmations, 7);
125136
assert_eq!(builder.block_range_scanner.max_block_range, 60);
126137
}
138+
139+
#[tokio::test]
140+
async fn test_latest_returns_error_with_zero_count() {
141+
let provider = RootProvider::<Ethereum>::new(RpcClient::mocked(Asserter::new()));
142+
let result = EventScannerBuilder::latest(0).connect(provider).await;
143+
144+
match result {
145+
Err(ScannerError::InvalidEventCount) => {}
146+
_ => panic!("Expected InvalidEventCount error"),
147+
}
148+
}
127149
}

src/event_scanner/scanner/mod.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,6 @@ impl EventScannerBuilder<Unspecified> {
228228

229229
/// Streams the latest `count` matching events per registered listener.
230230
///
231-
/// # Panics
232-
///
233-
/// If `count` is zero
234-
///
235231
/// # Example
236232
///
237233
/// ```no_run
@@ -302,7 +298,7 @@ impl EventScannerBuilder<Unspecified> {
302298
///
303299
/// # Arguments
304300
///
305-
/// * `count` - Maximum number of recent events to collect per listener
301+
/// * `count` - Maximum number of recent events to collect per listener (must be greater than 0)
306302
///
307303
/// # Reorg behavior
308304
///
@@ -326,7 +322,6 @@ impl EventScannerBuilder<Unspecified> {
326322
/// [reorg]: crate::ScannerStatus::ReorgDetected
327323
#[must_use]
328324
pub fn latest(count: usize) -> EventScannerBuilder<LatestEvents> {
329-
assert!(count != 0, "count must be greater than 0");
330325
EventScannerBuilder::<LatestEvents>::new(count)
331326
}
332327
}
@@ -491,10 +486,18 @@ mod tests {
491486
Ok(())
492487
}
493488

494-
#[test]
495-
#[should_panic(expected = "count must be greater than 0")]
496-
fn test_latest_panics_with_zero_count() {
497-
let _ = EventScannerBuilder::latest(0);
489+
#[tokio::test]
490+
async fn test_latest_returns_error_with_zero_count() {
491+
use alloy::providers::{RootProvider, mock::Asserter};
492+
use alloy::rpc::client::RpcClient;
493+
494+
let provider = RootProvider::<Ethereum>::new(RpcClient::mocked(Asserter::new()));
495+
let result = EventScannerBuilder::latest(0).connect(provider).await;
496+
497+
match result {
498+
Err(ScannerError::InvalidEventCount) => {}
499+
_ => panic!("Expected InvalidEventCount error"),
500+
}
498501
}
499502

500503
#[test]

src/event_scanner/scanner/sync/from_latest.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,16 @@ impl EventScannerBuilder<SyncFromLatestEvents> {
3232
///
3333
/// # Errors
3434
///
35-
/// Returns an error if the provider connection fails.
35+
/// Returns an error if:
36+
/// - The provider connection fails
37+
/// - The event count is zero
3638
pub async fn connect<N: Network>(
3739
self,
3840
provider: impl IntoRobustProvider<N>,
3941
) -> Result<EventScanner<SyncFromLatestEvents, N>, ScannerError> {
42+
if self.config.count == 0 {
43+
return Err(ScannerError::InvalidEventCount);
44+
}
4045
self.build(provider).await
4146
}
4247
}
@@ -116,3 +121,21 @@ impl<N: Network> EventScanner<SyncFromLatestEvents, N> {
116121
Ok(())
117122
}
118123
}
124+
125+
#[cfg(test)]
126+
mod tests {
127+
use alloy::{network::Ethereum, providers::{RootProvider, mock::Asserter}, rpc::client::RpcClient};
128+
129+
use super::*;
130+
131+
#[tokio::test]
132+
async fn test_sync_from_latest_returns_error_with_zero_count() {
133+
let provider = RootProvider::<Ethereum>::new(RpcClient::mocked(Asserter::new()));
134+
let result = EventScannerBuilder::sync().from_latest(0).connect(provider).await;
135+
136+
match result {
137+
Err(ScannerError::InvalidEventCount) => {}
138+
_ => panic!("Expected InvalidEventCount error"),
139+
}
140+
}
141+
}

src/event_scanner/scanner/sync/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl EventScannerBuilder<Synchronize> {
8282
/// # Arguments
8383
///
8484
/// * `count` - Maximum number of recent events to collect per listener before switching to live
85-
/// streaming
85+
/// streaming (must be greater than 0)
8686
///
8787
/// # Important notes
8888
///

0 commit comments

Comments
 (0)