2011-12-16 00:04:31 +05:30
|
|
|
// Copyright (C) 2011 Red Hat, Inc. All rights reserved.
|
2011-12-06 19:23:05 +05:30
|
|
|
//
|
2011-12-06 19:13:56 +05:30
|
|
|
// 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/>.
|
|
|
|
|
2013-03-22 20:49:40 +05:30
|
|
|
#include "gmock/gmock.h"
|
2013-01-10 01:54:11 +05:30
|
|
|
#include "persistent-data/transaction_manager.h"
|
2013-01-11 03:06:38 +05:30
|
|
|
#include "persistent-data/space-maps/core.h"
|
2011-07-14 20:01:00 +05:30
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace persistent_data;
|
2013-03-22 20:49:40 +05:30
|
|
|
using namespace testing;
|
2011-07-14 20:01:00 +05:30
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace {
|
2013-01-10 01:54:11 +05:30
|
|
|
unsigned const MAX_HELD_LOCKS = 16;
|
2011-07-14 20:01:00 +05:30
|
|
|
block_address const NR_BLOCKS = 1024;
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
transaction_manager::ptr
|
2011-07-14 20:01:00 +05:30
|
|
|
create_tm() {
|
2013-01-15 11:01:48 +05:30
|
|
|
block_manager<>::ptr bm(
|
2014-07-22 21:11:39 +05:30
|
|
|
new block_manager<>("./test.data", NR_BLOCKS, MAX_HELD_LOCKS, block_manager<>::READ_WRITE));
|
2011-07-14 20:01:00 +05:30
|
|
|
space_map::ptr sm(new core_map(NR_BLOCKS));
|
2011-08-31 17:18:41 +05:30
|
|
|
transaction_manager::ptr tm(new transaction_manager(bm, sm));
|
2011-07-22 21:02:47 +05:30
|
|
|
tm->get_sm()->inc(0);
|
2011-07-14 20:01:00 +05:30
|
|
|
return tm;
|
|
|
|
}
|
2013-01-10 01:54:11 +05:30
|
|
|
|
|
|
|
typedef block_manager<>::validator::ptr validator_ptr;
|
|
|
|
|
|
|
|
validator_ptr noop_validator() {
|
|
|
|
return block_manager<>::validator::ptr(
|
|
|
|
new block_manager<>::noop_validator);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef block_manager<>::write_ref write_ref;
|
2011-07-14 20:01:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2013-03-22 20:49:40 +05:30
|
|
|
TEST(TransactionManagerTests, commit_succeeds)
|
2011-07-14 20:01:00 +05:30
|
|
|
{
|
2013-01-10 01:54:11 +05:30
|
|
|
transaction_manager::ptr tm = create_tm();
|
2013-01-22 15:29:06 +05:30
|
|
|
tm->begin(0, noop_validator());
|
2011-07-14 20:01:00 +05:30
|
|
|
}
|
|
|
|
|
2013-03-22 20:49:40 +05:30
|
|
|
TEST(TransactionManagerTests, shadowing)
|
2011-07-14 20:01:00 +05:30
|
|
|
{
|
2013-01-10 01:54:11 +05:30
|
|
|
transaction_manager::ptr tm = create_tm();
|
|
|
|
block_manager<>::write_ref superblock = tm->begin(0, noop_validator());
|
2011-07-14 20:01:00 +05:30
|
|
|
|
2013-01-10 01:54:11 +05:30
|
|
|
space_map::ptr sm = tm->get_sm();
|
2011-07-14 20:01:00 +05:30
|
|
|
sm->inc(1);
|
2011-07-22 21:02:47 +05:30
|
|
|
block_address b;
|
|
|
|
|
|
|
|
{
|
2013-01-10 01:54:11 +05:30
|
|
|
pair<write_ref, bool> p = tm->shadow(1, noop_validator());
|
2011-07-22 21:02:47 +05:30
|
|
|
b = p.first.get_location();
|
2013-03-22 20:49:40 +05:30
|
|
|
ASSERT_THAT(b, Ne(1u));
|
|
|
|
ASSERT_FALSE(p.second);
|
|
|
|
ASSERT_THAT(sm->get_count(1), Eq(0u));
|
2011-07-22 21:02:47 +05:30
|
|
|
}
|
2011-07-14 20:01:00 +05:30
|
|
|
|
2011-07-22 21:02:47 +05:30
|
|
|
{
|
2013-01-10 01:54:11 +05:30
|
|
|
pair<write_ref, bool> p = tm->shadow(b, noop_validator());
|
2013-03-22 20:49:40 +05:30
|
|
|
ASSERT_THAT(p.first.get_location(), Eq(b));
|
|
|
|
ASSERT_FALSE(p.second);
|
2011-07-22 21:02:47 +05:30
|
|
|
}
|
2011-07-14 20:01:00 +05:30
|
|
|
|
|
|
|
sm->inc(b);
|
2011-07-22 21:02:47 +05:30
|
|
|
|
|
|
|
{
|
2013-01-10 01:54:11 +05:30
|
|
|
pair<write_ref, bool> p = tm->shadow(b, noop_validator());
|
2013-03-22 20:49:40 +05:30
|
|
|
ASSERT_THAT(p.first.get_location(), Ne(b));
|
|
|
|
ASSERT_TRUE(p.second);
|
2011-07-22 21:02:47 +05:30
|
|
|
}
|
2011-07-14 20:01:00 +05:30
|
|
|
}
|
|
|
|
|
2013-03-22 20:49:40 +05:30
|
|
|
TEST(TransactionManagerTests, multiple_shadowing)
|
2011-07-14 20:01:00 +05:30
|
|
|
{
|
2013-01-10 01:54:11 +05:30
|
|
|
transaction_manager::ptr tm = create_tm();
|
|
|
|
space_map::ptr sm = tm->get_sm();
|
2011-07-14 20:01:00 +05:30
|
|
|
sm->set_count(1, 3);
|
2013-01-11 03:41:59 +05:30
|
|
|
block_address b, b2;
|
|
|
|
|
|
|
|
{
|
|
|
|
write_ref superblock = tm->begin(0, noop_validator());
|
|
|
|
pair<write_ref, bool> p = tm->shadow(1, noop_validator());
|
|
|
|
b = p.first.get_location();
|
2013-03-22 20:49:40 +05:30
|
|
|
ASSERT_THAT(b, Ne(1u));
|
|
|
|
ASSERT_TRUE(p.second);
|
2013-01-11 03:41:59 +05:30
|
|
|
sm->commit();
|
|
|
|
}
|
2011-07-14 20:01:00 +05:30
|
|
|
|
2013-01-11 03:41:59 +05:30
|
|
|
{
|
|
|
|
write_ref superblock = tm->begin(0, noop_validator());
|
|
|
|
pair<write_ref, bool> p = tm->shadow(1, noop_validator());
|
|
|
|
b2 = p.first.get_location();
|
2013-03-22 20:49:40 +05:30
|
|
|
ASSERT_THAT(b2, Ne(1u));
|
|
|
|
ASSERT_THAT(b2, Ne(b));
|
|
|
|
ASSERT_TRUE(p.second);
|
2013-01-11 03:41:59 +05:30
|
|
|
sm->commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
write_ref superblock = tm->begin(0, noop_validator());
|
|
|
|
pair<write_ref, bool> p = tm->shadow(1, noop_validator());
|
|
|
|
block_address b3 = p.first.get_location();
|
2013-03-22 20:49:40 +05:30
|
|
|
ASSERT_THAT(b3, Ne(b2));
|
|
|
|
ASSERT_THAT(b3, Ne(b));
|
|
|
|
ASSERT_THAT(b3, Ne(1u));
|
|
|
|
ASSERT_FALSE(p.second);
|
2013-01-11 03:41:59 +05:30
|
|
|
sm->commit();
|
|
|
|
}
|
2011-07-14 20:01:00 +05:30
|
|
|
}
|
|
|
|
|
2013-03-22 20:49:40 +05:30
|
|
|
TEST(TransactionManagerTests, shadow_free_block_fails)
|
2011-07-14 20:01:00 +05:30
|
|
|
{
|
2013-01-10 01:54:11 +05:30
|
|
|
transaction_manager::ptr tm = create_tm();
|
|
|
|
write_ref superblock = tm->begin(0, noop_validator());
|
2013-03-22 20:49:40 +05:30
|
|
|
ASSERT_THROW(tm->shadow(1, noop_validator()), runtime_error);
|
2011-07-14 20:01:00 +05:30
|
|
|
}
|
2013-03-22 20:49:40 +05:30
|
|
|
|
|
|
|
//----------------------------------------------------------------
|