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
use std::{pin::Pin};
use cs_utils::{random_number};
use tokio::io::{AsyncRead, AsyncWrite};

use crate::{
    create_framed_stream,
    test::{test_framed_stream, test_async_stream_pinned, TestStreamMessage, TestOptions},
};

/// Test asynchronous duplex with both `framed` and raw `binary` streams.
pub async fn test_stream<TAsyncDuplex: AsyncRead + AsyncWrite + Send + Unpin + ?Sized + 'static>(
    channel1: Box<TAsyncDuplex>,
    channel2: Box<TAsyncDuplex>,
    options: TestOptions,
) -> (Pin<Box<TAsyncDuplex>>, Pin<Box<TAsyncDuplex>>) {
    // create framed streams
    let stream1 = create_framed_stream::<TestStreamMessage, _>(channel1);
    let stream2 = create_framed_stream::<TestStreamMessage, _>(channel2);

    // since frames are big, the framed stream data must be much smaller, so adjust it
    let framed_stream_data_len = if (options.data_len() / 128) > 16 {
        options.data_len() / 128
    } else {
        random_number(6..=16)
    };

    // run the framed stream test
    let (stream1, stream2) = test_framed_stream(
        stream1,
        stream2,
        options.clone()
            .with_data_len(framed_stream_data_len),
    ).await;

    // run the raw binarytest
    return test_async_stream_pinned(
        stream1.into_inner(),
        stream2.into_inner(),
        options,
    ).await;
}