Skip to content

Commit 8b54a80

Browse files
committed
Feature: Raft::config() returns a ref to Config this raft node uses
1 parent 4cfe30e commit 8b54a80

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

openraft/src/raft/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,11 @@ where C: RaftTypeConfig
274274
RuntimeConfigHandle::new(self.inner.as_ref())
275275
}
276276

277+
/// Return the config of this Raft node.
278+
pub fn config(&self) -> &Arc<Config> {
279+
&self.inner.config
280+
}
281+
277282
/// Enable or disable raft internal ticker.
278283
#[deprecated(since = "0.8.4", note = "use `Raft::runtime_config().tick()` instead")]
279284
pub fn enable_tick(&self, enabled: bool) {

tests/tests/management/main.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![cfg_attr(feature = "bt", feature(error_generic_member_access))]
2+
3+
#[macro_use]
4+
#[path = "../fixtures/mod.rs"]
5+
mod fixtures;
6+
7+
// The number indicate the preferred running order for these case.
8+
// The later tests may depend on the earlier ones.
9+
10+
mod t10_raft_config;
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use std::sync::Arc;
2+
3+
use anyhow::Result;
4+
use maplit::btreeset;
5+
use openraft::Config;
6+
7+
use crate::fixtures::init_default_ut_tracing;
8+
use crate::fixtures::RaftRouter;
9+
10+
/// Get config via [`Raft::config`](openraft::Raft::config)
11+
#[async_entry::test(worker_threads = 4, init = "init_default_ut_tracing()", tracing_span = "debug")]
12+
async fn raft_config() -> Result<()> {
13+
let config = Arc::new(
14+
Config {
15+
enable_tick: false,
16+
election_timeout_min: 123,
17+
election_timeout_max: 124,
18+
..Default::default()
19+
}
20+
.validate()?,
21+
);
22+
23+
let mut router = RaftRouter::new(config.clone());
24+
25+
tracing::info!("--- initializing cluster");
26+
let log_index = router.new_cluster(btreeset! {0}, btreeset! {}).await?;
27+
28+
tracing::info!(log_index, "--- get config");
29+
{
30+
let n0 = router.get_raft_handle(&0)?;
31+
let c = n0.config();
32+
33+
#[allow(clippy::bool_assert_comparison)]
34+
{
35+
assert_eq!(c.enable_tick, false);
36+
}
37+
assert_eq!(c.election_timeout_min, 123);
38+
assert_eq!(c.election_timeout_max, 124);
39+
}
40+
41+
Ok(())
42+
}

0 commit comments

Comments
 (0)