-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(code): Add unit tests to
consensus
crate
- Loading branch information
Showing
8 changed files
with
160 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,138 @@ | ||
use eyre::{eyre, Result}; | ||
use rand::rngs::StdRng; | ||
use rand::SeedableRng; | ||
|
||
use malachite_consensus::{process, Params, Round, ThresholdParams, Timeout, ValuePayload}; | ||
use malachite_metrics::Metrics; | ||
use malachite_test::utils::validators::make_validator_set; | ||
use malachite_test::{Address, Height, PrivateKey, TestContext}; | ||
|
||
type State = malachite_consensus::State<TestContext>; | ||
type Resume = malachite_consensus::Resume<TestContext>; | ||
type Effect = malachite_consensus::Effect<TestContext>; | ||
type Input = malachite_consensus::Input<TestContext>; | ||
|
||
fn setup(height: Height) -> (State, Metrics) { | ||
let mut rng = StdRng::seed_from_u64(42); | ||
let private_key = PrivateKey::generate(&mut rng); | ||
let address = Address::from_public_key(&private_key.public_key()); | ||
let ctx = TestContext::new(private_key); | ||
let validator_set = make_validator_set([1, 1, 1]); | ||
let params = Params { | ||
start_height: height, | ||
initial_validator_set: validator_set.clone(), | ||
address, | ||
threshold_params: ThresholdParams::default(), | ||
value_payload: ValuePayload::ProposalAndParts, | ||
}; | ||
|
||
let state = State::new(ctx, params); | ||
let metrics = Metrics::default(); | ||
|
||
(state, metrics) | ||
} | ||
|
||
#[test] | ||
fn start_height() -> Result<()> { | ||
let height = Height::new(1); | ||
let (mut state, metrics) = setup(height); | ||
let validator_set = state.validator_set().clone(); | ||
|
||
let mut expected = vec![ | ||
(Effect::CancelAllTimeouts, Resume::Continue), | ||
(Effect::ResetTimeouts, Resume::Continue), | ||
(Effect::CancelAllTimeouts, Resume::Continue), | ||
( | ||
Effect::StartRound(height, Round::new(0), *state.address()), | ||
Resume::Continue, | ||
), | ||
( | ||
Effect::ScheduleTimeout(Timeout::propose(Round::new(0))), | ||
Resume::Continue, | ||
), | ||
] | ||
.into_iter(); | ||
|
||
let mut handle_effect = |effect: Effect| -> Result<Resume> { | ||
match expected.next() { | ||
Some((expected, resume)) if expected == effect => Ok(resume), | ||
|
||
Some((expected, _)) => Err(eyre!( | ||
"unexpected effect: got {effect:?}, expected {expected:?}" | ||
)), | ||
|
||
None => Err(eyre!("unexpected effect: {effect:?}")), | ||
} | ||
}; | ||
|
||
process!( | ||
input: Input::StartHeight(height, validator_set), | ||
state: &mut state, | ||
metrics: &metrics, | ||
with: effect => handle_effect(effect) | ||
) | ||
} | ||
|
||
#[test] | ||
fn timeout_elapsed_propose() -> Result<()> { | ||
let height = Height::new(1); | ||
let (mut state, metrics) = setup(height); | ||
|
||
let handle_effect = | ||
|effect: Effect| -> Result<Resume> { Err(eyre!("unexpected effect: {effect:?}")) }; | ||
|
||
process!( | ||
input: Input::TimeoutElapsed(Timeout::propose(Round::new(0))), | ||
state: &mut state, | ||
metrics: &metrics, | ||
with: effect => handle_effect(effect) | ||
) | ||
} | ||
|
||
#[test] | ||
fn timeout_elapsed_prevote() -> Result<()> { | ||
let height = Height::new(1); | ||
let (mut state, metrics) = setup(height); | ||
|
||
let handle_effect = | ||
|effect: Effect| -> Result<Resume> { Err(eyre!("unexpected effect: {effect:?}")) }; | ||
|
||
process!( | ||
input: Input::TimeoutElapsed(Timeout::prevote(Round::new(0))), | ||
state: &mut state, | ||
metrics: &metrics, | ||
with: effect => handle_effect(effect) | ||
) | ||
} | ||
|
||
#[test] | ||
fn timeout_elapsed_precommit() -> Result<()> { | ||
let height = Height::new(1); | ||
let (mut state, metrics) = setup(height); | ||
|
||
let handle_effect = | ||
|effect: Effect| -> Result<Resume> { Err(eyre!("unexpected effect: {effect:?}")) }; | ||
|
||
process!( | ||
input: Input::TimeoutElapsed(Timeout::precommit(Round::new(0))), | ||
state: &mut state, | ||
metrics: &metrics, | ||
with: effect => handle_effect(effect) | ||
) | ||
} | ||
|
||
// #[test] | ||
// fn timeout_elapsed_commit() -> Result<()> { | ||
// let height = Height::new(1); | ||
// let (mut state, metrics) = setup(height); | ||
// | ||
// let handle_effect = | ||
// |effect: Effect| -> Result<Resume> { Err(eyre!("unexpected effect: {effect:?}")) }; | ||
// | ||
// process!( | ||
// input: Input::TimeoutElapsed(Timeout::commit(Round::new(0))), | ||
// state: &mut state, | ||
// metrics: &metrics, | ||
// with: effect => handle_effect(effect) | ||
// ) | ||
// } |
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