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
This commit is contained in:
teor 2022-12-08 11:06:11 +10:00 committed by GitHub
parent 09836d2800
commit 39bcefb569
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 73 additions and 44 deletions

View File

@ -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<AtomicBool>,
}
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)
}
}

View File

@ -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<AtomicBool>,
}
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)
}
}

View File

@ -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;

View File

@ -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 {

View File

@ -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);
}
}
}

View File

@ -1,3 +1,5 @@
//! Tests for syncer status.
use std::{env, sync::Arc, time::Duration};
use futures::{select, FutureExt};