1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//! Provides a wrapper for sinks, to test blocking and errors.

use futures::{Sink, StartSend, Poll, task, AsyncSink, Async, Stream};
use quickcheck::{Arbitrary, Gen};

/// What to do the next time `start_send` is called.
#[derive(Clone, Debug, PartialEq)]
pub enum SendOp<E> {
    /// Simply delegate to the underlying Sink.
    Delegate,

    /// Return `AsyncSink::NotReady` instead of calling into the underlying
    /// operation. The task is immediately notified.
    NotReady,

    /// Return an error instead of calling into the underlying operation.
    Err(E),
}

impl<E> Arbitrary for SendOp<E>
    where E: Arbitrary
{
    /// Generates 75% Delegate, 25% NotReady.
    fn arbitrary<G: Gen>(g: &mut G) -> Self {
        if g.next_f32() < 0.25 {
            SendOp::NotReady
        } else {
            SendOp::Delegate
        }
    }
}

/// What to do the next time `poll_complete` is called.
#[derive(Clone, Debug, PartialEq)]
pub enum FlushOp<E> {
    /// Simply delegate to the underlying Sink.
    Delegate,

    /// Return `Async::NotReady` instead of calling into the underlying
    /// operation. The task is immediately notified.
    NotReady,

    /// Return an error instead of calling into the underlying operation.
    Err(E),
}

impl<E> Arbitrary for FlushOp<E>
    where E: 'static + Clone + Send
{
    /// Generates 75% Delegate, 25% NotReady.
    fn arbitrary<G: Gen>(g: &mut G) -> Self {
        if g.next_f32() < 0.25 {
            FlushOp::NotReady
        } else {
            FlushOp::Delegate
        }
    }
}

/// A Sink wrapper that modifies operations of the inner Sink according to the
/// provided iterators.
pub struct TestSink<S: Sink> {
    inner: S,
    send_ops: Box<Iterator<Item = SendOp<S::SinkError>> + Send>,
    flush_ops: Box<Iterator<Item = FlushOp<S::SinkError>> + Send>,
}

impl<S: Sink> TestSink<S> {
    /// Creates a new `TestSink` wrapper over the Sink with the specified `SendOps`s and `FlushOps`.
    pub fn new<I, J>(inner: S, send_iter: I, flush_iter: J) -> Self
        where I: IntoIterator<Item = SendOp<S::SinkError>> + 'static,
              I::IntoIter: Send,
              J: IntoIterator<Item = FlushOp<S::SinkError>> + 'static,
              J::IntoIter: Send
    {
        TestSink {
            inner: inner,
            send_ops: Box::new(send_iter.into_iter().fuse()),
            flush_ops: Box::new(flush_iter.into_iter().fuse()),
        }
    }

    /// Sets the `SendOp`s for this Sink.
    pub fn set_send_ops<I>(&mut self, send_iter: I) -> &mut Self
        where I: IntoIterator<Item = SendOp<S::SinkError>> + 'static,
              I::IntoIter: Send
    {
        self.send_ops = Box::new(send_iter.into_iter().fuse());
        self
    }

    /// Sets the `FlushOp`s for this Sink.
    pub fn set_flush_ops<I>(&mut self, flush_iter: I) -> &mut Self
        where I: IntoIterator<Item = FlushOp<S::SinkError>> + 'static,
              I::IntoIter: Send
    {
        self.flush_ops = Box::new(flush_iter.into_iter().fuse());
        self
    }

    /// Acquires a reference to the underlying Sink.
    pub fn get_ref(&self) -> &S {
        &self.inner
    }

    /// Acquires a mutable reference to the underlying Sink.
    pub fn get_mut(&mut self) -> &mut S {
        &mut self.inner
    }

    /// Consumes this wrapper, returning the underlying Sink.
    pub fn into_inner(self) -> S {
        self.inner
    }
}

impl<S: Sink> Sink for TestSink<S> {
    type SinkItem = S::SinkItem;
    type SinkError = S::SinkError;

    fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> {
        match self.send_ops.next() {
            Some(SendOp::NotReady) => {
                task::current().notify();
                Ok(AsyncSink::NotReady(item))
            }
            Some(SendOp::Err(err)) => Err(err),
            Some(SendOp::Delegate) |
            None => self.inner.start_send(item),
        }
    }

    fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
        match self.flush_ops.next() {
            Some(FlushOp::NotReady) => {
                task::current().notify();
                Ok(Async::NotReady)
            }
            Some(FlushOp::Err(err)) => Err(err),
            Some(FlushOp::Delegate) |
            None => self.inner.poll_complete(),
        }
    }

    fn close(&mut self) -> Poll<(), Self::SinkError> {
        self.inner.close()
    }
}

impl<S: Sink + Stream> Stream for TestSink<S> {
    type Item = S::Item;
    type Error = S::Error;

    fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
        self.inner.poll()
    }
}