Skip to content

Commit

Permalink
Test: add an AsyncRuntime test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveLauC committed Aug 6, 2024
1 parent 801ca4d commit a3cfb1b
Show file tree
Hide file tree
Showing 13 changed files with 461 additions and 51 deletions.
4 changes: 2 additions & 2 deletions cluster_benchmark/tests/benchmark/store/test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use openraft::testing::StoreBuilder;
use openraft::testing::Suite;
use openraft::testing::log::StoreBuilder;
use openraft::testing::log::Suite;
use openraft::StorageError;

use crate::store::LogStore;
Expand Down
4 changes: 2 additions & 2 deletions examples/raft-kv-memstore/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use openraft::testing::StoreBuilder;
use openraft::testing::Suite;
use openraft::testing::log::StoreBuilder;
use openraft::testing::log::Suite;
use openraft::StorageError;

use crate::store::LogStore;
Expand Down
6 changes: 3 additions & 3 deletions openraft/src/docs/getting_started/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ Most of the APIs are quite straightforward, except two indirect APIs:
### Ensure the storage implementation is correct
There is a [Test suite for RaftLogStorage and RaftStateMachine][`Suite`] available in Openraft.
There is a [Test suite for RaftLogStorage and RaftStateMachine][`LogSuite`] available in Openraft.
If your implementation passes the tests, Openraft should work well with it.
To test your implementation, run `Suite::test_all()` with a [`StoreBuilder`] implementation,
as shown in the [`RocksStore` test](https://github.com/datafuselabs/openraft/blob/main/stores/rocksstore/src/test.rs).
Expand Down Expand Up @@ -456,7 +456,7 @@ Additionally, two test scripts for setting up a cluster are available:
[`build_snapshot()`]: `crate::storage::RaftSnapshotBuilder::build_snapshot`
[`Snapshot`]: `crate::storage::Snapshot`

[`StoreBuilder`]: `crate::testing::StoreBuilder`
[`Suite`]: `crate::testing::Suite`
[`StoreBuilder`]: `crate::testing::log::StoreBuilder`
[`LogSuite`]: `crate::testing::log::Suite`

[`docs::connect-to-correct-node`]: `crate::docs::cluster_control::dynamic_membership#ensure-connection-to-the-correct-node`
34 changes: 34 additions & 0 deletions openraft/src/testing/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//! Testing utilities used by all kinds of tests.
use std::collections::BTreeSet;

use crate::entry::RaftEntry;
use crate::CommittedLeaderId;
use crate::LogId;
use crate::RaftTypeConfig;

/// Builds a log id, for testing purposes.
pub fn log_id<NID: crate::NodeId>(term: u64, node_id: NID, index: u64) -> LogId<NID> {
LogId::<NID> {
leader_id: CommittedLeaderId::new(term, node_id),
index,
}
}

/// Create a blank log entry for test.
pub fn blank_ent<C: RaftTypeConfig>(term: u64, node_id: C::NodeId, index: u64) -> crate::Entry<C> {
crate::Entry::<C>::new_blank(LogId::new(CommittedLeaderId::new(term, node_id), index))
}

/// Create a membership log entry without learner config for test.
pub fn membership_ent<C: RaftTypeConfig>(
term: u64,
node_id: C::NodeId,
index: u64,
config: Vec<BTreeSet<C::NodeId>>,
) -> crate::Entry<C> {
crate::Entry::new_membership(
LogId::new(CommittedLeaderId::new(term, node_id), index),
crate::Membership::new(config, None),
)
}
10 changes: 10 additions & 0 deletions openraft/src/testing/log/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//! Suite for testing implementations of [`RaftLogStorage`] and [`RaftStateMachine`].
//!
//! [`RaftLogStorage`]: crate::storage::RaftLogStorage
//! [`RaftStateMachine`]: crate::storage::RaftStateMachine
mod store_builder;
mod suite;

pub use store_builder::StoreBuilder;
pub use suite::Suite;
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::storage::RaftLogReaderExt;
use crate::storage::RaftLogStorage;
use crate::storage::RaftStateMachine;
use crate::storage::StorageHelper;
use crate::testing::StoreBuilder;
use crate::testing::log::StoreBuilder;
use crate::type_config::TypeConfigExt;
use crate::vote::CommittedLeaderId;
use crate::LogId;
Expand Down
41 changes: 4 additions & 37 deletions openraft/src/testing/mod.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,7 @@
//! Testing utilities for OpenRaft.
mod store_builder;
mod suite;
pub mod common;
pub mod log;
pub mod runtime;

use std::collections::BTreeSet;

pub use store_builder::StoreBuilder;
pub use suite::Suite;

use crate::entry::RaftEntry;
use crate::CommittedLeaderId;
use crate::LogId;
use crate::RaftTypeConfig;

/// Builds a log id, for testing purposes.
pub fn log_id<NID: crate::NodeId>(term: u64, node_id: NID, index: u64) -> LogId<NID> {
LogId::<NID> {
leader_id: CommittedLeaderId::new(term, node_id),
index,
}
}

/// Create a blank log entry for test.
pub fn blank_ent<C: RaftTypeConfig>(term: u64, node_id: C::NodeId, index: u64) -> crate::Entry<C> {
crate::Entry::<C>::new_blank(LogId::new(CommittedLeaderId::new(term, node_id), index))
}

/// Create a membership log entry without learner config for test.
pub fn membership_ent<C: RaftTypeConfig>(
term: u64,
node_id: C::NodeId,
index: u64,
config: Vec<BTreeSet<C::NodeId>>,
) -> crate::Entry<C> {
crate::Entry::new_membership(
LogId::new(CommittedLeaderId::new(term, node_id), index),
crate::Membership::new(config, None),
)
}
pub use common::*;
Loading

0 comments on commit a3cfb1b

Please sign in to comment.