tokio-rs / turmoil

Add hardship to your tests
MIT License
790 stars 50 forks source link

Fix AsyncRead impl for TcpStream #106

Closed mcches closed 1 year ago

mcches commented 1 year ago

Is your feature request related to a problem? Please describe.

Yes, turmoil::net::TcpStream does not behave like tokio::net::TcpStream.

AsyncRead is broken if the supplied buf does not have capacity for the next message.

#[test]
fn read_buf_smaller_than_msg() -> Result {
    let mut sim = Builder::new().build();

    sim.client("server", async {
        let listener = bind().await?;
        let (mut s, _) = listener.accept().await?;

        s.write_u64(1234).await?;

        Ok(())
    });

    sim.client("client", async {
        let mut s = TcpStream::connect(("server", PORT)).await?;

        let mut buf = [0; 1];
        // panic!: buf.len() must fit in remaining()
        let _r = s.read(&mut buf).await?;

        Ok(())
    });

    sim.run()
}

See: https://github.com/tokio-rs/turmoil/blob/2d0fadde00de4011621607b933eb170f723111a3/src/net/tcp/stream.rs#L137

Describe the solution you'd like

Align turmoil with tokio::net.