1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
// Copyright 2021 TiKV Project Authors. Licensed under Apache-2.0.

//! Tests for the `engine_traits` crate
//!
//! These are basic tests that can be used to verify the conformance of
//! engines that implement the traits in the `engine_traits` crate.
//!
//! All engine instances are constructed through the `engine_test` crate,
//! so individual engines can be tested by setting that crate's feature flags.
//!
//! e.g. to test the `engine_panic` crate
//!
//! ```no_test
//! cargo test -p engine_traits_tests --no-default-features --features=protobuf-codec,test-engines-panic
//! ```
//!
//! As of now this mostly tests the essential features of
//!
//! - point get / delete
//! - range deletes
//! - snapshots
//! - write batches
//! - iterators
//!
//! It is intended though to cover essentially all features of storage engines.
//!
//! It is poor at testing data consistency and error recovery,
//! as such tests require hooks into the engine which are not
//! presented by the engine traits.
//!
//! Note that in some cases there are multiple ways to accomplish the same
//! thing. In particular, engines have methods that operate on the default
//! column family, and these are effectively the same as using the `_cf` methods
//! with `CF_DEFAULT`; and writing can be performed directly through the engine,
//! or through write batches. As such, some tests are parameterized over
//! multiple APIs. This is particularly true of the `scenario_writes` module,
//! which contains much of the basic writing tests, and the `iterator` module.

#![cfg(test)]

mod basic_read_write;
mod cf_names;
mod ctor;
mod delete_range;
mod iterator;
mod misc;
mod read_consistency;
mod scenario_writes;
mod snapshot_basic;
mod write_batch;

/// The engine / tempdir pair used in all tests
struct TempDirEnginePair {
    // NB engine must drop before tempdir
    engine: engine_test::kv::KvTestEngine,
    tempdir: tempfile::TempDir,
}

/// Create an engine with only CF_DEFAULT
fn default_engine() -> TempDirEnginePair {
    use engine_test::ctor::EngineConstructorExt;
    use engine_test::kv::KvTestEngine;
    use engine_traits::CF_DEFAULT;

    let dir = tempdir();
    let path = dir.path().to_str().unwrap();
    let engine = KvTestEngine::new_engine(path, None, &[CF_DEFAULT], None).unwrap();
    TempDirEnginePair {
        engine,
        tempdir: dir,
    }
}

/// Create an engine with the specified column families
fn engine_cfs(cfs: &[&str]) -> TempDirEnginePair {
    use engine_test::ctor::EngineConstructorExt;
    use engine_test::kv::KvTestEngine;

    let dir = tempdir();
    let path = dir.path().to_str().unwrap();
    let engine = KvTestEngine::new_engine(path, None, cfs, None).unwrap();
    TempDirEnginePair {
        engine,
        tempdir: dir,
    }
}

fn tempdir() -> tempfile::TempDir {
    tempfile::Builder::new()
        .prefix("tikv-engine-traits-tests")
        .tempdir()
        .unwrap()
}

fn assert_engine_error<T>(r: engine_traits::Result<T>) {
    match r {
        Err(engine_traits::Error::Engine(_)) => {}
        _ => panic!("expected Error::Engine"),
    }
}