Skip to content

Commit

Permalink
import: refactor, move engine::tests::new_engine into test_helpers
Browse files Browse the repository at this point in the history
Let other tests using the same pattern reuse this function.
  • Loading branch information
kennytm committed Apr 26, 2019
1 parent 7cf147f commit 5271e2b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 27 deletions.
14 changes: 3 additions & 11 deletions src/import/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,18 +376,10 @@ mod tests {

use crate::raftstore::store::RegionSnapshot;
use crate::storage::mvcc::MvccReader;
use crate::import::test_helpers::new_engine;
use engine::rocks::util::security::encrypted_env_from_cipher_file;
use tikv_util::file::file_exists;

fn new_engine() -> (TempDir, Engine) {
let dir = TempDir::new("test_import_engine").unwrap();
let uuid = Uuid::new_v4();
let db_cfg = DbConfig::default();
let security_cfg = SecurityConfig::default();
let engine = Engine::new(dir.path(), uuid, db_cfg, security_cfg).unwrap();
(dir, engine)
}

fn new_write_batch(n: u8, ts: u64) -> WriteBatch {
let mut wb = WriteBatch::new();
for i in 0..n {
Expand All @@ -407,7 +399,7 @@ mod tests {

#[test]
fn test_write() {
let (_dir, engine) = new_engine();
let (_dir, engine) = new_engine("test_write");

let n = 10;
let commit_ts = 10;
Expand Down Expand Up @@ -517,7 +509,7 @@ mod tests {

#[test]
fn test_approximate_ranges() {
let (_dir, engine) = new_engine();
let (_dir, engine) = new_engine("test_approximate_ranges");

let num_files = 3;
let num_entries = 3;
Expand Down
11 changes: 2 additions & 9 deletions src/import/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,8 @@ mod tests {
use crate::import::test_helpers::*;

use engine::rocks::Writable;
use tempdir::TempDir;
use uuid::Uuid;

use crate::config::DbConfig;
use crate::storage::types::Key;
use tikv_util::security::SecurityConfig;

fn new_encoded_key(k: &[u8]) -> Vec<u8> {
if k.is_empty() {
Expand All @@ -340,11 +336,8 @@ mod tests {

#[test]
fn test_prepare_job() {
let dir = TempDir::new("test_import_prepare_job").unwrap();
let uuid = Uuid::new_v4();
let db_cfg = DbConfig::default();
let security_cfg = SecurityConfig::default();
let engine = Arc::new(Engine::new(dir.path(), uuid, db_cfg, security_cfg).unwrap());
let (_dir, engine) = new_engine("test_import_prepare_job");
let engine = Arc::new(engine);

// Generate entries to prepare.
let (n, m) = (4, 4);
Expand Down
9 changes: 2 additions & 7 deletions src/import/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,7 @@ mod tests {
use engine::rocks::{DBIterator, DBOptions, ReadOptions, Writable, DB};
use tempdir::TempDir;

use crate::config::DbConfig;
use crate::storage::types::Key;
use tikv_util::security::SecurityConfig;

fn open_db<P: AsRef<Path>>(path: P) -> Arc<DB> {
let path = path.as_ref().to_str().unwrap();
Expand Down Expand Up @@ -363,11 +361,8 @@ mod tests {

#[test]
fn test_sst_file_stream() {
let dir = TempDir::new("test_import_sst_file_stream").unwrap();
let uuid = Uuid::new_v4();
let db_cfg = DbConfig::default();
let security_cfg = SecurityConfig::default();
let engine = Arc::new(Engine::new(dir.path(), uuid, db_cfg, security_cfg).unwrap());
let (_dir, engine) = new_engine("test_import_sst_file_stream");
let engine = Arc::new(engine);

for i in 0..16 {
let k = Key::from_raw(&[i]).append_ts(0);
Expand Down
14 changes: 14 additions & 0 deletions src/import/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ use crc::crc32::{self, Hasher32};
use kvproto::import_sstpb::*;
use kvproto::kvrpcpb::*;
use kvproto::metapb::*;
use tempdir::TempDir;
use uuid::Uuid;

use crate::config::DbConfig;
use crate::import::engine::Engine;
use crate::pd::RegionInfo;
use crate::raftstore::store::keys;
use engine::rocks::{ColumnFamilyOptions, EnvOptions, SstFileWriter, DB};
use tikv_util::collections::HashMap;
use tikv_util::security::SecurityConfig;

use super::client::*;
use super::common::*;
Expand Down Expand Up @@ -63,6 +67,16 @@ pub fn read_sst_file<P: AsRef<Path>>(path: P, range: (u8, u8)) -> (SSTMeta, Vec<
(meta, data)
}

/// Creates a new, empty engine in a temporary directory.
pub fn new_engine(name: &str) -> (TempDir, Engine) {
let dir = TempDir::new(name).unwrap();
let uuid = Uuid::new_v4();
let db_cfg = DbConfig::default();
let security_cfg = SecurityConfig::default();
let engine = Engine::new(dir.path(), uuid, db_cfg, security_cfg).unwrap();
(dir, engine)
}

#[derive(Clone)]
pub struct MockClient {
counter: Arc<AtomicUsize>,
Expand Down

0 comments on commit 5271e2b

Please sign in to comment.