-
Notifications
You must be signed in to change notification settings - Fork 711
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
4487: Add tests factory r=AurelienFT a=AurelienFT Massa factory now has 73% coverage Co-authored-by: AurelienFT <[email protected]>
- Loading branch information
Showing
7 changed files
with
399 additions
and
101 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
use std::{str::FromStr, sync::Arc}; | ||
|
||
use super::BlockTestFactory; | ||
use massa_consensus_exports::MockConsensusController; | ||
use massa_hash::Hash; | ||
use massa_models::{ | ||
address::Address, | ||
amount::Amount, | ||
block_id::BlockId, | ||
config::THREAD_COUNT, | ||
operation::{Operation, OperationSerializer, OperationType}, | ||
secure_share::SecureShareContent, | ||
slot::Slot, | ||
}; | ||
use massa_pool_exports::MockPoolController; | ||
use massa_pos_exports::MockSelectorController; | ||
use massa_signature::KeyPair; | ||
use massa_storage::Storage; | ||
use parking_lot::{Condvar, Mutex}; | ||
use serial_test::serial; | ||
|
||
/// Creates a basic empty block with the factory. | ||
#[test] | ||
#[serial] | ||
fn basic_creation() { | ||
let default_panic = std::panic::take_hook(); | ||
std::panic::set_hook(Box::new(move |info| { | ||
default_panic(info); | ||
std::process::exit(1); | ||
})); | ||
let keypair = KeyPair::generate(0).unwrap(); | ||
let storage = Storage::create_root(); | ||
let staking_address = Address::from_public_key(&keypair.get_public_key()); | ||
let parent = BlockId::generate_from_hash(Hash::compute_from("test".as_bytes())); | ||
let mut parents = Vec::new(); | ||
for i in 0..THREAD_COUNT as u64 { | ||
parents.push((parent, i)); | ||
} | ||
let pair = Arc::new((Mutex::new(false), Condvar::new())); | ||
let pair2 = pair.clone(); | ||
let mut consensus_controller = Box::new(MockConsensusController::new()); | ||
consensus_controller | ||
.expect_get_best_parents() | ||
.times(1) | ||
.return_once(move || parents); | ||
consensus_controller | ||
.expect_register_block() | ||
.times(1) | ||
.return_once(move |_, _, storage, created| { | ||
assert!(created); | ||
let block = storage.get_block_refs(); | ||
assert_eq!(block.len(), 1); | ||
let (lock, cvar) = &*pair2; | ||
let mut started = lock.lock(); | ||
*started = true; | ||
cvar.notify_one(); | ||
}); | ||
let mut selector_controller = Box::new(MockSelectorController::new()); | ||
selector_controller | ||
.expect_get_producer() | ||
.times(1) | ||
.return_once(move |_| Ok(staking_address)); | ||
let mut pool_controller = Box::new(MockPoolController::new()); | ||
pool_controller | ||
.expect_get_block_denunciations() | ||
.returning(|slot| { | ||
assert_eq!(*slot, Slot::new(1, 0)); | ||
vec![] | ||
}); | ||
pool_controller | ||
.expect_get_block_operations() | ||
.returning(|slot| { | ||
assert_eq!(*slot, Slot::new(1, 0)); | ||
(vec![], Storage::create_root()) | ||
}); | ||
pool_controller | ||
.expect_get_block_endorsements() | ||
.returning(|_, slot| { | ||
assert_eq!(*slot, Slot::new(1, 0)); | ||
(vec![], Storage::create_root()) | ||
}); | ||
let mut test_factory = BlockTestFactory::new( | ||
&keypair, | ||
storage, | ||
consensus_controller, | ||
selector_controller, | ||
pool_controller, | ||
); | ||
let (ref lock, ref cvar) = *pair; | ||
let mut started = lock.lock(); | ||
if !*started { | ||
cvar.wait(&mut started); | ||
} | ||
test_factory.stop(); | ||
} | ||
|
||
/// Creates a block with a roll buy operation in it. | ||
#[test] | ||
#[serial] | ||
fn basic_creation_with_operation() { | ||
let default_panic = std::panic::take_hook(); | ||
std::panic::set_hook(Box::new(move |info| { | ||
default_panic(info); | ||
std::process::exit(1); | ||
})); | ||
let keypair = KeyPair::generate(0).unwrap(); | ||
let staking_address = Address::from_public_key(&keypair.get_public_key()); | ||
let parent = BlockId::generate_from_hash(Hash::compute_from("test".as_bytes())); | ||
let mut parents = Vec::new(); | ||
for i in 0..THREAD_COUNT as u64 { | ||
parents.push((parent, i)); | ||
} | ||
let mut selector_controller = Box::new(MockSelectorController::new()); | ||
selector_controller | ||
.expect_get_producer() | ||
.times(1) | ||
.return_once(move |_| Ok(staking_address)); | ||
let mut pool_controller = Box::new(MockPoolController::new()); | ||
pool_controller | ||
.expect_get_block_denunciations() | ||
.returning(|slot| { | ||
assert_eq!(*slot, Slot::new(1, 0)); | ||
vec![] | ||
}); | ||
let storage = Storage::create_root(); | ||
let keypair_clone = keypair.clone(); | ||
let mut pool_storage = storage.clone_without_refs(); | ||
pool_controller | ||
.expect_get_block_operations() | ||
.returning(move |slot| { | ||
assert_eq!(*slot, Slot::new(1, 0)); | ||
let content = Operation { | ||
fee: Amount::from_str("0.01").unwrap(), | ||
expire_period: 2, | ||
op: OperationType::RollBuy { roll_count: 1 }, | ||
}; | ||
let operation = | ||
Operation::new_verifiable(content, OperationSerializer::new(), &keypair_clone) | ||
.unwrap(); | ||
pool_storage.store_operations(vec![operation.clone()]); | ||
(vec![operation.id], pool_storage.clone()) | ||
}); | ||
let pool_storage_2 = storage.clone_without_refs(); | ||
pool_controller | ||
.expect_get_block_endorsements() | ||
.returning(move |_, slot| { | ||
assert_eq!(*slot, Slot::new(1, 0)); | ||
(vec![], pool_storage_2.clone()) | ||
}); | ||
let pair = Arc::new((Mutex::new(false), Condvar::new())); | ||
let pair2 = pair.clone(); | ||
let mut consensus_controller = Box::new(MockConsensusController::new()); | ||
consensus_controller | ||
.expect_get_best_parents() | ||
.times(1) | ||
.return_once(move || parents); | ||
consensus_controller | ||
.expect_register_block() | ||
.times(1) | ||
.return_once(move |_, _, storage, created| { | ||
assert!(created); | ||
let block = storage.get_block_refs(); | ||
assert_eq!(block.len(), 1); | ||
let ops = storage.get_op_refs(); | ||
assert_eq!(ops.len(), 1); | ||
let (lock, cvar) = &*pair2; | ||
let mut started = lock.lock(); | ||
*started = true; | ||
cvar.notify_one(); | ||
}); | ||
let mut test_factory = BlockTestFactory::new( | ||
&keypair, | ||
storage, | ||
consensus_controller, | ||
selector_controller, | ||
pool_controller, | ||
); | ||
let (lock, cvar) = &*pair; | ||
let mut started = lock.lock(); | ||
if !*started { | ||
cvar.wait(&mut started); | ||
} | ||
test_factory.stop(); | ||
} |
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,96 @@ | ||
use std::sync::Arc; | ||
|
||
use super::EndorsementTestFactory; | ||
use massa_consensus_exports::MockConsensusController; | ||
use massa_hash::Hash; | ||
use massa_models::{ | ||
address::Address, | ||
block_id::BlockId, | ||
config::{ENDORSEMENT_COUNT, THREAD_COUNT}, | ||
slot::Slot, | ||
}; | ||
use massa_pool_exports::MockPoolController; | ||
use massa_pos_exports::{MockSelectorController, Selection}; | ||
use massa_protocol_exports::MockProtocolController; | ||
use massa_signature::KeyPair; | ||
use massa_storage::Storage; | ||
use parking_lot::{Condvar, Mutex}; | ||
use serial_test::serial; | ||
|
||
/// Creates a basic empty block with the factory. | ||
#[test] | ||
#[serial] | ||
fn basic_creation() { | ||
let default_panic = std::panic::take_hook(); | ||
std::panic::set_hook(Box::new(move |info| { | ||
default_panic(info); | ||
std::process::exit(1); | ||
})); | ||
let keypair = KeyPair::generate(0).unwrap(); | ||
let storage = Storage::create_root(); | ||
let staking_address = Address::from_public_key(&keypair.get_public_key()); | ||
let parent = BlockId::generate_from_hash(Hash::compute_from("test".as_bytes())); | ||
let mut parents = Vec::new(); | ||
for i in 0..THREAD_COUNT as u64 { | ||
parents.push((parent, i)); | ||
} | ||
let pair = Arc::new((Mutex::new(false), Condvar::new())); | ||
let pair2 = pair.clone(); | ||
let mut consensus_controller = Box::new(MockConsensusController::new()); | ||
consensus_controller | ||
.expect_get_latest_blockclique_block_at_slot() | ||
.times(1) | ||
.returning(move |slot| { | ||
assert_eq!(slot, Slot::new(1, 0)); | ||
parent | ||
}); | ||
let mut selector_controller = Box::new(MockSelectorController::new()); | ||
selector_controller | ||
.expect_get_selection() | ||
.times(1) | ||
.returning(move |slot| { | ||
assert_eq!(slot, Slot::new(1, 0)); | ||
Ok(Selection { | ||
producer: staking_address, | ||
endorsements: vec![staking_address; ENDORSEMENT_COUNT as usize], | ||
}) | ||
}); | ||
let mut pool_controller = Box::new(MockPoolController::new()); | ||
pool_controller | ||
.expect_add_endorsements() | ||
.times(1) | ||
.returning(|_| {}); | ||
let mut protocol_controller = Box::new(MockProtocolController::new()); | ||
protocol_controller | ||
.expect_propagate_endorsements() | ||
.times(1) | ||
.returning(move |storage| { | ||
let endorsement_ids = storage.get_endorsement_refs(); | ||
assert_eq!(endorsement_ids.len(), ENDORSEMENT_COUNT as usize); | ||
let first_endorsement = storage | ||
.read_endorsements() | ||
.get(endorsement_ids.iter().next().unwrap()) | ||
.unwrap() | ||
.clone(); | ||
assert_eq!(first_endorsement.content.slot, Slot::new(1, 0)); | ||
let (lock, cvar) = &*pair2; | ||
let mut started = lock.lock(); | ||
*started = true; | ||
cvar.notify_one(); | ||
Ok(()) | ||
}); | ||
let mut test_factory = EndorsementTestFactory::new( | ||
&keypair, | ||
storage, | ||
consensus_controller, | ||
selector_controller, | ||
pool_controller, | ||
protocol_controller, | ||
); | ||
let (lock, cvar) = &*pair; | ||
let mut started = lock.lock(); | ||
if !*started { | ||
cvar.wait(&mut started); | ||
} | ||
test_factory.stop(); | ||
} |
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,4 +1,5 @@ | ||
mod scenarios; | ||
mod block_scenarios; | ||
mod endorsement_scenarios; | ||
mod tools; | ||
|
||
pub use tools::*; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.