-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: add err: NotAMembershipEntry, NotInMembers
- Change: fix error usage for `initialize()`: new error to return: NotAMembershipEntry and NotInMembers; MissingNodeInfo should not be returned when the node to initialize is not a member. - Refactor: move engine to dir engine/ - Test: add engine test for `Engine::initialize()` - part of #292
- Loading branch information
1 parent
5946bcc
commit 67c870e
Showing
6 changed files
with
158 additions
and
9 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 |
---|---|---|
@@ -0,0 +1,120 @@ | ||
use maplit::btreeset; | ||
|
||
use crate::engine::Command; | ||
use crate::engine::Engine; | ||
use crate::entry::EntryRef; | ||
use crate::error::InitializeError; | ||
use crate::error::NotAMembershipEntry; | ||
use crate::error::NotAllowed; | ||
use crate::error::NotInMembers; | ||
use crate::EntryPayload; | ||
use crate::LeaderId; | ||
use crate::LogId; | ||
use crate::Membership; | ||
use crate::MetricsChangeFlags; | ||
use crate::Vote; | ||
|
||
#[derive(Clone, serde::Serialize, serde::Deserialize)] | ||
pub(crate) struct Req {} | ||
#[derive(Clone, serde::Serialize, serde::Deserialize)] | ||
pub(crate) struct Resp {} | ||
|
||
crate::declare_raft_types!( | ||
pub(crate) Config: D = Req, R = Resp, NodeId = u64 | ||
); | ||
|
||
#[test] | ||
fn test_initialize() -> anyhow::Result<()> { | ||
let eng = Engine::<u64>::default; | ||
|
||
let log_id0 = LogId { | ||
leader_id: LeaderId::new(0, 0), | ||
index: 0, | ||
}; | ||
let vote0 = Vote::new(0, 0); | ||
|
||
let m12 = || Membership::<u64>::new(vec![btreeset! {1,2}], None); | ||
let payload = EntryPayload::<Config>::Membership(m12()); | ||
let mut entries = [EntryRef::new(&payload)]; | ||
|
||
tracing::info!("--- ok"); | ||
{ | ||
let mut eng = eng(); | ||
eng.id = 1; | ||
|
||
eng.initialize(&mut entries)?; | ||
assert_eq!(Some(log_id0), eng.state.last_log_id); | ||
assert_eq!( | ||
MetricsChangeFlags { | ||
leader: false, | ||
other_metrics: true | ||
}, | ||
eng.metrics_flags | ||
); | ||
assert_eq!(m12(), eng.state.effective_membership.membership); | ||
assert_eq!( | ||
vec![ | ||
Command::AppendInputEntries { range: 0..1 }, | ||
Command::UpdateMembership { membership: m12() }, | ||
Command::MoveInputCursorBy { n: 1 } | ||
], | ||
eng.commands | ||
); | ||
} | ||
|
||
tracing::info!("--- not allowed because of last_log_id"); | ||
{ | ||
let mut eng = eng(); | ||
eng.state.last_log_id = Some(log_id0); | ||
|
||
assert_eq!( | ||
Err(InitializeError::NotAllowed(NotAllowed { | ||
last_log_id: Some(log_id0), | ||
vote: vote0, | ||
})), | ||
eng.initialize(&mut entries) | ||
); | ||
} | ||
|
||
tracing::info!("--- not allowed because of vote"); | ||
{ | ||
let mut eng = eng(); | ||
eng.state.vote = Vote::new(0, 1); | ||
|
||
assert_eq!( | ||
Err(InitializeError::NotAllowed(NotAllowed { | ||
last_log_id: None, | ||
vote: Vote::new(0, 1), | ||
})), | ||
eng.initialize(&mut entries) | ||
); | ||
} | ||
|
||
tracing::info!("--- node id 0 is not in membership"); | ||
{ | ||
let mut eng = eng(); | ||
|
||
assert_eq!( | ||
Err(InitializeError::NotInMembers(NotInMembers { | ||
node_id: 0, | ||
membership: m12() | ||
})), | ||
eng.initialize(&mut entries) | ||
); | ||
} | ||
|
||
tracing::info!("--- log entry is not a membership entry"); | ||
{ | ||
let mut eng = eng(); | ||
|
||
let payload = EntryPayload::<Config>::Blank; | ||
let mut entries = [EntryRef::new(&payload)]; | ||
|
||
assert_eq!( | ||
Err(InitializeError::NotAMembershipEntry(NotAMembershipEntry {})), | ||
eng.initialize(&mut entries) | ||
); | ||
} | ||
|
||
Ok(()) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#[allow(clippy::module_inception)] | ||
mod engine; | ||
|
||
#[cfg(test)] | ||
mod initialize_test; | ||
|
||
pub(crate) use engine::Command; | ||
pub(crate) use engine::Engine; |
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