-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
114 lines (83 loc) · 2.25 KB
/
test.cpp
File metadata and controls
114 lines (83 loc) · 2.25 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <cstdlib>
#include <iostream>
#include <iostream>
#include <cassert>
#include <filesystem>
#include "key_store.hpp"
[[noreturn]] void hardCrash() {
std::fflush(stdout);
std::_Exit(1); // no destructors, no flushing
}
void clean() {
std::filesystem::remove_all("sstables");
std::filesystem::remove("wal.log");
}
void test_wal_recovery() {
clean();
{
KeyStore store("wal.log", 1, true);
for (int i = 0; i < 30; i++)
store.putKey(i, i * 10);
hardCrash();
}
}
void verify_wal_recovery() {
KeyStore store("wal.log", 1, true);
for (int i = 0; i < 30; i++) {
auto v = store.getValue(i);
assert(v.has_value());
assert(*v == i * 10);
}
std::cout << "WAL recovery passed\n";
}
void test_flush_and_crash() {
clean();
{
KeyStore store("wal.log", 1, true);
for (int i = 0; i < 40; i++)
store.putKey(i, i * 2);
hardCrash();
}
}
void verify_flush_and_crash() {
KeyStore store("wal.log", 1, true);
for (int i = 0; i < 40; i++) {
auto v = store.getValue(i);
assert(v.has_value());
assert(*v == i * 2);
}
std::cout << "Flush + crash passed\n";
}
void test_delete_and_compaction() {
clean();
{
KeyStore store("wal.log", 1, true);
for (int i = 0; i < 100; i++)
store.putKey(i, i);
for (int i = 30; i < 60; i++)
store.deleteKey(i);
}
{
KeyStore store("wal.log", 1, true);
for (int i = 0; i < 30; i++)
assert(store.getValue(i).value() == i);
for (int i = 30; i < 60; i++)
assert(!store.getValue(i).has_value());
for (int i = 60; i < 100; i++)
assert(store.getValue(i).value() == i);
}
std::cout << "Deletes passed\n";
}
int main(int argc, char** argv) {
if (argc < 2) {
std::cerr << "Usage: ./test <phase>\n";
return 1;
}
std::string phase = argv[1];
if (phase == "crash1") test_wal_recovery();
if (phase == "verify1") verify_wal_recovery();
if (phase == "crash2") test_flush_and_crash();
if (phase == "verify2") verify_flush_and_crash();
if (phase == "delete") test_delete_and_compaction();
return 0;
}