From 8b54a80f6e5b06a181f12f55179e0928a470e21b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=82=8E=E6=B3=BC?= Date: Thu, 14 Mar 2024 22:38:53 +0800 Subject: [PATCH] Feature: `Raft::config()` returns a ref to Config this raft node uses --- openraft/src/raft/mod.rs | 5 +++ tests/tests/management/main.rs | 10 ++++++ tests/tests/management/t10_raft_config.rs | 42 +++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 tests/tests/management/main.rs create mode 100644 tests/tests/management/t10_raft_config.rs diff --git a/openraft/src/raft/mod.rs b/openraft/src/raft/mod.rs index d180e05a9..b2dd99cb8 100644 --- a/openraft/src/raft/mod.rs +++ b/openraft/src/raft/mod.rs @@ -274,6 +274,11 @@ where C: RaftTypeConfig RuntimeConfigHandle::new(self.inner.as_ref()) } + /// Return the config of this Raft node. + pub fn config(&self) -> &Arc { + &self.inner.config + } + /// Enable or disable raft internal ticker. #[deprecated(since = "0.8.4", note = "use `Raft::runtime_config().tick()` instead")] pub fn enable_tick(&self, enabled: bool) { diff --git a/tests/tests/management/main.rs b/tests/tests/management/main.rs new file mode 100644 index 000000000..89663730d --- /dev/null +++ b/tests/tests/management/main.rs @@ -0,0 +1,10 @@ +#![cfg_attr(feature = "bt", feature(error_generic_member_access))] + +#[macro_use] +#[path = "../fixtures/mod.rs"] +mod fixtures; + +// The number indicate the preferred running order for these case. +// The later tests may depend on the earlier ones. + +mod t10_raft_config; diff --git a/tests/tests/management/t10_raft_config.rs b/tests/tests/management/t10_raft_config.rs new file mode 100644 index 000000000..6f7ecf4cc --- /dev/null +++ b/tests/tests/management/t10_raft_config.rs @@ -0,0 +1,42 @@ +use std::sync::Arc; + +use anyhow::Result; +use maplit::btreeset; +use openraft::Config; + +use crate::fixtures::init_default_ut_tracing; +use crate::fixtures::RaftRouter; + +/// Get config via [`Raft::config`](openraft::Raft::config) +#[async_entry::test(worker_threads = 4, init = "init_default_ut_tracing()", tracing_span = "debug")] +async fn raft_config() -> Result<()> { + let config = Arc::new( + Config { + enable_tick: false, + election_timeout_min: 123, + election_timeout_max: 124, + ..Default::default() + } + .validate()?, + ); + + let mut router = RaftRouter::new(config.clone()); + + tracing::info!("--- initializing cluster"); + let log_index = router.new_cluster(btreeset! {0}, btreeset! {}).await?; + + tracing::info!(log_index, "--- get config"); + { + let n0 = router.get_raft_handle(&0)?; + let c = n0.config(); + + #[allow(clippy::bool_assert_comparison)] + { + assert_eq!(c.enable_tick, false); + } + assert_eq!(c.election_timeout_min, 123); + assert_eq!(c.election_timeout_max, 124); + } + + Ok(()) +}