connection-utils 0.8.0

Connection related utilities.
Documentation
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 + ?Sized + Unpin + '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;
}