-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogfile_test.cpp
More file actions
87 lines (74 loc) · 2.89 KB
/
logfile_test.cpp
File metadata and controls
87 lines (74 loc) · 2.89 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
#define BOOST_TEST_MODULE logfile
#include <boost/test/included/unit_test.hpp>
#include <filesystem>
#include <exception>
#include "logfile.h"
namespace fs = std::filesystem;
static void writeLogFile(const Options options) {
fs::remove("test/log.0");
fs::create_directory("test");
auto lf = LogFile("test",0,options);
lf.write("mykey","myvalue");
lf.startBatch(2);
lf.write("batchkey1","batchvalue1");
lf.write("batchkey2","batchvalue2");
lf.endBatch(2);
lf.close();
}
static bool testKeyValue(SkipList<const KeyValue> &list,const ByteBuffer& key,const ByteBuffer& value) {
auto kv = list.get(KeyValue(key,value));
return kv.value==value;
}
BOOST_AUTO_TEST_CASE( logfile ) {
Options options;
writeLogFile(options);
SkipList<const KeyValue> list(keyValueCompare(options));
readLogFile(list,"test/log.0",options);
BOOST_TEST(testKeyValue(list,"mykey","myvalue"));
BOOST_TEST(testKeyValue(list,"batchkey1","batchvalue1"));
BOOST_TEST(testKeyValue(list,"batchkey2","batchvalue2"));
}
BOOST_AUTO_TEST_CASE( logfile_sync ) {
Options options;
options.enableSyncWrite=true;
writeLogFile(options);
SkipList<const KeyValue> list(keyValueCompare(options));
readLogFile(list,"test/log.0",options);
BOOST_TEST(testKeyValue(list,"mykey","myvalue"));
BOOST_TEST(testKeyValue(list,"batchkey1","batchvalue1"));
BOOST_TEST(testKeyValue(list,"batchkey2","batchvalue2"));
}
BOOST_AUTO_TEST_CASE( logfile_invalidfile ) {
Options options;
writeLogFile(options);
truncate("test/log.0",1);
options.batchReadMode = Options::BatchReadMode::returnOpenError;
SkipList<const KeyValue> list(keyValueCompare(options));
BOOST_REQUIRE_THROW(readLogFile(list,"test/log.0",options),std::exception);
}
BOOST_AUTO_TEST_CASE( logfile_partialbatch ) {
Options options;
writeLogFile(options);
truncate("test/log.0",84-12); // truncate into batch
options.batchReadMode = Options::BatchReadMode::returnOpenError;
{
SkipList<const KeyValue> list(keyValueCompare(options));
BOOST_REQUIRE_THROW(readLogFile(list,"test/log.0",options),std::exception);
}
options.batchReadMode = Options::BatchReadMode::discardPartial;
{
SkipList<const KeyValue> list(keyValueCompare(options));
readLogFile(list,"test/log.0",options);
BOOST_TEST(testKeyValue(list,"mykey","myvalue"));
BOOST_TEST(!testKeyValue(list,"batchkey1","batchvalue1"));
BOOST_TEST(!testKeyValue(list,"batchkey2","batchvalue2"));
}
options.batchReadMode = Options::BatchReadMode::applyPartial;
{
SkipList<const KeyValue> list(keyValueCompare(options));
readLogFile(list,"test/log.0",options);
BOOST_TEST(testKeyValue(list,"mykey","myvalue"));
BOOST_TEST(testKeyValue(list,"batchkey1","batchvalue1"));
BOOST_TEST(!testKeyValue(list,"batchkey2","batchvalue2"));
}
}