bt_bass/
test_utils.rs

1// Copyright 2023 Google LLC
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use std::task::Poll;
6
7use futures::channel::mpsc::UnboundedReceiver;
8use futures::stream::FusedStream;
9use futures::{Stream, StreamExt};
10
11use crate::client::error::Error;
12use crate::client::event::*;
13
14pub struct FakeBassEventStream {
15    receiver: UnboundedReceiver<Result<Event, Error>>,
16    terminated: bool,
17}
18
19impl FakeBassEventStream {
20    pub fn new(receiver: UnboundedReceiver<Result<Event, Error>>) -> Self {
21        Self { receiver, terminated: false }
22    }
23}
24
25impl FusedStream for FakeBassEventStream {
26    fn is_terminated(&self) -> bool {
27        self.terminated
28    }
29}
30
31impl Stream for FakeBassEventStream {
32    type Item = Result<Event, Error>;
33
34    fn poll_next(
35        mut self: std::pin::Pin<&mut Self>,
36        cx: &mut std::task::Context<'_>,
37    ) -> Poll<Option<Self::Item>> {
38        if self.terminated {
39            return Poll::Ready(None);
40        }
41        let polled = self.receiver.poll_next_unpin(cx);
42        if let Poll::Ready(Some(Err(_))) = &polled {
43            self.terminated = true;
44        }
45        polled
46    }
47}