-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change: StoreBuilder does not need to run a test, it only needs to bu…
…ild a store `StoreBuilder::run_test()` is removed, and a new method `build()` is added. To test a `RaftStorage` implementation, just implementing `StoreBuilder::build()` is enough now. It returns a store instance and a **guard**, for disk backed store to clean up the data when the guard is dropped.
- Loading branch information
1 parent
14c8d26
commit a92499f
Showing
6 changed files
with
111 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,58 @@ | ||
use std::fmt::Debug; | ||
use std::future::Future; | ||
use std::marker::PhantomData; | ||
|
||
use async_trait::async_trait; | ||
|
||
use crate::AppData; | ||
use crate::AppDataResponse; | ||
use crate::DefensiveCheckBase; | ||
use crate::RaftStorage; | ||
use crate::RaftTypeConfig; | ||
use crate::StorageError; | ||
use crate::StoreExt; | ||
|
||
/// The trait to build a [`RaftStorage`] implementation. | ||
/// | ||
/// The generic parameter `C` is type config for a `RaftStorage` implementation, | ||
/// `S` is the type that implements `RaftStorage`, | ||
/// and `G` is a guard type that cleanup resource when being dropped. | ||
/// | ||
/// By default `G` is a trivial guard `()`. To test a store that is backed by a folder on disk, `G` | ||
/// could be the dropper of the temp-dir that stores data. | ||
#[async_trait] | ||
pub trait StoreBuilder<C, S>: Send + Sync | ||
pub trait StoreBuilder<C, S, G = ()>: Send + Sync | ||
where | ||
C: RaftTypeConfig, | ||
S: RaftStorage<C>, | ||
{ | ||
async fn run_test<Fun, Ret, Res>(&self, t: Fun) -> Result<Ret, StorageError<C::NodeId>> | ||
where | ||
Res: Future<Output = Result<Ret, StorageError<C::NodeId>>> + Send, | ||
Fun: Fn(S) -> Res + Sync + Send; | ||
/// Build a [`RaftStorage`] implementation | ||
async fn build(&self) -> Result<(G, S), StorageError<C::NodeId>>; | ||
} | ||
|
||
/// A builder for testing [`StoreExt`]. | ||
pub struct DefensiveStoreBuilder<C, BaseStore, BaseBuilder> | ||
pub struct DefensiveStoreBuilder<C, BaseStore, BaseBuilder, G> | ||
where | ||
C: RaftTypeConfig, | ||
C::D: AppData + Debug, | ||
C::R: AppDataResponse + Debug, | ||
BaseStore: RaftStorage<C>, | ||
BaseBuilder: StoreBuilder<C, BaseStore>, | ||
BaseBuilder: StoreBuilder<C, BaseStore, G>, | ||
{ | ||
pub base_builder: BaseBuilder, | ||
|
||
pub s: PhantomData<(C, BaseStore)>, | ||
pub s: PhantomData<(C, BaseStore, G)>, | ||
} | ||
|
||
#[async_trait] | ||
impl<C, BaseStore, BaseBuilder> StoreBuilder<C, StoreExt<C, BaseStore>> | ||
for DefensiveStoreBuilder<C, BaseStore, BaseBuilder> | ||
impl<C, G, BaseStore, BaseBuilder> StoreBuilder<C, StoreExt<C, BaseStore>, G> | ||
for DefensiveStoreBuilder<C, BaseStore, BaseBuilder, G> | ||
where | ||
C: RaftTypeConfig, | ||
C::D: AppData + Debug, | ||
C::R: AppDataResponse + Debug, | ||
BaseStore: RaftStorage<C>, | ||
BaseBuilder: StoreBuilder<C, BaseStore>, | ||
BaseBuilder: StoreBuilder<C, BaseStore, G>, | ||
G: Send + Sync, | ||
{ | ||
async fn run_test<Fun, Ret, Res>(&self, t: Fun) -> Result<Ret, StorageError<C::NodeId>> | ||
where | ||
Res: Future<Output = Result<Ret, StorageError<C::NodeId>>> + Send, | ||
Fun: Fn(StoreExt<C, BaseStore>) -> Res + Sync + Send, | ||
{ | ||
self.base_builder | ||
.run_test(|base_store| async { | ||
let sto_ext = StoreExt::new(base_store); | ||
sto_ext.set_defensive(true); | ||
assert!(sto_ext.is_defensive(), "must impl defensive check"); | ||
t(sto_ext).await | ||
}) | ||
.await | ||
async fn build(&self) -> Result<(G, StoreExt<C, BaseStore>), StorageError<C::NodeId>> { | ||
let (g, store) = self.base_builder.build().await?; | ||
let sto_ext = StoreExt::new(store); | ||
sto_ext.set_defensive(true); | ||
assert!(sto_ext.is_defensive(), "must impl defensive check"); | ||
|
||
Ok((g, sto_ext)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.