summaryrefslogtreecommitdiff
path: root/src/test/fixtures/future.rs
blob: c0fa528b6743c59616b277c14ef2a202169fcefa (plain)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use std::{future::Future, pin::Pin, task};

use futures::stream;

// Combinators for futures that prevent waits, even when the underlying future
// would block.
//
// These are only useful for futures with no bound on how long they may wait,
// and this trait is only implemented on futures that are likely to have that
// characteristic. Trying to apply this to futures that already have some
// bounded wait time may make tests fail inappropriately and can hide other
// logic errors.
pub trait Expect: Sized {
    // The returned future expects the underlying future to be ready immediately,
    // and panics with the provided message if it is not.
    //
    // For stream operations, can be used to assert immediate completion.
    fn expect_ready(self, message: &str) -> Ready<Self>
    where
        Self: Future;

    // The returned future expects the underlying future _not_ to be ready, and
    // panics if it is. This is usually a useful proxy for "I expect this to never
    // arrive" or "to not be here yet." The future is transformed to return `()`,
    // since the underlying future can never provide a value.
    //
    // For stream operations, can be used to assert that completion hasn't happened
    // yet.
    fn expect_wait(self, message: &str) -> Wait<Self>
    where
        Self: Future;

    // The returned future expects the underlying future to resolve immediately, to
    // a `Some` value. If it resolves to `None` or is not ready, it panics. The
    // future is transformed to return the inner value from the `Some` case, like
    // [`Option::expect`].
    //
    // For stream operations, can be used to assert that the stream has at least one
    // message.
    fn expect_some<T>(self, message: &str) -> Some<Self>
    where
        Self: Future<Output = Option<T>>;

    // The returned future expects the underlying future to resolve immediately, to
    // a `None` value. If it resolves to `Some(_)`, or is not ready, it panics. The
    // future is transformed to return `()`, since the underlying future's value is
    // fixed.
    //
    // For stream operations, can be used to assert that the stream has ended.
    fn expect_none<T>(self, message: &str) -> None<Self>
    where
        Self: Future<Output = Option<T>>;
}

impl<St> Expect for stream::Next<'_, St> {
    fn expect_ready(self, message: &str) -> Ready<Self> {
        Ready {
            future: self,
            message,
        }
    }

    fn expect_wait(self, message: &str) -> Wait<Self> {
        Wait {
            future: self,
            message,
        }
    }

    fn expect_some<T>(self, message: &str) -> Some<Self>
    where
        Self: Future<Output = Option<T>>,
    {
        Some {
            future: self,
            message,
        }
    }

    fn expect_none<T>(self, message: &str) -> None<Self>
    where
        Self: Future<Output = Option<T>>,
    {
        None {
            future: self,
            message,
        }
    }
}

impl<St, C> Expect for stream::Collect<St, C> {
    fn expect_ready(self, message: &str) -> Ready<Self> {
        Ready {
            future: self,
            message,
        }
    }

    fn expect_wait(self, message: &str) -> Wait<Self> {
        Wait {
            future: self,
            message,
        }
    }

    fn expect_some<T>(self, message: &str) -> Some<Self>
    where
        Self: Future<Output = Option<T>>,
    {
        Some {
            future: self,
            message,
        }
    }

    fn expect_none<T>(self, message: &str) -> None<Self>
    where
        Self: Future<Output = Option<T>>,
    {
        None {
            future: self,
            message,
        }
    }
}

#[pin_project::pin_project]
pub struct Ready<'m, F> {
    #[pin]
    future: F,
    message: &'m str,
}

impl<F> Future for Ready<'_, F>
where
    F: Future + std::fmt::Debug,
{
    type Output = F::Output;

    fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> task::Poll<Self::Output> {
        let this = self.project();

        if let task::Poll::Ready(value) = this.future.poll(cx) {
            task::Poll::Ready(value)
        } else {
            panic!("{}", this.message);
        }
    }
}

#[pin_project::pin_project]
pub struct Wait<'m, F> {
    #[pin]
    future: F,
    message: &'m str,
}

impl<F> Future for Wait<'_, F>
where
    F: Future + std::fmt::Debug,
{
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> task::Poll<Self::Output> {
        let this = self.project();

        if this.future.poll(cx).is_pending() {
            task::Poll::Ready(())
        } else {
            panic!("{}", this.message);
        }
    }
}

#[pin_project::pin_project]
pub struct Some<'m, F> {
    #[pin]
    future: F,
    message: &'m str,
}

impl<F, T> Future for Some<'_, F>
where
    F: Future<Output = Option<T>> + std::fmt::Debug,
{
    type Output = T;

    fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> task::Poll<Self::Output> {
        let this = self.project();

        if let task::Poll::Ready(Option::Some(value)) = this.future.poll(cx) {
            task::Poll::Ready(value)
        } else {
            panic!("{}", this.message)
        }
    }
}

#[pin_project::pin_project]
pub struct None<'m, F> {
    #[pin]
    future: F,
    message: &'m str,
}

impl<F, T> Future for None<'_, F>
where
    F: Future<Output = Option<T>> + std::fmt::Debug,
{
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> task::Poll<Self::Output> {
        let this = self.project();

        if let task::Poll::Ready(Option::None) = this.future.poll(cx) {
            task::Poll::Ready(())
        } else {
            panic!("{}", this.message)
        }
    }
}