summaryrefslogtreecommitdiffstats
path: root/tests/failpoints/mod.rs
blob: 807ca7abc23207eea0c64299f713778db2c6709f (plain)
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
use fail;
use std::io::Write;
use std::path::Path;
use tantivy::directory::{Directory, ManagedDirectory, RAMDirectory};
use tantivy::doc;
use tantivy::schema::{Schema, TEXT};
use tantivy::{Index, Term};

#[test]
fn test_failpoints_managed_directory_gc_if_delete_fails() {
    let _scenario = fail::FailScenario::setup();

    let test_path: &'static Path = Path::new("some_path_for_test");

    let ram_directory = RAMDirectory::create();
    let mut managed_directory = ManagedDirectory::wrap(ram_directory).unwrap();
    managed_directory
        .open_write(test_path)
        .unwrap()
        .flush()
        .unwrap();
    assert!(managed_directory.exists(test_path));
    // triggering gc and setting the delete operation to fail.
    //
    // We are checking that the gc operation is not removing the
    // file from managed.json to ensure that the file will be removed
    // in the next gc.
    //
    // The initial 1*off is there to allow for the removal of the
    // lock file.
    fail::cfg("RAMDirectory::delete", "1*off->1*return").unwrap();
    managed_directory.garbage_collect(Default::default);
    assert!(managed_directory.exists(test_path));

    // running the gc a second time should remove the file.
    managed_directory.garbage_collect(Default::default);
    assert!(
        !managed_directory.exists(test_path),
        "The file should have been deleted"
    );
}

#[test]
fn test_write_commit_fails() {
    let _fail_scenario_guard = fail::FailScenario::setup();
    let mut schema_builder = Schema::builder();
    let text_field = schema_builder.add_text_field("text", TEXT);
    let index = Index::create_in_ram(schema_builder.build());

    let mut index_writer = index.writer_with_num_threads(1, 3_000_000).unwrap();
    for _ in 0..100 {
        index_writer.add_document(doc!(text_field => "a"));
    }
    index_writer.commit().unwrap();
    fail::cfg("RAMDirectory::atomic_write", "return(error_write_failed)").unwrap();
    for _ in 0..100 {
        index_writer.add_document(doc!(text_field => "b"));
    }
    assert!(index_writer.commit().is_err());

    let num_docs_containing = |s: &str| {
        let term_a = Term::from_field_text(text_field, s);
        index.reader().unwrap().searcher().doc_freq(&term_a)
    };
    assert_eq!(num_docs_containing("a"), 100);
    assert_eq!(num_docs_containing("b"), 0);
}