From 3ee008147c2694111cdf55b552d505b3024c36c2 Mon Sep 17 00:00:00 2001 From: Brian Shih Date: Wed, 3 Jan 2024 18:58:10 +0800 Subject: [PATCH] Start fixing tests --- src/concurrency/concurrency_manager.rs | 9 ++- src/db/db_test.rs | 67 +++++++++++-------- src/interval/interval_tree.rs | 12 ++-- .../latch_interval_btree_test.rs | 62 ++++++++++++----- src/latch_manager/latch_manager.rs | 17 ++--- src/llrb/llrb.rs | 22 +++--- src/storage/mvcc_iterator.rs | 26 +++++-- src/storage/mvcc_scanner_test.rs | 2 - src/timestamp_oracle/oracle.rs | 13 ++-- 9 files changed, 139 insertions(+), 91 deletions(-) diff --git a/src/concurrency/concurrency_manager.rs b/src/concurrency/concurrency_manager.rs index 10f6573..a532cc5 100644 --- a/src/concurrency/concurrency_manager.rs +++ b/src/concurrency/concurrency_manager.rs @@ -54,7 +54,9 @@ impl ConcurrencyManager { if let Err(err) = wait_res { match err { WaitForGuardError::TxnAborted => return Err(SequenceReqError::TxnAborted), - WaitForGuardError::TxnCommitted => return Err(SequenceReqError::TxnCommitted), + WaitForGuardError::TxnCommitted => { + return Err(SequenceReqError::TxnCommitted) + } } }; @@ -96,10 +98,11 @@ impl ConcurrencyManager { } } +#[cfg(test)] mod test { - + use std::time::Duration; - + use tokio::{sync::mpsc::channel, time}; #[tokio::test] async fn test_select() { diff --git a/src/db/db_test.rs b/src/db/db_test.rs index 0c474de..0df0ebe 100644 --- a/src/db/db_test.rs +++ b/src/db/db_test.rs @@ -1,10 +1,12 @@ +#[cfg(test)] mod test { // simple tests that involve writes and reads + #[cfg(test)] mod single_txn_simple_test { - + use std::sync::Arc; - + use crate::db::db::{Timestamp, DB}; #[tokio::test] async fn two_writes_with_different_keys() { @@ -29,6 +31,7 @@ mod test { } } + #[cfg(test)] mod transaction_conflicts { // A read running into an uncommitted intent with a lower timestamp will wait for the // earlier transaction @@ -42,12 +45,9 @@ mod test { */ mod write_read { mod uncommitted_intent_has_lower_timestamp { + use std::sync::Arc; - - - - - + use crate::db::db::{Timestamp, DB}; #[tokio::test] async fn read_waits_for_uncommitted_write() { @@ -76,9 +76,9 @@ mod test { // A read running into an uncommitted intent with a higher timestamp ignores the // intent and does not need to wait. mod uncommitted_intent_has_higher_timestamp { - + use std::sync::Arc; - + use crate::db::db::{Timestamp, DB}; #[tokio::test] async fn ignores_intent_with_higher_timestamp() { @@ -98,11 +98,14 @@ mod test { // A write running into an uncommitted intent with a lower timestamp will wait for the transaction // to finish. // A write running into a committed value with a higher tiestamp will bump its timestamp. + #[cfg(test)] mod write_write { mod run_into_uncommitted_intent { - + use std::sync::Arc; + + use crate::db::db::{CommitTxnResult, Timestamp, DB}; - + use crate::hlc::timestamp::Timestamp as HLCTimestamp; #[tokio::test] async fn write_waits_for_uncommitted_write() { @@ -145,10 +148,11 @@ mod test { } } + #[cfg(test)] mod run_into_committed_intent { - + use std::sync::Arc; - + use crate::db::db::{CommitTxnResult, Timestamp, DB}; #[tokio::test] async fn bump_write_timestamp_before_committing() { @@ -196,10 +200,11 @@ mod test { * If a write detects a read on the same key with a higher timestamp, * the writeTimestamp is bumped */ + #[cfg(test)] mod read_write { - + use std::sync::Arc; - + use crate::db::db::{CommitTxnResult, Timestamp, DB}; #[tokio::test] async fn bump_write_timestamp_before_committing() { @@ -234,15 +239,13 @@ mod test { } } + #[cfg(test)] mod read_refresh { - - - - + #[cfg(test)] mod read_refresh_success { - + use std::sync::Arc; - + use crate::db::db::{CommitTxnResult, Timestamp, DB}; #[tokio::test] async fn read_refresh_from_write_write_conflict() { @@ -277,10 +280,12 @@ mod test { // Advancing a transaction read timestamp from ta to tb is possible // if we can prove that none of the data + + #[cfg(test)] mod read_refresh_failure { - + use std::sync::Arc; - + use crate::db::db::{CommitTxnResult, Timestamp, DB}; #[tokio::test] async fn read_refresh_failure() { @@ -321,8 +326,9 @@ mod test { } } + #[cfg(test)] mod abort_txn { - + use crate::db::db::{Timestamp, DB}; #[tokio::test] async fn read_write_after_abort_transaction() { @@ -346,10 +352,14 @@ mod test { } } + #[cfg(test)] mod deadlock { - + use std::sync::Arc; - + use crate::{ + db::db::{CommitTxnResult, Timestamp, DB}, + storage::str_to_key, + }; #[tokio::test] async fn conflicting_writes() { @@ -454,10 +464,11 @@ mod test { } } + #[cfg(test)] mod run_txn { - + use std::sync::Arc; - + use crate::db::db::{Timestamp, DB}; #[tokio::test] async fn reading_its_txn_own_write() { diff --git a/src/interval/interval_tree.rs b/src/interval/interval_tree.rs index a8cb76f..a4cfe48 100644 --- a/src/interval/interval_tree.rs +++ b/src/interval/interval_tree.rs @@ -1,8 +1,4 @@ - - -use crate::{ - llrb::llrb::{NodeKey, NodeValue, RbTree, NIL}, -}; +use crate::llrb::llrb::{NodeKey, NodeValue, RbTree, NIL}; // TODO: Eviction policy to make sure IntervalTree is bounded pub struct IntervalTree { @@ -102,12 +98,12 @@ impl IntervalTree { } } +#[cfg(test)] mod tests { + #[cfg(test)] mod get_overlap { - - - + use crate::interval::interval_tree::{IntervalTree, RangeValue}; #[test] fn test_overlap() { diff --git a/src/latch_manager/latch_interval_btree_test.rs b/src/latch_manager/latch_interval_btree_test.rs index ae18d5a..f3d75f9 100644 --- a/src/latch_manager/latch_interval_btree_test.rs +++ b/src/latch_manager/latch_interval_btree_test.rs @@ -428,12 +428,13 @@ mod Test { } } + #[cfg(test)] mod split { - - - - - + use crate::latch_manager::latch_interval_btree_test::Test::{ + assert_leaf_with_siblings, assert_node, create_test_node, create_test_tree, + find_node_and_parent_with_indices, get_all_leaves, get_start_keys_from_weak_link, + TestInternalNode, TestLeafNode, TestNode, + }; #[test] fn split_internal() { @@ -533,10 +534,11 @@ mod Test { } } + #[cfg(test)] mod insert { - + use crate::latch_manager::latch_interval_btree::{BTree, LatchKeyGuard, Range}; - + use super::{assert_tree, print_tree, TestInternalNode, TestLeafNode, TestNode}; #[test] fn insert_and_split() { @@ -684,10 +686,11 @@ mod Test { } } + #[cfg(test)] mod leaf_underflow { - + use std::sync::RwLock; - + use crate::latch_manager::latch_interval_btree::{LatchWaiters, LeafNode}; #[test] fn underflows() { @@ -705,9 +708,13 @@ mod Test { } } + #[cfg(test)] mod delete { + #[cfg(test)] mod core_delete { - + use crate::latch_manager::latch_interval_btree_test::Test::{ + assert_tree, create_test_tree, print_tree, TestInternalNode, TestLeafNode, TestNode, + }; #[test] fn internal_node_stealing_from_left_sibling_3_layers() { @@ -972,12 +979,16 @@ mod Test { } mod leaf_stealing { - mod has_spare_keys { - + use std::sync::RwLock; - + use crate::latch_manager::{ + latch_interval_btree::{LatchWaiters, LeafNode}, + latch_interval_btree_test::Test::{ + assert_tree, create_test_tree, TestInternalNode, TestLeafNode, TestNode, + }, + }; #[test] fn internal_node() {} @@ -1076,7 +1087,10 @@ mod Test { } mod stealing_core { - + use crate::latch_manager::latch_interval_btree_test::Test::{ + assert_tree, create_test_tree, print_tree, TestInternalNode, TestLeafNode, + TestNode, + }; #[test] fn leaf_steals_left_sibling() { @@ -1203,8 +1217,12 @@ mod Test { } } + #[cfg(test)] mod internal_node_stealing { - + use crate::latch_manager::latch_interval_btree_test::Test::{ + assert_tree, create_test_tree, find_node_and_parent_with_indices, TestInternalNode, + TestLeafNode, TestNode, + }; #[test] fn simple_steal_from_left_sibling() { @@ -1365,9 +1383,15 @@ mod Test { } } + #[cfg(test)] mod merge { + + #[cfg(test)] mod internal_node { - + use crate::latch_manager::latch_interval_btree_test::Test::{ + assert_tree, create_test_tree, find_node_and_parent_with_indices, TestInternalNode, + TestLeafNode, TestNode, + }; #[test] fn merge_with_left() { @@ -1494,8 +1518,12 @@ mod Test { } } + #[cfg(test)] mod leaf { - + use crate::latch_manager::latch_interval_btree_test::Test::{ + assert_tree, create_test_tree, find_node_and_parent_with_indices, print_tree, + TestInternalNode, TestLeafNode, TestNode, + }; #[test] fn merge_with_left_leaf() { diff --git a/src/latch_manager/latch_manager.rs b/src/latch_manager/latch_manager.rs index fc3ae17..ed80df3 100644 --- a/src/latch_manager/latch_manager.rs +++ b/src/latch_manager/latch_manager.rs @@ -1,13 +1,9 @@ - - use rand::Rng; use tokio::time::{self, Duration}; use crate::execute::request::SpanSet; -use super::{ - latch_interval_btree::{BTree, LatchKeyGuard, NodeKey, Range}, -}; +use super::latch_interval_btree::{BTree, LatchKeyGuard, NodeKey, Range}; pub struct LatchManager { tree: BTree, @@ -86,13 +82,14 @@ impl LatchManager { } } -mod Test { - mod Acquire { - +mod test { + #[cfg(test)] + mod acquire { + use std::{sync::Arc, time::Duration}; - + use tokio::time::sleep; - + use crate::latch_manager::{latch_interval_btree::Range, latch_manager::LatchManager}; #[tokio::test] async fn test_select() { diff --git a/src/llrb/llrb.rs b/src/llrb/llrb.rs index acafcfb..630db8f 100644 --- a/src/llrb/llrb.rs +++ b/src/llrb/llrb.rs @@ -1,7 +1,4 @@ -use std::{ - cmp::Ordering, - fmt, -}; +use std::{cmp::Ordering, fmt}; pub trait NodeKey: std::fmt::Debug + Clone + Eq + PartialOrd + Ord {} @@ -507,15 +504,12 @@ impl NodeKey for i32 {} impl NodeValue for i32 {} -mod Test { - - - - - +#[cfg(test)] +mod test { + use std::{cell::RefCell, rc::Rc}; mod insert_node { - + use crate::llrb::llrb::RbTree; #[test] fn insert_into_empty_tree() { @@ -578,8 +572,9 @@ mod Test { } } + #[cfg(test)] mod left_rotate { - + use crate::llrb::llrb::{Node, RbTree, TreeColor}; #[test] fn left_rotate_on_root() { @@ -644,8 +639,9 @@ mod Test { } } + #[cfg(test)] mod right_rotate { - + use crate::llrb::llrb::{Node, RbTree, TreeColor}; #[test] fn simple_right_rotate_on_root() { diff --git a/src/storage/mvcc_iterator.rs b/src/storage/mvcc_iterator.rs index f0f80cf..f15161f 100644 --- a/src/storage/mvcc_iterator.rs +++ b/src/storage/mvcc_iterator.rs @@ -150,12 +150,17 @@ impl<'a> MVCCIterator<'a> { } } +#[cfg(test)] mod tests { - - - - - + use crate::{ + hlc::timestamp::{get_intent_timestamp, Timestamp}, + storage::{ + mvcc_iterator::{IterOptions, MVCCIterator}, + mvcc_key::MVCCKey, + storage::Storage, + str_to_key, + }, + }; #[test] fn test_current_key_and_current_value() { @@ -245,8 +250,17 @@ mod tests { assert_eq!(iterator.valid(), false); } + #[cfg(test)] mod test_seek_ge { - + use crate::{ + hlc::timestamp::Timestamp, + storage::{ + mvcc_iterator::{IterOptions, MVCCIterator}, + mvcc_key::MVCCKey, + storage::Storage, + str_to_key, + }, + }; #[test] fn test_multiple_timestamps_with_same_prefix() { diff --git a/src/storage/mvcc_scanner_test.rs b/src/storage/mvcc_scanner_test.rs index d9ccb44..877b766 100644 --- a/src/storage/mvcc_scanner_test.rs +++ b/src/storage/mvcc_scanner_test.rs @@ -175,8 +175,6 @@ mod tests { #[cfg(test)] mod scan { - use core::time; - use uuid::Uuid; use crate::{ diff --git a/src/timestamp_oracle/oracle.rs b/src/timestamp_oracle/oracle.rs index 6705738..58a60c1 100644 --- a/src/timestamp_oracle/oracle.rs +++ b/src/timestamp_oracle/oracle.rs @@ -1,4 +1,4 @@ -use std::{sync::RwLock}; +use std::sync::RwLock; use uuid::Uuid; @@ -8,7 +8,7 @@ use crate::{ interval::interval_tree::IntervalTree, latch_manager::latch_interval_btree::Range, llrb::llrb::{NodeKey, NodeValue}, - storage::{Key}, + storage::Key, }; #[derive(Clone)] @@ -104,10 +104,15 @@ impl TimestampOracle { } } -mod Test { +#[cfg(test)] +mod test { + #[cfg(test)] mod get_max_timestamp { - + use crate::{ + hlc::timestamp::Timestamp, storage::str_to_key, + timestamp_oracle::oracle::TimestampOracle, + }; #[test] fn test_overlap() {