Module tokio_test::stream_mock

source ·
Expand description

A mock stream implementing [Stream].

§Overview

This crate provides a StreamMock that can be used to test code that interacts with streams. It allows you to mock the behavior of a stream and control the items it yields and the waiting intervals between items.

§Usage

To use the StreamMock, you need to create a builder using StreamMockBuilder. The builder allows you to enqueue actions such as returning items or waiting for a certain duration.

§Example


use futures_util::StreamExt;
use std::time::Duration;
use tokio_test::stream_mock::StreamMockBuilder;

async fn test_stream_mock_wait() {
    let mut stream_mock = StreamMockBuilder::new()
        .next(1)
        .wait(Duration::from_millis(300))
        .next(2)
        .build();

    assert_eq!(stream_mock.next().await, Some(1));
    let start = std::time::Instant::now();
    assert_eq!(stream_mock.next().await, Some(2));
    let elapsed = start.elapsed();
    assert!(elapsed >= Duration::from_millis(300));
    assert_eq!(stream_mock.next().await, None);
}

Structs§