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 2, 2024
1 parent fdbf0eb commit 5d1056e
Show file tree
Hide file tree
Showing 40 changed files with 418 additions and 80 deletions.
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, you have two options:

Expand Down Expand Up @@ -469,7 +469,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`
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::engine::Condition;
use crate::engine::Engine;
use crate::raft_state::IOId;
use crate::raft_state::LogStateReader;
use crate::testing::blank_ent;
use crate::testing::log_id;
use crate::testing::log::blank_ent;
use crate::testing::log::log_id;
use crate::type_config::TypeConfigExt;
use crate::utime::Leased;
use crate::EffectiveMembership;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::engine::Command;
use crate::engine::Engine;
use crate::raft_state::IOId;
use crate::raft_state::LogStateReader;
use crate::testing::log_id;
use crate::testing::log::log_id;
use crate::type_config::TypeConfigExt;
use crate::utime::Leased;
use crate::EffectiveMembership;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::engine::Command;
use crate::engine::Engine;
use crate::entry::RaftEntry;
use crate::raft_state::LogStateReader;
use crate::testing::blank_ent;
use crate::testing::log_id;
use crate::testing::log::blank_ent;
use crate::testing::log::log_id;
use crate::type_config::TypeConfigExt;
use crate::utime::Leased;
use crate::EffectiveMembership;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::engine::Engine;
use crate::engine::LogIdList;
use crate::raft_state::IOId;
use crate::raft_state::LogStateReader;
use crate::testing::log_id;
use crate::testing::log::log_id;
use crate::type_config::TypeConfigExt;
use crate::EffectiveMembership;
use crate::Membership;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::engine::Command;
use crate::engine::Engine;
use crate::engine::LogIdList;
use crate::raft_state::LogStateReader;
use crate::testing::log_id;
use crate::testing::log::log_id;
use crate::type_config::TypeConfigExt;
use crate::utime::Leased;
use crate::EffectiveMembership;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use maplit::btreeset;
use crate::core::ServerState;
use crate::engine::testing::UTConfig;
use crate::engine::Engine;
use crate::testing::log_id;
use crate::testing::log::log_id;
use crate::type_config::TypeConfigExt;
use crate::utime::Leased;
use crate::EffectiveMembership;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use crate::progress::entry::ProgressEntry;
use crate::progress::Inflight;
use crate::raft_state::IOId;
use crate::raft_state::LogStateReader;
use crate::testing::blank_ent;
use crate::testing::log_id;
use crate::testing::log::blank_ent;
use crate::testing::log::log_id;
use crate::type_config::TypeConfigExt;
use crate::utime::Leased;
use crate::vote::CommittedLeaderId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use pretty_assertions::assert_str_eq;

use crate::engine::testing::UTConfig;
use crate::engine::Engine;
use crate::testing::log_id;
use crate::testing::log::log_id;
use crate::type_config::TypeConfigExt;
use crate::utime::Leased;
use crate::EffectiveMembership;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::engine::Command;
use crate::engine::Engine;
use crate::progress::Inflight;
use crate::progress::Progress;
use crate::testing::log_id;
use crate::testing::log::log_id;
use crate::type_config::TypeConfigExt;
use crate::utime::Leased;
use crate::EffectiveMembership;
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/engine/handler/log_handler/purge_log_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::engine::Command;
use crate::engine::Engine;
use crate::engine::LogIdList;
use crate::raft_state::LogStateReader;
use crate::testing::log_id;
use crate::testing::log::log_id;

fn eng() -> Engine<UTConfig> {
let mut eng = Engine::testing_default(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::engine::ReplicationProgress;
use crate::progress::entry::ProgressEntry;
use crate::progress::Inflight;
use crate::progress::Progress;
use crate::testing::log_id;
use crate::testing::log::log_id;
use crate::type_config::TypeConfigExt;
use crate::utime::Leased;
use crate::CommittedLeaderId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::engine::Engine;
use crate::progress::Inflight;
use crate::progress::Progress;
use crate::raft_state::LogStateReader;
use crate::testing::log_id;
use crate::testing::log::log_id;
use crate::type_config::TypeConfigExt;
use crate::utime::Leased;
use crate::EffectiveMembership;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use pretty_assertions::assert_eq;

use crate::engine::testing::UTConfig;
use crate::engine::Engine;
use crate::testing::log_id;
use crate::testing::log::log_id;
use crate::type_config::TypeConfigExt;
use crate::utime::Leased;
use crate::EffectiveMembership;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use pretty_assertions::assert_eq;

use crate::engine::testing::UTConfig;
use crate::engine::Engine;
use crate::testing::log_id;
use crate::testing::log::log_id;
use crate::Membership;
use crate::SnapshotMeta;
use crate::StoredMembership;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::engine::Respond;
use crate::error::Infallible;
use crate::raft::VoteResponse;
use crate::raft_state::IOId;
use crate::testing::log_id;
use crate::testing::log::log_id;
use crate::type_config::TypeConfigExt;
use crate::utime::Leased;
use crate::EffectiveMembership;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::engine::ReplicationProgress;
use crate::entry::RaftEntry;
use crate::progress::entry::ProgressEntry;
use crate::progress::Inflight;
use crate::testing::log_id;
use crate::testing::log::log_id;
use crate::type_config::alias::EntryOf;
use crate::type_config::TypeConfigExt;
use crate::utime::Leased;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::engine::Command;
use crate::engine::Engine;
use crate::engine::LogIdList;
use crate::error::RejectVoteRequest;
use crate::testing::log_id;
use crate::testing::log::log_id;
use crate::type_config::TypeConfigExt;
use crate::utime::Leased;
use crate::EffectiveMembership;
Expand Down
4 changes: 2 additions & 2 deletions openraft/src/engine/tests/append_entries_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use crate::entry::RaftEntry;
use crate::error::RejectAppendEntries;
use crate::raft_state::IOId;
use crate::raft_state::LogStateReader;
use crate::testing::blank_ent;
use crate::testing::log_id;
use crate::testing::log::blank_ent;
use crate::testing::log::log_id;
use crate::type_config::TypeConfigExt;
use crate::utime::Leased;
use crate::EffectiveMembership;
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/engine/tests/elect_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::engine::Command;
use crate::engine::Engine;
use crate::engine::LogIdList;
use crate::raft::VoteRequest;
use crate::testing::log_id;
use crate::testing::log::log_id;
use crate::type_config::TypeConfigExt;
use crate::utime::Leased;
use crate::CommittedLeaderId;
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/engine/tests/handle_vote_req_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::engine::Engine;
use crate::engine::LogIdList;
use crate::raft::VoteRequest;
use crate::raft::VoteResponse;
use crate::testing::log_id;
use crate::testing::log::log_id;
use crate::type_config::TypeConfigExt;
use crate::utime::Leased;
use crate::EffectiveMembership;
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/engine/tests/handle_vote_resp_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::progress::entry::ProgressEntry;
use crate::progress::Inflight;
use crate::raft::VoteResponse;
use crate::raft_state::IOId;
use crate::testing::log_id;
use crate::testing::log::log_id;
use crate::type_config::TypeConfigExt;
use crate::utime::Leased;
use crate::CommittedLeaderId;
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/engine/tests/initialize_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::error::NotAllowed;
use crate::error::NotInMembers;
use crate::raft::VoteRequest;
use crate::raft_state::LogStateReader;
use crate::testing::log_id;
use crate::testing::log::log_id;
use crate::type_config::TypeConfigExt;
use crate::utime::Leased;
use crate::vote::CommittedLeaderId;
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/engine/tests/install_full_snapshot_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::engine::LogIdList;
use crate::engine::Respond;
use crate::raft::SnapshotResponse;
use crate::raft_state::IOId;
use crate::testing::log_id;
use crate::testing::log::log_id;
use crate::type_config::TypeConfigExt;
use crate::Membership;
use crate::Snapshot;
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/engine/tests/log_id_list_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::engine::testing::UTConfig;
use crate::engine::LogIdList;
use crate::testing::log_id;
use crate::testing::log::log_id;

#[test]
fn test_log_id_list_extend_from_same_leader() -> anyhow::Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/engine/tests/startup_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::engine::ReplicationProgress;
use crate::entry::RaftEntry;
use crate::progress::entry::ProgressEntry;
use crate::progress::Inflight;
use crate::testing::log_id;
use crate::testing::log::log_id;
use crate::type_config::TypeConfigExt;
use crate::utime::Leased;
use crate::EffectiveMembership;
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/engine/tests/trigger_purge_log_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::engine::Command;
use crate::engine::Engine;
use crate::engine::LogIdList;
use crate::progress::Progress;
use crate::testing::log_id;
use crate::testing::log::log_id;
use crate::type_config::TypeConfigExt;
use crate::utime::Leased;
use crate::CommittedLeaderId;
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/metrics/wait_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::engine::testing::UTConfig;
use crate::log_id::LogIdOptionExt;
use crate::metrics::Wait;
use crate::metrics::WaitError;
use crate::testing::log_id;
use crate::testing::log::log_id;
use crate::type_config::alias::NodeIdOf;
use crate::type_config::alias::WatchSenderOf;
use crate::type_config::TypeConfigExt;
Expand Down
4 changes: 2 additions & 2 deletions openraft/src/proposer/leader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ mod tests {
use crate::entry::RaftEntry;
use crate::progress::Progress;
use crate::proposer::Leader;
use crate::testing::blank_ent;
use crate::testing::log_id;
use crate::testing::log::blank_ent;
use crate::testing::log::log_id;
use crate::type_config::TypeConfigExt;
use crate::Entry;
use crate::RaftLogId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::error::ChangeMembershipError;
use crate::error::EmptyMembership;
use crate::error::InProgress;
use crate::error::LearnerNotFound;
use crate::testing::log_id;
use crate::testing::log::log_id;
use crate::ChangeMembers;
use crate::EffectiveMembership;
use crate::Membership;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;
use maplit::btreeset;

use crate::engine::testing::UTConfig;
use crate::testing::log_id;
use crate::testing::log::log_id;
use crate::EffectiveMembership;
use crate::Membership;
use crate::MembershipState;
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/raft_state/tests/is_initialized_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::time::Duration;

use crate::engine::testing::UTConfig;
use crate::engine::LogIdList;
use crate::testing::log_id;
use crate::testing::log::log_id;
use crate::type_config::TypeConfigExt;
use crate::utime::Leased;
use crate::RaftState;
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/replication/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ where C: RaftTypeConfig
mod tests {
use crate::engine::testing::UTConfig;
use crate::replication::response::ReplicationResult;
use crate::testing::log_id;
use crate::testing::log::log_id;
use crate::type_config::TypeConfigExt;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ mod tests {
fn test_display() {
use crate::MessageSummary;

let lid = crate::testing::log_id(1, 2, 3);
let lid = crate::testing::log::log_id(1, 2, 3);
assert_eq!("T1-N2.3", lid.to_string());
assert_eq!("T1-N2.3", lid.summary());
assert_eq!("Some(T1-N2.3)", Some(&lid).summary());
Expand Down
43 changes: 43 additions & 0 deletions openraft/src/testing/log/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//! Suite for testing implementations of [`RaftLogStorage`] and [`RaftStateMachine`].
//!
//! [`RaftLogStorage`]: crate::storage::RaftLogStorage
//! [`RaftStateMachine`]: crate::storage::RaftStateMachine

mod store_builder;
mod suite;

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),
)
}
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
Loading

0 comments on commit 5d1056e

Please sign in to comment.