• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2023 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 //! Provides useful subscribers for testing specifically. These should not be used in normal code.
16 
17 use crate::*;
18 
19 /// Represents a callback called by a [BufferPublisher] on a [BufferSubscriber].
20 pub enum TestingSubscriberEvent {
21     /// Represents a call to [BufferSubscriber::on_subscribe].
22     Subscribe,
23     /// Represents a call to [BufferSubscriber::on_next].
24     Next(Frame),
25     /// Represents a call to [BufferSubscriber::on_error].
26     Error(BufferError),
27     /// Represents a call to [BufferSubscriber::on_complete].
28     Complete,
29 }
30 
31 /// A [BufferSubscriber] specifically for testing. Logs events as they happen which can be retrieved
32 /// by the test to ensure appropriate behavior.
33 pub struct TestSubscriber {
34     config: StreamConfig,
35     subscription: Option<Box<dyn BufferSubscription>>,
36     events: Vec<TestingSubscriberEvent>,
37 }
38 
39 impl TestSubscriber {
40     /// Create a new [TestSubscriber].
new(config: StreamConfig) -> Self41     pub fn new(config: StreamConfig) -> Self {
42         Self { config, subscription: None, events: Vec::new() }
43     }
44 
45     /// Returns true if this [BufferSubscriber] has an active subscription.
has_subscription(&self) -> bool46     pub fn has_subscription(&self) -> bool {
47         self.subscription.is_some()
48     }
49 
50     /// Make a request on behalf of this test subscriber.
51     ///
52     /// This will panic if there is no owned subscription.
request(&self, n: u64)53     pub fn request(&self, n: u64) {
54         let subscription = self
55             .subscription
56             .as_deref()
57             .expect("Tried to request on a TestSubscriber with no subscription");
58         subscription.request(n);
59     }
60 
61     /// Cancel on behalf of this test subscriber.
62     ///
63     /// # Panics
64     ///
65     /// This will panic if there is no owned subscription.
cancel(&self)66     pub fn cancel(&self) {
67         let subscription = self
68             .subscription
69             .as_deref()
70             .expect("Tried to cancel a TestSubscriber with no subscription");
71         subscription.cancel();
72     }
73 
74     /// Gets all of the events that have happened to this [BufferSubscriber] since the last call
75     /// to this function or it was created.
take_events(&mut self) -> Vec<TestingSubscriberEvent>76     pub fn take_events(&mut self) -> Vec<TestingSubscriberEvent> {
77         let mut out = Vec::new();
78         out.append(&mut self.events);
79         out
80     }
81 }
82 
83 impl BufferSubscriber for TestSubscriber {
get_subscriber_stream_config(&self) -> StreamConfig84     fn get_subscriber_stream_config(&self) -> StreamConfig {
85         self.config
86     }
87 
on_subscribe(&mut self, subscription: Box<dyn BufferSubscription>)88     fn on_subscribe(&mut self, subscription: Box<dyn BufferSubscription>) {
89         assert!(self.subscription.is_none(), "TestSubscriber must only be subscribed to once");
90         self.subscription = Some(subscription);
91 
92         self.events.push(TestingSubscriberEvent::Subscribe);
93     }
94 
on_next(&mut self, frame: Frame)95     fn on_next(&mut self, frame: Frame) {
96         self.events.push(TestingSubscriberEvent::Next(frame));
97     }
98 
on_error(&mut self, error: BufferError)99     fn on_error(&mut self, error: BufferError) {
100         self.events.push(TestingSubscriberEvent::Error(error));
101     }
102 
on_complete(&mut self)103     fn on_complete(&mut self) {
104         self.events.push(TestingSubscriberEvent::Complete);
105     }
106 }
107