-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP Create a
MockedClientHandle
helper type
Used to create and then track a mock `Client` instance.
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} | ||
} |