Przeglądaj źródła

Merge sleep utility functions together.

Jing Yang 4 lat temu
rodzic
commit
f7bbfbadef

+ 2 - 3
kvraft/tests/service_test.rs

@@ -2,12 +2,11 @@ use std::sync::atomic::{AtomicUsize, Ordering};
 use std::sync::Arc;
 
 use scopeguard::defer;
-use test_configs::kvraft::config::{
-    make_config, sleep_election_timeouts, sleep_millis,
-};
+use test_configs::kvraft::config::make_config;
 use test_configs::kvraft::generic_test::{
     generic_test, spawn_clients, GenericTestParams,
 };
+use test_configs::utils::{sleep_election_timeouts, sleep_millis};
 use test_utils::init_test_log;
 use test_utils::thread_local_logger::LocalLogger;
 

+ 0 - 9
test_configs/src/kvraft/config.rs

@@ -316,12 +316,3 @@ pub fn make_config(
 
     cfg
 }
-
-pub fn sleep_millis(mills: u64) {
-    std::thread::sleep(std::time::Duration::from_millis(mills))
-}
-
-pub const LONG_ELECTION_TIMEOUT_MILLIS: u64 = 1000;
-pub fn sleep_election_timeouts(count: u64) {
-    sleep_millis(LONG_ELECTION_TIMEOUT_MILLIS * count)
-}

+ 4 - 4
test_configs/src/kvraft/generic_test.rs

@@ -9,9 +9,9 @@ use test_utils::thread_local_logger::LocalLogger;
 
 use linearizability::{KvInput, KvModel, KvOp, KvOutput, Operation};
 
-use super::config::{
-    make_config, sleep_election_timeouts, sleep_millis, Config,
-    LONG_ELECTION_TIMEOUT_MILLIS,
+use super::config::{make_config, Config};
+use crate::utils::{
+    sleep_election_timeouts, sleep_millis, LONG_ELECTION_TIMEOUT_MILLIS,
 };
 use kvraft::Clerk;
 
@@ -129,7 +129,7 @@ fn run_partition(cfg: Arc<Config>, stop: Arc<AtomicBool>) {
             LONG_ELECTION_TIMEOUT_MILLIS
                 ..LONG_ELECTION_TIMEOUT_MILLIS + PARTITION_MAX_DELAY_MILLIS,
         );
-        std::thread::sleep(Duration::from_millis(delay));
+        sleep_millis(delay);
     }
 }
 

+ 1 - 0
test_configs/src/lib.rs

@@ -2,6 +2,7 @@ pub mod kvraft;
 mod persister;
 pub mod raft;
 mod rpcs;
+pub mod utils;
 
 pub use persister::Persister;
 use rpcs::{register_kv_server, register_server, RpcClient};

+ 1 - 9
test_configs/src/raft/config.rs

@@ -10,6 +10,7 @@ use parking_lot::Mutex;
 use rand::{thread_rng, Rng};
 
 use crate::register_server;
