From be70f810e90fea6b00d43dbcd658eba4687cfc66 Mon Sep 17 00:00:00 2001 From: Francisco Krause Arnim <56402156+fkrause98@users.noreply.github.com> Date: Tue, 31 Jan 2023 00:48:17 -0300 Subject: [PATCH] test: Modify can_serde_config_test (#1048) Co-authored-by: lambdaclass-user Co-authored-by: xqft --- Cargo.lock | 1 + crates/net/common/src/ban_list.rs | 2 +- crates/net/network/src/peers/manager.rs | 6 +-- crates/net/network/src/peers/reputation.rs | 2 +- crates/staged-sync/Cargo.toml | 1 + crates/staged-sync/src/config.rs | 44 +++++++++++++++++----- 6 files changed, 42 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7a9de8125..2348f4f6b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4576,6 +4576,7 @@ dependencies = [ "serde", "serde_json", "shellexpand", + "tempfile", "tracing", "walkdir", ] diff --git a/crates/net/common/src/ban_list.rs b/crates/net/common/src/ban_list.rs index 44505fc30..bf9db48ce 100644 --- a/crates/net/common/src/ban_list.rs +++ b/crates/net/common/src/ban_list.rs @@ -4,7 +4,7 @@ use std::{collections::HashMap, net::IpAddr, time::Instant}; /// Stores peers that should be taken out of circulation either indefinitely or until a certain /// timestamp -#[derive(Debug, Clone, Default)] +#[derive(Debug, Clone, Default, PartialEq)] pub struct BanList { /// A set of IPs whose packets get dropped instantly. banned_ips: HashMap>, diff --git a/crates/net/network/src/peers/manager.rs b/crates/net/network/src/peers/manager.rs index 989a04d9c..b0ebba669 100644 --- a/crates/net/network/src/peers/manager.rs +++ b/crates/net/network/src/peers/manager.rs @@ -658,7 +658,7 @@ impl Default for PeersManager { } /// Tracks stats about connected nodes -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct ConnectionInfo { /// Counter for currently occupied slots for active outbound connections. @@ -917,7 +917,7 @@ pub enum PeerAction { } /// Config type for initiating a [`PeersManager`] instance -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct PeersConfig { /// How often to recheck free slots for outbound connections. @@ -1003,7 +1003,7 @@ impl PeersConfig { /// The durations to use when a backoff should be applied to a peer. /// /// See also [`BackoffKind`](BackoffKind). -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct PeerBackoffDurations { /// Applies to connection problems where there is a chance that they will be resolved after the diff --git a/crates/net/network/src/peers/reputation.rs b/crates/net/network/src/peers/reputation.rs index 738862f6b..7ed2bdd40 100644 --- a/crates/net/network/src/peers/reputation.rs +++ b/crates/net/network/src/peers/reputation.rs @@ -37,7 +37,7 @@ pub(crate) fn is_banned_reputation(reputation: i32) -> bool { } /// How the [`ReputationChangeKind`] are weighted. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct ReputationChangeWeights { /// Weight for [`ReputationChangeKind::BadMessage`] diff --git a/crates/staged-sync/Cargo.toml b/crates/staged-sync/Cargo.toml index 402fbd595..8f0892211 100644 --- a/crates/staged-sync/Cargo.toml +++ b/crates/staged-sync/Cargo.toml @@ -32,3 +32,4 @@ walkdir = "2.3.2" eyre = "0.6.8" shellexpand = "3.0.0" tracing = "0.1.37" +tempfile = "3.3.0" diff --git a/crates/staged-sync/src/config.rs b/crates/staged-sync/src/config.rs index 8a666b58c..8dba4820e 100644 --- a/crates/staged-sync/src/config.rs +++ b/crates/staged-sync/src/config.rs @@ -12,7 +12,7 @@ use reth_provider::ShareableDatabase; use serde::{Deserialize, Serialize}; /// Configuration for the reth node. -#[derive(Debug, Clone, Default, Deserialize, Serialize)] +#[derive(Debug, Clone, Default, Deserialize, PartialEq, Serialize)] #[serde(default)] pub struct Config { /// Configuration for each stage in the pipeline. @@ -48,7 +48,7 @@ impl Config { } /// Configuration for each stage in the pipeline. -#[derive(Debug, Clone, Default, Deserialize, Serialize)] +#[derive(Debug, Clone, Default, Deserialize, PartialEq, Serialize)] pub struct StageConfig { /// Header stage configuration. pub headers: HeadersConfig, @@ -63,7 +63,7 @@ pub struct StageConfig { } /// Header stage configuration. -#[derive(Debug, Clone, Deserialize, Serialize)] +#[derive(Debug, Clone, Deserialize, PartialEq, Serialize)] pub struct HeadersConfig { /// The maximum number of headers to download before committing progress to the database. pub commit_threshold: u64, @@ -80,7 +80,7 @@ impl Default for HeadersConfig { } /// Total difficulty stage configuration -#[derive(Debug, Clone, Deserialize, Serialize)] +#[derive(Debug, Clone, Deserialize, PartialEq, Serialize)] pub struct TotalDifficultyConfig { /// The maximum number of total difficulty entries to sum up before committing progress to the /// database. @@ -94,7 +94,7 @@ impl Default for TotalDifficultyConfig { } /// Body stage configuration. -#[derive(Debug, Clone, Deserialize, Serialize)] +#[derive(Debug, Clone, Deserialize, PartialEq, Serialize)] pub struct BodiesConfig { /// The batch size of non-empty blocks per one request pub downloader_request_limit: u64, @@ -121,7 +121,7 @@ impl Default for BodiesConfig { } /// Sender recovery stage configuration. -#[derive(Debug, Clone, Deserialize, Serialize)] +#[derive(Debug, Clone, Deserialize, PartialEq, Eq, Serialize)] pub struct SenderRecoveryConfig { /// The maximum number of blocks to process before committing progress to the database. pub commit_threshold: u64, @@ -136,7 +136,7 @@ impl Default for SenderRecoveryConfig { } /// Execution stage configuration. -#[derive(Debug, Clone, Deserialize, Serialize)] +#[derive(Debug, Clone, Deserialize, PartialEq, Serialize)] pub struct ExecutionConfig { /// The maximum number of blocks to execution before committing progress to the database. pub commit_threshold: u64, @@ -151,8 +151,34 @@ impl Default for ExecutionConfig { #[cfg(test)] mod tests { use super::Config; + + const EXTENSION: &str = "toml"; + + fn with_tempdir(filename: &str, proc: fn(&std::path::Path)) { + let temp_dir = tempfile::tempdir().unwrap(); + let config_path = temp_dir.path().join(filename).with_extension(EXTENSION); + + proc(&config_path); + + temp_dir.close().unwrap() + } + #[test] - fn can_serde_config() { - let _: Config = confy::load("test", None).unwrap(); + fn test_store_config() { + with_tempdir("config-store-test", |config_path| { + let config = Config::default(); + confy::store_path(config_path, config).unwrap(); + }) + } + + #[test] + fn test_load_config() { + with_tempdir("config-load-test", |config_path| { + let config = Config::default(); + confy::store_path(config_path, &config).unwrap(); + + let loaded_config: Config = confy::load_path(config_path).unwrap(); + assert_eq!(config, loaded_config); + }) } }