Skip to content

Commit

Permalink
refactor: move testing method out of MemStore and defines a trait `Ra…
Browse files Browse the repository at this point in the history
…ftStorageTrait` to define what debug API a store could have
  • Loading branch information
drmingdrmer committed Aug 23, 2021
1 parent 915f86a commit 2566e51
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions async-raft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub use crate::raft_types::SnapshotSegmentId;
pub use crate::raft_types::Update;
pub use crate::replication::ReplicationMetrics;
pub use crate::storage::RaftStorage;
pub use crate::storage::RaftStorageDebug;
pub use crate::storage::SnapshotMeta;
pub use crate::summary::MessageSummary;

Expand Down
11 changes: 11 additions & 0 deletions async-raft/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,14 @@ where
/// Errors returned from this method will cause Raft to go into shutdown.
async fn get_current_snapshot(&self) -> Result<Option<CurrentSnapshotData<Self::Snapshot>>>;
}

/// APIs for debugging a store.
#[async_trait]
pub trait RaftStorageDebug<SM> {
/// Get a handle to the state machine for testing purposes.
async fn get_state_machine(&self) -> SM;

/// Get the current hard state for testing purposes.
async fn read_hard_state(&self) -> Option<HardState>;
}

1 change: 1 addition & 0 deletions async-raft/tests/fixtures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use async_raft::NodeId;
use async_raft::Raft;
use async_raft::RaftMetrics;
use async_raft::RaftNetwork;
use async_raft::RaftStorageDebug;
use async_raft::State;
use maplit::btreeset;
use memstore::ClientRequest as MemClientRequest;
Expand Down
1 change: 1 addition & 0 deletions async-raft/tests/initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use async_raft::raft::EntryPayload;
use async_raft::raft::MembershipConfig;
use async_raft::Config;
use async_raft::RaftStorage;
use async_raft::RaftStorageDebug;
use async_raft::State;
use fixtures::RaftRouter;
use maplit::btreeset;
Expand Down
1 change: 1 addition & 0 deletions async-raft/tests/metrics_state_machine_consistency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::sync::Arc;

use anyhow::Result;
use async_raft::Config;
use async_raft::RaftStorageDebug;
use async_raft::State;
use fixtures::RaftRouter;
use maplit::btreeset;
Expand Down
1 change: 1 addition & 0 deletions async-raft/tests/state_machien_apply_membership.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::sync::Arc;
use anyhow::Result;
use async_raft::raft::MembershipConfig;
use async_raft::Config;
use async_raft::RaftStorageDebug;
use async_raft::State;
use fixtures::RaftRouter;
use futures::stream::StreamExt;
Expand Down
8 changes: 6 additions & 2 deletions memstore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use async_raft::AppDataResponse;
use async_raft::LogId;
use async_raft::NodeId;
use async_raft::RaftStorage;
use async_raft::RaftStorageDebug;
use async_raft::SnapshotMeta;
use serde::Deserialize;
use serde::Serialize;
Expand Down Expand Up @@ -136,14 +137,17 @@ impl MemStore {
current_snapshot,
}
}
}

#[async_trait]
impl RaftStorageDebug<MemStoreStateMachine> for MemStore {
/// Get a handle to the state machine for testing purposes.
pub async fn get_state_machine(&self) -> MemStoreStateMachine {
async fn get_state_machine(&self) -> MemStoreStateMachine {
self.sm.write().await.clone()
}

/// Get a handle to the current hard state for testing purposes.
pub async fn read_hard_state(&self) -> Option<HardState> {
async fn read_hard_state(&self) -> Option<HardState> {
self.hs.read().await.clone()
}
}
Expand Down

0 comments on commit 2566e51

Please sign in to comment.