Skip to content

Commit

Permalink
Use an initializer fn for logger initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoverbear committed Aug 14, 2018
1 parent 2d98bbb commit dcd607c
Show file tree
Hide file tree
Showing 13 changed files with 231 additions and 203 deletions.
6 changes: 3 additions & 3 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ pub type Result<T> = result::Result<T, Error>;
#[cfg(test)]
mod tests {
use super::*;
use env_logger;
use std::io;
use test_init;

#[test]
fn test_error_equal() {
let _ = env_logger::try_init();
test_init();
assert_eq!(Error::StepPeerNotFound, Error::StepPeerNotFound);
assert_eq!(
Error::Store(StorageError::Compacted),
Expand Down Expand Up @@ -170,7 +170,7 @@ mod tests {

#[test]
fn test_storage_error_equal() {
let _ = env_logger::try_init();
test_init();
assert_eq!(StorageError::Compacted, StorageError::Compacted);
assert_eq!(StorageError::Unavailable, StorageError::Unavailable);
assert_eq!(
Expand Down
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,17 @@ pub mod prelude {

pub use read_only::{ReadOnlyOption, ReadState};
}

/// Initialization code common across all tests.
///
/// A duplicate exists in `tests/tests.rs`, please update them at the same time.
///
/// They both exist because `cargo test` on things in `tests/` builds the crate without `#[cfg(test)]` and we don't want to require `env_logger` for non-test builds.
///
/// Appropriate things:
/// * logger setup
/// * fail-rs setup
#[cfg(test)]
pub fn test_init() {
env_logger::try_init().ok();
}
14 changes: 7 additions & 7 deletions src/log_unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl Unstable {

#[cfg(test)]
mod test {
use env_logger;
use test_init;
use eraftpb::{Entry, Snapshot, SnapshotMetadata};
use log_unstable::Unstable;

Expand All @@ -205,7 +205,7 @@ mod test {

#[test]
fn test_maybe_first_index() {
let _ = env_logger::try_init();
test_init();
// entry, offset, snap, wok, windex,
let tests = vec![
// no snapshot
Expand Down Expand Up @@ -233,7 +233,7 @@ mod test {

#[test]
fn test_maybe_last_index() {
let _ = env_logger::try_init();
test_init();
// entry, offset, snap, wok, windex,
let tests = vec![
(Some(new_entry(5, 1)), 5, None, true, 5),
Expand Down Expand Up @@ -261,7 +261,7 @@ mod test {

#[test]
fn test_maybe_term() {
let _ = env_logger::try_init();
test_init();
// entry, offset, snap, index, wok, wterm
let tests = vec![
// term from entries
Expand Down Expand Up @@ -323,7 +323,7 @@ mod test {

#[test]
fn test_restore() {
let _ = env_logger::try_init();
test_init();
let mut u = Unstable {
entries: vec![new_entry(5, 1)],
offset: 5,
Expand All @@ -341,7 +341,7 @@ mod test {

#[test]
fn test_stable_to() {
let _ = env_logger::try_init();
test_init();
// entries, offset, snap, index, term, woffset, wlen
let tests = vec![
(vec![], 0, None, 5, 1, 0, 0),
Expand Down Expand Up @@ -421,7 +421,7 @@ mod test {

#[test]
fn test_truncate_and_append() {
let _ = env_logger::try_init();
test_init();
// entries, offset, snap, to_append, woffset, wentries
let tests = vec![
// replace to the end
Expand Down
8 changes: 4 additions & 4 deletions src/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,12 +418,12 @@ impl Inflights {

#[cfg(test)]
mod test {
use env_logger;
use test_init;
use progress::Inflights;

#[test]
fn test_inflight_add() {
let _ = env_logger::try_init();
test_init();
let mut inflight = Inflights::new(10);
for i in 0..5 {
inflight.add(i);
Expand Down Expand Up @@ -483,7 +483,7 @@ mod test {

#[test]
fn test_inflight_free_to() {
let _ = env_logger::try_init();
test_init();
let mut inflight = Inflights::new(10);
for i in 0..10 {
inflight.add(i);
Expand Down Expand Up @@ -536,7 +536,7 @@ mod test {

#[test]
fn test_inflight_free_first_one() {
let _ = env_logger::try_init();
test_init();
let mut inflight = Inflights::new(10);
for i in 0..10 {
inflight.add(i);
Expand Down
36 changes: 18 additions & 18 deletions src/raft_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ impl<T: Storage> RaftLog<T> {
mod test {
use std::panic::{self, AssertUnwindSafe};

use env_logger;
use test_init;
use eraftpb;
use errors::{Error, StorageError};
use protobuf;
Expand Down Expand Up @@ -516,7 +516,7 @@ mod test {

#[test]
fn test_find_conflict() {
let _ = env_logger::try_init();
test_init();
let previous_ents = vec![new_entry(1, 1), new_entry(2, 2), new_entry(3, 3)];
let tests = vec![
// no conflict, empty ent
Expand Down Expand Up @@ -574,7 +574,7 @@ mod test {

#[test]
fn test_is_up_to_date() {
let _ = env_logger::try_init();
test_init();
let previous_ents = vec![new_entry(1, 1), new_entry(2, 2), new_entry(3, 3)];
let store = MemStorage::new();
let mut raft_log = new_raft_log(store);
Expand Down Expand Up @@ -603,7 +603,7 @@ mod test {

#[test]
fn test_append() {
let _ = env_logger::try_init();
test_init();
let previous_ents = vec![new_entry(1, 1), new_entry(2, 2)];
let tests = vec![
(vec![], 2, vec![new_entry(1, 1), new_entry(2, 2)], 3),
Expand Down Expand Up @@ -646,7 +646,7 @@ mod test {

#[test]
fn test_compaction_side_effects() {
let _ = env_logger::try_init();
test_init();
let last_index = 1000u64;
let unstable_index = 750u64;
let last_term = last_index;
Expand Down Expand Up @@ -699,7 +699,7 @@ mod test {

#[test]
fn test_term_with_unstable_snapshot() {
let _ = env_logger::try_init();
test_init();
let storagesnapi = 10064;
let unstablesnapi = storagesnapi + 5;
let store = MemStorage::new();
Expand Down Expand Up @@ -730,7 +730,7 @@ mod test {

#[test]
fn test_term() {
let _ = env_logger::try_init();
test_init();
let offset = 100u64;
let num = 100u64;

Expand Down Expand Up @@ -762,7 +762,7 @@ mod test {

#[test]
fn test_log_restore() {
let _ = env_logger::try_init();
test_init();
let (index, term) = (1000u64, 1000u64);
let store = MemStorage::new();
store
Expand All @@ -781,7 +781,7 @@ mod test {

#[test]
fn test_stable_to_with_snap() {
let _ = env_logger::try_init();
test_init();
let (snap_index, snap_term) = (5u64, 2u64);
let tests = vec![
(snap_index + 1, snap_term, vec![], snap_index + 1),
Expand Down Expand Up @@ -848,7 +848,7 @@ mod test {

#[test]
fn test_stable_to() {
let _ = env_logger::try_init();
test_init();
let tests = vec![(1, 1, 2), (2, 2, 3), (2, 1, 1), (3, 1, 1)];
for (i, &(stablei, stablet, wunstable)) in tests.iter().enumerate() {
let store = MemStorage::new();
Expand All @@ -868,7 +868,7 @@ mod test {
// entries correctly.
#[test]
fn test_unstable_ents() {
let _ = env_logger::try_init();
test_init();
let previous_ents = vec![new_entry(1, 1), new_entry(2, 2)];
let tests = vec![(3, vec![]), (1, previous_ents.clone())];

Expand Down Expand Up @@ -902,7 +902,7 @@ mod test {

#[test]
fn test_next_ents() {
let _ = env_logger::try_init();
test_init();
let ents = [new_entry(4, 1), new_entry(5, 1), new_entry(6, 1)];
let tests = vec![
(0, Some(&ents[..2])),
Expand Down Expand Up @@ -930,7 +930,7 @@ mod test {

#[test]
fn test_has_next_ents() {
let _ = env_logger::try_init();
test_init();
let ents = [new_entry(4, 1), new_entry(5, 1), new_entry(6, 1)];
let tests = vec![(0, true), (3, true), (4, true), (5, false)];

Expand All @@ -951,7 +951,7 @@ mod test {

#[test]
fn test_slice() {
let _ = env_logger::try_init();
test_init();
let (offset, num) = (100u64, 100u64);
let (last, half) = (offset + num, offset + num / 2);
let halfe = new_entry(half, half);
Expand Down Expand Up @@ -1089,7 +1089,7 @@ mod test {
/// return false
#[test]
fn test_log_maybe_append() {
let _ = env_logger::try_init();
test_init();
let previous_ents = vec![new_entry(1, 1), new_entry(2, 2), new_entry(3, 3)];
let (last_index, last_term, commit) = (3u64, 3u64, 1u64);

Expand Down Expand Up @@ -1268,7 +1268,7 @@ mod test {

#[test]
fn test_commit_to() {
let _ = env_logger::try_init();
test_init();
let previous_ents = vec![new_entry(1, 1), new_entry(2, 2), new_entry(3, 3)];
let previous_commit = 2u64;
let tests = vec![
Expand Down Expand Up @@ -1296,7 +1296,7 @@ mod test {
// TestCompaction ensures that the number of log entries is correct after compactions.
#[test]
fn test_compaction() {
let _ = env_logger::try_init();
test_init();
let tests = vec![
// out of upper bound
(1000, vec![1001u64], vec![0usize], false),
Expand Down Expand Up @@ -1346,7 +1346,7 @@ mod test {

#[test]
fn test_is_outofbounds() {
let _ = env_logger::try_init();
test_init();
let (offset, num) = (100u64, 100u64);
let store = MemStorage::new();
store
Expand Down
4 changes: 2 additions & 2 deletions src/raw_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,12 +479,12 @@ impl<T: Storage> RawNode<T> {
#[cfg(test)]
mod test {
use super::is_local_msg;
use env_logger;
use test_init;
use eraftpb::MessageType;

#[test]
fn test_is_local_msg() {
let _ = env_logger::try_init();
test_init();
let tests = vec![
(MessageType::MsgHup, true),
(MessageType::MsgBeat, true),
Expand Down
18 changes: 9 additions & 9 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ mod test {
use eraftpb::{ConfState, Entry, Snapshot};
use protobuf;

use env_logger;
use test_init;
use errors::{Error as RaftError, StorageError};
use storage::{MemStorage, Storage};

Expand Down Expand Up @@ -351,7 +351,7 @@ mod test {

#[test]
fn test_storage_term() {
let _ = env_logger::try_init();
test_init();
let ents = vec![new_entry(3, 3), new_entry(4, 4), new_entry(5, 5)];
let mut tests = vec![
(2, Err(RaftError::Store(StorageError::Compacted))),
Expand All @@ -374,7 +374,7 @@ mod test {

#[test]
fn test_storage_entries() {
let _ = env_logger::try_init();
test_init();
let ents = vec![
new_entry(3, 3),
new_entry(4, 4),
Expand Down Expand Up @@ -444,7 +444,7 @@ mod test {

#[test]
fn test_storage_last_index() {
let _ = env_logger::try_init();
test_init();
let ents = vec![new_entry(3, 3), new_entry(4, 4), new_entry(5, 5)];
let storage = MemStorage::new();
storage.wl().entries = ents;
Expand All @@ -468,7 +468,7 @@ mod test {

#[test]
fn test_storage_first_index() {
let _ = env_logger::try_init();
test_init();
let ents = vec![new_entry(3, 3), new_entry(4, 4), new_entry(5, 5)];
let storage = MemStorage::new();
storage.wl().entries = ents;
Expand All @@ -489,7 +489,7 @@ mod test {

#[test]
fn test_storage_compact() {
let _ = env_logger::try_init();
test_init();
let ents = vec![new_entry(3, 3), new_entry(4, 4), new_entry(5, 5)];
let mut tests = vec![
(2, Err(RaftError::Store(StorageError::Compacted)), 3, 3, 3),
Expand Down Expand Up @@ -522,7 +522,7 @@ mod test {

#[test]
fn test_storage_create_snapshot() {
let _ = env_logger::try_init();
test_init();
let ents = vec![new_entry(3, 3), new_entry(4, 4), new_entry(5, 5)];
let nodes = vec![1, 2, 3];
let mut cs = ConfState::new();
Expand Down Expand Up @@ -550,7 +550,7 @@ mod test {

#[test]
fn test_storage_append() {
let _ = env_logger::try_init();
test_init();
let ents = vec![new_entry(3, 3), new_entry(4, 4), new_entry(5, 5)];
let mut tests = vec![
(
Expand Down Expand Up @@ -619,7 +619,7 @@ mod test {

#[test]
fn test_storage_apply_snapshot() {
let _ = env_logger::try_init();
test_init();
let nodes = vec![1, 2, 3];
let data = b"data".to_vec();

Expand Down
Loading

0 comments on commit dcd607c

Please sign in to comment.