Sha256: 3dd28d2a7ccfaa1eb74e321b08c05e1ce5d015d3f05f22a95f949b3acc5280ba

Contents?: true

Size: 1.4 KB

Versions: 8

Compression:

Stored size: 1.4 KB

Contents

#![warn(rust_2018_idioms)]
#![cfg(all(feature = "full", not(tokio_wasi)))] // Wasi doesn't support bind

use std::io::Read;
use std::io::Result;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;
use tokio::net::TcpStream;

#[tokio::test]
async fn tcp_into_std() -> Result<()> {
    let mut data = [0u8; 12];
    let listener = TcpListener::bind("127.0.0.1:0").await?;
    let addr = listener.local_addr().unwrap().to_string();

    let handle = tokio::spawn(async {
        let stream: TcpStream = TcpStream::connect(addr).await.unwrap();
        stream
    });

    let (tokio_tcp_stream, _) = listener.accept().await?;
    let mut std_tcp_stream = tokio_tcp_stream.into_std()?;
    std_tcp_stream
        .set_nonblocking(false)
        .expect("set_nonblocking call failed");

    let mut client = handle.await.expect("The task being joined has panicked");
    client.write_all(b"Hello world!").await?;

    std_tcp_stream
        .read_exact(&mut data)
        .expect("std TcpStream read failed!");
    assert_eq!(b"Hello world!", &data);

    // test back to tokio stream
    std_tcp_stream
        .set_nonblocking(true)
        .expect("set_nonblocking call failed");
    let mut tokio_tcp_stream = TcpStream::from_std(std_tcp_stream)?;
    client.write_all(b"Hello tokio!").await?;
    let _size = tokio_tcp_stream.read_exact(&mut data).await?;
    assert_eq!(b"Hello tokio!", &data);

    Ok(())
}

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
wasmtime-9.0.1 ./ext/cargo-vendor/tokio-1.28.1/tests/tcp_into_std.rs
wasmtime-8.0.0 ./ext/cargo-vendor/tokio-1.27.0/tests/tcp_into_std.rs
wasmtime-7.0.0 ./ext/cargo-vendor/tokio-1.27.0/tests/tcp_into_std.rs
wasmtime-6.0.1 ./ext/cargo-vendor/tokio-1.25.0/tests/tcp_into_std.rs
wasmtime-6.0.0 ./ext/cargo-vendor/tokio-1.25.0/tests/tcp_into_std.rs
wasmtime-5.0.0 ./ext/cargo-vendor/tokio-1.24.2/tests/tcp_into_std.rs
wasmtime-0.4.1 ./ext/cargo-vendor/tokio-1.23.0/tests/tcp_into_std.rs
wasmtime-0.4.0 ./ext/cargo-vendor/tokio-1.23.0/tests/tcp_into_std.rs