Skip to content

Commit

Permalink
fix: do not initialize logger in QuicP2p constructor
Browse files Browse the repository at this point in the history
Initialize it only in tests.
  • Loading branch information
madadam committed Nov 19, 2020
1 parent 32f7df2 commit 4952639
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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]
Expand Down
2 changes: 0 additions & 2 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -113,7 +112,6 @@ impl QuicP2p {
bootstrap_nodes: &[SocketAddr],
use_bootstrap_cache: bool,
) -> Result<Self> {
init_logging();
let cfg = unwrap_config_or_default(cfg)?;
debug!("Config passed in to qp2p: {:?}", cfg);

Expand Down
18 changes: 17 additions & 1 deletion src/tests/common.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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?;
Expand All @@ -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?;
Expand Down Expand Up @@ -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?;
Expand Down Expand Up @@ -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()?;

Expand Down Expand Up @@ -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?;
Expand Down Expand Up @@ -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()?;

Expand Down Expand Up @@ -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?;
Expand Down Expand Up @@ -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?;
Expand Down
4 changes: 3 additions & 1 deletion src/tests/echo_service.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
9 changes: 6 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 4952639

Please sign in to comment.