2018-02-05 23:28:22 +08:00
|
|
|
#include <memory>
|
2018-02-05 16:48:45 +08:00
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include "database/graph_db.hpp"
|
|
|
|
#include "transactions/engine_master.hpp"
|
|
|
|
|
|
|
|
class DistributedGraphDbTest : public ::testing::Test {
|
|
|
|
const std::string kLocal = "127.0.0.1";
|
2018-02-05 23:28:22 +08:00
|
|
|
const int kWorkerCount = 2;
|
|
|
|
|
2018-02-05 16:48:45 +08:00
|
|
|
class WorkerInThread {
|
|
|
|
public:
|
2018-02-06 23:26:29 +08:00
|
|
|
explicit WorkerInThread(database::Config config) : worker_(config) {
|
2018-02-05 16:48:45 +08:00
|
|
|
thread_ = std::thread([this, config] { worker_.WaitForShutdown(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
~WorkerInThread() {
|
|
|
|
if (thread_.joinable()) thread_.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
database::Worker worker_;
|
|
|
|
std::thread thread_;
|
|
|
|
};
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void SetUp() override {
|
|
|
|
const auto kInitTime = 200ms;
|
|
|
|
|
|
|
|
database::Config master_config;
|
|
|
|
master_config.master_endpoint = {kLocal, 0};
|
2018-02-05 23:28:22 +08:00
|
|
|
master_ = std::make_unique<database::Master>(master_config);
|
2018-02-05 16:48:45 +08:00
|
|
|
std::this_thread::sleep_for(kInitTime);
|
|
|
|
|
|
|
|
auto worker_config = [this](int worker_id) {
|
|
|
|
database::Config config;
|
|
|
|
config.worker_id = worker_id;
|
|
|
|
config.master_endpoint = master_->endpoint();
|
|
|
|
config.worker_endpoint = {kLocal, 0};
|
|
|
|
return config;
|
|
|
|
};
|
|
|
|
|
2018-02-05 23:28:22 +08:00
|
|
|
for (int i = 0; i < kWorkerCount; ++i) {
|
|
|
|
workers_.emplace_back(
|
|
|
|
std::make_unique<WorkerInThread>(worker_config(i + 1)));
|
|
|
|
std::this_thread::sleep_for(kInitTime);
|
|
|
|
}
|
2018-02-05 16:48:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TearDown() override {
|
|
|
|
// Kill master first because it will expect a shutdown response from the
|
|
|
|
// workers.
|
2018-02-05 23:28:22 +08:00
|
|
|
master_ = nullptr;
|
|
|
|
for (int i = kWorkerCount - 1; i >= 0; --i) workers_[i] = nullptr;
|
2018-02-05 16:48:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
database::Master &master() { return *master_; }
|
|
|
|
auto &master_tx_engine() {
|
|
|
|
return dynamic_cast<tx::MasterEngine &>(master_->tx_engine());
|
|
|
|
}
|
2018-02-05 23:28:22 +08:00
|
|
|
|
|
|
|
database::Worker &worker(int worker_id) {
|
|
|
|
return workers_[worker_id - 1]->worker_;
|
|
|
|
}
|
2018-02-05 16:48:45 +08:00
|
|
|
|
|
|
|
private:
|
2018-02-05 23:28:22 +08:00
|
|
|
std::unique_ptr<database::Master> master_;
|
|
|
|
std::vector<std::unique_ptr<WorkerInThread>> workers_;
|
2018-02-05 16:48:45 +08:00
|
|
|
};
|