2015-03-12 14:27:51 +01:00
|
|
|
#include "../../../../implementations/ondisk/FileAlreadyExistsException.h"
|
|
|
|
#include "../../../../implementations/ondisk/OnDiskBlock.h"
|
2015-04-25 16:43:52 +02:00
|
|
|
#include <messmer/cpp-utils/data/DataFixture.h>
|
2015-02-17 00:23:33 +01:00
|
|
|
#include "google/gtest/gtest.h"
|
2014-12-09 17:19:59 +01:00
|
|
|
|
2015-04-25 02:48:41 +02:00
|
|
|
#include <messmer/cpp-utils/tempfile/TempFile.h>
|
|
|
|
#include <messmer/cpp-utils/tempfile/TempDir.h>
|
2014-12-09 17:19:59 +01:00
|
|
|
|
|
|
|
using ::testing::Test;
|
|
|
|
using ::testing::WithParamInterface;
|
|
|
|
using ::testing::Values;
|
|
|
|
|
|
|
|
using std::unique_ptr;
|
2015-04-25 02:48:41 +02:00
|
|
|
using cpputils::Data;
|
2015-04-25 16:43:52 +02:00
|
|
|
using cpputils::DataFixture;
|
2015-04-25 02:48:41 +02:00
|
|
|
using cpputils::TempFile;
|
|
|
|
using cpputils::TempDir;
|
2014-12-09 17:19:59 +01:00
|
|
|
|
|
|
|
using namespace blockstore;
|
|
|
|
using namespace blockstore::ondisk;
|
|
|
|
|
|
|
|
namespace bf = boost::filesystem;
|
|
|
|
|
|
|
|
class OnDiskBlockFlushTest: public Test, public WithParamInterface<size_t> {
|
|
|
|
public:
|
|
|
|
OnDiskBlockFlushTest()
|
|
|
|
// Don't create the temp file yet (therefore pass false to the TempFile constructor)
|
2015-01-24 22:08:41 +01:00
|
|
|
: dir(),
|
|
|
|
key(Key::FromString("1491BB4932A389EE14BC7090AC772972")),
|
|
|
|
file(dir.path() / key.ToString(), false),
|
2015-04-25 16:43:52 +02:00
|
|
|
randomData(DataFixture::generate(GetParam())) {
|
2014-12-09 17:19:59 +01:00
|
|
|
}
|
2015-01-24 22:08:41 +01:00
|
|
|
TempDir dir;
|
|
|
|
Key key;
|
2014-12-09 17:19:59 +01:00
|
|
|
TempFile file;
|
|
|
|
|
2015-04-25 16:43:52 +02:00
|
|
|
Data randomData;
|
2014-12-09 17:19:59 +01:00
|
|
|
|
|
|
|
unique_ptr<OnDiskBlock> CreateBlockAndLoadItFromDisk() {
|
|
|
|
{
|
2015-04-18 14:47:12 +02:00
|
|
|
Data data(randomData.size());
|
|
|
|
std::memcpy(data.data(), randomData.data(), randomData.size());
|
|
|
|
auto block = OnDiskBlock::CreateOnDisk(dir.path(), key, std::move(data));
|
2014-12-09 17:19:59 +01:00
|
|
|
}
|
2015-01-24 22:08:41 +01:00
|
|
|
return OnDiskBlock::LoadFromDisk(dir.path(), key);
|
2014-12-09 17:19:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
unique_ptr<OnDiskBlock> CreateBlock() {
|
2015-04-18 14:47:12 +02:00
|
|
|
Data data(randomData.size());
|
|
|
|
std::memcpy(data.data(), randomData.data(), randomData.size());
|
|
|
|
return OnDiskBlock::CreateOnDisk(dir.path(), key, std::move(data));
|
2014-12-09 17:19:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WriteDataToBlock(const unique_ptr<OnDiskBlock> &block) {
|
2015-03-04 20:47:02 +01:00
|
|
|
block->write(randomData.data(), 0, randomData.size());
|
2014-12-09 17:19:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EXPECT_BLOCK_DATA_CORRECT(const unique_ptr<OnDiskBlock> &block) {
|
|
|
|
EXPECT_EQ(randomData.size(), block->size());
|
|
|
|
EXPECT_EQ(0, std::memcmp(randomData.data(), block->data(), randomData.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void EXPECT_STORED_FILE_DATA_CORRECT() {
|
2015-04-25 02:48:41 +02:00
|
|
|
Data actual = Data::LoadFromFile(file.path()).value();
|
2014-12-09 17:19:59 +01:00
|
|
|
EXPECT_EQ(randomData.size(), actual.size());
|
|
|
|
EXPECT_EQ(0, std::memcmp(randomData.data(), actual.data(), randomData.size()));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
INSTANTIATE_TEST_CASE_P(OnDiskBlockFlushTest, OnDiskBlockFlushTest, Values((size_t)0, (size_t)1, (size_t)1024, (size_t)4096, (size_t)10*1024*1024));
|
|
|
|
|
|
|
|
// This test is also tested by OnDiskBlockStoreTest, but there the block is created using the BlockStore interface.
|
|
|
|
// Here, we create it using OnDiskBlock::CreateOnDisk()
|
|
|
|
TEST_P(OnDiskBlockFlushTest, AfterCreate_FlushingDoesntChangeBlock) {
|
|
|
|
auto block = CreateBlock();
|
|
|
|
WriteDataToBlock(block);
|
|
|
|
|
|
|
|
EXPECT_BLOCK_DATA_CORRECT(block);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test is also tested by OnDiskBlockStoreTest, but there the block is created using the BlockStore interface.
|
|
|
|
// Here, we create it using OnDiskBlock::CreateOnDisk() / OnDiskBlock::LoadFromDisk()
|
|
|
|
TEST_P(OnDiskBlockFlushTest, AfterLoad_FlushingDoesntChangeBlock) {
|
|
|
|
auto block = CreateBlockAndLoadItFromDisk();
|
|
|
|
WriteDataToBlock(block);
|
|
|
|
|
|
|
|
EXPECT_BLOCK_DATA_CORRECT(block);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(OnDiskBlockFlushTest, AfterCreate_FlushingWritesCorrectData) {
|
|
|
|
auto block = CreateBlock();
|
|
|
|
WriteDataToBlock(block);
|
|
|
|
|
|
|
|
EXPECT_STORED_FILE_DATA_CORRECT();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(OnDiskBlockFlushTest, AfterLoad_FlushingWritesCorrectData) {
|
|
|
|
auto block = CreateBlockAndLoadItFromDisk();
|
|
|
|
WriteDataToBlock(block);
|
|
|
|
|
|
|
|
EXPECT_STORED_FILE_DATA_CORRECT();
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test is also tested by OnDiskBlockStoreTest, but there it can only checks block content by loading it again.
|
|
|
|
// Here, we check the content on disk.
|
|
|
|
TEST_P(OnDiskBlockFlushTest, AfterCreate_FlushesWhenDestructed) {
|
|
|
|
{
|
|
|
|
auto block = CreateBlock();
|
|
|
|
WriteDataToBlock(block);
|
|
|
|
}
|
|
|
|
EXPECT_STORED_FILE_DATA_CORRECT();
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test is also tested by OnDiskBlockStoreTest, but there it can only checks block content by loading it again.
|
|
|
|
// Here, we check the content on disk.
|
|
|
|
TEST_P(OnDiskBlockFlushTest, AfterLoad_FlushesWhenDestructed) {
|
|
|
|
{
|
|
|
|
auto block = CreateBlockAndLoadItFromDisk();
|
|
|
|
WriteDataToBlock(block);
|
|
|
|
}
|
|
|
|
EXPECT_STORED_FILE_DATA_CORRECT();
|
|
|
|
}
|