Add rdb_test

This commit is contained in:
Roman Gershman 2022-01-29 11:37:48 +02:00
parent 4db619b081
commit b56408b51a
5 changed files with 106 additions and 1 deletions

2
helio

@ -1 +1 @@
Subproject commit bf4af91fad4ccd2fa1a2f64d01547a0701e4978a
Subproject commit 3ee017cce280493c845a010a28caa4cf1d0f4e9b

View File

@ -19,6 +19,7 @@ cxx_test(list_family_test dfly_test_lib LABELS DFLY)
cxx_test(string_family_test dfly_test_lib LABELS DFLY)
cxx_test(generic_family_test dfly_test_lib LABELS DFLY)
cxx_test(memcache_parser_test dfly_test_lib LABELS DFLY)
cxx_test(rdb_test dfly_test_lib DATA testdata/empty.rdb testdata/small.rdb LABELS DFLY)
add_custom_target(check_dfly WORKING_DIRECTORY .. COMMAND ctest -L DFLY)
add_dependencies(check_dfly dragonfly_test list_family_test

104
server/rdb_test.cc Normal file
View File

@ -0,0 +1,104 @@
// Copyright 2022, Roman Gershman. All rights reserved.
// See LICENSE for licensing terms.
//
#include <gmock/gmock.h>
extern "C" {
#include "redis/crc64.h"
#include "redis/zmalloc.h"
}
#include "base/gtest.h"
#include "base/logging.h"
#include "io/file.h"
#include "server/engine_shard_set.h"
#include "server/rdb_load.h"
#include "util/uring/uring_pool.h"
using namespace testing;
using namespace std;
using namespace util;
namespace dfly {
class RdbTest : public testing::Test {
protected:
void SetUp() final;
void TearDown() final {
pp_->AwaitFiberOnAll([](auto*) { EngineShard::DestroyThreadLocal(); });
ess_.reset();
pp_->Stop();
}
static void SetUpTestCase() {
crc64_init();
init_zmalloc_threadlocal();
}
protected:
io::FileSource GetSource(string name);
unique_ptr<ProactorPool> pp_;
unique_ptr<EngineShardSet> ess_;
};
inline const uint8_t* to_byte(const void* s) {
return reinterpret_cast<const uint8_t*>(s);
}
void RdbTest::SetUp() {
pp_.reset(new uring::UringPool(16, 2));
pp_->Run();
ess_.reset(new EngineShardSet(pp_.get()));
ess_->Init(pp_->size());
pp_->Await([&](uint32_t index, ProactorBase* pb) { ess_->InitThreadLocal(pb, false); });
}
io::FileSource RdbTest::GetSource(string name) {
string rdb_file = base::ProgramRunfile("testdata/" + name);
auto open_res = io::OpenRead(rdb_file, io::ReadonlyFile::Options{});
CHECK(open_res) << rdb_file;
return io::FileSource(*open_res);
}
TEST_F(RdbTest, Crc) {
std::string_view s{"TEST"};
uint64_t c = crc64(0, to_byte(s.data()), s.size());
ASSERT_NE(c, 0);
uint64_t c2 = crc64(c, to_byte(s.data()), s.size());
EXPECT_NE(c, c2);
uint64_t c3 = crc64(c, to_byte(&c), sizeof(c));
EXPECT_EQ(c3, 0);
s = "COOLTEST";
c = crc64(0, to_byte(s.data()), 8);
c2 = crc64(0, to_byte(s.data()), 4);
c3 = crc64(c2, to_byte(s.data() + 4), 4);
EXPECT_EQ(c, c3);
c2 = crc64(0, to_byte(s.data() + 4), 4);
c3 = crc64(c2, to_byte(s.data()), 4);
EXPECT_NE(c, c3);
}
TEST_F(RdbTest, LoadEmpty) {
io::FileSource fs = GetSource("empty.rdb");
RdbLoader loader;
auto ec = loader.Load(&fs);
CHECK(!ec);
}
TEST_F(RdbTest, LoadSmall) {
io::FileSource fs = GetSource("small.rdb");
RdbLoader loader;
auto ec = loader.Load(&fs);
CHECK(!ec);
}
} // namespace dfly

BIN
server/testdata/empty.rdb vendored Normal file

Binary file not shown.

BIN
server/testdata/small.rdb vendored Normal file

Binary file not shown.