Sha256: 701032ff3d5a6e6a3d8cb4e373d1c93e4708f2e5ee0a6742fa626f27b6094b4d

Contents?: true

Size: 1.87 KB

Versions: 26

Compression:

Stored size: 1.87 KB

Contents

use futures::io::AsyncWrite;
use futures_test::task::panic_context;
use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};

struct MockWriter {
    fun: Box<dyn FnMut(&[u8]) -> Poll<io::Result<usize>>>,
}

impl MockWriter {
    fn new(fun: impl FnMut(&[u8]) -> Poll<io::Result<usize>> + 'static) -> Self {
        Self { fun: Box::new(fun) }
    }
}

impl AsyncWrite for MockWriter {
    fn poll_write(
        self: Pin<&mut Self>,
        _cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<io::Result<usize>> {
        (self.get_mut().fun)(buf)
    }

    fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        panic!()
    }

    fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        panic!()
    }
}

/// Verifies that the default implementation of `poll_write_vectored`
/// calls `poll_write` with an empty slice if no buffers are provided.
#[test]
fn write_vectored_no_buffers() {
    let mut writer = MockWriter::new(|buf| {
        assert_eq!(buf, b"");
        Err(io::ErrorKind::BrokenPipe.into()).into()
    });
    let cx = &mut panic_context();
    let bufs = &mut [];

    let res = Pin::new(&mut writer).poll_write_vectored(cx, bufs);
    let res = res.map_err(|e| e.kind());
    assert_eq!(res, Poll::Ready(Err(io::ErrorKind::BrokenPipe)))
}

/// Verifies that the default implementation of `poll_write_vectored`
/// calls `poll_write` with the first non-empty buffer.
#[test]
fn write_vectored_first_non_empty() {
    let mut writer = MockWriter::new(|buf| {
        assert_eq!(buf, b"four");
        Poll::Ready(Ok(4))
    });
    let cx = &mut panic_context();
    let bufs = &mut [io::IoSlice::new(&[]), io::IoSlice::new(&[]), io::IoSlice::new(b"four")];

    let res = Pin::new(&mut writer).poll_write_vectored(cx, bufs);
    let res = res.map_err(|e| e.kind());
    assert_eq!(res, Poll::Ready(Ok(4)));
}

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/futures-0.3.31/tests/io_write.rs
wasmtime-28.0.0 ./ext/cargo-vendor/futures-0.3.31/tests/io_write.rs
wasmtime-27.0.0 ./ext/cargo-vendor/futures-0.3.31/tests/io_write.rs
wasmtime-26.0.0 ./ext/cargo-vendor/futures-0.3.31/tests/io_write.rs
wasmtime-25.0.2 ./ext/cargo-vendor/futures-0.3.30/tests/io_write.rs
wasmtime-25.0.1 ./ext/cargo-vendor/futures-0.3.30/tests/io_write.rs
wasmtime-25.0.0 ./ext/cargo-vendor/futures-0.3.30/tests/io_write.rs
wasmtime-24.0.0 ./ext/cargo-vendor/futures-0.3.30/tests/io_write.rs
wasmtime-23.0.2 ./ext/cargo-vendor/futures-0.3.30/tests/io_write.rs
wasmtime-22.0.0 ./ext/cargo-vendor/futures-0.3.30/tests/io_write.rs
wasmtime-21.0.1 ./ext/cargo-vendor/futures-0.3.30/tests/io_write.rs
wasmtime-20.0.2 ./ext/cargo-vendor/futures-0.3.30/tests/io_write.rs
wasmtime-20.0.0 ./ext/cargo-vendor/futures-0.3.30/tests/io_write.rs
wasmtime-18.0.3 ./ext/cargo-vendor/futures-0.3.30/tests/io_write.rs
wasmtime-17.0.1 ./ext/cargo-vendor/futures-0.3.30/tests/io_write.rs
wasmtime-17.0.0 ./ext/cargo-vendor/futures-0.3.30/tests/io_write.rs
wasmtime-16.0.0 ./ext/cargo-vendor/futures-0.3.30/tests/io_write.rs
wasmtime-15.0.1 ./ext/cargo-vendor/futures-0.3.30/tests/io_write.rs
wasmtime-15.0.0 ./ext/cargo-vendor/futures-0.3.30/tests/io_write.rs
wasmtime-14.0.4 ./ext/cargo-vendor/futures-0.3.28/tests/io_write.rs