Skip to content

Commit

Permalink
Initialize the env_logger in tests for tracing. (#103)
Browse files Browse the repository at this point in the history
* Initialize the env_logger in tests for tracing.

* fmt run

* Log in unit tests as well.

* Use extracted init function
  • Loading branch information
A. Hobden authored Sep 4, 2018
1 parent 8b547c5 commit e4124e8
Show file tree
Hide file tree
Showing 14 changed files with 219 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ quick-error = "1.2.2"
rand = "0.5.4"
fxhash = "0.2.1"

[dev-dependencies]
env_logger = "0.5.12"

[badges]
travis-ci = { repository = "pingcap/raft-rs" }

Expand Down
3 changes: 3 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,12 @@ pub type Result<T> = result::Result<T, Error>;
#[cfg(test)]
mod tests {
use super::*;
use setup_for_test;
use std::io;

#[test]
fn test_error_equal() {
setup_for_test();
assert_eq!(Error::StepPeerNotFound, Error::StepPeerNotFound);
assert_eq!(
Error::Store(StorageError::Compacted),
Expand Down Expand Up @@ -175,6 +177,7 @@ mod tests {

#[test]
fn test_storage_error_equal() {
setup_for_test();
assert_eq!(StorageError::Compacted, StorageError::Compacted);
assert_eq!(StorageError::Unavailable, StorageError::Unavailable);
assert_eq!(
Expand Down
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ extern crate log;
extern crate protobuf;
#[macro_use]
extern crate quick_error;
#[cfg(test)]
extern crate env_logger;
extern crate rand;

mod config;
Expand Down Expand Up @@ -284,3 +286,9 @@ pub mod prelude {

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

/// Do any common test initialization. Eg set up logging, setup fail-rs.
#[cfg(test)]
pub fn setup_for_test() {
let _ = env_logger::try_init();
}
7 changes: 7 additions & 0 deletions src/log_unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ impl Unstable {
mod test {
use eraftpb::{Entry, Snapshot, SnapshotMetadata};
use log_unstable::Unstable;
use setup_for_test;

fn new_entry(index: u64, term: u64) -> Entry {
let mut e = Entry::new();
Expand All @@ -204,6 +205,7 @@ mod test {

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

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

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

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

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

#[test]
fn test_truncate_and_append() {
setup_for_test();
// entries, offset, snap, to_append, woffset, wentries
let tests = vec![
// replace to the end
Expand Down
4 changes: 4 additions & 0 deletions src/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,11 @@ impl Inflights {
#[cfg(test)]
mod test {
use progress::Inflights;
use setup_for_test;

#[test]
fn test_inflight_add() {
setup_for_test();
let mut inflight = Inflights::new(10);
for i in 0..5 {
inflight.add(i);
Expand Down Expand Up @@ -479,6 +481,7 @@ mod test {

#[test]
fn test_inflight_free_to() {
setup_for_test();
let mut inflight = Inflights::new(10);
for i in 0..10 {
inflight.add(i);
Expand Down Expand Up @@ -531,6 +534,7 @@ mod test {

#[test]
fn test_inflight_free_first_one() {
setup_for_test();
let mut inflight = Inflights::new(10);
for i in 0..10 {
inflight.add(i);
Expand Down
18 changes: 18 additions & 0 deletions src/raft_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ mod test {
use errors::{Error, StorageError};
use protobuf;
use raft_log::{self, RaftLog};
use setup_for_test;
use storage::MemStorage;

fn new_raft_log(s: MemStorage) -> RaftLog<MemStorage> {
Expand All @@ -515,6 +516,7 @@ mod test {

#[test]
fn test_find_conflict() {
setup_for_test();
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 @@ -572,6 +574,7 @@ mod test {

#[test]
fn test_is_up_to_date() {
setup_for_test();
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 @@ -600,6 +603,7 @@ mod test {

#[test]
fn test_append() {
setup_for_test();
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 @@ -642,6 +646,7 @@ mod test {

#[test]
fn test_compaction_side_effects() {
setup_for_test();
let last_index = 1000u64;
let unstable_index = 750u64;
let last_term = last_index;
Expand Down Expand Up @@ -694,6 +699,7 @@ mod test {

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

#[test]
fn test_term() {
setup_for_test();
let offset = 100u64;
let num = 100u64;

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

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

#[test]
fn test_stable_to_with_snap() {
setup_for_test();
let (snap_index, snap_term) = (5u64, 2u64);
let tests = vec![
(snap_index + 1, snap_term, vec![], snap_index + 1),
Expand Down Expand Up @@ -839,6 +848,7 @@ mod test {

#[test]
fn test_stable_to() {
setup_for_test();
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 @@ -858,6 +868,7 @@ mod test {
// entries correctly.
#[test]
fn test_unstable_ents() {
setup_for_test();
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 @@ -891,6 +902,7 @@ mod test {

#[test]
fn test_next_ents() {
setup_for_test();
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 @@ -918,6 +930,7 @@ mod test {

#[test]
fn test_has_next_ents() {
setup_for_test();
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 @@ -938,6 +951,7 @@ mod test {

#[test]
fn test_slice() {
setup_for_test();
let (offset, num) = (100u64, 100u64);
let (last, half) = (offset + num, offset + num / 2);
let halfe = new_entry(half, half);
Expand Down Expand Up @@ -1075,6 +1089,7 @@ mod test {
/// return false
#[test]
fn test_log_maybe_append() {
setup_for_test();
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 @@ -1253,6 +1268,7 @@ mod test {

#[test]
fn test_commit_to() {
setup_for_test();
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 @@ -1280,6 +1296,7 @@ mod test {
// TestCompaction ensures that the number of log entries is correct after compactions.
#[test]
fn test_compaction() {
setup_for_test();
let tests = vec![
// out of upper bound
(1000, vec![1001u64], vec![0usize], false),
Expand Down Expand Up @@ -1329,6 +1346,7 @@ mod test {

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

#[test]
fn test_is_local_msg() {
setup_for_test();
let tests = vec![
(MessageType::MsgHup, true),
(MessageType::MsgBeat, true),
Expand Down
9 changes: 9 additions & 0 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ mod test {
use protobuf;

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

// TODO extract these duplicated utility functions for tests
Expand All @@ -350,6 +351,7 @@ mod test {

#[test]
fn test_storage_term() {
setup_for_test();
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 @@ -372,6 +374,7 @@ mod test {

#[test]
fn test_storage_entries() {
setup_for_test();
let ents = vec![
new_entry(3, 3),
new_entry(4, 4),
Expand Down Expand Up @@ -441,6 +444,7 @@ mod test {

#[test]
fn test_storage_last_index() {
setup_for_test();
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 @@ -464,6 +468,7 @@ mod test {

#[test]
fn test_storage_first_index() {
setup_for_test();
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 @@ -484,6 +489,7 @@ mod test {

#[test]
fn test_storage_compact() {
setup_for_test();
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 @@ -516,6 +522,7 @@ mod test {

#[test]
fn test_storage_create_snapshot() {
setup_for_test();
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 @@ -543,6 +550,7 @@ mod test {

#[test]
fn test_storage_append() {
setup_for_test();
let ents = vec![new_entry(3, 3), new_entry(4, 4), new_entry(5, 5)];
let mut tests = vec![
(
Expand Down Expand Up @@ -611,6 +619,7 @@ mod test {

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

Expand Down
Loading

0 comments on commit e4124e8

Please sign in to comment.