Skip to content

Commit

Permalink
Log in unit tests as well.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoverbear committed Aug 11, 2018
1 parent 09a7947 commit 4a1299b
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,12 @@ pub type Result<T> = result::Result<T, Error>;
#[cfg(test)]
mod tests {
use super::*;
use env_logger;
use std::io;

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

#[test]
fn test_storage_error_equal() {
let _ = env_logger::try_init();
assert_eq!(StorageError::Compacted, StorageError::Compacted);
assert_eq!(StorageError::Unavailable, StorageError::Unavailable);
assert_eq!(
Expand Down
2 changes: 2 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
7 changes: 7 additions & 0 deletions src/log_unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ impl Unstable {

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

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

#[test]
fn test_maybe_first_index() {
let _ = env_logger::try_init();
// 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() {
let _ = env_logger::try_init();
// 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() {
let _ = env_logger::try_init();
// 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() {
let _ = env_logger::try_init();
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() {
let _ = env_logger::try_init();
// 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() {
let _ = env_logger::try_init();
// 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 @@ -418,10 +418,12 @@ impl Inflights {

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

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

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

#[test]
fn test_inflight_free_first_one() {
let _ = env_logger::try_init();
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 @@ -487,6 +487,7 @@ impl<T: Storage> RaftLog<T> {
mod test {
use std::panic::{self, AssertUnwindSafe};

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

#[test]
fn test_find_conflict() {
let _ = env_logger::try_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 @@ -572,6 +574,7 @@ mod test {

#[test]
fn test_is_up_to_date() {
let _ = env_logger::try_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 @@ -600,6 +603,7 @@ mod test {

#[test]
fn test_append() {
let _ = env_logger::try_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 @@ -642,6 +646,7 @@ mod test {

#[test]
fn test_compaction_side_effects() {
let _ = env_logger::try_init();
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() {
let _ = env_logger::try_init();
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() {
let _ = env_logger::try_init();
let offset = 100u64;
let num = 100u64;

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

#[test]
fn test_log_restore() {
let _ = env_logger::try_init();
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() {
let _ = env_logger::try_init();
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() {
let _ = env_logger::try_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 @@ -858,6 +868,7 @@ mod test {
// entries correctly.
#[test]
fn test_unstable_ents() {
let _ = env_logger::try_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 @@ -891,6 +902,7 @@ mod test {

#[test]
fn test_next_ents() {
let _ = env_logger::try_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 @@ -918,6 +930,7 @@ mod test {

#[test]
fn test_has_next_ents() {
let _ = env_logger::try_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 @@ -938,6 +951,7 @@ mod test {

#[test]
fn test_slice() {
let _ = env_logger::try_init();
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() {
let _ = env_logger::try_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 @@ -1253,6 +1268,7 @@ mod test {

#[test]
fn test_commit_to() {
let _ = env_logger::try_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 @@ -1280,6 +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();
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() {
let _ = env_logger::try_init();
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 @@ -479,10 +479,12 @@ impl<T: Storage> RawNode<T> {
#[cfg(test)]
mod test {
use super::is_local_msg;
use env_logger;
use eraftpb::MessageType;

#[test]
fn test_is_local_msg() {
let _ = env_logger::try_init();
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 @@ -323,6 +323,7 @@ mod test {
use eraftpb::{ConfState, Entry, Snapshot};
use protobuf;

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

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

#[test]
fn test_storage_term() {
let _ = env_logger::try_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 @@ -372,6 +374,7 @@ mod test {

#[test]
fn test_storage_entries() {
let _ = env_logger::try_init();
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() {
let _ = env_logger::try_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 @@ -464,6 +468,7 @@ mod test {

#[test]
fn test_storage_first_index() {
let _ = env_logger::try_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 @@ -484,6 +489,7 @@ mod test {

#[test]
fn test_storage_compact() {
let _ = env_logger::try_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 @@ -516,6 +522,7 @@ mod test {

#[test]
fn test_storage_create_snapshot() {
let _ = env_logger::try_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 @@ -543,6 +550,7 @@ mod test {

#[test]
fn test_storage_append() {
let _ = env_logger::try_init();
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() {
let _ = env_logger::try_init();
let nodes = vec![1, 2, 3];
let data = b"data".to_vec();

Expand Down

0 comments on commit 4a1299b

Please sign in to comment.