diff --git a/crates/language/src/tests.rs b/crates/language/src/tests.rs index 34062ee601..3783f1e66d 100644 --- a/crates/language/src/tests.rs +++ b/crates/language/src/tests.rs @@ -11,8 +11,9 @@ use std::{ rc::Rc, time::{Duration, Instant}, }; +use text::network::Network; use unindent::Unindent as _; -use util::{post_inc, test::Network}; +use util::post_inc; #[cfg(test)] #[ctor::ctor] diff --git a/crates/text/src/network.rs b/crates/text/src/network.rs new file mode 100644 index 0000000000..2f49756ca3 --- /dev/null +++ b/crates/text/src/network.rs @@ -0,0 +1,69 @@ +use clock::ReplicaId; + +pub struct Network { + inboxes: std::collections::BTreeMap>>, + all_messages: Vec, + rng: R, +} + +#[derive(Clone)] +struct Envelope { + message: T, +} + +impl Network { + pub fn new(rng: R) -> Self { + Network { + inboxes: Default::default(), + all_messages: Vec::new(), + rng, + } + } + + pub fn add_peer(&mut self, id: ReplicaId) { + self.inboxes.insert(id, Vec::new()); + } + + pub fn replicate(&mut self, old_replica_id: ReplicaId, new_replica_id: ReplicaId) { + self.inboxes + .insert(new_replica_id, self.inboxes[&old_replica_id].clone()); + } + + pub fn is_idle(&self) -> bool { + self.inboxes.values().all(|i| i.is_empty()) + } + + pub fn broadcast(&mut self, sender: ReplicaId, messages: Vec) { + for (replica, inbox) in self.inboxes.iter_mut() { + if *replica != sender { + for message in &messages { + // Insert one or more duplicates of this message, potentially *before* the previous + // message sent by this peer to simulate out-of-order delivery. + for _ in 0..self.rng.gen_range(1..4) { + let insertion_index = self.rng.gen_range(0..inbox.len() + 1); + inbox.insert( + insertion_index, + Envelope { + message: message.clone(), + }, + ); + } + } + } + } + self.all_messages.extend(messages); + } + + pub fn has_unreceived(&self, receiver: ReplicaId) -> bool { + !self.inboxes[&receiver].is_empty() + } + + pub fn receive(&mut self, receiver: ReplicaId) -> Vec { + let inbox = self.inboxes.get_mut(&receiver).unwrap(); + let count = self.rng.gen_range(0..inbox.len() + 1); + inbox + .drain(0..count) + .map(|envelope| envelope.message) + .collect() + } +} diff --git a/crates/text/src/tests.rs b/crates/text/src/tests.rs index 4f5e6effb6..05cf0af6ec 100644 --- a/crates/text/src/tests.rs +++ b/crates/text/src/tests.rs @@ -1,4 +1,4 @@ -use super::*; +use super::{network::Network, *}; use clock::ReplicaId; use rand::prelude::*; use std::{ @@ -7,7 +7,6 @@ use std::{ iter::Iterator, time::{Duration, Instant}, }; -use util::test::Network; #[cfg(test)] #[ctor::ctor] diff --git a/crates/text/src/text.rs b/crates/text/src/text.rs index 89943777e0..849d6326f2 100644 --- a/crates/text/src/text.rs +++ b/crates/text/src/text.rs @@ -1,5 +1,7 @@ mod anchor; pub mod locator; +#[cfg(any(test, feature = "test-support"))] +pub mod network; pub mod operation_queue; mod patch; mod point; diff --git a/crates/util/Cargo.toml b/crates/util/Cargo.toml index 634e031aee..9d39fb04e2 100644 --- a/crates/util/Cargo.toml +++ b/crates/util/Cargo.toml @@ -7,10 +7,9 @@ edition = "2021" doctest = false [features] -test-support = ["clock", "rand", "serde_json", "tempdir"] +test-support = ["rand", "serde_json", "tempdir"] [dependencies] -clock = { path = "../clock", optional = true } anyhow = "1.0.38" futures = "0.3" log = "0.4" diff --git a/crates/util/src/test.rs b/crates/util/src/test.rs index 73b5461261..71b847df69 100644 --- a/crates/util/src/test.rs +++ b/crates/util/src/test.rs @@ -1,75 +1,6 @@ -use clock::ReplicaId; use std::path::{Path, PathBuf}; use tempdir::TempDir; -#[derive(Clone)] -struct Envelope { - message: T, -} - -pub struct Network { - inboxes: std::collections::BTreeMap>>, - all_messages: Vec, - rng: R, -} - -impl Network { - pub fn new(rng: R) -> Self { - Network { - inboxes: Default::default(), - all_messages: Vec::new(), - rng, - } - } - - pub fn add_peer(&mut self, id: ReplicaId) { - self.inboxes.insert(id, Vec::new()); - } - - pub fn replicate(&mut self, old_replica_id: ReplicaId, new_replica_id: ReplicaId) { - self.inboxes - .insert(new_replica_id, self.inboxes[&old_replica_id].clone()); - } - - pub fn is_idle(&self) -> bool { - self.inboxes.values().all(|i| i.is_empty()) - } - - pub fn broadcast(&mut self, sender: ReplicaId, messages: Vec) { - for (replica, inbox) in self.inboxes.iter_mut() { - if *replica != sender { - for message in &messages { - // Insert one or more duplicates of this message, potentially *before* the previous - // message sent by this peer to simulate out-of-order delivery. - for _ in 0..self.rng.gen_range(1..4) { - let insertion_index = self.rng.gen_range(0..inbox.len() + 1); - inbox.insert( - insertion_index, - Envelope { - message: message.clone(), - }, - ); - } - } - } - } - self.all_messages.extend(messages); - } - - pub fn has_unreceived(&self, receiver: ReplicaId) -> bool { - !self.inboxes[&receiver].is_empty() - } - - pub fn receive(&mut self, receiver: ReplicaId) -> Vec { - let inbox = self.inboxes.get_mut(&receiver).unwrap(); - let count = self.rng.gen_range(0..inbox.len() + 1); - inbox - .drain(0..count) - .map(|envelope| envelope.message) - .collect() - } -} - pub fn temp_tree(tree: serde_json::Value) -> TempDir { let dir = TempDir::new("").unwrap(); write_tree(dir.path(), tree);