memgraph/tests/unit/snapshot.cpp
matej.gradicek 15b57e2d52 Implemented recovery and tests.
Summary: Implemented durability recovery and tesats.

Reviewers: mislav.bradac, dgleich, buda

Reviewed By: mislav.bradac, dgleich, buda

Subscribers: dtomicevic, mferencevic, pullbot

Differential Revision: https://phabricator.memgraph.io/D374
2017-06-06 15:12:34 +00:00

114 lines
3.3 KiB
C++

#include <experimental/filesystem>
#include "dbms/dbms.hpp"
#include "durability/snapshooter.hpp"
#include "gtest/gtest.h"
namespace fs = std::experimental::filesystem;
const std::string SNAPSHOTS_FOLDER_ALL_DB = "snapshots_test";
const std::string SNAPSHOTS_TEST_DEFAULT_DB_DIR = "snapshots_test/default";
// Other functionality will be tested in recovery tests.
std::vector<fs::path> GetFilesFromDir(
const std::string &snapshots_default_db_dir) {
std::vector<fs::path> files;
for (auto &file : fs::directory_iterator(snapshots_default_db_dir))
files.push_back(file.path());
return files;
}
void CleanDbDir() {
if (!fs::exists(SNAPSHOTS_TEST_DEFAULT_DB_DIR)) return;
std::vector<fs::path> files = GetFilesFromDir(SNAPSHOTS_TEST_DEFAULT_DB_DIR);
for (auto file : files) {
fs::remove(file);
}
}
class SnapshotTest : public ::testing::Test {
protected:
virtual void TearDown() {
CleanDbDir();
CONFIG(config::SNAPSHOT_CYCLE_SEC) = snapshot_cycle_sec_setup_;
}
virtual void SetUp() {
CleanDbDir();
snapshot_cycle_sec_setup_ = CONFIG(config::SNAPSHOT_CYCLE_SEC);
CONFIG(config::SNAPSHOT_CYCLE_SEC) = "-1";
}
std::string snapshot_cycle_sec_setup_;
};
TEST_F(SnapshotTest, CreateLessThanMaxRetainedSnapshotsTests) {
const int max_retained_snapshots = 10;
Dbms dbms;
for (int i = 0; i < 3; ++i) {
auto dba = dbms.active();
Snapshooter snapshooter;
snapshooter.MakeSnapshot(*dba.get(), SNAPSHOTS_TEST_DEFAULT_DB_DIR,
max_retained_snapshots);
}
std::vector<fs::path> files = GetFilesFromDir(SNAPSHOTS_TEST_DEFAULT_DB_DIR);
EXPECT_EQ(files.size(), 3);
}
TEST_F(SnapshotTest, CreateMoreThanMaxRetainedSnapshotsTests) {
const int max_retained_snapshots = 2;
Dbms dbms;
fs::path first_snapshot;
for (int i = 0; i < 3; ++i) {
auto dba = dbms.active();
Snapshooter snapshooter;
snapshooter.MakeSnapshot(*dba.get(), SNAPSHOTS_TEST_DEFAULT_DB_DIR,
max_retained_snapshots);
if (i == 0) {
std::vector<fs::path> files_begin =
GetFilesFromDir(SNAPSHOTS_TEST_DEFAULT_DB_DIR);
EXPECT_EQ(files_begin.size(), 1);
first_snapshot = files_begin[0];
}
}
std::vector<fs::path> files_end =
GetFilesFromDir(SNAPSHOTS_TEST_DEFAULT_DB_DIR);
EXPECT_EQ(files_end.size(), 2);
EXPECT_EQ(fs::exists(first_snapshot), false);
}
TEST_F(SnapshotTest, CreateSnapshotWithUnlimitedMaxRetainedSnapshots) {
const int max_retained_snapshots = -1;
Dbms dbms;
for (int i = 0; i < 10; ++i) {
auto dba = dbms.active();
Snapshooter snapshooter;
snapshooter.MakeSnapshot(*dba.get(), SNAPSHOTS_TEST_DEFAULT_DB_DIR,
max_retained_snapshots);
}
std::vector<fs::path> files = GetFilesFromDir(SNAPSHOTS_TEST_DEFAULT_DB_DIR);
EXPECT_EQ(files.size(), 10);
}
TEST_F(SnapshotTest, TestSnapshotFileOnDbDestruct) {
{
CONFIG(config::SNAPSHOTS_PATH) = SNAPSHOTS_FOLDER_ALL_DB;
CONFIG(config::SNAPSHOT_DB_DESTRUCTION) = "true";
Dbms dbms;
auto dba = dbms.active();
}
std::vector<fs::path> files = GetFilesFromDir(SNAPSHOTS_TEST_DEFAULT_DB_DIR);
// snapshot is created on dbms destruction
EXPECT_EQ(files.size(), 1);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}