Skip to content

Commit

Permalink
Add mock for Selector and start for pool.
Browse files Browse the repository at this point in the history
  • Loading branch information
AurelienFT committed Aug 18, 2022
1 parent d437ac5 commit a13d61e
Show file tree
Hide file tree
Showing 18 changed files with 160 additions and 21 deletions.
2 changes: 1 addition & 1 deletion massa-consensus-exports/src/test_exports/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct MockConsensusController {

impl MockConsensusController {
/// Create a new mock consensus controller.
pub fn new() -> (Self, ConsensusCommandSender, ConsensusEventReceiver) {
pub fn new_with_receiver() -> (Self, ConsensusCommandSender, ConsensusEventReceiver) {
let (consensus_command_tx, consensus_command_rx) =
mpsc::channel::<ConsensusCommand>(CHANNEL_SIZE);
let (consensus_event_tx, consensus_event_rx) =
Expand Down
2 changes: 1 addition & 1 deletion massa-factory-exports/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ pub use types::*;

//TODO: readd
//#[cfg(feature = "testing")]
pub mod test_exports;
pub mod test_exports;
2 changes: 1 addition & 1 deletion massa-factory-exports/src/test_exports/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ impl Default for FactoryConfig {
max_block_gas: MAX_GAS_PER_BLOCK,
}
}
}
}
3 changes: 1 addition & 2 deletions massa-factory-exports/src/test_exports/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@

mod config;
pub use config::*;
pub use config::*;
2 changes: 1 addition & 1 deletion massa-factory-worker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ serial_test = "0.9"

[features]
sandbox = []
testing = ["massa_factory_exports/testing"]
testing = ["massa_factory_exports/testing", "massa_pos_exports/testing", "massa_pool_exports/testing"]
2 changes: 1 addition & 1 deletion massa-factory-worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ mod run;
pub use run::start_factory;

#[cfg(test)]
mod tests;
mod tests;
2 changes: 1 addition & 1 deletion massa-factory-worker/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mod scenarios;
mod scenarios;
24 changes: 18 additions & 6 deletions massa-factory-worker/src/tests/scenarios.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::sync::{Arc, RwLock};

use massa_factory_exports::{FactoryConfig, FactoryChannels};
use massa_consensus_exports::test_exports::MockConsensusController;
use massa_factory_exports::{FactoryChannels, FactoryConfig};
use massa_pos_exports::test_exports::MockSelectorController;
use massa_storage::Storage;
use serial_test::serial;

use crate::start_factory;
Expand All @@ -9,8 +12,17 @@ use massa_wallet::test_exports::create_test_wallet;
#[test]
#[serial]
fn basic_creation() {

start_factory(FactoryConfig::default(), Arc::new(RwLock::new(create_test_wallet())), FactoryChannels {

});
}
let (selector_controller, selector_receiver) = MockSelectorController::new_with_receiver();
let (consensus_controller, consensus_command_sender, consensus_event_receiver) = MockConsensusController::new_with_receiver();
let storage = Storage::default();
start_factory(
FactoryConfig::default(),
Arc::new(RwLock::new(create_test_wallet())),
FactoryChannels {
selector: selector_controller,
consensus: consensus_command_sender,
pool:
storage
},
);
}
5 changes: 3 additions & 2 deletions massa-pool-exports/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ pub use controller_traits::PoolController;
pub use error::PoolError;
pub use types::PoolOperationCursor;

#[cfg(feature = "testing")]
pub mod tests;
// TODO: Readd
/// #[cfg(feature = "testing")]
pub mod test_exports;
File renamed without changes.
1 change: 1 addition & 0 deletions massa-pool-exports/src/test_exports/mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

3 changes: 3 additions & 0 deletions massa-pool-exports/src/test_exports/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod config;

pub use config::*;
1 change: 0 additions & 1 deletion massa-pool-exports/src/tests/mod.rs

This file was deleted.

124 changes: 124 additions & 0 deletions massa-pos-exports/src/test_exports/mock.rs
Original file line number Diff line number Diff line change
@@ -1 +1,125 @@
use std::sync::{
mpsc::{self, Receiver},
Arc, Mutex,
};

use anyhow::Result;
use massa_models::{api::IndexedSlot, Address, Slot};

