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
use futures::{Sink, StartSend, Poll, task, Async, Stream};
use quickcheck::{Arbitrary, Gen};
#[derive(Clone, Debug, PartialEq)]
pub enum PollOp<E> {
Delegate,
NotReady,
Err(E),
}
impl<E> Arbitrary for PollOp<E>
where E: Arbitrary
{
fn arbitrary<G: Gen>(g: &mut G) -> Self {
if g.next_f32() < 0.25 {
PollOp::NotReady
} else {
PollOp::Delegate
}
}
}
pub struct TestStream<S: Stream> {
inner: S,
poll_ops: Box<Iterator<Item = PollOp<S::Error>> + Send>,
}
impl<S: Stream> TestStream<S> {
pub fn new<I>(inner: S, poll_iter: I) -> Self
where I: IntoIterator<Item = PollOp<S::Error>> + 'static,
I::IntoIter: Send
{
TestStream {
inner: inner,
poll_ops: Box::new(poll_iter.into_iter().fuse()),
}
}
pub fn set_poll_ops<I>(&mut self, poll_iter: I) -> &mut Self
where I: IntoIterator<Item = PollOp<S::Error>> + 'static,
I::IntoIter: Send
{
self.poll_ops = Box::new(poll_iter.into_iter().fuse());
self
}
pub fn get_ref(&self) -> &S {
&self.inner
}
pub fn get_mut(&mut self) -> &mut S {
&mut self.inner
}
pub fn into_inner(self) -> S {
self.inner
}
}
impl<S: Stream> Stream for TestStream<S> {
type Item = S::Item;
type Error = S::Error;
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
match self.poll_ops.next() {
Some(PollOp::NotReady) => {
task::current().notify();
Ok(Async::NotReady)
}
Some(PollOp::Err(err)) => Err(err),
Some(PollOp::Delegate) |
None => self.inner.poll(),
}
}
}
impl<S: Sink + Stream> Sink for TestStream<S> {
type SinkItem = S::SinkItem;
type SinkError = S::SinkError;
fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> {
self.inner.start_send(item)
}
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
self.inner.poll_complete()
}
fn close(&mut self) -> Poll<(), Self::SinkError> {
self.inner.close()
}
}