2 releases

0.1.1 Feb 7, 2020
0.1.0 Feb 6, 2020

#827 in Testing

Download history 21/week @ 2024-03-11 22/week @ 2024-03-18 25/week @ 2024-03-25 81/week @ 2024-04-01 16/week @ 2024-04-08 49/week @ 2024-04-15 18/week @ 2024-04-22 7/week @ 2024-04-29 17/week @ 2024-05-06 9/week @ 2024-05-13 16/week @ 2024-05-20 8/week @ 2024-05-27 9/week @ 2024-06-03 14/week @ 2024-06-10 18/week @ 2024-06-17 45/week @ 2024-06-24

86 downloads per month

MIT/Apache

37KB
475 lines

crates.io Documentation CI master

futures-test-sink

This crate provide a handy mock sink implementations that can be used test own Sink.

Examples

:MockSink allow to create a handy tests

This example contains a 3 tests. See documentation of MockSink for details.

use async_task::waker_fn;
use std::iter;
use std::{
  task::{Poll, Context},
}
use futures::{self, stream};

   fn drain_test() {
       let e = iter::repeat::<Poll<Result<(), Never>>>(Poll::Ready(Ok(())));
       let sink = SinkMock::with_flush_feedback(e);

       let stream =
           stream::iter(vec![Ok::<u8, Never>(5u8), Ok(7), Ok(9), Ok(77), Ok(79)].into_iter());
       let send_all = stream.forward(sink);
       assert_eq!(Ok(()), futures::executor::block_on(send_all));
   }

   fn interleave_pending() {
       let e = vec![Poll::Ready(Ok::<_, Never>(())), Poll::Pending]
           .into_iter()
           .cycle();
       let sink = SinkMock::with_flush_feedback(e);

       let stream =
           stream::iter(vec![Ok::<u8, Never>(5u8), Ok(7), Ok(9), Ok(77), Ok(79)].into_iter());
       let send_all = stream.forward(sink);
       assert_eq!(Ok(()), futures::executor::block_on(send_all));
   }

   fn error() {
       let e = vec![Poll::Ready(Ok(())), Poll::Pending, Poll::Ready(Err(()))]
           .into_iter()
           .cycle();
       let sink = SinkMock::with_flush_feedback(e);

       let stream = stream::iter(vec![Ok(5u8), Ok(7), Ok(9), Ok(77), Ok(79)].into_iter());
       let send_all = stream.forward(sink);
       assert_eq!(Err(()), futures::executor::block_on(send_all));
   }

   fn main() {
    drain_test();
    interleave_pending();
    error();
   }

SinkFeedback mock provide a full control of returned items.

You should first use MockSink if this doesn't this one may be useful.

use async_task::waker_fn;
use futures::sink::Sink;
use futures_test_sink::from_iter;
use std::{
    pin::Pin,
    sync::{atomic, Arc},
    task::{Context, Poll},
};

// create a Context
let wake_cnt = Arc::new(atomic::AtomicUsize::new(0));
let cnt = wake_cnt.clone();
let waker = waker_fn(move || {
    wake_cnt.fetch_add(1, atomic::Ordering::SeqCst);
});
let mut cx = Context::from_waker(&waker);
// actual test
let poll_fallback = vec![
    Poll::Ready(Ok(())),
    Poll::Ready(Ok(())),
    Poll::Pending,
    Poll::Ready(Err(12)),
]
.into_iter();
let start_send_fallback = vec![Ok::<_, u32>(())].into_iter().cycle();
// ours sink implementation
let mut s = from_iter(poll_fallback, start_send_fallback);

let r1 = Pin::new(&mut s).poll_ready(&mut cx);
assert_eq!(r1, Poll::Ready(Ok(())));
let s1 = Pin::new(&mut s).start_send(1);
assert_eq!(s1, Ok(()));

let r2 = Pin::new(&mut s).poll_ready(&mut cx);
assert_eq!(r2, Poll::Ready(Ok(())));
// start send don't panic because start_send_fallback is cycle
let s2 = Pin::new(&mut s).start_send(2);
assert_eq!(s2, Ok(()));

// ctx.wake() wasn't called.
assert_eq!(0, cnt.load(atomic::Ordering::SeqCst));

let r3 = Pin::new(&mut s).poll_ready(&mut cx);
assert_eq!(r3, Poll::Pending);
assert_eq!(1, cnt.load(atomic::Ordering::SeqCst));

let r4 = Pin::new(&mut s).poll_ready(&mut cx);
assert_eq!(r4, Poll::Ready(Err(12)));
assert_eq!(1, cnt.load(atomic::Ordering::SeqCst));

You can be interested in FuseLast container for Iterator.

use async_task::waker_fn;
use futures::sink::Sink;
use futures_test_sink::{from_iter, fuse_last::IteratorExt};
use std::{
    pin::Pin,
    sync::{atomic, Arc},
    task::{Context, Poll},
};

// create a Context
let wake_cnt = Arc::new(atomic::AtomicUsize::new(0));
let cnt = wake_cnt.clone();
let waker = waker_fn(move || {
    wake_cnt.fetch_add(1, atomic::Ordering::SeqCst);
});
let mut cx = Context::from_waker(&waker);
// actual test
let poll_fallback = vec![
    Poll::Ready(Ok(())),
    Poll::Ready(Err(12)),
    Poll::Ready(Ok(())),
]
.into_iter()
.fuse_last();
let start_send_fallback = vec![Ok::<_, u32>(())].into_iter().cycle();
// ours sink implementation
let mut s = from_iter(poll_fallback, start_send_fallback);

let r1 = Pin::new(&mut s).poll_ready(&mut cx);
assert_eq!(r1, Poll::Ready(Ok(())));
let s1 = Pin::new(&mut s).start_send(1);
assert_eq!(s1, Ok(()));

let r2 = Pin::new(&mut s).poll_ready(&mut cx);
assert_eq!(r2, Poll::Ready(Err(12)));

let r3 = Pin::new(&mut s).poll_ready(&mut cx);
assert_eq!(r3, Poll::Ready(Ok(())));

// if not `fuse_last` this would panic!
let r4 = Pin::new(&mut s).poll_ready(&mut cx);
assert_eq!(r3, Poll::Ready(Ok(())));

let r5 = Pin::new(&mut s).poll_ready(&mut cx);
assert_eq!(r3, Poll::Ready(Ok(())));

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

This project try follow rules:

This README was generated with cargo-readme from template

Dependencies

~1MB
~16K SLoC