use std::sync::Mutex;
use dactor::node::NodeId;
pub struct MockNetwork {
partitions: Mutex<Vec<(NodeId, NodeId)>>,
delivered: Mutex<u64>,
dropped: Mutex<u64>,
}
impl MockNetwork {
pub fn new() -> Self {
Self {
partitions: Mutex::new(Vec::new()),
delivered: Mutex::new(0),
dropped: Mutex::new(0),
}
}
pub fn partition(&self, a: &NodeId, b: &NodeId) {
let mut parts = self.partitions.lock().unwrap();
parts.push((a.clone(), b.clone()));
}
pub fn remove_partition(&self, a: &NodeId, b: &NodeId) {
let mut parts = self.partitions.lock().unwrap();
parts.retain(|(x, y)| !((x == a && y == b) || (x == b && y == a)));
}
pub fn is_partitioned(&self, a: &NodeId, b: &NodeId) -> bool {
let parts = self.partitions.lock().unwrap();
parts
.iter()
.any(|(x, y)| (x == a && y == b) || (x == b && y == a))
}
pub fn delivered_count(&self) -> u64 {
*self.delivered.lock().unwrap()
}
pub fn dropped_count(&self) -> u64 {
*self.dropped.lock().unwrap()
}
pub fn can_deliver(&self, src: &NodeId, dst: &NodeId) -> bool {
if src == dst {
return true;
}
!self.is_partitioned(src, dst)
}
pub fn record_delivered(&self) {
*self.delivered.lock().unwrap() += 1;
}
pub fn record_dropped(&self) {
*self.dropped.lock().unwrap() += 1;
}
}
impl Default for MockNetwork {
fn default() -> Self {
Self::new()
}
}