Skip to main content

assert_range_coverage

Macro assert_range_coverage 

Source
macro_rules! assert_range_coverage {
    ($stream: expr, $expected_range: expr) => { ... };
    ($stream: expr, $expected_range: expr, timeout = $secs: expr) => { ... };
}
Expand description

Asserts that a stream of block ranges completely covers an expected block range.

This macro consumes messages from a stream and verifies that the block ranges received sequentially cover the entire expected_range without gaps or overlaps. Each streamed range must start exactly where the previous one ended, and all ranges must fit within the expected bounds.

The macro expects the stream to yield ScannerMessage::Data(range) variants containing RangeInclusive<u64> values representing block ranges. It tracks coverage by ensuring each new range starts at the next expected block number and doesn’t exceed the end of the expected range. Once the entire range is covered, the assertion succeeds.

§Example

use event_scanner::{ScannerMessage, assert_range_coverage};
use tokio::sync::mpsc;
use tokio_stream::wrappers::ReceiverStream;

#[tokio::test]
async fn test_scanner_covers_range() {
    let (tx, rx) = mpsc::channel(10);
    let mut stream = ReceiverStream::new(rx);

    // Simulate a scanner that splits blocks 100-199 into chunks
    tokio::spawn(async move {
        tx.send(ScannerMessage::Data(100..=149)).await.unwrap();
        tx.send(ScannerMessage::Data(150..=199)).await.unwrap();
    });

    // Assert that the stream covers blocks 100-199
    assert_range_coverage!(stream, 100..=199);
}

§Panics

  • Timeout: The stream doesn’t produce the next expected range within the timeout period (default 5 seconds, configurable via timeout = N parameter).
  • Gap or overlap: A streamed range doesn’t start exactly at the next expected block number, indicating a gap or overlap in coverage.
  • Out of bounds: A streamed range extends beyond the end of the expected range.
  • Wrong message type: The stream yields a non-Data message (e.g., Error or Notification) when a block range is expected.
  • Stream closed early: The stream ends before the entire expected range is covered.

On panic, the error message includes the expected remaining range and all previously streamed ranges for debugging.