-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add test cases for Loader #21
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -98,3 +98,111 @@ where | |
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use election::Term; | ||
use log::{LogEntry, LogPosition, LogPrefix, LogSuffix}; | ||
use node::NodeId; | ||
use test_util::tests::TestIoBuilder; | ||
use trackable::result::TestResult; | ||
|
||
#[test] | ||
fn it_works() -> TestResult { | ||
let node_id: NodeId = "node1".into(); | ||
let io = TestIoBuilder::new().add_member(node_id.clone()).finish(); | ||
let mut handle = io.handle(); | ||
let cluster = io.cluster.clone(); | ||
let mut common = Common::new(node_id.clone(), io, cluster.clone()); | ||
let mut loader = Loader::new(&mut common); | ||
|
||
// prefix には空の snapshot があり、tail は 1 を指している。 | ||
// suffix には position 1 から 1 エントリが保存されている。 | ||
// term は変更なし。 | ||
let term = Term::new(1); | ||
let suffix_head = LogIndex::new(1); | ||
let prefix_tail = LogPosition { | ||
prev_term: term.clone(), | ||
index: suffix_head.clone(), | ||
}; | ||
handle.set_initial_log_prefix(LogPrefix { | ||
tail: prefix_tail.clone(), | ||
config: cluster.clone(), | ||
snapshot: vec![], | ||
}); | ||
handle.set_initial_log_suffix( | ||
suffix_head.clone(), | ||
LogSuffix { | ||
head: LogPosition { | ||
prev_term: term.clone(), | ||
index: suffix_head.clone(), | ||
}, | ||
entries: vec![LogEntry::Noop { term: term.clone() }], | ||
}, | ||
); | ||
loop { | ||
if let Some(next) = track!(loader.run_once(&mut common))? { | ||
assert!(next.is_candidate()); | ||
// term は変化なし | ||
assert_eq!(term, common.log().tail().prev_term); | ||
// 追記されたログエントリの tail が 1 つ先に進んでいる | ||
assert_eq!(LogIndex::new(2), common.log().tail().index); | ||
// consumed と committed は prefix の状態のまま | ||
assert_eq!(prefix_tail.index, common.log().consumed_tail().index); | ||
assert_eq!(prefix_tail.index, common.log().committed_tail().index); | ||
break; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn it_fails_if_log_suffix_contains_older_term() -> TestResult { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #18 を再現させるテストコード。 |
||
let node_id: NodeId = "node1".into(); | ||
let io = TestIoBuilder::new().add_member(node_id.clone()).finish(); | ||
let mut handle = io.handle(); | ||
let cluster = io.cluster.clone(); | ||
let mut common = Common::new(node_id.clone(), io, cluster.clone()); | ||
let mut loader = Loader::new(&mut common); | ||
|
||
// 古い term のログが紛れ込んでいるとエラーになる | ||
let term = Term::new(308); | ||
let suffix_head = LogIndex::new(28405496); | ||
let prefix_tail = LogPosition { | ||
prev_term: term.clone(), | ||
index: suffix_head.clone(), | ||
}; | ||
handle.set_initial_log_prefix(LogPrefix { | ||
tail: prefix_tail.clone(), | ||
config: cluster.clone(), | ||
snapshot: vec![], | ||
}); | ||
handle.set_initial_log_suffix( | ||
suffix_head.clone(), | ||
LogSuffix { | ||
head: LogPosition { | ||
prev_term: term.clone(), | ||
index: suffix_head.clone(), | ||
}, | ||
entries: vec![ | ||
LogEntry::Noop { term: term.clone() }, | ||
LogEntry::Noop { | ||
term: Term::new(term.as_u64() - 1), | ||
}, | ||
], | ||
}, | ||
); | ||
|
||
// Error: Other (cause; assertion failed: `self.last_record().head.prev_term < tail.prev_term`; last_record.head=LogPosition { prev_term: Term(308), index: LogIndex(28405496) }, tail=LogPosition { prev_term: Term(307), index: LogIndex(28405498) }) | ||
//HISTORY: | ||
// [0] at src/log/history.rs:104 | ||
// [1] at src/node_state/common/mod.rs:78 | ||
// [2] at src/node_state/loader.rs:58 | ||
// [3] at src/node_state/loader.rs:198 | ||
assert!(loader.run_once(&mut common).is_err()); | ||
|
||
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 |
---|---|---|
|
@@ -35,11 +35,7 @@ impl<IO: Io> NodeState<IO> { | |
NodeState { common, role } | ||
} | ||
pub fn is_loading(&self) -> bool { | ||
if let RoleState::Loader(_) = self.role { | ||
true | ||
} else { | ||
false | ||
} | ||
self.role.is_loader() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. コードを整理して、触ったコードに関わる箇所はテストを追加。 |
||
} | ||
pub fn start_election(&mut self) { | ||
if let RoleState::Follower(_) = self.role { | ||
|
@@ -152,3 +148,58 @@ pub enum RoleState<IO: Io> { | |
/// リーダ (詳細はRaftの論文を参照) | ||
Leader(Leader<IO>), | ||
} | ||
|
||
impl<IO: Io> RoleState<IO> { | ||
/// Returns true if this role state is `Loader`. | ||
pub fn is_loader(&self) -> bool { | ||
if let RoleState::Loader(_) = self { | ||
true | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
/// Returns true if this role state is `Candidate`. | ||
#[cfg(test)] | ||
pub fn is_candidate(&self) -> bool { | ||
if let RoleState::Candidate(_) = self { | ||
true | ||
} else { | ||
false | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use test_util::tests::TestIoBuilder; | ||
|
||
#[test] | ||
fn node_state_is_loading_works() { | ||
let io = TestIoBuilder::new().finish(); | ||
let cluster = io.cluster.clone(); | ||
let node = NodeState::load("test".into(), cluster, io); | ||
assert!(node.is_loading()); | ||
} | ||
|
||
#[test] | ||
fn role_state_is_loader_works() { | ||
let io = TestIoBuilder::new().finish(); | ||
let cluster = io.cluster.clone(); | ||
let mut common = Common::new("test".into(), io, cluster); | ||
let state = RoleState::Loader(Loader::new(&mut common)); | ||
assert!(state.is_loader()); | ||
assert!(!state.is_candidate()); | ||
} | ||
|
||
#[test] | ||
fn role_state_is_candidate_works() { | ||
let io = TestIoBuilder::new().finish(); | ||
let cluster = io.cluster.clone(); | ||
let mut common = Common::new("test".into(), io, cluster); | ||
let state = RoleState::Candidate(Candidate::new(&mut common)); | ||
assert!(!state.is_loader()); | ||
assert!(state.is_candidate()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NullIo
は削除して、まともにテストで使えるようにTestIo
として再実装。