async_helpers/
responding_channel.rs

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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! A multi-producer, single-consumer queue for sending requests across asynchronous tasks.
//!
//! Channel creation provides `Receiver` and `Sender` handles. `Sender` can make requests that
//! await a response from the `Receiver`. Every message sent across the channel is packaged with
//! a `Responder` that is used to respond to that request. A `Sender` will wait until a response is
//! received before `Sender::request` completes.
//!
//! ### Disconnection
//! When all `Sender` handles have been dropped, it is no longer possible to send requests into the
//! channel. As such, `Receiver::receive` will return an error.
//!
//! ### Clean Shutdown
//! If a `Receiver` is dropped, it is possible for there to be messages in the channel that will
//! never be processed. If a clean shutdown is desired, a receiver can first call `Receiver::close`
//! to prevent further messages from being sent into the channel. Then, the receiver can handle all
//! messages in the channel and be dropped.

use anyhow::Error;
use futures::channel::{mpsc, oneshot};
use futures::stream::{FusedStream, Stream};
use futures::SinkExt;
use std::pin::Pin;
use std::task::{Context, Poll};

/// The requesting end of a channel.
pub struct Sender<Req, Resp> {
    inner: mpsc::Sender<(Req, Responder<Resp>)>,
}

impl<Req, Resp> Clone for Sender<Req, Resp> {
    fn clone(&self) -> Self {
        Self { inner: self.inner.clone() }
    }
}

impl<Req, Resp> Sender<Req, Resp> {
    /// Send a request on the channel and wait for a response from the responding end of the
    /// channel.
    /// An error is returned if the `Receiver` has been dropped or the `Receiver` drops the
    /// `Responder` for this request.
    pub async fn request(&mut self, value: Req) -> Result<Resp, Error> {
        let (responder, response) = oneshot::channel();
        self.inner.send((value, Responder { inner: responder })).await?;
        Ok(response.await?)
    }
}

/// Responds to a single request with a value.
pub struct Responder<Resp> {
    inner: oneshot::Sender<Resp>,
}

impl<Resp> Responder<Resp> {
    /// Send a response value. If the `Sender` is no longer waiting on a response because the
    /// request future has been dropped, this method will return the original response `value` as
    /// an `Err`.
    pub fn respond(self, value: Resp) -> Result<(), Resp> {
        self.inner.send(value)
    }
}

/// The responding end of a channel.
// TODO(https://fxbug.dev/42162679): Consider replacing this with this alias:
//
//   pub type Receiver<Req, Resp> = mpsc::Receiver<(Req, Responder<Resp>)>;
pub struct Receiver<Req, Resp> {
    inner: mpsc::Receiver<(Req, Responder<Resp>)>,
}

impl<Req, Resp> Receiver<Req, Resp> {
    /// Close the responding end of the channel.
    ///
    /// This prevents further messages from being sent on the channel while still enabling the
    /// receiver to drain messages that are buffered.
    pub fn close(&mut self) {
        self.inner.close();
    }

    /// Try to receive the next message without notifying a context if empty.
    ///
    /// This function will panic if called after `try_next` has returned `None` or `receive` has
    /// returned an `Err`.
    pub fn try_receive(&mut self) -> Result<Option<(Req, Responder<Resp>)>, Error> {
        Ok(self.inner.try_next()?)
    }
}

impl<Req, Resp> Stream for Receiver<Req, Resp> {
    type Item = (Req, Responder<Resp>);

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        Pin::new(&mut self.inner).poll_next(cx)
    }
}

impl<Req, Resp> FusedStream for Receiver<Req, Resp> {
    fn is_terminated(&self) -> bool {
        self.inner.is_terminated()
    }
}

/// Create a new asynchronous channel with a bounded capacity, returning the sender/receiver
/// halves.
///
/// This channel follows the semantics of a futures::mpsc::channel when at capacity.
pub fn channel<Req, Resp>(buffer: usize) -> (Sender<Req, Resp>, Receiver<Req, Resp>) {
    let (inner_sender, inner_receiver) = mpsc::channel(buffer);
    (Sender { inner: inner_sender }, Receiver { inner: inner_receiver })
}

#[cfg(test)]
mod tests {
    use super::*;
    use fuchsia_async as fasync;
    use futures::StreamExt;
    use std::pin::pin;

    macro_rules! unwrap_ready {
        ($poll:expr) => {
            match $poll {
                Poll::Ready(value) => value,
                Poll::Pending => panic!("not ready"),
            }
        };
    }

    #[test]
    fn sender_receives_response() {
        let mut ex = fasync::TestExecutor::new();
        let (mut sender, mut receiver) = channel(0);

        let received = receiver.next();
        let mut received = pin!(received);
        assert!(ex.run_until_stalled(&mut received).is_pending());

        let request = sender.request(());
        let mut request = pin!(request);
        assert!(ex.run_until_stalled(&mut request).is_pending());

        let ((), responder) = unwrap_ready!(ex.run_until_stalled(&mut received)).unwrap();

        assert!(ex.run_until_stalled(&mut request).is_pending());

        responder.respond(()).unwrap();

        unwrap_ready!(ex.run_until_stalled(&mut request)).unwrap();
    }

