2015-03-08 02:15:31 +01:00
|
|
|
// This file is meant to be included by BlockStoreTest.h only
|
|
|
|
|
2015-04-25 02:48:41 +02:00
|
|
|
#include <messmer/cpp-utils/data/Data.h>
|
|
|
|
#include <messmer/cpp-utils/data/DataBlockFixture.h>
|
2015-04-18 14:47:12 +02:00
|
|
|
|
2015-03-08 02:15:31 +01:00
|
|
|
class BlockStoreSizeParameterizedTest {
|
|
|
|
public:
|
|
|
|
BlockStoreSizeParameterizedTest(std::unique_ptr<blockstore::BlockStore> blockStore_, size_t size_): blockStore(std::move(blockStore_)), size(size_) {}
|
|
|
|
|
|
|
|
void TestCreatedBlockHasCorrectSize() {
|
2015-04-18 14:47:12 +02:00
|
|
|
auto block = CreateBlock();
|
2015-03-08 02:15:31 +01:00
|
|
|
EXPECT_EQ(size, block->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestLoadingUnchangedBlockHasCorrectSize() {
|
2015-04-18 14:47:12 +02:00
|
|
|
blockstore::Key key = CreateBlock()->key();
|
2015-03-28 18:44:22 +01:00
|
|
|
auto loaded_block = blockStore->load(key);
|
2015-03-08 02:15:31 +01:00
|
|
|
EXPECT_EQ(size, loaded_block->size());
|
|
|
|
}
|
|
|
|
|
2015-04-18 14:47:12 +02:00
|
|
|
void TestCreatedBlockData() {
|
2015-04-25 02:48:41 +02:00
|
|
|
cpputils::DataBlockFixture dataFixture(size);
|
|
|
|
cpputils::Data data(size);
|
2015-04-18 14:47:12 +02:00
|
|
|
std::memcpy(data.data(), dataFixture.data(), size);
|
|
|
|
auto block = blockStore->create(data);
|
|
|
|
EXPECT_EQ(0, std::memcmp(dataFixture.data(), block->data(), size));
|
|
|
|
|
2015-03-08 02:15:31 +01:00
|
|
|
}
|
|
|
|
|
2015-04-18 14:47:12 +02:00
|
|
|
void TestLoadingUnchangedBlockData() {
|
2015-04-25 02:48:41 +02:00
|
|
|
cpputils::DataBlockFixture dataFixture(size);
|
|
|
|
cpputils::Data data(size);
|
2015-04-18 14:47:12 +02:00
|
|
|
std::memcpy(data.data(), dataFixture.data(), size);
|
|
|
|
blockstore::Key key = blockStore->create(data)->key();
|
2015-03-28 18:44:22 +01:00
|
|
|
auto loaded_block = blockStore->load(key);
|
2015-04-18 14:47:12 +02:00
|
|
|
EXPECT_EQ(0, std::memcmp(dataFixture.data(), loaded_block->data(), size));
|
2015-03-08 02:15:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestLoadedBlockIsCorrect() {
|
2015-04-25 02:48:41 +02:00
|
|
|
cpputils::DataBlockFixture randomData(size);
|
2015-03-08 02:15:31 +01:00
|
|
|
auto loaded_block = StoreDataToBlockAndLoadIt(randomData);
|
|
|
|
EXPECT_EQ(size, loaded_block->size());
|
|
|
|
EXPECT_EQ(0, std::memcmp(randomData.data(), loaded_block->data(), size));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestLoadedBlockIsCorrectWhenLoadedDirectlyAfterFlushing() {
|
2015-04-25 02:48:41 +02:00
|
|
|
cpputils::DataBlockFixture randomData(size);
|
2015-03-08 02:15:31 +01:00
|
|
|
auto loaded_block = StoreDataToBlockAndLoadItDirectlyAfterFlushing(randomData);
|
|
|
|
EXPECT_EQ(size, loaded_block->size());
|
|
|
|
EXPECT_EQ(0, std::memcmp(randomData.data(), loaded_block->data(), size));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestAfterCreate_FlushingDoesntChangeBlock() {
|
2015-04-25 02:48:41 +02:00
|
|
|
cpputils::DataBlockFixture randomData(size);
|
2015-03-08 02:15:31 +01:00
|
|
|
auto block = CreateBlock();
|
|
|
|
WriteDataToBlock(block.get(), randomData);
|
|
|
|
block->flush();
|
|
|
|
|
|
|
|
EXPECT_BLOCK_DATA_CORRECT(*block, randomData);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestAfterLoad_FlushingDoesntChangeBlock() {
|
2015-04-25 02:48:41 +02:00
|
|
|
cpputils::DataBlockFixture randomData(size);
|
2015-03-08 02:15:31 +01:00
|
|
|
auto block = CreateBlockAndLoadIt();
|
|
|
|
WriteDataToBlock(block.get(), randomData);
|
|
|
|
block->flush();
|
|
|
|
|
|
|
|
EXPECT_BLOCK_DATA_CORRECT(*block, randomData);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestAfterCreate_FlushesWhenDestructed() {
|
2015-04-25 02:48:41 +02:00
|
|
|
cpputils::DataBlockFixture randomData(size);
|
2015-03-08 02:15:31 +01:00
|
|
|
blockstore::Key key = key;
|
|
|
|
{
|
2015-04-25 02:48:41 +02:00
|
|
|
auto block = blockStore->create(cpputils::Data(size));
|
2015-03-08 02:15:31 +01:00
|
|
|
key = block->key();
|
|
|
|
WriteDataToBlock(block.get(), randomData);
|
|
|
|
}
|
|
|
|
auto loaded_block = blockStore->load(key);
|
|
|
|
EXPECT_BLOCK_DATA_CORRECT(*loaded_block, randomData);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestAfterLoad_FlushesWhenDestructed() {
|
2015-04-25 02:48:41 +02:00
|
|
|
cpputils::DataBlockFixture randomData(size);
|
2015-03-08 02:15:31 +01:00
|
|
|
blockstore::Key key = key;
|
|
|
|
{
|
2015-04-18 14:47:12 +02:00
|
|
|
key = CreateBlock()->key();
|
2015-03-08 02:15:31 +01:00
|
|
|
auto block = blockStore->load(key);
|
|
|
|
WriteDataToBlock(block.get(), randomData);
|
|
|
|
}
|
|
|
|
auto loaded_block = blockStore->load(key);
|
|
|
|
EXPECT_BLOCK_DATA_CORRECT(*loaded_block, randomData);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestLoadNonExistingBlock() {
|
|
|
|
EXPECT_FALSE(
|
|
|
|
(bool)blockStore->load(key)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const blockstore::Key key = blockstore::Key::FromString("1491BB4932A389EE14BC7090AC772972");
|
|
|
|
std::unique_ptr<blockstore::BlockStore> blockStore;
|
|
|
|
size_t size;
|
|
|
|
|
2015-04-25 02:48:41 +02:00
|
|
|
cpputils::Data ZEROES(size_t size) {
|
|
|
|
cpputils::Data ZEROES(size);
|
2015-03-08 02:15:31 +01:00
|
|
|
ZEROES.FillWithZeroes();
|
|
|
|
return ZEROES;
|
|
|
|
}
|
|
|
|
|
2015-04-25 02:48:41 +02:00
|
|
|
std::unique_ptr<blockstore::Block> StoreDataToBlockAndLoadIt(const cpputils::DataBlockFixture &data) {
|
2015-03-08 02:15:31 +01:00
|
|
|
blockstore::Key key = StoreDataToBlockAndGetKey(data);
|
|
|
|
return blockStore->load(key);
|
|
|
|
}
|
|
|
|
|
2015-04-25 02:48:41 +02:00
|
|
|
blockstore::Key StoreDataToBlockAndGetKey(const cpputils::DataBlockFixture &dataFixture) {
|
|
|
|
cpputils::Data data(dataFixture.size());
|
2015-04-18 14:47:12 +02:00
|
|
|
std::memcpy(data.data(), dataFixture.data(), dataFixture.size());
|
|
|
|
return blockStore->create(data)->key();
|
2015-03-08 02:15:31 +01:00
|
|
|
}
|
|
|
|
|
2015-04-25 02:48:41 +02:00
|
|
|
std::unique_ptr<blockstore::Block> StoreDataToBlockAndLoadItDirectlyAfterFlushing(const cpputils::DataBlockFixture &dataFixture) {
|
|
|
|
cpputils::Data data(dataFixture.size());
|
2015-04-18 14:47:12 +02:00
|
|
|
std::memcpy(data.data(), dataFixture.data(), dataFixture.size());
|
|
|
|
auto block = blockStore->create(data);
|
2015-03-08 02:15:31 +01:00
|
|
|
block->flush();
|
|
|
|
return blockStore->load(block->key());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<blockstore::Block> CreateBlockAndLoadIt() {
|
2015-04-18 14:47:12 +02:00
|
|
|
blockstore::Key key = CreateBlock()->key();
|
2015-03-08 02:15:31 +01:00
|
|
|
return blockStore->load(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<blockstore::Block> CreateBlock() {
|
2015-04-25 02:48:41 +02:00
|
|
|
return blockStore->create(cpputils::Data(size));
|
2015-03-08 02:15:31 +01:00
|
|
|
}
|
|
|
|
|
2015-04-25 02:48:41 +02:00
|
|
|
void WriteDataToBlock(blockstore::Block *block, const cpputils::DataBlockFixture &randomData) {
|
2015-03-08 02:15:31 +01:00
|
|
|
block->write(randomData.data(), 0, randomData.size());
|
|
|
|
}
|
|
|
|
|
2015-04-25 02:48:41 +02:00
|
|
|
void EXPECT_BLOCK_DATA_CORRECT(const blockstore::Block &block, const cpputils::DataBlockFixture &randomData) {
|
2015-03-08 02:15:31 +01:00
|
|
|
EXPECT_EQ(randomData.size(), block.size());
|
|
|
|
EXPECT_EQ(0, std::memcmp(randomData.data(), block.data(), randomData.size()));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr std::initializer_list<size_t> SIZES = {0, 1, 1024, 4096, 10*1024*1024};
|
|
|
|
#define TYPED_TEST_P_FOR_ALL_SIZES(TestName) \
|
|
|
|
TYPED_TEST_P(BlockStoreTest, TestName) { \
|
|
|
|
for (auto size: SIZES) { \
|
|
|
|
BlockStoreSizeParameterizedTest(this->fixture.createBlockStore(), size) \
|
|
|
|
.Test##TestName(); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
|
|
|
|
TYPED_TEST_P_FOR_ALL_SIZES(CreatedBlockHasCorrectSize);
|
|
|
|
TYPED_TEST_P_FOR_ALL_SIZES(LoadingUnchangedBlockHasCorrectSize);
|
2015-04-18 14:47:12 +02:00
|
|
|
TYPED_TEST_P_FOR_ALL_SIZES(CreatedBlockData);
|
|
|
|
TYPED_TEST_P_FOR_ALL_SIZES(LoadingUnchangedBlockData);
|
2015-03-08 02:15:31 +01:00
|
|
|
TYPED_TEST_P_FOR_ALL_SIZES(LoadedBlockIsCorrect);
|
2015-03-28 18:44:22 +01:00
|
|
|
//TYPED_TEST_P_FOR_ALL_SIZES(LoadedBlockIsCorrectWhenLoadedDirectlyAfterFlushing);
|
2015-03-08 02:15:31 +01:00
|
|
|
TYPED_TEST_P_FOR_ALL_SIZES(AfterCreate_FlushingDoesntChangeBlock);
|
|
|
|
TYPED_TEST_P_FOR_ALL_SIZES(AfterLoad_FlushingDoesntChangeBlock);
|
|
|
|
TYPED_TEST_P_FOR_ALL_SIZES(AfterCreate_FlushesWhenDestructed);
|
|
|
|
TYPED_TEST_P_FOR_ALL_SIZES(AfterLoad_FlushesWhenDestructed);
|
|
|
|
TYPED_TEST_P_FOR_ALL_SIZES(LoadNonExistingBlock);
|