[unit-tests] copier and mem_pool tests.
More to come
This commit is contained in:
@@ -55,9 +55,11 @@ TEST_SOURCE=\
|
||||
unit-tests/btree_counter_t.cc \
|
||||
unit-tests/btree_damage_visitor_t.cc \
|
||||
unit-tests/cache_superblock_t.cc \
|
||||
unit-tests/copier_t.cc \
|
||||
unit-tests/damage_tracker_t.cc \
|
||||
unit-tests/endian_t.cc \
|
||||
unit-tests/error_state_t.cc \
|
||||
unit-tests/mem_pool_t.cc \
|
||||
unit-tests/rmap_visitor_t.cc \
|
||||
unit-tests/rolling_hash_t.cc \
|
||||
unit-tests/run_set_t.cc \
|
||||
|
103
unit-tests/copier_t.cc
Normal file
103
unit-tests/copier_t.cc
Normal file
@@ -0,0 +1,103 @@
|
||||
// Copyright (C) 2016 Red Hat, Inc. All rights reserved.
|
||||
//
|
||||
// This file is part of the thin-provisioning-tools source.
|
||||
//
|
||||
// thin-provisioning-tools is free software: you can redistribute it
|
||||
// and/or modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation, either version 3 of
|
||||
// the License, or (at your option) any later version.
|
||||
//
|
||||
// thin-provisioning-tools is distributed in the hope that it will be
|
||||
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
||||
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along
|
||||
// with thin-provisioning-tools. If not, see
|
||||
// <http://www.gnu.org/licenses/>.
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "block-cache/copier.h"
|
||||
#include "test_utils.h"
|
||||
|
||||
|
||||
#include <fcntl.h>
|
||||
|
||||
using namespace boost;
|
||||
using namespace std;
|
||||
using namespace test;
|
||||
using namespace testing;
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
namespace {
|
||||
class temp_file {
|
||||
public:
|
||||
temp_file(string const &name_base, unsigned meg_size)
|
||||
: path_(gen_path(name_base)) {
|
||||
|
||||
int fd = ::open(path_.c_str(), O_CREAT | O_RDWR, 0666);
|
||||
if (fd < 0)
|
||||
throw runtime_error("couldn't open file");
|
||||
|
||||
if (::fallocate(fd, 0, 0, 1024 * 1024 * meg_size))
|
||||
throw runtime_error("couldn't fallocate");
|
||||
|
||||
::close(fd);
|
||||
}
|
||||
|
||||
~temp_file() {
|
||||
::unlink(path_.c_str());
|
||||
}
|
||||
|
||||
string const &get_path() const {
|
||||
return path_;
|
||||
}
|
||||
|
||||
private:
|
||||
static string gen_path(string const &base) {
|
||||
return string("./") + base + string(".tmp");
|
||||
}
|
||||
|
||||
string path_;
|
||||
};
|
||||
|
||||
class CopierTests : public Test {
|
||||
public:
|
||||
CopierTests()
|
||||
: src_file_("copy_src", 32),
|
||||
dest_file_("copy_dest", 32),
|
||||
copier_(src_file_.get_path(),
|
||||
dest_file_.get_path(),
|
||||
64, 1 * 1024 * 1024) {
|
||||
}
|
||||
|
||||
copier &get_copier() {
|
||||
return copier_;
|
||||
}
|
||||
|
||||
private:
|
||||
temp_file src_file_;
|
||||
temp_file dest_file_;
|
||||
|
||||
copier copier_;
|
||||
};
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
TEST_F(CopierTests, empty_test)
|
||||
{
|
||||
// Copy first block
|
||||
copy_op op1(0, 1, 0);
|
||||
get_copier().issue(op1);
|
||||
auto mop = get_copier().wait();
|
||||
|
||||
if (mop) {
|
||||
cerr << "op completed\n";
|
||||
} else {
|
||||
cerr << "no op completed\n";
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
97
unit-tests/mem_pool_t.cc
Normal file
97
unit-tests/mem_pool_t.cc
Normal file
@@ -0,0 +1,97 @@
|
||||
// Copyright (C) 2016 Red Hat, Inc. All rights reserved.
|
||||
//
|
||||
// This file is part of the thin-provisioning-tools source.
|
||||
//
|
||||
// thin-provisioning-tools is free software: you can redistribute it
|
||||
// and/or modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation, either version 3 of
|
||||
// the License, or (at your option) any later version.
|
||||
//
|
||||
// thin-provisioning-tools is distributed in the hope that it will be
|
||||
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
||||
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along
|
||||
// with thin-provisioning-tools. If not, see
|
||||
// <http://www.gnu.org/licenses/>.
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "block-cache/mem_pool.h"
|
||||
#include "test_utils.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
|
||||
using namespace boost;
|
||||
using namespace std;
|
||||
using namespace test;
|
||||
using namespace testing;
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
namespace {
|
||||
class MempoolTests : public Test {
|
||||
public:
|
||||
bool aligned(void *data, size_t alignment) {
|
||||
return (reinterpret_cast<size_t>(data) % alignment) == 0;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
TEST_F(MempoolTests, empty_test)
|
||||
{
|
||||
}
|
||||
|
||||
TEST_F(MempoolTests, create_destroy_cycle)
|
||||
{
|
||||
for (size_t bs = 64; bs <= 512; bs *= 2) {
|
||||
mempool mp(bs, 4 * 1024 * 1024, bs);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(MempoolTests, alignments_observed)
|
||||
{
|
||||
for (size_t bs = 64; bs <= 512; bs *= 2) {
|
||||
mempool mp(bs, 512 * 1024, bs);
|
||||
|
||||
for (unsigned i = 0; i < 100; i++) {
|
||||
auto md = mp.alloc();
|
||||
|
||||
if (!md)
|
||||
throw runtime_error("couldn't alloc");
|
||||
|
||||
ASSERT_THAT(aligned(*md, bs), Eq(true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(MempoolTests, alloc_free_cycle)
|
||||
{
|
||||
mempool mp(512, 512 * 1024, 512);
|
||||
|
||||
for (unsigned i = 0; i < 10000; i++) {
|
||||
auto md = mp.alloc();
|
||||
mp.free(*md);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(MempoolTests, exhaust_pool)
|
||||
{
|
||||
mempool mp(512, 100 * 512, 512);
|
||||
|
||||
for (unsigned i = 0; i < 100; i++) {
|
||||
auto md = mp.alloc();
|
||||
|
||||
ASSERT_THAT(*md, NEq(0));
|
||||
}
|
||||
|
||||
auto md = mp.alloc();
|
||||
ASSERT(*md, Eq(0));
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
Reference in New Issue
Block a user