connection_utils/test/
test_stream.rs

1use std::{pin::Pin};
2use cs_utils::{random_number};
3use tokio::io::{AsyncRead, AsyncWrite};
4
5use crate::{
6    create_framed_stream,
7    test::{test_framed_stream, test_async_stream_pinned, TestStreamMessage, TestOptions},
8};
9
10/// Test asynchronous duplex with both `framed` and raw `binary` streams.
11pub async fn test_stream<TAsyncDuplex: AsyncRead + AsyncWrite + Send + ?Sized + Unpin + 'static>(
12    channel1: Box<TAsyncDuplex>,
13    channel2: Box<TAsyncDuplex>,
14    options: TestOptions,
15) -> (Pin<Box<TAsyncDuplex>>, Pin<Box<TAsyncDuplex>>) {
16    // create framed streams
17    let stream1 = create_framed_stream::<TestStreamMessage, _>(channel1);
18    let stream2 = create_framed_stream::<TestStreamMessage, _>(channel2);
19
20    // since frames are big, the framed stream data must be much smaller, so adjust it
21    let framed_stream_data_len = if (options.data_len() / 128) > 16 {
22        options.data_len() / 128
23    } else {
24        random_number(6..=16)
25    };
26
27    // run the framed stream test
28    let (stream1, stream2) = test_framed_stream(
29        stream1,
30        stream2,
31        options.clone()
32            .with_data_len(framed_stream_data_len),
33    ).await;
34
35    // run the raw binarytest
36    return test_async_stream_pinned(
37        stream1.into_inner(),
38        stream2.into_inner(),
39        options,
40    ).await;
41}