Skip to content

Commit

Permalink
let the system choose an available port
Browse files Browse the repository at this point in the history
  • Loading branch information
ayyt committed May 16, 2019
1 parent a328dfb commit 5111b91
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 60 deletions.
9 changes: 3 additions & 6 deletions src/graph/test/TestMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
#include "graph/test/TestEnv.h"
#include "fs/TempDir.h"
#include "meta/test/TestUtils.h"
#include "network/NetworkUtils.h"

using nebula::graph::TestEnv;
using nebula::graph::gEnv;
using nebula::meta::TestUtils;
using nebula::fs::TempDir;
using nebula::network::NetworkUtils;

DECLARE_string(meta_server_addrs);

Expand All @@ -27,13 +25,12 @@ int main(int argc, char **argv) {
gEnv = new TestEnv(); // gtest will delete this env object for us
::testing::AddGlobalTestEnvironment(gEnv);

// use an ephemeral port
int32_t localMetaPort = NetworkUtils::getAvailablePort();
FLAGS_meta_server_addrs = folly::stringPrintf("127.0.0.1:%d", localMetaPort);

// Let the system choose an available port for us
int32_t localMetaPort = 0;
// need to start meta server
TempDir rootPath("/tmp/MetaClientTest.XXXXXX");
auto metaServer = TestUtils::mockServer(localMetaPort, rootPath.path());

FLAGS_meta_server_addrs = folly::stringPrintf("127.0.0.1:%d", metaServer->port_);
return RUN_ALL_TESTS();
}
4 changes: 2 additions & 2 deletions src/interface/storage.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,15 @@ struct AddVerticesRequest {
1: common.GraphSpaceID space_id,
// partId => vertices
2: map<common.PartitionID, list<Vertex>>(cpp.template = "std::unordered_map") parts,
// If true, it equals an upsert operation.
// If true, it equals an upset operation.
3: bool overwritable,
}

struct AddEdgesRequest {
1: common.GraphSpaceID space_id,
// partId => edges
2: map<common.PartitionID, list<Edge>>(cpp.template = "std::unordered_map") parts,
// If true, it equals an upsert operation.
// If true, it equals an upset operation.
3: bool overwritable,
}

