-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCountersHandlerTest.cpp
More file actions
193 lines (161 loc) · 8.02 KB
/
CountersHandlerTest.cpp
File metadata and controls
193 lines (161 loc) · 8.02 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <memory>
#include <string>
#include <vector>
#include "codec/RedisMessage.h"
#include "counters/CountersHandler.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "rocksdb/options.h"
#include "rocksdb/slice.h"
#include "rocksdb/status.h"
#include "stesting/TestWithRocksDb.h"
namespace counters {
class CountersHandlerTest : public stesting::TestWithRocksDb {
protected:
CountersHandlerTest()
: stesting::TestWithRocksDb({}, {{"default", CountersHandler::optimizeColumnFamily}}) {}
// Counters does not support async command handling, so use default key
codec::RedisMessage getRedisMessage(codec::RedisValue&& val) {
return codec::RedisMessage(std::move(val));
}
};
class MockCountersHandler : public CountersHandler {
public:
explicit MockCountersHandler(std::shared_ptr<pipeline::DatabaseManager> databaseManager)
: CountersHandler(databaseManager, nullptr) {}
MOCK_METHOD2(write, folly::Future<folly::Unit>(Context*, codec::RedisMessage));
// Counters does not support async command handling, so use default key
bool handleCommand(const std::string& cmdNameLower, const std::vector<std::string>& cmd, Context* ctx) {
return CountersHandler::handleCommand(0L, cmdNameLower, cmd, ctx);
}
};
TEST_F(CountersHandlerTest, EnsureCommand) {
MockCountersHandler handler(databaseManager());
// seed values
boost::endian::big_int64_buf_t value1(10);
db()->Put(rocksdb::WriteOptions(), "key1", rocksdb::Slice(value1.data(), sizeof(int64_t)));
// same value
EXPECT_CALL(handler,
write(nullptr, getRedisMessage(codec::RedisValue(codec::RedisValue::Type::kSimpleString, "OK"))))
.Times(1);
EXPECT_TRUE(handler.handleCommand("ensure", { "ensure", "key1", "10" }, nullptr));
// different values
EXPECT_CALL(
handler,
write(nullptr, getRedisMessage(codec::RedisValue(codec::RedisValue::Type::kError, "ENSURE value different"))))
.Times(1);
EXPECT_TRUE(handler.handleCommand("ensure", { "ensure", "key1", "5" }, nullptr));
// key not found
EXPECT_CALL(
handler,
write(nullptr, getRedisMessage(codec::RedisValue(codec::RedisValue::Type::kError, "ENSURE key not found"))))
.Times(1);
EXPECT_TRUE(handler.handleCommand("ensure", { "ensure", "key2", "5" }, nullptr));
// value not a valid integer
EXPECT_CALL(handler, write(nullptr, getRedisMessage(codec::RedisValue(codec::RedisValue::Type::kError,
"Value is not an integer or out of range"))))
.Times(1);
EXPECT_TRUE(handler.handleCommand("ensure", { "ensure", "key2", "a" }, nullptr));
}
TEST_F(CountersHandlerTest, GetCommand) {
MockCountersHandler handler(databaseManager());
// seed values
boost::endian::big_int64_buf_t value1(10);
db()->Put(rocksdb::WriteOptions(), "key1", rocksdb::Slice(value1.data(), sizeof(int64_t)));
// key exists
EXPECT_CALL(handler, write(nullptr, getRedisMessage(codec::RedisValue(10)))).Times(1);
EXPECT_TRUE(handler.handleCommand("get", { "get", "key1" }, nullptr));
// key does not exist
EXPECT_CALL(handler, write(nullptr, getRedisMessage(codec::RedisValue::nullString()))).Times(1);
EXPECT_TRUE(handler.handleCommand("get", { "get", "key2" }, nullptr));
}
TEST_F(CountersHandlerTest, IncrbyCommand) {
MockCountersHandler handler(databaseManager());
// seed values
boost::endian::big_int64_buf_t value1(10);
db()->Put(rocksdb::WriteOptions(), "key1", rocksdb::Slice(value1.data(), sizeof(int64_t)));
// value not a valid integer
EXPECT_CALL(handler, write(nullptr, getRedisMessage(codec::RedisValue(codec::RedisValue::Type::kError,
"Value is not an integer or out of range"))))
.Times(1);
EXPECT_TRUE(handler.handleCommand("incrby", { "incrby", "key1", "a" }, nullptr));
// key exists
EXPECT_CALL(handler, write(nullptr, getRedisMessage(codec::RedisValue(15)))).Times(1);
EXPECT_TRUE(handler.handleCommand("incrby", { "incrby", "key1", "5" }, nullptr));
// key does not exist
EXPECT_CALL(handler, write(nullptr, getRedisMessage(codec::RedisValue(-5)))).Times(1);
EXPECT_TRUE(handler.handleCommand("incrby", { "incrby", "key2", "-5" }, nullptr));
}
TEST_F(CountersHandlerTest, SetCommand) {
MockCountersHandler handler(databaseManager());
// value not a valid integer
EXPECT_CALL(handler, write(nullptr, getRedisMessage(codec::RedisValue(codec::RedisValue::Type::kError,
"Value is not an integer or out of range"))))
.Times(1);
EXPECT_TRUE(handler.handleCommand("set", { "set", "key1", "a" }, nullptr));
// valid integer
EXPECT_CALL(handler,
write(nullptr, getRedisMessage(codec::RedisValue(codec::RedisValue::Type::kSimpleString, "OK"))))
.Times(1);
EXPECT_TRUE(handler.handleCommand("set", { "set", "key1", "10" }, nullptr));
std::string newValue1;
rocksdb::Status s1 = db()->Get(rocksdb::ReadOptions(), "key1", &newValue1);
EXPECT_TRUE(s1.ok());
int64_t intNewValue1 = boost::endian::detail::load_big_endian<int64_t, sizeof(int64_t)>(newValue1.data());
EXPECT_EQ(10, intNewValue1);
}
TEST_F(CountersHandlerTest, ZeroValueCompactionFilter) {
// no change after compaction for non-zero values
boost::endian::big_int64_buf_t value1(10);
db()->Put(rocksdb::WriteOptions(), "key1", rocksdb::Slice(value1.data(), sizeof(int64_t)));
db()->CompactRange(rocksdb::CompactRangeOptions(), nullptr, nullptr);
std::string newValue1;
rocksdb::Status s1 = db()->Get(rocksdb::ReadOptions(), "key1", &newValue1);
EXPECT_TRUE(s1.ok());
int64_t intNewValue1 = boost::endian::detail::load_big_endian<int64_t, sizeof(int64_t)>(newValue1.data());
EXPECT_EQ(10, intNewValue1);
// compaction deletes zero values
boost::endian::big_int64_buf_t value2(0);
db()->Put(rocksdb::WriteOptions(), "key1", rocksdb::Slice(value2.data(), sizeof(int64_t)));
std::string newValue2;
rocksdb::Status s2 = db()->Get(rocksdb::ReadOptions(), "key1", &newValue2);
EXPECT_TRUE(s2.ok());
int64_t intNewValue2 = boost::endian::detail::load_big_endian<int64_t, sizeof(int64_t)>(newValue2.data());
EXPECT_EQ(0, intNewValue2);
db()->CompactRange(rocksdb::CompactRangeOptions(), nullptr, nullptr);
s2 = db()->Get(rocksdb::ReadOptions(), "key1", &newValue2);
EXPECT_TRUE(s2.IsNotFound());
}
TEST_F(CountersHandlerTest, IncrbyMergeOperator) {
// no existing value
boost::endian::big_int64_buf_t value1(10);
db()->Merge(rocksdb::WriteOptions(), "key1", rocksdb::Slice(value1.data(), sizeof(int64_t)));
std::string newValue1;
rocksdb::Status s1 = db()->Get(rocksdb::ReadOptions(), "key1", &newValue1);
EXPECT_TRUE(s1.ok());
int64_t intNewValue1 = boost::endian::detail::load_big_endian<int64_t, sizeof(int64_t)>(newValue1.data());
EXPECT_EQ(10, intNewValue1);
// with existing values
boost::endian::big_int64_buf_t value2(5);
db()->Merge(rocksdb::WriteOptions(), "key1", rocksdb::Slice(value2.data(), sizeof(int64_t)));
std::string newValue2;
rocksdb::Status s2 = db()->Get(rocksdb::ReadOptions(), "key1", &newValue2);
EXPECT_TRUE(s2.ok());
int64_t intNewValue2 = boost::endian::detail::load_big_endian<int64_t, sizeof(int64_t)>(newValue2.data());
EXPECT_EQ(15, intNewValue2);
boost::endian::big_int64_buf_t value3(-16);
db()->Merge(rocksdb::WriteOptions(), "key1", rocksdb::Slice(value3.data(), sizeof(int64_t)));
std::string newValue3;
rocksdb::Status s3 = db()->Get(rocksdb::ReadOptions(), "key1", &newValue3);
EXPECT_TRUE(s3.ok());
int64_t intNewValue3 = boost::endian::detail::load_big_endian<int64_t, sizeof(int64_t)>(newValue3.data());
EXPECT_EQ(-1, intNewValue3);
boost::endian::big_int64_buf_t value4(1);
db()->Merge(rocksdb::WriteOptions(), "key1", rocksdb::Slice(value4.data(), sizeof(int64_t)));
std::string newValue4;
rocksdb::Status s4 = db()->Get(rocksdb::ReadOptions(), "key1", &newValue4);
EXPECT_TRUE(s4.ok());
int64_t intNewValue4 = boost::endian::detail::load_big_endian<int64_t, sizeof(int64_t)>(newValue4.data());
EXPECT_EQ(0, intNewValue4);
}
} // namespace counters