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:
parent
09836d2800
commit
39bcefb569
|
@ -1,32 +1,13 @@
|
||||||
//! Defines method signatures for checking if the synchronizer is likely close to the network chain tip.
|
//! Defines method signatures for checking if the synchronizer is likely close to the network chain tip.
|
||||||
|
|
||||||
use std::sync::{
|
#[cfg(any(test, feature = "proptest-impl"))]
|
||||||
atomic::{AtomicBool, Ordering},
|
pub mod mock;
|
||||||
Arc,
|
|
||||||
};
|
#[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.
|
/// An interface for checking if the synchronization is likely close to the network chain tip.
|
||||||
pub trait ChainSyncStatus {
|
pub trait ChainSyncStatus {
|
||||||
/// Check if the synchronization is likely close to the network chain tip.
|
/// Check if the synchronization is likely close to the network chain tip.
|
||||||
fn is_close_to_tip(&self) -> bool;
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,15 +4,17 @@ use std::sync::Arc;
|
||||||
|
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
|
|
||||||
use self::network_chain_tip_height_estimator::NetworkChainTipHeightEstimator;
|
|
||||||
use crate::{block, parameters::Network, transaction};
|
use crate::{block, parameters::Network, transaction};
|
||||||
|
|
||||||
|
mod network_chain_tip_height_estimator;
|
||||||
|
|
||||||
#[cfg(any(test, feature = "proptest-impl"))]
|
#[cfg(any(test, feature = "proptest-impl"))]
|
||||||
pub mod mock;
|
pub mod mock;
|
||||||
mod network_chain_tip_height_estimator;
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
|
use network_chain_tip_height_estimator::NetworkChainTipHeightEstimator;
|
||||||
|
|
||||||
/// An interface for querying the chain tip.
|
/// An interface for querying the chain tip.
|
||||||
///
|
///
|
||||||
/// This trait helps avoid dependencies between:
|
/// This trait helps avoid dependencies between:
|
||||||
|
@ -83,6 +85,9 @@ pub trait ChainTip {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A chain tip that is always empty.
|
/// 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)]
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||||
pub struct NoChainTip;
|
pub struct NoChainTip;
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
|
//! Syncer chain tip status, based on recent block locator responses from peers.
|
||||||
|
|
||||||
use tokio::sync::watch;
|
use tokio::sync::watch;
|
||||||
use zebra_chain::chain_sync_status::ChainSyncStatus;
|
use zebra_chain::chain_sync_status::ChainSyncStatus;
|
||||||
|
|
||||||
use super::RecentSyncLengths;
|
use super::RecentSyncLengths;
|
||||||
|
|
||||||
|
#[cfg(any(test, feature = "proptest-impl"))]
|
||||||
|
pub mod mock;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
|
@ -43,24 +47,6 @@ impl SyncStatus {
|
||||||
|
|
||||||
Ok(())
|
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 {
|
impl ChainSyncStatus for SyncStatus {
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,5 @@
|
||||||
|
//! Tests for syncer status.
|
||||||
|
|
||||||
use std::{env, sync::Arc, time::Duration};
|
use std::{env, sync::Arc, time::Duration};
|
||||||
|
|
||||||
use futures::{select, FutureExt};
|
use futures::{select, FutureExt};
|
||||||
|
|
Loading…
Reference in New Issue