summaryrefslogtreecommitdiff
path: root/vendor/tokio-test/tests/stream_mock.rs
blob: a54ea838a5b5ea3bab5f7d2572c5dd70ce487284 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use futures_util::StreamExt;
use std::time::Duration;
use tokio_test::stream_mock::StreamMockBuilder;

#[tokio::test]
async fn test_stream_mock_empty() {
    let mut stream_mock = StreamMockBuilder::<u32>::new().build();

    assert_eq!(stream_mock.next().await, None);
    assert_eq!(stream_mock.next().await, None);
}

#[tokio::test]
async fn test_stream_mock_items() {
    let mut stream_mock = StreamMockBuilder::new().next(1).next(2).build();

    assert_eq!(stream_mock.next().await, Some(1));
    assert_eq!(stream_mock.next().await, Some(2));
    assert_eq!(stream_mock.next().await, None);
}

#[tokio::test]
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);
}

#[tokio::test]
#[should_panic(expected = "StreamMock was dropped before all actions were consumed")]
async fn test_stream_mock_drop_without_consuming_all() {
    let stream_mock = StreamMockBuilder::new().next(1).next(2).build();
    drop(stream_mock);
}

#[tokio::test]
#[should_panic(expected = "test panic was not masked")]
async fn test_stream_mock_drop_during_panic_doesnt_mask_panic() {
    let _stream_mock = StreamMockBuilder::new().next(1).next(2).build();
    panic!("test panic was not masked");
}