use crate::{CycleInfo, SelectorController, Selection};

/// All events that can be sent by the selector to your callbacks.
pub enum MockSelectorControllerMessage {
/// Feed a new cycle info to the selector
FeedCycle {
/// cycle infos
cycle_info: CycleInfo,
},
/// Get a list of slots where address has been chosen to produce a block and a list where he is chosen for the endorsements.
/// Look from the start slot to the end slot.
GetAddressSelections {
/// Address to search
address: Address,
/// Start of the search range
start: Slot,
/// End of the search range
end: Slot,
/// Receiver to send the result to
response_tx: mpsc::Sender<(Vec<Slot>, Vec<IndexedSlot>)>,
},
/// Get the producer for a block at a specific slot
GetProducer {
/// Slot to search
slot: Slot,
/// Receiver to send the result to
response_tx: mpsc::Sender<Result<Address>>,
},
/// Get the selection for a block at a specific slot
GetSelection {
/// Slot to search
slot: Slot,
/// Receiver to send the result to
response_tx: mpsc::Sender<Result<Selection>>,
}
}

/// Mock implementation of the SelectorController trait.
/// This mock will be called by the others modules and you will receive events in the receiver.
/// You can choose to manage them how you want.
#[derive(Clone)]
pub struct MockSelectorController(Arc<Mutex<mpsc::Sender<MockSelectorControllerMessage>>>);

impl MockSelectorController {
/// Create a new pair (mock execution controller, mpsc receiver for emitted messages)
/// Note that unbounded mpsc channels are used
pub fn new_with_receiver() -> (
Box<dyn SelectorController>,
Receiver<MockSelectorControllerMessage>,
) {
let (tx, rx) = mpsc::channel();
(
Box::new(MockSelectorController(Arc::new(Mutex::new(tx)))),
rx,
)
}
}

impl SelectorController for MockSelectorController {
fn feed_cycle(&self, cycle_info: CycleInfo) {
self.0
.lock()
.unwrap()
.send(MockSelectorControllerMessage::FeedCycle { cycle_info })
.unwrap();
}

fn get_address_selections(
&self,
address: &Address,
start: Slot,
end: Slot,
) -> (Vec<Slot>, Vec<IndexedSlot>) {
let (response_tx, response_rx) = mpsc::channel();
self.0
.lock()
.unwrap()
.send(MockSelectorControllerMessage::GetAddressSelections {
address: *address,
start,
end,
response_tx,
})
.unwrap();
response_rx.recv().unwrap()
}

fn get_producer(&self, slot: Slot) -> Result<Address> {
let (response_tx, response_rx) = mpsc::channel();
self.0
.lock()
.unwrap()
.send(MockSelectorControllerMessage::GetProducer {
slot,
response_tx,
})
.unwrap();
response_rx.recv().unwrap()
}

fn get_selection(&self, slot: Slot) -> Result<Selection> {
let (response_tx, response_rx) = mpsc::channel();
self.0
.lock()
.unwrap()
.send(MockSelectorControllerMessage::GetSelection {
slot,
response_tx,
})
.unwrap();
response_rx.recv().unwrap()
}

fn clone_box(&self) -> Box<dyn SelectorController> {
Box::new(self.clone())
}
}
2 changes: 1 addition & 1 deletion massa-pos-exports/src/test_exports/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ mod bootstrap;
mod mock;

pub use bootstrap::*;
pub use mock::*;
pub use mock::*;
2 changes: 1 addition & 1 deletion massa-wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,4 @@ impl std::fmt::Display for Wallet {

// TODO: Readd
//#[cfg(feature = "testing")]
pub mod test_exports;
pub mod test_exports;
2 changes: 1 addition & 1 deletion massa-wallet/src/test_exports/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mod tools;

pub use tools::*;
pub use tools::*;
2 changes: 1 addition & 1 deletion massa-wallet/src/test_exports/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ use crate::Wallet;
pub fn create_test_wallet() -> Wallet {
let wallet_file = NamedTempFile::new().expect("cannot create temp file");
Wallet::new(wallet_file.path().to_path_buf(), "test".to_string()).unwrap()
}
}

0 comments on commit a13d61e

Please sign in to comment.