+use crate::utils::sleep_millis;
 use ruaft::{ApplyCommandMessage, Persister, Raft, Term};
 
 struct ConfigState {
@@ -497,12 +498,3 @@ pub fn make_config(
 
     cfg
 }
-
-pub fn sleep_millis(mills: u64) {
-    std::thread::sleep(Duration::from_millis(mills))
-}
-
-pub const LONG_ELECTION_TIMEOUT_MILLIS: u64 = 1000;
-pub fn sleep_election_timeouts(count: u64) {
-    sleep_millis(LONG_ELECTION_TIMEOUT_MILLIS * count)
-}

+ 10 - 0
test_configs/src/utils.rs

@@ -0,0 +1,10 @@
+use std::time::Duration;
+
+pub fn sleep_millis(mills: u64) {
+    std::thread::sleep(Duration::from_millis(mills))
+}
+
+pub const LONG_ELECTION_TIMEOUT_MILLIS: u64 = 1000;
+pub fn sleep_election_timeouts(count: u64) {
+    sleep_millis(LONG_ELECTION_TIMEOUT_MILLIS * count)
+}

+ 8 - 7
tests/agreement_tests.rs

@@ -1,6 +1,7 @@
 #![allow(clippy::identity_op)]
 use rand::{thread_rng, Rng};
 use scopeguard::defer;
+use test_configs::utils::{sleep_election_timeouts, sleep_millis};
 use test_configs::{make_config, raft::config};
 
 #[test]
@@ -43,7 +44,7 @@ fn fail_agree() -> config::Result<()> {
     // agree despite one disconnected server?
     cfg.one(102, SERVERS - 1, false)?;
     cfg.one(103, SERVERS - 1, false)?;
-    config::sleep_election_timeouts(1);
+    sleep_election_timeouts(1);
     cfg.one(104, SERVERS - 1, false)?;
     cfg.one(105, SERVERS - 1, false)?;
 
@@ -52,7 +53,7 @@ fn fail_agree() -> config::Result<()> {
 
     // agree with full set of servers?
     cfg.one(106, SERVERS, true)?;
-    config::sleep_election_timeouts(1);
+    sleep_election_timeouts(1);
     cfg.one(107, SERVERS, true)?;
 
     cfg.end();
@@ -80,7 +81,7 @@ fn fail_no_agree() -> config::Result<()> {
     let index = result.unwrap().1;
     assert_eq!(2, index, "expected index 2, got {}", index);
 
-    config::sleep_election_timeouts(2);
+    sleep_election_timeouts(2);
 
     let (commit_count, _) = cfg.committed_count(index)?;
     assert_eq!(
@@ -171,7 +172,7 @@ fn backup() -> config::Result<()> {
         cfg.leader_start(leader1, thread_rng().gen());
     }
 
-    config::sleep_election_timeouts(2);
+    sleep_election_timeouts(2);
 
     cfg.disconnect((leader1 + 0) % SERVERS);
     cfg.disconnect((leader1 + 1) % SERVERS);
@@ -199,7 +200,7 @@ fn backup() -> config::Result<()> {
         cfg.leader_start(leader2, thread_rng().gen());
     }
 
-    config::sleep_election_timeouts(2);
+    sleep_election_timeouts(2);
 
     // bring original leader back to life,
     for i in 0..SERVERS {
@@ -247,7 +248,7 @@ fn count() -> config::Result<()> {
             break (false, 0);
         }
         if retries != 0 {
-            config::sleep_millis(3000);
+            sleep_millis(3000);
         }
         retries += 1;
 
@@ -318,7 +319,7 @@ fn count() -> config::Result<()> {
 
     assert!(success, "term change too often");
 
-    config::sleep_election_timeouts(1);
+    sleep_election_timeouts(1);
 
     let diff = cfg.total_rpcs() - total;
     assert!(

+ 3 - 2
tests/election_tests.rs

@@ -1,4 +1,5 @@
 use scopeguard::defer;
+use test_configs::utils::{sleep_election_timeouts, sleep_millis};
 use test_configs::{make_config, raft::config};
 
 #[test]
@@ -11,10 +12,10 @@ fn initial_election() -> config::Result<()> {
 
     cfg.check_one_leader()?;
 
-    config::sleep_millis(50);
+    sleep_millis(50);
 
     let first_term = cfg.check_terms()?;
-    config::sleep_election_timeouts(2);
+    sleep_election_timeouts(2);
 
     let second_term = cfg.check_terms()?;
 

+ 13 - 10
tests/persist_tests.rs

@@ -6,6 +6,9 @@ use std::sync::Arc;
 
 use rand::{thread_rng, Rng};
 use scopeguard::defer;
+use test_configs::utils::{
+    sleep_election_timeouts, sleep_millis, LONG_ELECTION_TIMEOUT_MILLIS,
+};
 use test_configs::{make_config, raft::config};
 
 #[test]
@@ -88,7 +91,7 @@ fn persist2() -> config::Result<()> {
         cfg.connect((leader1 + 1) % SERVERS);
         cfg.connect((leader1 + 2) % SERVERS);
 
-        config::sleep_election_timeouts(1);
+        sleep_election_timeouts(1);
 
         cfg.start1((leader1 + 3) % SERVERS)?;
         cfg.connect((leader1 + 3) % SERVERS);
@@ -163,13 +166,13 @@ fn figure8() -> config::Result<()> {
         }
 
         let millis_upper = if thread_rng().gen_ratio(100, 1000) {
-            config::LONG_ELECTION_TIMEOUT_MILLIS >> 1
+            LONG_ELECTION_TIMEOUT_MILLIS >> 1
         } else {
             // Magic number 13?
             13
         };
         let millis = thread_rng().gen_range(0..millis_upper);
-        config::sleep_millis(millis);
+        sleep_millis(millis);
 
         if let Some(leader) = leader {
             cfg.crash1(leader);
@@ -261,13 +264,13 @@ fn figure8_unreliable() -> config::Result<()> {
         }
 
         let millis_upper = if thread_rng().gen_ratio(100, 1000) {
-            config::LONG_ELECTION_TIMEOUT_MILLIS >> 1
+            LONG_ELECTION_TIMEOUT_MILLIS >> 1
         } else {
             // Magic number 13?
             13
         };
         let millis = thread_rng().gen_range(0..millis_upper);
-        config::sleep_millis(millis);
+        sleep_millis(millis);
 
         if let Some(leader) = leader {
             if thread_rng().gen_ratio(1, 2) {
@@ -344,10 +347,10 @@ fn internal_churn(unreliable: bool) -> config::Result<()> {
                             }
                             // The contract we started might not get
                         }
-                        config::sleep_millis(*millis);
+                        sleep_millis(*millis);
                     }
                 } else {
-                    config::sleep_millis(79 + client_index * 17);
+                    sleep_millis(79 + client_index * 17);
                 }
             }
 
@@ -374,10 +377,10 @@ fn internal_churn(unreliable: bool) -> config::Result<()> {
                 cfg.crash1(server);
             }
         }
-        config::sleep_millis(config::LONG_ELECTION_TIMEOUT_MILLIS / 10 * 7);
+        sleep_millis(LONG_ELECTION_TIMEOUT_MILLIS / 10 * 7);
     }
 
-    config::sleep_election_timeouts(1);
+    sleep_election_timeouts(1);
     cfg.set_unreliable(false);
     for i in 0..SERVERS {
         if !cfg.is_server_alive(i) {
@@ -393,7 +396,7 @@ fn internal_churn(unreliable: bool) -> config::Result<()> {
         all_cmds.append(&mut cmds);
     }
 
-    config::sleep_election_timeouts(1);
+    sleep_election_timeouts(1);
 
     let last_cmd_index = cfg.one(thread_rng().gen(), SERVERS, true)?;
     let mut consented = vec![];

+ 2 - 1
tests/snapshot_tests.rs

@@ -2,8 +2,9 @@ use std::sync::Arc;
 
 use scopeguard::defer;
 
-use test_configs::kvraft::config::{make_config, sleep_election_timeouts};
+use test_configs::kvraft::config::make_config;
 use test_configs::kvraft::generic_test::{generic_test, GenericTestParams};
+use test_configs::utils::sleep_election_timeouts;
 use test_utils::init_test_log;
 
 #[test]