| 1 | use futures_util::StreamExt; |
| 2 | use std::time::Duration; |
| 3 | use tokio_test::stream_mock::StreamMockBuilder; |
| 4 | |
| 5 | #[tokio::test ] |
| 6 | async fn test_stream_mock_empty() { |
| 7 | let mut stream_mock = StreamMockBuilder::<u32>::new().build(); |
| 8 | |
| 9 | assert_eq!(stream_mock.next().await, None); |
| 10 | assert_eq!(stream_mock.next().await, None); |
| 11 | } |
| 12 | |
| 13 | #[tokio::test ] |
| 14 | async fn test_stream_mock_items() { |
| 15 | let mut stream_mock = StreamMockBuilder::new().next(1).next(2).build(); |
| 16 | |
| 17 | assert_eq!(stream_mock.next().await, Some(1)); |
| 18 | assert_eq!(stream_mock.next().await, Some(2)); |
| 19 | assert_eq!(stream_mock.next().await, None); |
| 20 | } |
| 21 | |
| 22 | #[tokio::test ] |
| 23 | async fn test_stream_mock_wait() { |
| 24 | let mut stream_mock = StreamMockBuilder::new() |
| 25 | .next(1) |
| 26 | .wait(Duration::from_millis(300)) |
| 27 | .next(2) |
| 28 | .build(); |
| 29 | |
| 30 | assert_eq!(stream_mock.next().await, Some(1)); |
| 31 | let start = std::time::Instant::now(); |
| 32 | assert_eq!(stream_mock.next().await, Some(2)); |
| 33 | let elapsed = start.elapsed(); |
| 34 | assert!(elapsed >= Duration::from_millis(300)); |
| 35 | assert_eq!(stream_mock.next().await, None); |
| 36 | } |
| 37 | |
| 38 | #[tokio::test ] |
| 39 | #[should_panic (expected = "StreamMock was dropped before all actions were consumed" )] |
| 40 | async fn test_stream_mock_drop_without_consuming_all() { |
| 41 | let stream_mock = StreamMockBuilder::new().next(1).next(2).build(); |
| 42 | drop(stream_mock); |
| 43 | } |
| 44 | |
| 45 | #[tokio::test ] |
| 46 | #[should_panic (expected = "test panic was not masked" )] |
| 47 | async fn test_stream_mock_drop_during_panic_doesnt_mask_panic() { |
| 48 | let _stream_mock = StreamMockBuilder::new().next(1).next(2).build(); |
| 49 | panic!("test panic was not masked" ); |
| 50 | } |
| 51 | |