Skip to content

Commit 2ae5a7b

Browse files
authored
Merge pull request #451 from Migorithm/fix/450
fix: electiont test
2 parents 885daaa + 5a7b460 commit 2ae5a7b

3 files changed

Lines changed: 16 additions & 7 deletions

File tree

duva/src/domains/cluster_actors/heartbeats/scheduler.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use crate::domains::cluster_actors::commands::ClusterCommand;
2-
use std::time::Duration;
2+
use std::{ops::Range, time::Duration};
33
use tokio::{select, sync::mpsc::Sender, time::interval};
44
const LEADER_HEARTBEAT_INTERVAL: u64 = 300;
5+
pub const LEADER_HEARTBEAT_INTERVAL_MAX: u64 = LEADER_HEARTBEAT_INTERVAL * 5;
6+
const LEADER_HEARTBEAT_INTERVAL_RANGE: Range<u64> =
7+
LEADER_HEARTBEAT_INTERVAL * 3..LEADER_HEARTBEAT_INTERVAL_MAX;
58

69
#[derive(Debug)]
710
pub(crate) struct HeartBeatScheduler {
@@ -80,7 +83,7 @@ impl HeartBeatScheduler {
8083
ElectionTimeOutCommand::Ping => {},
8184
}
8285
},
83-
_ = tokio::time::sleep(Duration::from_millis(rand::random_range(LEADER_HEARTBEAT_INTERVAL*3..LEADER_HEARTBEAT_INTERVAL*5)))=>{
86+
_ = tokio::time::sleep(Duration::from_millis(rand::random_range(LEADER_HEARTBEAT_INTERVAL_RANGE)))=>{
8487
eprintln!("\x1b[33m[WARN] Election timeout\x1b[0m");
8588
let _ = cluster_handler.send(ClusterCommand::StartLeaderElection).await;
8689

duva/tests/common.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ pub fn run_server_process(env: &ServerEnv) -> TestProcessChild {
186186
let mut command = Command::new("cargo");
187187
command.args([
188188
"run",
189+
"-p",
190+
"duva",
189191
"--",
190192
"--port",
191193
&env.port.to_string(),

duva/tests/replication_ops/test_leader_election.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use std::{thread::sleep, time::Duration};
22

33
use crate::common::{ServerEnv, array, check_internodes_communication, spawn_server_process};
4-
use duva::clients::ClientStreamHandler;
4+
use duva::{
5+
clients::ClientStreamHandler,
6+
domains::cluster_actors::heartbeats::scheduler::LEADER_HEARTBEAT_INTERVAL_MAX,
7+
};
58

69
#[tokio::test]
710
async fn test_leader_election() {
@@ -22,7 +25,7 @@ async fn test_leader_election() {
2225

2326
// WHEN
2427
leader_p.kill().unwrap();
25-
sleep(Duration::from_secs(2));
28+
sleep(Duration::from_millis(LEADER_HEARTBEAT_INTERVAL_MAX));
2629

2730
// THEN
2831
let mut flag = false;
@@ -59,7 +62,7 @@ async fn test_set_twice_after_election() {
5962

6063
// WHEN
6164
leader_p.kill().unwrap();
62-
sleep(Duration::from_secs(1));
65+
sleep(Duration::from_millis(LEADER_HEARTBEAT_INTERVAL_MAX));
6366

6467
let mut flag = false;
6568
for f in [&follower_p1, &follower_p2] {
@@ -96,7 +99,7 @@ async fn test_leader_election_twice() {
9699

97100
// !first leader is killed -> election happens
98101
leader_p.kill().unwrap();
99-
sleep(Duration::from_secs(1));
102+
sleep(Duration::from_millis(LEADER_HEARTBEAT_INTERVAL_MAX));
100103

101104
let mut processes = vec![];
102105

@@ -109,11 +112,12 @@ async fn test_leader_election_twice() {
109112
}
110113
let new_process =
111114
spawn_server_process(&ServerEnv::default().with_leader_bind_addr(f.bind_addr().into()));
112-
sleep(Duration::from_secs(1));
115+
sleep(Duration::from_millis(LEADER_HEARTBEAT_INTERVAL_MAX));
113116

114117
// WHEN
115118
// ! second leader is killed -> election happens
116119
f.kill().unwrap();
120+
sleep(Duration::from_millis(LEADER_HEARTBEAT_INTERVAL_MAX));
117121
processes.push(new_process);
118122
}
119123
assert_eq!(processes.len(), 2);

0 commit comments

Comments
 (0)