Skip to content

Commit

Permalink
chore: add test utils for xline-client
Browse files Browse the repository at this point in the history
  • Loading branch information
bsbds committed Jun 7, 2023
1 parent a99a5eb commit 8bf8022
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 0 deletions.
1 change: 1 addition & 0 deletions xline-client/tests/auth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod common;
1 change: 1 addition & 0 deletions xline-client/tests/cluster.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod common;
124 changes: 124 additions & 0 deletions xline-client/tests/common/cluster.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#![allow(dead_code, unused)]

use std::{
collections::{BTreeMap, HashMap},
path::PathBuf,
sync::Arc,
};

use curp::members::ClusterMember;
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use tokio::{
net::TcpListener,
sync::broadcast::{self, Sender},
time::{self, Duration},
};
use utils::config::{ClientTimeout, CurpConfig, ServerTimeout, StorageConfig};
use xline::{server::XlineServer, storage::db::DB};

/// Cluster
pub struct Cluster {
/// listeners of members
listeners: BTreeMap<usize, TcpListener>,
/// address of members
all_members: HashMap<String, String>,
/// Stop sender
stop_tx: Option<Sender<()>>,
/// Cluster size
size: usize,
/// storage paths
paths: Vec<PathBuf>,
}

impl Cluster {
/// New `Cluster`
pub(crate) async fn new(size: usize) -> Self {
let mut listeners = BTreeMap::new();
for i in 0..size {
listeners.insert(i, TcpListener::bind("0.0.0.0:0").await.unwrap());
}
let all_members: HashMap<String, String> = listeners
.iter()
.map(|(i, l)| (format!("server{}", i), l.local_addr().unwrap().to_string()))
.collect();

Self {
listeners,
all_members,
stop_tx: None,
size,
paths: vec![],
}
}

/// Start `Cluster`
pub(crate) async fn start(&mut self) {
let (stop_tx, _) = broadcast::channel(1);

for i in 0..self.size {
let name = format!("server{}", i);
let is_leader = i == 0;
let mut rx = stop_tx.subscribe();
let listener = self.listeners.remove(&i).unwrap();
let path = if let Some(path) = self.paths.get(i) {
path.clone()
} else {
let path = PathBuf::from(format!("/tmp/xline-{}", random_id()));
self.paths.push(path.clone());
path
};
#[allow(clippy::unwrap_used)]
let db: Arc<DB> = DB::open(&StorageConfig::RocksDB(path.clone())).unwrap();
let cluster_info = ClusterMember::new(self.all_members.clone(), name.clone());
tokio::spawn(async move {
let server = XlineServer::new(
cluster_info.into(),
is_leader,
CurpConfig {
data_dir: path.join("curp"),
..Default::default()
},
ClientTimeout::default(),
ServerTimeout::default(),
StorageConfig::Memory,
);
let signal = async {
let _ = rx.recv().await;
};
let result = server
.start_from_listener_shutdown(listener, signal, db, None)
.await;
if let Err(e) = result {
panic!("Server start error: {e}");
}
});
}
self.stop_tx = Some(stop_tx);
// Sleep 30ms, wait for the server to start
time::sleep(Duration::from_millis(300)).await;
}

#[allow(dead_code)] // used in tests but get warning
pub fn addrs(&self) -> &HashMap<String, String> {
&self.all_members
}
}

impl Drop for Cluster {
fn drop(&mut self) {
if let Some(ref stop_tx) = self.stop_tx {
let _ = stop_tx.send(());
}
for path in &self.paths {
let _ignore = std::fs::remove_dir_all(path);
}
}
}

fn random_id() -> String {
thread_rng()
.sample_iter(&Alphanumeric)
.take(8)
.map(char::from)
.collect()
}
1 change: 1 addition & 0 deletions xline-client/tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod cluster;
1 change: 1 addition & 0 deletions xline-client/tests/election.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod common;
1 change: 1 addition & 0 deletions xline-client/tests/kv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod common;
1 change: 1 addition & 0 deletions xline-client/tests/lease.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod common;
1 change: 1 addition & 0 deletions xline-client/tests/lock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod common;
1 change: 1 addition & 0 deletions xline-client/tests/maintenance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod common;
1 change: 1 addition & 0 deletions xline-client/tests/watch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod common;

0 comments on commit 8bf8022

Please sign in to comment.