Expand Down
21 changes: 10 additions & 11 deletions src/meta/test/MetaClientTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,21 @@ namespace meta {
using nebula::cpp2::SupportedType;
using nebula::cpp2::ValueType;
using apache::thrift::FragileConstructor::FRAGILE;
using nebula::network::NetworkUtils;

TEST(MetaClientTest, InterfacesTest) {
FLAGS_load_data_interval_second = 1;
fs::TempDir rootPath("/tmp/MetaClientTest.XXXXXX");

// use an ephemeral port
int32_t localMetaPort = NetworkUtils::getAvailablePort();
// Let the system choose an available port for us
uint32_t localMetaPort = 0;
auto sc = TestUtils::mockServer(localMetaPort, rootPath.path());

GraphSpaceID spaceId = 0;
auto threadPool = std::make_shared<folly::IOThreadPoolExecutor>(1);
uint32_t localIp;
NetworkUtils::ipv4ToInt("127.0.0.1", localIp);
network::NetworkUtils::ipv4ToInt("127.0.0.1", localIp);
auto client = std::make_shared<MetaClient>(threadPool,
std::vector<HostAddr>{HostAddr(localIp, localMetaPort)});
std::vector<HostAddr>{HostAddr(localIp, sc->port_)});

client->init();
{
Expand Down Expand Up @@ -270,16 +269,16 @@ TEST(MetaClientTest, TagTest) {
FLAGS_load_data_interval_second = 1;
fs::TempDir rootPath("/tmp/MetaClientTagTest.XXXXXX");

// use an ephemeral port
int32_t localMetaPort = NetworkUtils::getAvailablePort();
// Let the system choose an available port for us
int32_t localMetaPort = 0;
auto sc = TestUtils::mockServer(localMetaPort, rootPath.path());

GraphSpaceID spaceId = 0;
auto threadPool = std::make_shared<folly::IOThreadPoolExecutor>(1);
uint32_t localIp;
network::NetworkUtils::ipv4ToInt("127.0.0.1", localIp);
auto client = std::make_shared<MetaClient>(threadPool,
std::vector<HostAddr>{HostAddr(localIp, localMetaPort)});
std::vector<HostAddr>{HostAddr(localIp, sc->port_)});

client->init();
std::vector<HostAddr> hosts = {{0, 0}, {1, 1}, {2, 2}, {3, 3}};
Expand Down Expand Up @@ -367,16 +366,16 @@ TEST(MetaClientTest, DiffTest) {
FLAGS_load_data_interval_second = 1;
fs::TempDir rootPath("/tmp/MetaClientTest.XXXXXX");

// use an ephemeral port
int32_t localMetaPort = NetworkUtils::getAvailablePort();
// Let the system choose an available port for us
int32_t localMetaPort = 0;
auto sc = TestUtils::mockServer(localMetaPort, rootPath.path());

auto threadPool = std::make_shared<folly::IOThreadPoolExecutor>(1);
uint32_t localIp;
network::NetworkUtils::ipv4ToInt("127.0.0.1", localIp);
auto listener = std::make_unique<TestListener>();
auto client = std::make_shared<MetaClient>(threadPool,
std::vector<HostAddr>{HostAddr(localIp, localMetaPort)});
std::vector<HostAddr>{HostAddr(localIp, sc->port_)});

client->registerListener(listener.get());
client->init();
Expand Down
8 changes: 7 additions & 1 deletion src/meta/test/TestUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ class TestUtils {

std::unique_ptr<apache::thrift::ThriftServer> server_;
std::unique_ptr<std::thread> serverT_;
uint32_t port_;
};

static std::unique_ptr<ServerContext> mockServer(uint32_t port, const char* dataPath) {
Expand All @@ -177,7 +178,12 @@ class TestUtils {

LOG(INFO) << "Stop the server...";
});
sleep(1);
while (!sc->server_->getServeEventBase()
|| !sc->server_->getServeEventBase()->isRunning()) {
}
sc->port_ = sc->server_->getAddress().getPort();
LOG(INFO) << "Starting the Meta Daemon on port " << sc->port_
<< ", path " << dataPath;
return sc;
}
};
Expand Down
10 changes: 6 additions & 4 deletions src/storage/test/StorageClientTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ TEST(StorageClientTest, VerticesInterfacesTest) {
uint32_t localIp;
network::NetworkUtils::ipv4ToInt("127.0.0.1", localIp);

// use an ephemeral port
uint32_t localMetaPort = network::NetworkUtils::getAvailablePort();;
uint32_t localDataPort = network::NetworkUtils::getAvailablePort();;
FLAGS_meta_server_addrs = folly::stringPrintf("127.0.0.1:%d", localMetaPort);
// Let the system choose an available port for us
uint32_t localMetaPort = 0;

// for mockStorageServer MetaServerBasedPartManager, use ephemeral port
uint32_t localDataPort = network::NetworkUtils::getAvailablePort();

LOG(INFO) << "Start meta server....";
std::string metaPath = folly::stringPrintf("%s/meta", rootPath.path());
auto metaServerContext = meta::TestUtils::mockServer(localMetaPort, metaPath.c_str());
FLAGS_meta_server_addrs = folly::stringPrintf("127.0.0.1:%d", metaServerContext->port_);

LOG(INFO) << "Start data server....";
std::string dataPath = folly::stringPrintf("%s/data", rootPath.path());
Expand Down
71 changes: 35 additions & 36 deletions src/storage/test/TestUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,23 @@ class TestUtils {
}

struct ServerContext {
~ServerContext() {
server_->stop();
serverT_->join();
VLOG(3) << "~ServerContext";
}

std::unique_ptr<apache::thrift::ThriftServer> server_;
std::unique_ptr<std::thread> serverT_;
uint32_t port_;
};

static std::unique_ptr<ServerContext> mockServer(const char* dataPath,
uint32_t ip,
uint32_t port = 0) {
auto sc = std::make_unique<ServerContext>();
sc->server_ = std::make_unique<apache::thrift::ThriftServer>();
sc->serverT_ = std::make_unique<std::thread>([&]() {
~ServerContext() {
server_->stop();
serverT_->join();
VLOG(3) << "~ServerContext";
}

std::unique_ptr<apache::thrift::ThriftServer> server_;
std::unique_ptr<std::thread> serverT_;
uint32_t port_;
};

static std::unique_ptr<ServerContext> mockServer(const char* dataPath,
uint32_t ip,
uint32_t port = 0) {
auto sc = std::make_unique<ServerContext>();
sc->server_ = std::make_unique<apache::thrift::ThriftServer>();
sc->serverT_ = std::make_unique<std::thread>([&]() {
std::vector<std::string> paths;
paths.push_back(folly::stringPrintf("%s/disk1", dataPath));
paths.push_back(folly::stringPrintf("%s/disk2", dataPath));
Expand All @@ -181,26 +181,25 @@ class TestUtils {
= std::make_unique<kvstore::MetaServerBasedPartManager>(options.local_);
kvstore::NebulaStore* kvPtr = static_cast<kvstore::NebulaStore*>(
kvstore::KVStore::instance(std::move(options)));
std::unique_ptr<kvstore::KVStore> kv(kvPtr);
auto schemaMan = TestUtils::mockSchemaMan(1);
auto handler
= std::make_shared<nebula::storage::StorageServiceHandler>(
kv.get(),
std::unique_ptr<kvstore::KVStore> kv(kvPtr);
auto schemaMan = TestUtils::mockSchemaMan(1);
auto handler
= std::make_shared<nebula::storage::StorageServiceHandler>(kv.get(),
std::move(schemaMan));
CHECK(!!sc->server_) << "Failed to create the thrift server";
sc->server_->setInterface(handler);
sc->server_->setPort(port);
sc->server_->serve(); // Will wait until the server shuts down
LOG(INFO) << "Stop the server...";
});
while (!sc->server_->getServeEventBase()
|| !sc->server_->getServeEventBase()->isRunning()) {
}
sc->port_ = sc->server_->getAddress().getPort();
LOG(INFO) << "Starting the storage Daemon on port " << sc->port_
<< ", path " << dataPath;
return sc;
}
CHECK(!!sc->server_) << "Failed to create the thrift server";
sc->server_->setInterface(handler);
sc->server_->setPort(port);
sc->server_->serve(); // Will wait until the server shuts down
LOG(INFO) << "Stop the server...";
});
while (!sc->server_->getServeEventBase() ||
!sc->server_->getServeEventBase()->isRunning()) {
}
sc->port_ = sc->server_->getAddress().getPort();
LOG(INFO) << "Starting the storage Daemon on port " << sc->port_
<< ", path " << dataPath;
return sc;
}
};

} // namespace storage
Expand Down

0 comments on commit 5111b91

Please sign in to comment.