memgraph/tests/unit/mvcc_find_update_common.hpp
Florijan Stamenkovic 1333bfeb39 Mvcc - unit test infrastructure setup
Summary:
Gradicek's Mvcc test have seen the following changes:
- provided a test infrastructure (fixture and macros) to facilitate testing and increase readability
- split tests into multi-transaction update, VersionList::find and general Mvcc testing
- multi-transaction update tests have been refactored (i *think* nothing got deleted, but it was a mess so I don't guarantee)
- changed all multithreaded tests to be single-threaded because multiple threads were not necessary
- changed transaction naming from T5, T8, T10 to T1, T2... for consistency with actual transaction indices

What still needs to be done:
- Gleich and Gradicek need to review the infrastructure (possible improvements)
- multi-transaction update tests need to be addressed by Gradicek (see "TODO gradicek" in code, discuss with Flor)
- the wiki/draw.io documentation needs to be updated. it is not imperative that all the tests be drawn in draw.io, only the general infrastructure explained. perhaps only a few examples drawn. Gradicek discuss with Flor
- Gleich see the "TODO Gleich" lines in the diff and discuss with flor

Suggested workflow:
- review this diff, hopefully land (before resolving all the TODOs)
- discard D169
- Gradicek and Gleich address the TODOs
- Flor reviews the results (in following diffs)

Reviewers: dgleich, matej.gradicek, buda

Reviewed By: matej.gradicek, buda

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D348
2017-05-05 14:46:18 +02:00

79 lines
2.6 KiB
C++

#include <vector>
#include "gtest/gtest.h"
#include "mvcc/id.hpp"
#include "mvcc/record.hpp"
#include "mvcc/version.hpp"
#include "mvcc/version_list.hpp"
#include "threading/sync/lock_timeout_exception.hpp"
#include "transactions/engine.hpp"
#include "transactions/transaction.cpp"
// make it easy to compare Id with int
bool operator==(const Id &left, const int right) {
return static_cast<uint64_t>(left) == static_cast<uint64_t>(right);
}
class TestClass : public mvcc::Record<TestClass> {
friend std::ostream &operator<<(std::ostream &stream, TestClass &test_class) {
stream << test_class.tx.cre() << " " << test_class.tx.exp();
return stream;
}
};
/**
* Testing mvcc::VersionList::find behavior in
* different situations (preceeding update/remove ops
* in different transactions).
*
* The setup for each case is:
* - transaction t1 has created a new version_list v1 and commited
* - transaction t2 has strated
* - *********************
* - here the test fixture ends and custom test behavior should be added
* - *********************
* - tests should check every legal sequence of the following ops
* - creation of transaction t3
* - [commit/abort] of [t2/t3]
* - [removal/update] on version_list by [t2/t3]
* - illegal sequences (for example double commit) don't have to be checked
*/
class Mvcc : public ::testing::Test {
protected:
virtual void SetUp() {
t1 = &engine.advance(t1->id);
v1 = version_list.find(*t1);
t1->commit();
t2 = engine.begin();
}
tx::Engine engine;
tx::Transaction *t1 = engine.begin();
mvcc::VersionList<TestClass> version_list{*t1};
TestClass *v1 = nullptr;
tx::Transaction *t2 = nullptr;
};
// helper macros. important:
// - TX_FIND and TX_UPDATE set the record variable vX
// - TX_BEGIN sets the transaction variable tX
#define T2_FIND __attribute__((unused)) auto v2 = version_list.find(*t2)
#define T3_FIND __attribute__((unused)) auto v3 = version_list.find(*t3)
#define T2_UPDATE __attribute__((unused)) auto v2 = version_list.update(*t2)
#define T3_UPDATE __attribute__((unused)) auto v3 = version_list.update(*t3)
#define T2_COMMIT t2->commit();
#define T3_COMMIT t3->commit();
#define T2_ABORT t2->abort();
#define T3_ABORT t3->abort();
#define T3_BEGIN auto t3 = engine.begin();
#define T2_REMOVE version_list.remove(*t2)
#define T3_REMOVE version_list.remove(*t3)
#define EXPECT_CRE(record, expected) EXPECT_EQ(record->tx.cre(), expected)
#define EXPECT_EXP(record, expected) EXPECT_EQ(record->tx.exp(), expected)
// test the fixture
TEST_F(Mvcc, Fixture) {
EXPECT_CRE(v1, 1);
EXPECT_EXP(v1, 0);
}