Skip to content

Commit

Permalink
WIP Create a MockedClientHandle helper type
Browse files Browse the repository at this point in the history
Used to create and then track a mock `Client` instance.
  • Loading branch information
jvff committed Dec 3, 2021
1 parent 41fe088 commit 0f1e426
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
3 changes: 3 additions & 0 deletions zebra-network/src/peer_set/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ use crate::{
AddressBook, BoxError, Config,
};

#[cfg(test)]
mod tests;

/// A signal sent by the [`PeerSet`] when it has no ready peers, and gets a request from Zebra.
///
/// In response to this signal, the crawler tries to open more peer connections.
Expand Down
51 changes: 51 additions & 0 deletions zebra-network/src/peer_set/set/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use futures::channel::{mpsc, oneshot};
use proptest::prelude::*;

use crate::{
peer::{Client, ClientRequest, ErrorSlot, LoadTrackedClient},
protocol::external::types::Version,
};

/// A handle to a mocked [`Client`] instance.
struct MockedClientHandle {
request_receiver: mpsc::Receiver<ClientRequest>,
shutdown_receiver: oneshot::Receiver<()>,
version: Version,
}

impl MockedClientHandle {
/// Create a new mocked [`Client`] instance, returning it together with a handle to track it.
pub fn new(version: Version) -> (Self, LoadTrackedClient) {
let (shutdown_sender, shutdown_receiver) = oneshot::channel();
let (request_sender, request_receiver) = mpsc::channel(1);

let client = Client {
shutdown_tx: Some(shutdown_sender),
server_tx: request_sender,
error_slot: ErrorSlot::default(),
version,
};

let handle = MockedClientHandle {
request_receiver,
shutdown_receiver,
version,
};

(handle, client.into())
}

/// Gets the peer protocol version associated to the [`Client`].
pub fn version(&self) -> Version {
self.version
}

/// Checks if the [`Client`] instance has not been dropped, which would have disconnected from
/// the peer.
pub fn is_connected(&mut self) -> bool {
match self.shutdown_receiver.try_recv() {
Ok(None) => true,
Ok(Some(())) | Err(oneshot::Canceled) => false,
}
}
}

0 comments on commit 0f1e426

Please sign in to comment.