Sha256: 5263afeb2499f4ed80e05bb19a68c9c407b798686715d53595c10beac6229193

Contents?: true

Size: 1.14 KB

Versions: 8

Compression:

Stored size: 1.14 KB

Contents

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

use tokio::io::{self, AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::oneshot;
use tokio_test::assert_ok;

#[tokio::test]
async fn echo_server() {
    const ITER: usize = 1024;

    let (tx, rx) = oneshot::channel();

    let srv = assert_ok!(TcpListener::bind("127.0.0.1:0").await);
    let addr = assert_ok!(srv.local_addr());

    let msg = "foo bar baz";
    tokio::spawn(async move {
        let mut stream = assert_ok!(TcpStream::connect(&addr).await);

        for _ in 0..ITER {
            // write
            assert_ok!(stream.write_all(msg.as_bytes()).await);

            // read
            let mut buf = [0; 11];
            assert_ok!(stream.read_exact(&mut buf).await);
            assert_eq!(&buf[..], msg.as_bytes());
        }

        assert_ok!(tx.send(()));
    });

    let (mut stream, _) = assert_ok!(srv.accept().await);
    let (mut rd, mut wr) = stream.split();

    let n = assert_ok!(io::copy(&mut rd, &mut wr).await);
    assert_eq!(n, (ITER * msg.len()) as u64);

    assert_ok!(rx.await);
}

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_echo.rs
wasmtime-8.0.0 ./ext/cargo-vendor/tokio-1.27.0/tests/tcp_echo.rs
wasmtime-7.0.0 ./ext/cargo-vendor/tokio-1.27.0/tests/tcp_echo.rs
wasmtime-6.0.1 ./ext/cargo-vendor/tokio-1.25.0/tests/tcp_echo.rs
wasmtime-6.0.0 ./ext/cargo-vendor/tokio-1.25.0/tests/tcp_echo.rs
wasmtime-5.0.0 ./ext/cargo-vendor/tokio-1.24.2/tests/tcp_echo.rs
wasmtime-0.4.1 ./ext/cargo-vendor/tokio-1.23.0/tests/tcp_echo.rs
wasmtime-0.4.0 ./ext/cargo-vendor/tokio-1.23.0/tests/tcp_echo.rs