Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(unittest): Init with dbfilename= before attempting to save #2127

Merged
merged 2 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/server/cluster/cluster_family_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,8 @@ TEST_F(ClusterFamilyTest, ClusterModeSelectNotAllowed) {
}

TEST_F(ClusterFamilyTest, ClusterFirstConfigCallDropsEntriesNotOwnedByNode) {
InitWithDbFilename();

Run({"debug", "populate", "50000"});

EXPECT_EQ(Run({"save", "df"}), "OK");
Expand Down
21 changes: 12 additions & 9 deletions src/server/generic_family_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -633,41 +633,44 @@ TEST_F(GenericFamilyTest, Restore) {
}

TEST_F(GenericFamilyTest, Info) {
InitWithDbFilename(); // Needed for `save`

auto get_rdb_changes_since_last_save = [](const string& str) -> size_t {
const string matcher = "rdb_changes_since_last_save:";
const auto pos = str.find(matcher) + matcher.size();
const auto sub = str.substr(pos, 1);
return atoi(sub.c_str());
};
auto resp = Run({"set", "k", "1"});
resp = Run({"info", "persistence"});

EXPECT_EQ(Run({"set", "k", "1"}), "OK");
auto resp = Run({"info", "persistence"});
EXPECT_EQ(1, get_rdb_changes_since_last_save(resp.GetString()));

resp = Run({"set", "k", "1"});
EXPECT_EQ(Run({"set", "k", "1"}), "OK");
resp = Run({"info", "persistence"});
EXPECT_EQ(2, get_rdb_changes_since_last_save(resp.GetString()));

resp = Run({"set", "k2", "2"});
EXPECT_EQ(Run({"set", "k2", "2"}), "OK");
resp = Run({"info", "persistence"});
EXPECT_EQ(3, get_rdb_changes_since_last_save(resp.GetString()));

resp = Run({"save"});
EXPECT_EQ(Run({"save"}), "OK");
resp = Run({"info", "persistence"});
EXPECT_EQ(0, get_rdb_changes_since_last_save(resp.GetString()));

resp = Run({"set", "k2", "2"});
EXPECT_EQ(Run({"set", "k2", "2"}), "OK");
resp = Run({"info", "persistence"});
EXPECT_EQ(1, get_rdb_changes_since_last_save(resp.GetString()));

resp = Run({"bgsave"});
EXPECT_EQ(Run({"bgsave"}), "OK");
resp = Run({"info", "persistence"});
EXPECT_EQ(0, get_rdb_changes_since_last_save(resp.GetString()));

resp = Run({"set", "k3", "3"});
EXPECT_EQ(Run({"set", "k3", "3"}), "OK");
resp = Run({"info", "persistence"});
EXPECT_EQ(1, get_rdb_changes_since_last_save(resp.GetString()));

resp = Run({"del", "k3"});
EXPECT_THAT(Run({"del", "k3"}), IntArg(1));
resp = Run({"info", "persistence"});
EXPECT_EQ(1, get_rdb_changes_since_last_save(resp.GetString()));
}
Expand Down
18 changes: 1 addition & 17 deletions src/server/rdb_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ extern "C" {
#include "base/logging.h"
#include "facade/facade_test.h" // needed to find operator== for RespExpr.
#include "io/file.h"
#include "io/file_util.h"
#include "server/engine_shard_set.h"
#include "server/rdb_load.h"
#include "server/test_utils.h"
Expand All @@ -33,33 +32,18 @@ using absl::StrCat;
ABSL_DECLARE_FLAG(int32, list_compress_depth);
ABSL_DECLARE_FLAG(int32, list_max_listpack_size);
ABSL_DECLARE_FLAG(int, compression_mode);
ABSL_DECLARE_FLAG(string, dbfilename);

namespace dfly {

class RdbTest : public BaseFamilyTest {
protected:
void TearDown();
void SetUp();

io::FileSource GetSource(string name);
};

void RdbTest::SetUp() {
SetFlag(&FLAGS_dbfilename, "rdbtestdump");
BaseFamilyTest::SetUp();
}

void RdbTest::TearDown() {
// Disable save on shutdown
SetFlag(&FLAGS_dbfilename, "");

auto rdb_files = io::StatFiles("rdbtestdump*");
CHECK(rdb_files);
for (const auto& fl : *rdb_files) {
unlink(fl.name.c_str());
}
BaseFamilyTest::TearDown();
InitWithDbFilename();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I understand now the context but I think for these tests SetFlag(&FLAGS_dbfilename, "rdbtestdump"); is enough.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if not, that's a good infra to have, which already semi exists for rdb_test, I think it's better to keep it for future use as well

}

inline const uint8_t* to_byte(const void* s) {
Expand Down
29 changes: 28 additions & 1 deletion src/server/test_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ extern "C" {
#include "base/logging.h"
#include "base/stl_util.h"
#include "facade/dragonfly_connection.h"
#include "io/file_util.h"
#include "server/acl/acl_log.h"
#include "util/fibers/pool.h"

Expand Down Expand Up @@ -257,7 +258,13 @@ void BaseFamilyTest::ResetService() {
}

void BaseFamilyTest::ShutdownService() {
DCHECK(service_);
if (service_ == nullptr) {
return;
}

// Don't save files during shutdown
CleanupSnapshots();
absl::SetFlag(&FLAGS_dbfilename, "");

service_->Shutdown();
service_.reset();
Expand All @@ -270,6 +277,26 @@ void BaseFamilyTest::ShutdownService() {
pp_->Stop();
}

void BaseFamilyTest::InitWithDbFilename() {
ShutdownService();

absl::SetFlag(&FLAGS_dbfilename, "rdbtestdump");
CleanupSnapshots();
ResetService();
}

void BaseFamilyTest::CleanupSnapshots() {
string dbfilename = absl::GetFlag(FLAGS_dbfilename);
if (dbfilename.empty())
return;

auto rdb_files = io::StatFiles(absl::StrCat(dbfilename, "*"));
CHECK(rdb_files);
for (const auto& fl : *rdb_files) {
unlink(fl.name.c_str());
}
}

unsigned BaseFamilyTest::NumLocked() {
atomic_uint count = 0;
shard_set->RunBriefInParallel([&](EngineShard* shard) {
Expand Down
3 changes: 3 additions & 0 deletions src/server/test_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ class BaseFamilyTest : public ::testing::Test {

void ShutdownService();

void InitWithDbFilename();
void CleanupSnapshots();

bool IsLocked(DbIndex db_index, std::string_view key) const;
ConnectionContext::DebugInfo GetDebugInfo(const std::string& id) const;

Expand Down
Loading