Introduce a TestClient and associate it with a PeerId

This makes it easier to integration test peer interactions because now we know their PeerIds.
This commit is contained in:
Nathan Sobo 2021-11-27 12:33:25 -07:00
parent b307a7e91d
commit 4bd43e67ef
3 changed files with 156 additions and 96 deletions

View file

@ -8,6 +8,7 @@ use postage::{
prelude::{Sink as _, Stream as _},
};
use smol_timeout::TimeoutExt as _;
use std::sync::atomic::Ordering::SeqCst;
use std::{
collections::HashMap,
fmt,
@ -81,12 +82,12 @@ impl<T: RequestMessage> TypedEnvelope<T> {
}
pub struct Peer {
connections: RwLock<HashMap<ConnectionId, ConnectionState>>,
pub connections: RwLock<HashMap<ConnectionId, ConnectionState>>,
next_connection_id: AtomicU32,
}
#[derive(Clone)]
struct ConnectionState {
pub struct ConnectionState {
outgoing_tx: mpsc::Sender<proto::Envelope>,
next_message_id: Arc<AtomicU32>,
response_channels: Arc<Mutex<Option<HashMap<u32, mpsc::Sender<proto::Envelope>>>>>,
@ -110,10 +111,7 @@ impl Peer {
impl Future<Output = anyhow::Result<()>> + Send,
mpsc::Receiver<Box<dyn AnyTypedEnvelope>>,
) {
let connection_id = ConnectionId(
self.next_connection_id
.fetch_add(1, atomic::Ordering::SeqCst),
);
let connection_id = ConnectionId(self.next_connection_id.fetch_add(1, SeqCst));
let (mut incoming_tx, incoming_rx) = mpsc::channel(64);
let (outgoing_tx, mut outgoing_rx) = mpsc::channel(64);
let connection_state = ConnectionState {
@ -219,9 +217,7 @@ impl Peer {
let (tx, mut rx) = mpsc::channel(1);
async move {
let mut connection = this.connection_state(receiver_id).await?;
let message_id = connection
.next_message_id
.fetch_add(1, atomic::Ordering::SeqCst);
let message_id = connection.next_message_id.fetch_add(1, SeqCst);
connection
.response_channels
.lock()