    #[test]
    fn cloned_senders_go_to_same_receiver() {
        let mut ex = fasync::TestExecutor::new();
        let (mut sender, mut receiver) = channel(0);
        let mut sender2 = sender.clone();

        let received = receiver.next();
        let mut received = pin!(received);
        assert!(ex.run_until_stalled(&mut received).is_pending());

        let request = sender.request(());
        let mut request = pin!(request);
        assert!(ex.run_until_stalled(&mut request).is_pending());

        let ((), responder) = unwrap_ready!(ex.run_until_stalled(&mut received)).unwrap();

        assert!(ex.run_until_stalled(&mut request).is_pending());

        responder.respond(()).unwrap();

        unwrap_ready!(ex.run_until_stalled(&mut request)).unwrap();

        let received = receiver.next();
        let mut received = pin!(received);
        assert!(ex.run_until_stalled(&mut received).is_pending());

        let request = sender2.request(());
        let mut request = pin!(request);
        assert!(ex.run_until_stalled(&mut request).is_pending());

        let ((), responder) = unwrap_ready!(ex.run_until_stalled(&mut received)).unwrap();

        assert!(ex.run_until_stalled(&mut request).is_pending());

        responder.respond(()).unwrap();

        unwrap_ready!(ex.run_until_stalled(&mut request)).unwrap();
    }

    #[test]
    fn sender_receives_error_on_dropped_receiver() {
        let mut ex = fasync::TestExecutor::new();
        let (mut sender, receiver) = channel::<(), ()>(0);

        let request = sender.request(());
        let mut request = pin!(request);
        assert!(ex.run_until_stalled(&mut request).is_pending());

        drop(receiver);

        assert!(unwrap_ready!(ex.run_until_stalled(&mut request)).is_err());
    }

    #[test]
    fn sender_receives_error_on_dropped_responder() {
        let mut ex = fasync::TestExecutor::new();
        let (mut sender, mut receiver) = channel::<(), ()>(0);

        let request = sender.request(());
        let mut request = pin!(request);
        assert!(ex.run_until_stalled(&mut request).is_pending());

        let received = receiver.next();
        let mut received = pin!(received);
        let ((), responder) = unwrap_ready!(ex.run_until_stalled(&mut received)).unwrap();

        assert!(ex.run_until_stalled(&mut request).is_pending());
        drop(responder);

        assert!(unwrap_ready!(ex.run_until_stalled(&mut request)).is_err());
    }

    #[test]
    fn receiver_receives_error_on_dropped_sender() {
        let mut ex = fasync::TestExecutor::new();
        let (sender, mut receiver) = channel::<(), ()>(0);

        let received = receiver.next();
        let mut received = pin!(received);
        assert!(ex.run_until_stalled(&mut received).is_pending());

        drop(sender);

        assert!(unwrap_ready!(ex.run_until_stalled(&mut received)).is_none());
    }

    #[test]
    fn responder_returns_error_on_dropped_sender() {
        let mut ex = fasync::TestExecutor::new();
        let (mut sender, mut receiver) = channel(0);

        {
            let request = sender.request(());
            let mut request = pin!(request);
            assert!(ex.run_until_stalled(&mut request).is_pending());
        } // request is dropped at the end of the block

        let received = receiver.next();
        let mut received = pin!(received);
        let ((), responder) = unwrap_ready!(ex.run_until_stalled(&mut received)).unwrap();

        drop(sender);

        assert!(responder.respond(()).is_err());
    }

    #[fasync::run_until_stalled(test)]
    async fn cannot_request_after_receiver_closed() {
        let (mut sender, mut receiver) = channel::<(), ()>(0);
        receiver.close();
        assert!(sender.request(()).await.is_err());
    }

    #[test]
    fn try_receive_returns_none_when_channel_is_empty() {
        let (_, mut receiver) = channel::<(), ()>(0);
        assert!(receiver.try_receive().unwrap().is_none());
    }

    #[test]
    fn try_receive_returns_none_after_none_result() {
        let (_, mut receiver) = channel::<(), ()>(0);
        assert!(receiver.try_receive().unwrap().is_none());
        assert!(receiver.try_receive().unwrap().is_none());
    }

    #[test]
    fn try_receive_returns_value_when_channel_has_value() {
        let mut ex = fasync::TestExecutor::new();
        let (mut sender, mut receiver) = channel::<(), ()>(0);

        let request = sender.request(());
        let mut request = pin!(request);
        assert!(ex.run_until_stalled(&mut request).is_pending());

        assert!(receiver.try_receive().unwrap().is_some());
    }
}