Support two clients calling start simultaneously

pull/2656/head
yang-g 9 years ago
parent c9c69e27da
commit 4f61528b96
  1. 35
      test/cpp/interop/reconnect_interop_server.cc

@ -31,9 +31,10 @@
* *
*/ */
#include <condition_variable>
#include <memory> #include <memory>
#include <mutex>
#include <sstream> #include <sstream>
#include <thread>
#include <signal.h> #include <signal.h>
#include <unistd.h> #include <unistd.h>
@ -47,7 +48,6 @@
#include <grpc++/server_context.h> #include <grpc++/server_context.h>
#include <grpc++/server_credentials.h> #include <grpc++/server_credentials.h>
#include <grpc++/status.h> #include <grpc++/status.h>
#include <grpc++/stream.h>
#include "test/core/util/reconnect_server.h" #include "test/core/util/reconnect_server.h"
#include "test/cpp/util/test_config.h" #include "test/cpp/util/test_config.h"
#include "test/proto/test.grpc.pb.h" #include "test/proto/test.grpc.pb.h"
@ -76,7 +76,8 @@ static bool got_sigint = false;
class ReconnectServiceImpl : public ReconnectService::Service { class ReconnectServiceImpl : public ReconnectService::Service {
public: public:
explicit ReconnectServiceImpl(int retry_port) : retry_port_(retry_port) { explicit ReconnectServiceImpl(int retry_port)
: retry_port_(retry_port), serving_(false), shutdown_(false) {
reconnect_server_init(&tcp_server_); reconnect_server_init(&tcp_server_);
} }
@ -89,6 +90,16 @@ class ReconnectServiceImpl : public ReconnectService::Service {
void Poll(int seconds) { reconnect_server_poll(&tcp_server_, seconds); } void Poll(int seconds) { reconnect_server_poll(&tcp_server_, seconds); }
Status Start(ServerContext* context, const Empty* request, Empty* response) { Status Start(ServerContext* context, const Empty* request, Empty* response) {
std::unique_lock<std::mutex> lock(mu_);
while (serving_ && !shutdown_) {
cv_.wait(lock);
}
if (shutdown_) {
return Status(grpc::StatusCode::UNAVAILABLE, "shutting down");
}
serving_ = true;
lock.unlock();
if (!tcp_server_.tcp_server) { if (!tcp_server_.tcp_server) {
reconnect_server_start(&tcp_server_, retry_port_); reconnect_server_start(&tcp_server_, retry_port_);
} else { } else {
@ -102,6 +113,11 @@ class ReconnectServiceImpl : public ReconnectService::Service {
// extract timestamps and set response // extract timestamps and set response
Verify(response); Verify(response);
reconnect_server_clear_timestamps(&tcp_server_); reconnect_server_clear_timestamps(&tcp_server_);
{
std::lock_guard<std::mutex> lock(mu_);
serving_ = false;
}
cv_.notify_one();
return Status::OK; return Status::OK;
} }
@ -130,9 +146,21 @@ class ReconnectServiceImpl : public ReconnectService::Service {
response->set_passed(passed); response->set_passed(passed);
} }
void Shutdown() {
{
std::lock_guard<std::mutex> lock(mu_);
shutdown_ = true;
}
cv_.notify_all();
}
private: private:
int retry_port_; int retry_port_;
reconnect_server tcp_server_; reconnect_server tcp_server_;
bool serving_;
bool shutdown_;
std::mutex mu_;
std::condition_variable cv_;
}; };
void RunServer() { void RunServer() {
@ -149,6 +177,7 @@ void RunServer() {
while (!got_sigint) { while (!got_sigint) {
service.Poll(5); service.Poll(5);
} }
service.Shutdown();
} }
static void sigint_handler(int x) { got_sigint = true; } static void sigint_handler(int x) { got_sigint = true; }

Loading…
Cancel
Save