2017-11-28 17:50:30 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2017 The Cartographer Authors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cartographer_grpc/framework/server.h"
|
|
|
|
|
2017-12-19 04:27:03 +08:00
|
|
|
#include <future>
|
|
|
|
|
2018-02-01 00:45:57 +08:00
|
|
|
#include "cartographer_grpc/framework/client.h"
|
2017-11-29 21:05:31 +08:00
|
|
|
#include "cartographer_grpc/framework/execution_context.h"
|
2017-11-28 17:50:30 +08:00
|
|
|
#include "cartographer_grpc/framework/proto/math_service.grpc.pb.h"
|
|
|
|
#include "cartographer_grpc/framework/proto/math_service.pb.h"
|
|
|
|
#include "cartographer_grpc/framework/rpc_handler.h"
|
|
|
|
#include "glog/logging.h"
|
2018-01-31 19:26:56 +08:00
|
|
|
#include "google/protobuf/descriptor.h"
|
2017-11-28 17:50:30 +08:00
|
|
|
#include "grpc++/grpc++.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace cartographer_grpc {
|
|
|
|
namespace framework {
|
|
|
|
namespace {
|
|
|
|
|
2017-12-19 04:27:03 +08:00
|
|
|
using EchoResponder = std::function<bool()>;
|
2017-11-29 21:05:31 +08:00
|
|
|
class MathServerContext : public ExecutionContext {
|
|
|
|
public:
|
|
|
|
int additional_increment() { return 10; }
|
2017-12-19 04:27:03 +08:00
|
|
|
std::promise<EchoResponder> echo_responder;
|
2017-11-29 21:05:31 +08:00
|
|
|
};
|
|
|
|
|
2017-11-30 20:18:16 +08:00
|
|
|
class GetSumHandler
|
2017-11-29 17:40:26 +08:00
|
|
|
: public RpcHandler<Stream<proto::GetSumRequest>, proto::GetSumResponse> {
|
|
|
|
public:
|
2018-01-31 19:26:56 +08:00
|
|
|
std::string method_name() const override {
|
|
|
|
return "/cartographer_grpc.framework.proto.Math/GetSum";
|
|
|
|
}
|
2017-11-29 17:40:26 +08:00
|
|
|
void OnRequest(const proto::GetSumRequest& request) override {
|
2017-11-29 21:05:31 +08:00
|
|
|
sum_ += GetContext<MathServerContext>()->additional_increment();
|
2017-11-29 17:40:26 +08:00
|
|
|
sum_ += request.input();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnReadsDone() override {
|
|
|
|
auto response = cartographer::common::make_unique<proto::GetSumResponse>();
|
|
|
|
response->set_output(sum_);
|
|
|
|
Send(std::move(response));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int sum_ = 0;
|
|
|
|
};
|
|
|
|
|
2017-12-07 15:53:04 +08:00
|
|
|
class GetRunningSumHandler : public RpcHandler<Stream<proto::GetSumRequest>,
|
|
|
|
Stream<proto::GetSumResponse>> {
|
2017-12-04 22:28:19 +08:00
|
|
|
public:
|
2018-01-31 19:26:56 +08:00
|
|
|
std::string method_name() const override {
|
|
|
|
return "/cartographer_grpc.framework.proto.Math/GetRunningSum";
|
|
|
|
}
|
2017-12-04 22:28:19 +08:00
|
|
|
void OnRequest(const proto::GetSumRequest& request) override {
|
|
|
|
sum_ += request.input();
|
|
|
|
|
|
|
|
// Respond twice to demonstrate bidirectional streaming.
|
|
|
|
auto response = cartographer::common::make_unique<proto::GetSumResponse>();
|
|
|
|
response->set_output(sum_);
|
|
|
|
Send(std::move(response));
|
|
|
|
response = cartographer::common::make_unique<proto::GetSumResponse>();
|
|
|
|
response->set_output(sum_);
|
|
|
|
Send(std::move(response));
|
|
|
|
}
|
|
|
|
|
2017-12-07 15:53:04 +08:00
|
|
|
void OnReadsDone() override { Finish(::grpc::Status::OK); }
|
2017-12-04 22:28:19 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
int sum_ = 0;
|
|
|
|
};
|
|
|
|
|
2017-11-30 20:18:16 +08:00
|
|
|
class GetSquareHandler
|
|
|
|
: public RpcHandler<proto::GetSquareRequest, proto::GetSquareResponse> {
|
2018-01-31 19:26:56 +08:00
|
|
|
public:
|
|
|
|
std::string method_name() const override {
|
|
|
|
return "/cartographer_grpc.framework.proto.Math/GetSquare";
|
|
|
|
}
|
2017-11-30 20:18:16 +08:00
|
|
|
void OnRequest(const proto::GetSquareRequest& request) override {
|
|
|
|
auto response =
|
|
|
|
cartographer::common::make_unique<proto::GetSquareResponse>();
|
|
|
|
response->set_output(request.input() * request.input());
|
|
|
|
Send(std::move(response));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-19 04:27:03 +08:00
|
|
|
class GetEchoHandler
|
|
|
|
: public RpcHandler<proto::GetEchoRequest, proto::GetEchoResponse> {
|
2018-01-31 19:26:56 +08:00
|
|
|
public:
|
|
|
|
std::string method_name() const override {
|
|
|
|
return "/cartographer_grpc.framework.proto.Math/GetEcho";
|
|
|
|
}
|
2017-12-19 04:27:03 +08:00
|
|
|
void OnRequest(const proto::GetEchoRequest& request) override {
|
|
|
|
int value = request.input();
|
|
|
|
Writer writer = GetWriter();
|
|
|
|
GetContext<MathServerContext>()->echo_responder.set_value(
|
|
|
|
[writer, value]() {
|
|
|
|
auto response =
|
|
|
|
cartographer::common::make_unique<proto::GetEchoResponse>();
|
|
|
|
response->set_output(value);
|
2017-12-20 18:42:01 +08:00
|
|
|
return writer.Write(std::move(response));
|
2017-12-19 04:27:03 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-19 17:27:30 +08:00
|
|
|
class GetSequenceHandler
|
|
|
|
: public RpcHandler<proto::GetSequenceRequest,
|
|
|
|
Stream<proto::GetSequenceResponse>> {
|
|
|
|
public:
|
2018-01-31 19:26:56 +08:00
|
|
|
std::string method_name() const override {
|
|
|
|
return "/cartographer_grpc.framework.proto.Math/GetSequence";
|
|
|
|
}
|
2017-12-19 17:27:30 +08:00
|
|
|
void OnRequest(const proto::GetSequenceRequest& request) override {
|
|
|
|
for (int i = 0; i < request.input(); ++i) {
|
|
|
|
auto response =
|
|
|
|
cartographer::common::make_unique<proto::GetSequenceResponse>();
|
|
|
|
response->set_output(i);
|
|
|
|
Send(std::move(response));
|
|
|
|
}
|
|
|
|
Finish(::grpc::Status::OK);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-29 17:40:26 +08:00
|
|
|
// TODO(cschuet): Due to the hard-coded part these tests will become flaky when
|
|
|
|
// run in parallel. It would be nice to find a way to solve that. gRPC also
|
|
|
|
// allows to communicate over UNIX domain sockets.
|
|
|
|
const std::string kServerAddress = "localhost:50051";
|
|
|
|
const std::size_t kNumThreads = 1;
|
|
|
|
|
|
|
|
class ServerTest : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
void SetUp() override {
|
|
|
|
Server::Builder server_builder;
|
|
|
|
server_builder.SetServerAddress(kServerAddress);
|
2017-12-15 19:21:44 +08:00
|
|
|
server_builder.SetNumGrpcThreads(kNumThreads);
|
|
|
|
server_builder.SetNumEventThreads(kNumThreads);
|
2018-01-31 19:26:56 +08:00
|
|
|
server_builder.RegisterHandler<GetSumHandler>();
|
|
|
|
server_builder.RegisterHandler<GetSquareHandler>();
|
|
|
|
server_builder.RegisterHandler<GetRunningSumHandler>();
|
|
|
|
server_builder.RegisterHandler<GetEchoHandler>();
|
|
|
|
server_builder.RegisterHandler<GetSequenceHandler>();
|
2017-11-29 17:40:26 +08:00
|
|
|
server_ = server_builder.Build();
|
2017-11-30 20:18:16 +08:00
|
|
|
|
|
|
|
client_channel_ =
|
|
|
|
grpc::CreateChannel(kServerAddress, grpc::InsecureChannelCredentials());
|
2017-11-29 17:40:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Server> server_;
|
2017-11-30 20:18:16 +08:00
|
|
|
std::shared_ptr<grpc::Channel> client_channel_;
|
2017-11-29 17:40:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(ServerTest, StartAndStopServerTest) {
|
|
|
|
server_->Start();
|
|
|
|
server_->Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ServerTest, ProcessRpcStreamTest) {
|
2017-11-29 21:05:31 +08:00
|
|
|
server_->SetExecutionContext(
|
|
|
|
cartographer::common::make_unique<MathServerContext>());
|
2017-11-29 17:40:26 +08:00
|
|
|
server_->Start();
|
|
|
|
|
2018-02-01 00:45:57 +08:00
|
|
|
Client<GetSumHandler> client(client_channel_);
|
2017-11-29 17:40:26 +08:00
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
proto::GetSumRequest request;
|
|
|
|
request.set_input(i);
|
2018-02-01 00:45:57 +08:00
|
|
|
EXPECT_TRUE(client.Write(request));
|
2017-11-29 17:40:26 +08:00
|
|
|
}
|
2018-02-01 00:45:57 +08:00
|
|
|
EXPECT_TRUE(client.WritesDone());
|
|
|
|
EXPECT_TRUE(client.Finish().ok());
|
|
|
|
EXPECT_EQ(client.response().output(), 33);
|
2017-11-29 17:40:26 +08:00
|
|
|
|
|
|
|
server_->Shutdown();
|
2017-11-28 17:50:30 +08:00
|
|
|
}
|
|
|
|
|
2017-11-30 20:18:16 +08:00
|
|
|
TEST_F(ServerTest, ProcessUnaryRpcTest) {
|
|
|
|
server_->Start();
|
|
|
|
|
2018-02-01 00:45:57 +08:00
|
|
|
Client<GetSquareHandler> client(client_channel_);
|
2017-11-30 20:18:16 +08:00
|
|
|
proto::GetSquareRequest request;
|
|
|
|
request.set_input(11);
|
2018-02-01 00:45:57 +08:00
|
|
|
EXPECT_TRUE(client.Write(request));
|
|
|
|
EXPECT_EQ(client.response().output(), 121);
|
2017-11-30 20:18:16 +08:00
|
|
|
|
|
|
|
server_->Shutdown();
|
|
|
|
}
|
|
|
|
|
2017-12-04 22:28:19 +08:00
|
|
|
TEST_F(ServerTest, ProcessBidiStreamingRpcTest) {
|
|
|
|
server_->Start();
|
|
|
|
|
2018-02-01 00:45:57 +08:00
|
|
|
Client<GetRunningSumHandler> client(client_channel_);
|
2017-12-04 22:28:19 +08:00
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
proto::GetSumRequest request;
|
|
|
|
request.set_input(i);
|
2018-02-01 00:45:57 +08:00
|
|
|
EXPECT_TRUE(client.Write(request));
|
2017-12-04 22:28:19 +08:00
|
|
|
}
|
2018-02-01 00:45:57 +08:00
|
|
|
client.WritesDone();
|
2017-12-04 22:28:19 +08:00
|
|
|
proto::GetSumResponse response;
|
|
|
|
std::list<int> expected_responses = {0, 0, 1, 1, 3, 3};
|
2018-02-01 00:45:57 +08:00
|
|
|
while (client.Read(&response)) {
|
2017-12-04 22:28:19 +08:00
|
|
|
EXPECT_EQ(expected_responses.front(), response.output());
|
|
|
|
expected_responses.pop_front();
|
|
|
|
}
|
|
|
|
EXPECT_TRUE(expected_responses.empty());
|
2018-02-01 00:45:57 +08:00
|
|
|
EXPECT_TRUE(client.Finish().ok());
|
2017-12-04 22:28:19 +08:00
|
|
|
|
|
|
|
server_->Shutdown();
|
|
|
|
}
|
|
|
|
|
2017-12-19 04:27:03 +08:00
|
|
|
TEST_F(ServerTest, WriteFromOtherThread) {
|
|
|
|
server_->SetExecutionContext(
|
|
|
|
cartographer::common::make_unique<MathServerContext>());
|
|
|
|
server_->Start();
|
|
|
|
|
|
|
|
Server* server = server_.get();
|
|
|
|
std::thread response_thread([server]() {
|
|
|
|
std::future<EchoResponder> responder_future =
|
|
|
|
server->GetContext<MathServerContext>()->echo_responder.get_future();
|
|
|
|
responder_future.wait();
|
|
|
|
auto responder = responder_future.get();
|
|
|
|
CHECK(responder());
|
|
|
|
});
|
|
|
|
|
2018-02-01 00:45:57 +08:00
|
|
|
Client<GetEchoHandler> client(client_channel_);
|
|
|
|
proto::GetEchoRequest request;
|
|
|
|
request.set_input(13);
|
|
|
|
EXPECT_TRUE(client.Write(request));
|
2017-12-19 04:27:03 +08:00
|
|
|
response_thread.join();
|
2018-02-01 00:45:57 +08:00
|
|
|
EXPECT_EQ(client.response().output(), 13);
|
2017-12-19 04:27:03 +08:00
|
|
|
|
|
|
|
server_->Shutdown();
|
|
|
|
}
|
|
|
|
|
2017-12-19 17:27:30 +08:00
|
|
|
TEST_F(ServerTest, ProcessServerStreamingRpcTest) {
|
|
|
|
server_->Start();
|
|
|
|
|
2018-02-01 00:45:57 +08:00
|
|
|
Client<GetSequenceHandler> client(client_channel_);
|
2017-12-19 17:27:30 +08:00
|
|
|
proto::GetSequenceRequest request;
|
|
|
|
request.set_input(12);
|
|
|
|
|
2018-02-01 00:45:57 +08:00
|
|
|
client.Write(request);
|
2017-12-19 17:27:30 +08:00
|
|
|
proto::GetSequenceResponse response;
|
|
|
|
for (int i = 0; i < 12; ++i) {
|
2018-02-01 00:45:57 +08:00
|
|
|
EXPECT_TRUE(client.Read(&response));
|
2017-12-19 17:27:30 +08:00
|
|
|
EXPECT_EQ(response.output(), i);
|
|
|
|
}
|
2018-02-01 00:45:57 +08:00
|
|
|
EXPECT_FALSE(client.Read(&response));
|
|
|
|
EXPECT_TRUE(client.Finish().ok());
|
2017-12-19 17:27:30 +08:00
|
|
|
|
|
|
|
server_->Shutdown();
|
|
|
|
}
|
|
|
|
|
2017-11-28 17:50:30 +08:00
|
|
|
} // namespace
|
|
|
|
} // namespace framework
|
|
|
|
} // namespace cartographer_grpc
|