From b56408b51aaab7e419613b6a10dad2e3f3e5cbc9 Mon Sep 17 00:00:00 2001 From: Roman Gershman Date: Sat, 29 Jan 2022 11:37:48 +0200 Subject: [PATCH] Add rdb_test --- helio | 2 +- server/CMakeLists.txt | 1 + server/rdb_test.cc | 104 ++++++++++++++++++++++++++++++++++++++ server/testdata/empty.rdb | Bin 0 -> 98 bytes server/testdata/small.rdb | Bin 0 -> 3873 bytes 5 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 server/rdb_test.cc create mode 100644 server/testdata/empty.rdb create mode 100644 server/testdata/small.rdb diff --git a/helio b/helio index bf4af91..3ee017c 160000 --- a/helio +++ b/helio @@ -1 +1 @@ -Subproject commit bf4af91fad4ccd2fa1a2f64d01547a0701e4978a +Subproject commit 3ee017cce280493c845a010a28caa4cf1d0f4e9b diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index 94abffb..c15939e 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -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 diff --git a/server/rdb_test.cc b/server/rdb_test.cc new file mode 100644 index 0000000..bcfd83d --- /dev/null +++ b/server/rdb_test.cc @@ -0,0 +1,104 @@ +// Copyright 2022, Roman Gershman. All rights reserved. +// See LICENSE for licensing terms. +// +#include + +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 pp_; + unique_ptr ess_; +}; + +inline const uint8_t* to_byte(const void* s) { + return reinterpret_cast(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 \ No newline at end of file diff --git a/server/testdata/empty.rdb b/server/testdata/empty.rdb new file mode 100644 index 0000000000000000000000000000000000000000..1d3ddf5b34e53677aef3262572c347c7dff48b54 GIT binary patch literal 98 zcmWG?b@2=~Ffg$E#aWb^l3A=Y7jYd-Lk$>$i)=qOBiY&L^v@{jc-oPCoDldHq;`4H{sfc$P&o!b~ zPV@+KNg7$FClu7Xt{p-@5%8dzC-!g`4kY5|L0 zS9o3Nb%ocJJ|u`pTMV_1W#MpY5oBegq7aFkGS{42dm(4Mv{;)FV$&<0i`t6;^# z6*F{Y>MF=pm@5DZggk2WtT97<<~}F2hRG||tAsp~dnV5~jG2cqW4+AvJSz08upzK9 zFk$l(H{XNh$#P&hvI=&(@RB4*O_bUIH9`r2iGd!w=(&px1vM5FQBY(-9=SYoIY5q3 zgsYLR78F!iP(xs2V21ik^%<;8R)t@ceial{SWv?0N<3W=A|ge2usm4~xg2u^umV{L ztVC9Vg=uRcq(({&FehxyfzgFV2c!>7p8;h;5q`yR|KRTEWSaIqudc7Z9h&ub9MZ#&%|7i4Yk2+z zFx^ujg!QAM$;T}b$n-^J!J172fOZ_teuRZ(-U@-t!yu43oC2BmS0FPEC=2TQkRp(2 zaRQkhE0F0!8i=xca~7yz7dMSwJssv2<)G;P5NUFR_3=X>b7cfF?L-4{Kl-d8keORF z5JLMHCXi{+0!8HNcTIEQ==Om}SvY}_C(z6q!ZOd5K;w9c^w^Bv3^fpkq_=2+%)}_r zI36l%>}l%!FrjKDB=2vuKxT9j$h`LgnNF)L_|SWYKn~ERYpaB9!&G3HlqAUXb%Dk~ zO;eA(SRwOTssKE*eS8wgv`b}SQ#Z4csv1Tf;g~ik(D)0W$xZhc*7&0V?XaacC6$0i zvT4sMVVDJlV@4T)O!rg>hqm{AflS*~79{kUL?F`_1sW$tRW37N*hG^jo z|7e;RB74IX$jq<;ncp^L;ezxoCXksHm4yxMzkmXnp+W