From 495263925d3f6bb4e3c544df0fa569d5ef085665 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Cig=C3=A1nek?= Date: Thu, 19 Nov 2020 15:51:44 +0100 Subject: [PATCH] fix: do not initialize logger in QuicP2p constructor Initialize it only in tests. --- Cargo.toml | 2 +- src/api.rs | 2 -- src/tests/common.rs | 18 +++++++++++++++++- src/tests/echo_service.rs | 4 +++- src/utils.rs | 9 ++++++--- 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1467b7d6..b3b4035c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,6 @@ edition = "2018" base64 = "~0.12.2" bincode = "1.2.1" err-derive = "~0.2.4" -flexi_logger = "~0.16.1" futures = "~0.3.5" log = "~0.4.8" rcgen = "~0.8.4" @@ -50,6 +49,7 @@ webpki = "~0.21.3" [dev-dependencies] assert_matches = "1.3" +flexi_logger = "~0.16.1" rand = "~0.7.3" [target."cfg(any(all(unix, not(any(target_os = \"android\", target_os = \"androideabi\", target_os = \"ios\"))), windows))".dependencies] diff --git a/src/api.rs b/src/api.rs index eff1402b..5b74958e 100644 --- a/src/api.rs +++ b/src/api.rs @@ -14,7 +14,6 @@ use super::{ endpoint::Endpoint, error::{Error, Result}, peer_config::{self, DEFAULT_IDLE_TIMEOUT_MSEC, DEFAULT_KEEP_ALIVE_INTERVAL_MSEC}, - utils::init_logging, }; use bytes::Bytes; use futures::future; @@ -113,7 +112,6 @@ impl QuicP2p { bootstrap_nodes: &[SocketAddr], use_bootstrap_cache: bool, ) -> Result { - init_logging(); let cfg = unwrap_config_or_default(cfg)?; debug!("Config passed in to qp2p: {:?}", cfg); diff --git a/src/tests/common.rs b/src/tests/common.rs index 8e616ab6..67ccb285 100644 --- a/src/tests/common.rs +++ b/src/tests/common.rs @@ -1,4 +1,4 @@ -use crate::{Config, Error, Message, QuicP2p, Result}; +use crate::{utils, Config, Error, Message, QuicP2p, Result}; use assert_matches::assert_matches; use bytes::Bytes; use futures::future; @@ -34,6 +34,8 @@ fn random_msg() -> Bytes { #[tokio::test] async fn successful_connection() -> Result<()> { + utils::init_logging(); + let qp2p = new_qp2p(); let peer1 = qp2p.new_endpoint()?; let peer1_addr = peer1.socket_addr().await?; @@ -54,6 +56,8 @@ async fn successful_connection() -> Result<()> { #[tokio::test] async fn bi_directional_streams() -> Result<()> { + utils::init_logging(); + let qp2p = new_qp2p(); let peer1 = qp2p.new_endpoint()?; let peer1_addr = peer1.socket_addr().await?; @@ -104,6 +108,8 @@ async fn bi_directional_streams() -> Result<()> { #[tokio::test] async fn uni_directional_streams() -> Result<()> { + utils::init_logging(); + let qp2p = new_qp2p(); let peer1 = qp2p.new_endpoint()?; let peer1_addr = peer1.socket_addr().await?; @@ -175,6 +181,8 @@ async fn uni_directional_streams() -> Result<()> { #[tokio::test] async fn reuse_outgoing_connection() -> Result<()> { + utils::init_logging(); + let qp2p = new_qp2p(); let alice = qp2p.new_endpoint()?; @@ -229,6 +237,8 @@ async fn reuse_outgoing_connection() -> Result<()> { #[tokio::test] async fn reuse_incoming_connection() -> Result<()> { + utils::init_logging(); + let qp2p = new_qp2p(); let alice = qp2p.new_endpoint()?; let alice_addr = alice.socket_addr().await?; @@ -277,6 +287,8 @@ async fn reuse_incoming_connection() -> Result<()> { #[tokio::test] async fn remove_closed_connection_from_pool() -> Result<()> { + utils::init_logging(); + let qp2p = new_qp2p(); let alice = qp2p.new_endpoint()?; @@ -342,6 +354,8 @@ async fn simultaneous_incoming_and_outgoing_connections() -> Result<()> { // others connection first), two separate connections are created. This test verifies that // everything still works correctly even in this case. + utils::init_logging(); + let qp2p = new_qp2p(); let alice = qp2p.new_endpoint()?; let alice_addr = alice.socket_addr().await?; @@ -417,6 +431,8 @@ async fn simultaneous_incoming_and_outgoing_connections() -> Result<()> { #[tokio::test] async fn multiple_concurrent_connects_to_the_same_peer() -> Result<()> { + utils::init_logging(); + let qp2p = new_qp2p(); let alice = qp2p.new_endpoint()?; let alice_addr = alice.socket_addr().await?; diff --git a/src/tests/echo_service.rs b/src/tests/echo_service.rs index bce642ae..d629fe40 100644 --- a/src/tests/echo_service.rs +++ b/src/tests/echo_service.rs @@ -1,7 +1,9 @@ -use crate::{wire_msg::WireMsg, Config, Error, QuicP2p, Result}; +use crate::{utils, wire_msg::WireMsg, Config, Error, QuicP2p, Result}; #[tokio::test] async fn echo_service() -> Result<()> { + utils::init_logging(); + // Endpoint builder let qp2p = QuicP2p::with_config( Some(Config { diff --git a/src/utils.rs b/src/utils.rs index ea3e280c..0ac7c7f7 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -8,12 +8,10 @@ // Software. use crate::error::{Error, Result}; -use flexi_logger::{DeferredNow, Logger}; -use log::Record; use serde::de::DeserializeOwned; use serde::Serialize; use std::fs::File; -use std::io::{BufReader, BufWriter, Write}; +use std::io::{BufReader, BufWriter}; use std::path::{Path, PathBuf}; /// Get the project directory @@ -93,7 +91,12 @@ where Ok(()) } +#[cfg(test)] pub(crate) fn init_logging() { + use flexi_logger::{DeferredNow, Logger}; + use log::Record; + use std::io::Write; + // Custom formatter for logs let do_format = move |writer: &mut dyn Write, clock: &mut DeferredNow, record: &Record| { let handle = std::thread::current();