Skip to content

Commit

Permalink
unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Liang Zhao committed Mar 11, 2023
1 parent eccfc5e commit 8f82b65
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
13 changes: 7 additions & 6 deletions src/storage/src/hummock/compactor/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,8 @@ mod tests {
use crate::hummock::iterator::test_utils::mock_sstable_store;
use crate::hummock::iterator::HummockIterator;
use crate::hummock::test_utils::{
default_builder_opt_for_test, gen_test_sstable, test_key_of, test_value_of, TEST_KEYS_COUNT,
default_builder_opt_for_test, gen_test_sstable_and_info, test_key_of, test_value_of,
TEST_KEYS_COUNT,
};
use crate::hummock::value::HummockValue;

Expand All @@ -412,15 +413,15 @@ mod tests {
for sst_id in 0..3 {
let start_index = sst_id * TEST_KEYS_COUNT;
let end_index = (sst_id + 1) * TEST_KEYS_COUNT;
let table = gen_test_sstable(
let (_table, table_info) = gen_test_sstable_and_info(
default_builder_opt_for_test(),
sst_id as u64,
(start_index..end_index)
.map(|i| (test_key_of(i), HummockValue::put(test_value_of(i)))),
sstable_store.clone(),
)
.await;
table_infos.push(table.get_sstable_info());
table_infos.push(table_info);
}
let start_index = 5000;
let end_index = 25000;
Expand Down Expand Up @@ -512,15 +513,15 @@ mod tests {
for sst_id in 0..3 {
let start_index = sst_id * TEST_KEYS_COUNT + TEST_KEYS_COUNT / 2;
let end_index = (sst_id + 1) * TEST_KEYS_COUNT;
let table = gen_test_sstable(
let (_table, table_info) = gen_test_sstable_and_info(
default_builder_opt_for_test(),
sst_id as u64,
(start_index..end_index)
.map(|i| (test_key_of(i), HummockValue::put(test_value_of(i)))),
sstable_store.clone(),
)
.await;
table_infos.push(table.get_sstable_info());
table_infos.push(table_info);
}

// Test seek_idx. Result is dominated by given seek key rather than key range.
Expand Down Expand Up @@ -550,7 +551,7 @@ mod tests {
let block_1_second_key = iter.key().to_vec();
// Use a big enough seek key and result in invalid iterator.
let seek_key = test_key_of(30001);
iter.seek_idx(0, Some(seek_key.encode().as_slice()))
iter.seek_idx(table_infos.len() - 1, Some(seek_key.encode().as_slice()))
.await
.unwrap();
assert!(!iter.is_valid());
Expand Down
24 changes: 22 additions & 2 deletions src/storage/src/hummock/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ pub async fn gen_test_sstable_inner<B: AsRef<[u8]>>(
range_tombstones: Vec<DeleteRangeTombstone>,
sstable_store: SstableStoreRef,
policy: CachePolicy,
) -> Sstable {
) -> (Sstable, SstableInfo) {
let writer_opts = SstableWriterOptions {
capacity_hint: None,
tracker: None,
Expand All @@ -223,7 +223,7 @@ pub async fn gen_test_sstable_inner<B: AsRef<[u8]>>(
)
.await
.unwrap();
table.value().as_ref().clone()
(table.value().as_ref().clone(), output.sst_info.sst_info)
}

/// Generate a test table from the given `kv_iter` and put the kv value to `sstable_store`
Expand All @@ -242,6 +242,25 @@ pub async fn gen_test_sstable<B: AsRef<[u8]>>(
CachePolicy::NotFill,
)
.await
.0
}

/// Generate a test table from the given `kv_iter` and put the kv value to `sstable_store`
pub async fn gen_test_sstable_and_info<B: AsRef<[u8]>>(
opts: SstableBuilderOptions,
sst_id: HummockSstableId,
kv_iter: impl Iterator<Item = (FullKey<B>, HummockValue<B>)>,
sstable_store: SstableStoreRef,
) -> (Sstable, SstableInfo) {
gen_test_sstable_inner(
opts,
sst_id,
kv_iter,
vec![],
sstable_store,
CachePolicy::NotFill,
)
.await
}

/// Generate a test table from the given `kv_iter` and put the kv value to `sstable_store`
Expand All @@ -261,6 +280,7 @@ pub async fn gen_test_sstable_with_range_tombstone(
CachePolicy::NotFill,
)
.await
.0
}

/// Generates a user key with table id 0 and the given `table_key`
Expand Down

0 comments on commit 8f82b65

Please sign in to comment.