From 39bcefb569fa443ffa642ec7a14d5ff648c7044e Mon Sep 17 00:00:00 2001 From: teor Date: Thu, 8 Dec 2022 11:06:11 +1000 Subject: [PATCH] fix(test): Move test-only code to test-only modules (#5777) * Move NoChainTip to a test-only module * Move test-only ChainSyncStatus code into its own module * Move RecentSyncLengths mocks to their own module * Silence an unused test code lint * Actually, NoChainTip is used in production for isolated connections --- zebra-chain/src/chain_sync_status.rs | 29 ++++------------------ zebra-chain/src/chain_sync_status/mock.rs | 28 +++++++++++++++++++++ zebra-chain/src/chain_tip.rs | 9 +++++-- zebrad/src/components/sync/status.rs | 22 +++------------- zebrad/src/components/sync/status/mock.rs | 27 ++++++++++++++++++++ zebrad/src/components/sync/status/tests.rs | 2 ++ 6 files changed, 73 insertions(+), 44 deletions(-) create mode 100644 zebra-chain/src/chain_sync_status/mock.rs create mode 100644 zebrad/src/components/sync/status/mock.rs diff --git a/zebra-chain/src/chain_sync_status.rs b/zebra-chain/src/chain_sync_status.rs index 7a85c8962..034725183 100644 --- a/zebra-chain/src/chain_sync_status.rs +++ b/zebra-chain/src/chain_sync_status.rs @@ -1,32 +1,13 @@ //! Defines method signatures for checking if the synchronizer is likely close to the network chain tip. -use std::sync::{ - atomic::{AtomicBool, Ordering}, - Arc, -}; +#[cfg(any(test, feature = "proptest-impl"))] +pub mod mock; + +#[cfg(any(test, feature = "proptest-impl"))] +pub use mock::MockSyncStatus; /// An interface for checking if the synchronization is likely close to the network chain tip. pub trait ChainSyncStatus { /// Check if the synchronization is likely close to the network chain tip. fn is_close_to_tip(&self) -> bool; } - -/// A mock [`ChainSyncStatus`] implementation that allows setting the status externally. -#[derive(Clone, Default)] -pub struct MockSyncStatus { - is_close_to_tip: Arc, -} - -impl MockSyncStatus { - /// Sets mock sync status determining the return value of `is_close_to_tip()` - pub fn set_is_close_to_tip(&mut self, is_close_to_tip: bool) { - self.is_close_to_tip - .store(is_close_to_tip, Ordering::SeqCst); - } -} - -impl ChainSyncStatus for MockSyncStatus { - fn is_close_to_tip(&self) -> bool { - self.is_close_to_tip.load(Ordering::SeqCst) - } -} diff --git a/zebra-chain/src/chain_sync_status/mock.rs b/zebra-chain/src/chain_sync_status/mock.rs new file mode 100644 index 000000000..41eb79456 --- /dev/null +++ b/zebra-chain/src/chain_sync_status/mock.rs @@ -0,0 +1,28 @@ +//! Test-only mocks for [`ChainSyncStatus`]. + +use std::sync::{ + atomic::{AtomicBool, Ordering}, + Arc, +}; + +use super::ChainSyncStatus; + +/// A mock [`ChainSyncStatus`] implementation that allows setting the status externally. +#[derive(Clone, Default)] +pub struct MockSyncStatus { + is_close_to_tip: Arc, +} + +impl MockSyncStatus { + /// Sets mock sync status determining the return value of `is_close_to_tip()` + pub fn set_is_close_to_tip(&mut self, is_close_to_tip: bool) { + self.is_close_to_tip + .store(is_close_to_tip, Ordering::SeqCst); + } +} + +impl ChainSyncStatus for MockSyncStatus { + fn is_close_to_tip(&self) -> bool { + self.is_close_to_tip.load(Ordering::SeqCst) + } +} diff --git a/zebra-chain/src/chain_tip.rs b/zebra-chain/src/chain_tip.rs index 27fa08e13..ffb3dd61b 100644 --- a/zebra-chain/src/chain_tip.rs +++ b/zebra-chain/src/chain_tip.rs @@ -4,15 +4,17 @@ use std::sync::Arc; use chrono::{DateTime, Utc}; -use self::network_chain_tip_height_estimator::NetworkChainTipHeightEstimator; use crate::{block, parameters::Network, transaction}; +mod network_chain_tip_height_estimator; + #[cfg(any(test, feature = "proptest-impl"))] pub mod mock; -mod network_chain_tip_height_estimator; #[cfg(test)] mod tests; +use network_chain_tip_height_estimator::NetworkChainTipHeightEstimator; + /// An interface for querying the chain tip. /// /// This trait helps avoid dependencies between: @@ -83,6 +85,9 @@ pub trait ChainTip { } /// A chain tip that is always empty. +/// +/// Used in production for isolated network connections, +/// and as a mock chain tip in tests. #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct NoChainTip; diff --git a/zebrad/src/components/sync/status.rs b/zebrad/src/components/sync/status.rs index 01ac3853c..73cc112a3 100644 --- a/zebrad/src/components/sync/status.rs +++ b/zebrad/src/components/sync/status.rs @@ -1,8 +1,12 @@ +//! Syncer chain tip status, based on recent block locator responses from peers. + use tokio::sync::watch; use zebra_chain::chain_sync_status::ChainSyncStatus; use super::RecentSyncLengths; +#[cfg(any(test, feature = "proptest-impl"))] +pub mod mock; #[cfg(test)] mod tests; @@ -43,24 +47,6 @@ impl SyncStatus { Ok(()) } - - /// Feed the given [`RecentSyncLengths`] it order to make the matching - /// [`SyncStatus`] report that it's close to the tip. - #[cfg(test)] - pub(crate) fn sync_close_to_tip(recent_syncs: &mut RecentSyncLengths) { - for _ in 0..RecentSyncLengths::MAX_RECENT_LENGTHS { - recent_syncs.push_extend_tips_length(1); - } - } - - /// Feed the given [`RecentSyncLengths`] it order to make the matching - /// [`SyncStatus`] report that it's not close to the tip. - #[cfg(test)] - pub(crate) fn sync_far_from_tip(recent_syncs: &mut RecentSyncLengths) { - for _ in 0..RecentSyncLengths::MAX_RECENT_LENGTHS { - recent_syncs.push_extend_tips_length(Self::MIN_DIST_FROM_TIP * 10); - } - } } impl ChainSyncStatus for SyncStatus { diff --git a/zebrad/src/components/sync/status/mock.rs b/zebrad/src/components/sync/status/mock.rs new file mode 100644 index 000000000..154c8df72 --- /dev/null +++ b/zebrad/src/components/sync/status/mock.rs @@ -0,0 +1,27 @@ +//! Test-only mocking code for [`SyncStatus`]. + +// This code is currently unused with some feature combinations. +#![allow(dead_code)] + +use crate::components::sync::RecentSyncLengths; + +use super::SyncStatus; + +// TODO: move these methods to RecentSyncLengths +impl SyncStatus { + /// Feed the given [`RecentSyncLengths`] it order to make the matching + /// [`SyncStatus`] report that it's close to the tip. + pub(crate) fn sync_close_to_tip(recent_syncs: &mut RecentSyncLengths) { + for _ in 0..RecentSyncLengths::MAX_RECENT_LENGTHS { + recent_syncs.push_extend_tips_length(1); + } + } + + /// Feed the given [`RecentSyncLengths`] it order to make the matching + /// [`SyncStatus`] report that it's not close to the tip. + pub(crate) fn sync_far_from_tip(recent_syncs: &mut RecentSyncLengths) { + for _ in 0..RecentSyncLengths::MAX_RECENT_LENGTHS { + recent_syncs.push_extend_tips_length(Self::MIN_DIST_FROM_TIP * 10); + } + } +} diff --git a/zebrad/src/components/sync/status/tests.rs b/zebrad/src/components/sync/status/tests.rs index 31c300a40..08de36a6f 100644 --- a/zebrad/src/components/sync/status/tests.rs +++ b/zebrad/src/components/sync/status/tests.rs @@ -1,3 +1,5 @@ +//! Tests for syncer status. + use std::{env, sync::Arc, time::Duration}; use futures::{select, FutureExt};