Skip to content

Commit

Permalink
Feature: Raft::config() returns a ref to Config this raft node uses
Browse files Browse the repository at this point in the history
  • Loading branch information
drmingdrmer committed Mar 14, 2024
1 parent 87dfb38 commit 1876b7f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
5 changes: 5 additions & 0 deletions openraft/src/raft/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ where C: RaftTypeConfig
RuntimeConfigHandle::new(self.inner.as_ref())
}

/// Return the config of this Raft node.
pub fn config(&self) -> &Arc<Config> {
&self.inner.config
}

/// Return a handle to manually trigger raft actions, such as elect or build snapshot.
///
/// Example:
Expand Down
10 changes: 10 additions & 0 deletions tests/tests/management/main.rs
Original file line number Diff line number Diff line change
@@ -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;
42 changes: 42 additions & 0 deletions tests/tests/management/t10_raft_config.rs
Original file line number Diff line number Diff line change
@@ -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(())
}

0 comments on commit 1876b7f

Please sign in to comment.