use futures::StreamExt;
use crate::{Behavior, BehaviorOutput, InterfaceCommand, OutboundQueue, PeerId};
impl PeerId {
pub fn test(id: u8) -> Self {
PeerId {
host: format!("10.0.0.{}", id),
port: 3000 + id as u16,
}
}
}
impl<B: Behavior> OutboundQueue<B> {
pub fn drain_ready(&mut self) -> Vec<BehaviorOutput<B>> {
let mut outputs = Vec::new();
let waker = futures::task::noop_waker();
let mut cx = std::task::Context::from_waker(&waker);
while let std::task::Poll::Ready(Some(output)) = self.futures.poll_next_unpin(&mut cx) {
outputs.push(output);
}
outputs
}
}
pub(crate) trait BehaviorOutputExt<B: Behavior> {
fn has_connect_for(&self, pid: &PeerId) -> bool;
fn has_disconnect_for(&self, pid: &PeerId) -> bool;
fn has_send<F>(&self, pred: F) -> bool
where
F: Fn(&B::Message) -> bool;
fn has_event<F>(&self, pred: F) -> bool
where
F: Fn(&B::Event) -> bool;
}
impl<B: Behavior> BehaviorOutputExt<B> for [BehaviorOutput<B>] {
fn has_connect_for(&self, pid: &PeerId) -> bool {
self.iter().any(|o| {
matches!(o, BehaviorOutput::InterfaceCommand(InterfaceCommand::Connect(p)) if p == pid)
})
}
fn has_disconnect_for(&self, pid: &PeerId) -> bool {
self.iter().any(|o| {
matches!(o, BehaviorOutput::InterfaceCommand(InterfaceCommand::Disconnect(p)) if p == pid)
})
}
fn has_send<F>(&self, pred: F) -> bool
where
F: Fn(&B::Message) -> bool,
{
self.iter().any(|o| match o {
BehaviorOutput::InterfaceCommand(InterfaceCommand::Send(_, msg)) => pred(msg),
_ => false,
})
}
fn has_event<F>(&self, pred: F) -> bool
where
F: Fn(&B::Event) -> bool,
{
self.iter().any(|o| match o {
BehaviorOutput::ExternalEvent(e) => pred(e),
_ => false,
})
}
}