soda_pool/
mock.rs

1//! Mock implementation of the `ChannelPool` trait for testing purposes.
2//!
3//! This implementation is useful for unit testing. It should not conceivably be used in production.
4
5use std::net::{IpAddr, Ipv4Addr};
6
7use tonic::{async_trait, transport::Channel};
8
9use crate::ChannelPool;
10
11/// Mock implementation of the `ChannelPool` trait for unit testing.
12#[derive(Clone, Debug)]
13pub struct MockChannelPool {
14    channel: Channel,
15}
16
17impl MockChannelPool {
18    /// Creates a new [`MockChannelPool`] with the given channel.
19    ///
20    /// The passed here channel will always be returned by the [`get_channel`](MockChannelPool::get_channel)
21    /// method, together with IPv4 localhost address. While any channel will do,
22    /// one way is to use a local stream as shown in the [tonic's
23    /// example](https://github.com/hyperium/tonic/blob/master/examples/src/mock/mock.rs).
24    #[must_use]
25    pub fn new(channel: Channel) -> Self {
26        Self { channel }
27    }
28}
29
30#[async_trait]
31impl ChannelPool for MockChannelPool {
32    /// Returns a channel and an IP address (IPv4 localhost) for testing purposes.
33    ///
34    /// This method always returns the same channel that was provided during
35    /// the creation of the [`MockChannelPool`], along with the IPv4 localhost
36    /// address.
37    async fn get_channel(&self) -> Option<(IpAddr, Channel)> {
38        Some((IpAddr::V4(Ipv4Addr::LOCALHOST), self.channel.clone()))
39    }
40
41    /// Reports the given IP address as broken.
42    ///
43    /// This is a no-op in the mock implementation.
44    async fn report_broken(&self, _ip_address: IpAddr) {
45        // No-op
46    }
47}