Replace `mock_peer_set` function with `MockService` (#2790)

* Use `MockService` in inbound test

Refactor the `mempool_requsets_for_transactions` test so that it uses a
`MockService` instead of the `mock_peer_set` function.

* Use `MockService` in the basic mempool test

Refactor the `mempool_service_basic` test so that it uses a
`MockService` instead of the `mock_peer_set` helper function.

* Remove the `mock_peer_set` helper function

It is not used anymore, since the usages were replaced with
`MockService`s.

Co-authored-by: Conrado Gouvea <conrado@zfnd.org>
This commit is contained in:
Janito Vaqueiro Ferreira Filho 2021-09-22 12:44:13 -03:00 committed by GitHub
parent 776432978c
commit 1f1bf2ec4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 40 deletions

View File

@ -12,8 +12,5 @@ mod sync;
pub mod tokio;
pub mod tracing;
#[cfg(test)]
pub(crate) mod tests;
pub use inbound::Inbound;
pub use sync::ChainSync;

View File

@ -1,7 +1,7 @@
use std::{collections::HashSet, net::SocketAddr, str::FromStr, sync::Arc};
use super::mempool::{unmined_transactions_in_blocks, Mempool};
use crate::components::{sync::SyncStatus, tests::mock_peer_set};
use crate::components::sync::SyncStatus;
use tokio::sync::oneshot;
use tower::{builder::ServiceBuilder, util::BoxService, ServiceExt};
@ -14,13 +14,14 @@ use zebra_chain::{
use zebra_consensus::Config as ConsensusConfig;
use zebra_network::{AddressBook, Request, Response};
use zebra_state::Config as StateConfig;
use zebra_test::mock_service::MockService;
#[tokio::test]
async fn mempool_requests_for_transactions() {
let network = Network::Mainnet;
let consensus_config = ConsensusConfig::default();
let state_config = StateConfig::ephemeral();
let (peer_set, _) = mock_peer_set();
let peer_set = MockService::build().for_unit_tests();
let address_book = AddressBook::new(SocketAddr::from_str("0.0.0.0:0").unwrap(), Span::none());
let address_book = Arc::new(std::sync::Mutex::new(address_book));
let (sync_status, _recent_syncs) = SyncStatus::new();
@ -34,9 +35,13 @@ async fn mempool_requests_for_transactions() {
zebra_consensus::chain::init(consensus_config.clone(), network, state_service.clone())
.await;
let peer_set_service = ServiceBuilder::new()
.buffer(1)
.service(BoxService::new(peer_set));
let mut mempool_service = Mempool::new(
network,
peer_set.clone(),
peer_set_service.clone(),
state_service.clone(),
transaction_verifier,
sync_status,
@ -60,7 +65,7 @@ async fn mempool_requests_for_transactions() {
block_verifier.clone(),
));
let r = setup_tx.send((peer_set.clone(), address_book, mempool));
let r = setup_tx.send((peer_set_service, address_book, mempool));
// We can't expect or unwrap because the returned Result does not implement Debug
assert!(r.is_ok());

View File

@ -6,8 +6,7 @@ use tower::{ServiceBuilder, ServiceExt};
use zebra_consensus::Config as ConsensusConfig;
use zebra_state::Config as StateConfig;
use crate::components::tests::mock_peer_set;
use zebra_test::mock_service::MockService;
#[tokio::test]
async fn mempool_service_basic() -> Result<(), Report> {
@ -15,7 +14,7 @@ async fn mempool_service_basic() -> Result<(), Report> {
let network = Network::Mainnet;
let consensus_config = ConsensusConfig::default();
let state_config = StateConfig::ephemeral();
let (peer_set, _) = mock_peer_set();
let peer_set = MockService::build().for_unit_tests();
let (sync_status, _recent_syncs) = SyncStatus::new();
let (_state_service, _latest_chain_tip, chain_tip_change) =
zebra_state::init(state_config.clone(), network);
@ -31,7 +30,7 @@ async fn mempool_service_basic() -> Result<(), Report> {
// Start the mempool service
let mut service = Mempool::new(
network,
peer_set,
Buffer::new(BoxService::new(peer_set), 1),
state_service.clone(),
tx_verifier,
sync_status,

View File

@ -1,29 +0,0 @@
use tokio::sync::mpsc::{self, UnboundedReceiver};
use tower::{buffer::Buffer, util::BoxService};
use zebra_network::{BoxError, Request, Response};
/// Create a mock service to represent a [`PeerSet`][zebra_network::PeerSet] and intercept the
/// requests it receives.
///
/// The intercepted requests are sent through an unbounded channel to the receiver that's also
/// returned from this function.
pub(crate) fn mock_peer_set() -> (
Buffer<BoxService<Request, Response, BoxError>, Request>,
UnboundedReceiver<Request>,
) {
let (sender, receiver) = mpsc::unbounded_channel();
let proxy_service = tower::service_fn(move |request| {
let sender = sender.clone();
async move {
let _ = sender.send(request);
Ok(Response::TransactionIds(vec![]))
}
});
let service = Buffer::new(BoxService::new(proxy_service), 10);
(service, receiver)
}