Add a test to export two TestService on the same server, only with different

package names. Make sure they work as intended.

This is based on []
	Change on 2015/01/08 by yangg <yangg@google.com>
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=83558743
pull/3/merge
yangg 10 years ago committed by Nicolas Noble
parent 24200d3cbc
commit 1456d15221
  1. 24
      Makefile
  2. 3
      build.json
  3. 143
      test/cpp/end2end/end2end_test.cc
  4. 20
      test/cpp/util/echo.proto
  5. 12
      test/cpp/util/echo_duplicate.proto
  6. 21
      test/cpp/util/messages.proto

@ -965,6 +965,24 @@ gens/test/cpp/util/echo.pb.cc: test/cpp/util/echo.proto protoc_plugins
$(Q) mkdir -p `dirname $@`
$(Q) $(PROTOC) --cpp_out=gens --grpc_out=gens --plugin=protoc-gen-grpc=bins/$(CONFIG)/cpp_plugin $<
deps/$(CONFIG)/gens/test/cpp/util/echo_duplicate.pb.dep:
$(Q) mkdir -p `dirname $@`
$(Q) touch $@
gens/test/cpp/util/echo_duplicate.pb.cc: test/cpp/util/echo_duplicate.proto protoc_plugins
$(E) "[PROTOC] Generating protobuf CC file from $<"
$(Q) mkdir -p `dirname $@`
$(Q) $(PROTOC) --cpp_out=gens --grpc_out=gens --plugin=protoc-gen-grpc=bins/$(CONFIG)/cpp_plugin $<
deps/$(CONFIG)/gens/test/cpp/util/messages.pb.dep:
$(Q) mkdir -p `dirname $@`
$(Q) touch $@
gens/test/cpp/util/messages.pb.cc: test/cpp/util/messages.proto protoc_plugins
$(E) "[PROTOC] Generating protobuf CC file from $<"
$(Q) mkdir -p `dirname $@`
$(Q) $(PROTOC) --cpp_out=gens --grpc_out=gens --plugin=protoc-gen-grpc=bins/$(CONFIG)/cpp_plugin $<
deps/$(CONFIG)/%.dep : %.c
$(E) "[DEP] Generating dependencies for $<"
@ -1643,7 +1661,9 @@ clean_libgrpc++:
LIBGRPC++_TEST_UTIL_SRC = \
gens/test/cpp/util/messages.pb.cc \
gens/test/cpp/util/echo.pb.cc \
gens/test/cpp/util/echo_duplicate.pb.cc \
test/cpp/util/create_test_channel.cc \
test/cpp/end2end/async_test_server.cc \
@ -5199,10 +5219,10 @@ bins/$(CONFIG)/end2end_test: openssl_dep_error
else
bins/$(CONFIG)/end2end_test: $(END2END_TEST_OBJS) libs/$(CONFIG)/libgrpc_test_util.a libs/$(CONFIG)/libgrpc++.a libs/$(CONFIG)/libgrpc.a libs/$(CONFIG)/libgpr.a
bins/$(CONFIG)/end2end_test: $(END2END_TEST_OBJS) libs/$(CONFIG)/libgrpc++_test_util.a libs/$(CONFIG)/libgrpc_test_util.a libs/$(CONFIG)/libgrpc++.a libs/$(CONFIG)/libgrpc.a libs/$(CONFIG)/libgpr.a
$(E) "[LD] Linking $@"
$(Q) mkdir -p `dirname $@`
$(Q) $(LDXX) $(LDFLAGS) $(END2END_TEST_OBJS) $(GTEST_LIB) libs/$(CONFIG)/libgrpc_test_util.a libs/$(CONFIG)/libgrpc++.a libs/$(CONFIG)/libgrpc.a libs/$(CONFIG)/libgpr.a $(LDLIBSXX) $(LDLIBS) $(LDLIBS_SECURE) -o bins/$(CONFIG)/end2end_test
$(Q) $(LDXX) $(LDFLAGS) $(END2END_TEST_OBJS) $(GTEST_LIB) libs/$(CONFIG)/libgrpc++_test_util.a libs/$(CONFIG)/libgrpc_test_util.a libs/$(CONFIG)/libgrpc++.a libs/$(CONFIG)/libgrpc.a libs/$(CONFIG)/libgpr.a $(LDLIBSXX) $(LDLIBS) $(LDLIBS_SECURE) -o bins/$(CONFIG)/end2end_test
endif

@ -386,7 +386,9 @@
"name": "grpc++_test_util",
"build": "test",
"src": [
"test/cpp/util/messages.proto",
"test/cpp/util/echo.proto",
"test/cpp/util/echo_duplicate.proto",
"test/cpp/util/create_test_channel.cc",
"test/cpp/end2end/async_test_server.cc"
],
@ -1348,6 +1350,7 @@
"test/cpp/end2end/end2end_test.cc"
],
"deps": [
"grpc++_test_util",
"grpc_test_util",
"grpc++",
"grpc",

@ -34,6 +34,7 @@
#include <chrono>
#include <thread>
#include "net/grpc/cpp/echo_duplicate_proto_cc.pb.h"
#include "test/cpp/util/echo.pb.h"
#include "src/cpp/server/rpc_service_method.h"
#include "src/cpp/util/time.h"
@ -55,7 +56,6 @@
using grpc::cpp::test::util::EchoRequest;
using grpc::cpp::test::util::EchoResponse;
using grpc::cpp::test::util::TestService;
using std::chrono::system_clock;
namespace grpc {
@ -77,10 +77,10 @@ void MaybeEchoDeadline(ServerContext* context, const EchoRequest* request,
}
} // namespace
class TestServiceImpl : public TestService::Service {
class TestServiceImpl : public ::grpc::cpp::test::util::TestService::Service {
public:
Status Echo(ServerContext* context, const EchoRequest* request,
EchoResponse* response) {
EchoResponse* response) override {
response->set_message(request->message());
MaybeEchoDeadline(context, request, response);
return Status::OK;
@ -90,7 +90,7 @@ class TestServiceImpl : public TestService::Service {
Status RequestStream(ServerContext* context,
ServerReader<EchoRequest>* reader,
EchoResponse* response) {
EchoResponse* response) override {
EchoRequest request;
response->set_message("");
while (reader->Read(&request)) {
@ -102,7 +102,7 @@ class TestServiceImpl : public TestService::Service {
// Return 3 messages.
// TODO(yangg) make it generic by adding a parameter into EchoRequest
Status ResponseStream(ServerContext* context, const EchoRequest* request,
ServerWriter<EchoResponse>* writer) {
ServerWriter<EchoResponse>* writer) override {
EchoResponse response;
response.set_message(request->message() + "0");
writer->Write(response);
@ -114,8 +114,9 @@ class TestServiceImpl : public TestService::Service {
return Status::OK;
}
Status BidiStream(ServerContext* context,
ServerReaderWriter<EchoResponse, EchoRequest>* stream) {
Status BidiStream(
ServerContext* context,
ServerReaderWriter<EchoResponse, EchoRequest>* stream) override {
EchoRequest request;
EchoResponse response;
while (stream->Read(&request)) {
@ -127,6 +128,16 @@ class TestServiceImpl : public TestService::Service {
}
};
class TestServiceImplDupPkg
: public ::grpc::cpp::test::util::duplicate::TestService::Service {
public:
Status Echo(ServerContext* context, const EchoRequest* request,
EchoResponse* response) override {
response->set_message("no package");
return Status::OK;
}
};
class End2endTest : public ::testing::Test {
protected:
void SetUp() override {
@ -136,6 +147,7 @@ class End2endTest : public ::testing::Test {
ServerBuilder builder;
builder.AddPort(server_address_.str());
builder.RegisterService(service_.service());
builder.RegisterService(dup_pkg_service_.service());
server_ = builder.BuildAndStart();
}
@ -143,15 +155,21 @@ class End2endTest : public ::testing::Test {
server_->Shutdown();
}
void ResetStub() {
std::shared_ptr<ChannelInterface> channel =
CreateChannel(server_address_.str(), ChannelArguments());
stub_.reset(grpc::cpp::test::util::TestService::NewStub(channel));
}
std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub_;
std::unique_ptr<Server> server_;
std::ostringstream server_address_;
TestServiceImpl service_;
TestServiceImplDupPkg dup_pkg_service_;
};
static void SendRpc(const grpc::string& server_address, int num_rpcs) {
std::shared_ptr<ChannelInterface> channel =
CreateChannel(server_address, ChannelArguments());
TestService::Stub* stub = TestService::NewStub(channel);
static void SendRpc(grpc::cpp::test::util::TestService::Stub* stub,
int num_rpcs) {
EchoRequest request;
EchoResponse response;
request.set_message("Hello");
@ -162,18 +180,18 @@ static void SendRpc(const grpc::string& server_address, int num_rpcs) {
EXPECT_EQ(response.message(), request.message());
EXPECT_TRUE(s.IsOk());
}
delete stub;
}
TEST_F(End2endTest, SimpleRpc) {
SendRpc(server_address_.str(), 1);
ResetStub();
SendRpc(stub_.get(), 1);
}
TEST_F(End2endTest, MultipleRpcs) {
ResetStub();
vector<std::thread*> threads;
for (int i = 0; i < 10; ++i) {
threads.push_back(new std::thread(SendRpc, server_address_.str(), 10));
threads.push_back(new std::thread(SendRpc, stub_.get(), 10));
}
for (int i = 0; i < 10; ++i) {
threads[i]->join();
@ -183,9 +201,7 @@ TEST_F(End2endTest, MultipleRpcs) {
// Set a 10us deadline and make sure proper error is returned.
TEST_F(End2endTest, RpcDeadlineExpires) {
std::shared_ptr<ChannelInterface> channel =
CreateChannel(server_address_.str(), ChannelArguments());
TestService::Stub* stub = TestService::NewStub(channel);
ResetStub();
EchoRequest request;
EchoResponse response;
request.set_message("Hello");
@ -194,19 +210,15 @@ TEST_F(End2endTest, RpcDeadlineExpires) {
std::chrono::system_clock::time_point deadline =
std::chrono::system_clock::now() + std::chrono::microseconds(10);
context.set_absolute_deadline(deadline);
Status s = stub->Echo(&context, request, &response);
Status s = stub_->Echo(&context, request, &response);
// TODO(yangg) use correct error code when b/18793983 is fixed.
// EXPECT_EQ(StatusCode::DEADLINE_EXCEEDED, s.code());
EXPECT_EQ(StatusCode::CANCELLED, s.code());
delete stub;
}
// Set a long but finite deadline.
TEST_F(End2endTest, RpcLongDeadline) {
std::shared_ptr<ChannelInterface> channel =
CreateChannel(server_address_.str(), ChannelArguments());
TestService::Stub* stub = TestService::NewStub(channel);
ResetStub();
EchoRequest request;
EchoResponse response;
request.set_message("Hello");
@ -215,18 +227,14 @@ TEST_F(End2endTest, RpcLongDeadline) {
std::chrono::system_clock::time_point deadline =
std::chrono::system_clock::now() + std::chrono::hours(1);
context.set_absolute_deadline(deadline);
Status s = stub->Echo(&context, request, &response);
Status s = stub_->Echo(&context, request, &response);
EXPECT_EQ(response.message(), request.message());
EXPECT_TRUE(s.IsOk());
delete stub;
}
// Ask server to echo back the deadline it sees.
TEST_F(End2endTest, EchoDeadline) {
std::shared_ptr<ChannelInterface> channel =
CreateChannel(server_address_.str(), ChannelArguments());
TestService::Stub* stub = TestService::NewStub(channel);
ResetStub();
EchoRequest request;
EchoResponse response;
request.set_message("Hello");
@ -236,7 +244,7 @@ TEST_F(End2endTest, EchoDeadline) {
std::chrono::system_clock::time_point deadline =
std::chrono::system_clock::now() + std::chrono::seconds(100);
context.set_absolute_deadline(deadline);
Status s = stub->Echo(&context, request, &response);
Status s = stub_->Echo(&context, request, &response);
EXPECT_EQ(response.message(), request.message());
EXPECT_TRUE(s.IsOk());
gpr_timespec sent_deadline;
@ -244,56 +252,44 @@ TEST_F(End2endTest, EchoDeadline) {
// Allow 1 second error.
EXPECT_LE(response.param().request_deadline() - sent_deadline.tv_sec, 1);
EXPECT_GE(response.param().request_deadline() - sent_deadline.tv_sec, -1);
delete stub;
}
// Ask server to echo back the deadline it sees. The rpc has no deadline.
TEST_F(End2endTest, EchoDeadlineForNoDeadlineRpc) {
std::shared_ptr<ChannelInterface> channel =
CreateChannel(server_address_.str(), ChannelArguments());
TestService::Stub* stub = TestService::NewStub(channel);
ResetStub();
EchoRequest request;
EchoResponse response;
request.set_message("Hello");
request.mutable_param()->set_echo_deadline(true);
ClientContext context;
Status s = stub->Echo(&context, request, &response);
Status s = stub_->Echo(&context, request, &response);
EXPECT_EQ(response.message(), request.message());
EXPECT_TRUE(s.IsOk());
EXPECT_EQ(response.param().request_deadline(), gpr_inf_future.tv_sec);
delete stub;
}
TEST_F(End2endTest, UnimplementedRpc) {
std::shared_ptr<ChannelInterface> channel =
CreateChannel(server_address_.str(), ChannelArguments());
TestService::Stub* stub = TestService::NewStub(channel);
ResetStub();
EchoRequest request;
EchoResponse response;
request.set_message("Hello");
ClientContext context;
Status s = stub->Unimplemented(&context, request, &response);
Status s = stub_->Unimplemented(&context, request, &response);
EXPECT_FALSE(s.IsOk());
EXPECT_EQ(s.code(), grpc::StatusCode::UNIMPLEMENTED);
EXPECT_EQ(s.details(), "");
EXPECT_EQ(response.message(), "");
delete stub;
}
TEST_F(End2endTest, RequestStreamOneRequest) {
std::shared_ptr<ChannelInterface> channel =
CreateChannel(server_address_.str(), ChannelArguments());
TestService::Stub* stub = TestService::NewStub(channel);
ResetStub();
EchoRequest request;
EchoResponse response;
ClientContext context;
ClientWriter<EchoRequest>* stream = stub->RequestStream(&context, &response);
ClientWriter<EchoRequest>* stream = stub_->RequestStream(&context, &response);
request.set_message("hello");
EXPECT_TRUE(stream->Write(request));
stream->WritesDone();
@ -302,18 +298,15 @@ TEST_F(End2endTest, RequestStreamOneRequest) {
EXPECT_TRUE(s.IsOk());
delete stream;
delete stub;
}
TEST_F(End2endTest, RequestStreamTwoRequests) {
std::shared_ptr<ChannelInterface> channel =
CreateChannel(server_address_.str(), ChannelArguments());
TestService::Stub* stub = TestService::NewStub(channel);
ResetStub();
EchoRequest request;
EchoResponse response;
ClientContext context;
ClientWriter<EchoRequest>* stream = stub->RequestStream(&context, &response);
ClientWriter<EchoRequest>* stream = stub_->RequestStream(&context, &response);
request.set_message("hello");
EXPECT_TRUE(stream->Write(request));
EXPECT_TRUE(stream->Write(request));
@ -323,19 +316,17 @@ TEST_F(End2endTest, RequestStreamTwoRequests) {
EXPECT_TRUE(s.IsOk());
delete stream;
delete stub;
}
TEST_F(End2endTest, ResponseStream) {
std::shared_ptr<ChannelInterface> channel =
CreateChannel(server_address_.str(), ChannelArguments());
TestService::Stub* stub = TestService::NewStub(channel);
ResetStub();
EchoRequest request;
EchoResponse response;
ClientContext context;
request.set_message("hello");
ClientReader<EchoResponse>* stream = stub->ResponseStream(&context, &request);
ClientReader<EchoResponse>* stream =
stub_->ResponseStream(&context, &request);
EXPECT_TRUE(stream->Read(&response));
EXPECT_EQ(response.message(), request.message() + "0");
EXPECT_TRUE(stream->Read(&response));
@ -348,20 +339,17 @@ TEST_F(End2endTest, ResponseStream) {
EXPECT_TRUE(s.IsOk());
delete stream;
delete stub;
}
TEST_F(End2endTest, BidiStream) {
std::shared_ptr<ChannelInterface> channel =
CreateChannel(server_address_.str(), ChannelArguments());
TestService::Stub* stub = TestService::NewStub(channel);
ResetStub();
EchoRequest request;
EchoResponse response;
ClientContext context;
grpc::string msg("hello");
ClientReaderWriter<EchoRequest, EchoResponse>* stream =
stub->BidiStream(&context);
stub_->BidiStream(&context);
request.set_message(msg + "0");
EXPECT_TRUE(stream->Write(request));
@ -385,7 +373,32 @@ TEST_F(End2endTest, BidiStream) {
EXPECT_TRUE(s.IsOk());
delete stream;
delete stub;
}
// Talk to the two services with the same name but different package names.
// The two stubs are created on the same channel.
TEST_F(End2endTest, DiffPackageServices) {
std::shared_ptr<ChannelInterface> channel =
CreateChannel(server_address_.str(), ChannelArguments());
EchoRequest request;
EchoResponse response;
request.set_message("Hello");
std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub(
grpc::cpp::test::util::TestService::NewStub(channel));
ClientContext context;
Status s = stub->Echo(&context, request, &response);
EXPECT_EQ(response.message(), request.message());
EXPECT_TRUE(s.IsOk());
std::unique_ptr<grpc::cpp::test::util::duplicate::TestService::Stub>
dup_pkg_stub(
grpc::cpp::test::util::duplicate::TestService::NewStub(channel));
ClientContext context2;
s = dup_pkg_stub->Echo(&context2, request, &response);
EXPECT_EQ("no package", response.message());
EXPECT_TRUE(s.IsOk());
}
} // namespace testing

@ -1,24 +1,8 @@
syntax = "proto2";
package grpc.cpp.test.util;
message RequestParams {
optional bool echo_deadline = 1;
}
message EchoRequest {
optional string message = 1;
optional RequestParams param = 2;
}
import "test/cpp/util/messages.proto";
message ResponseParams {
optional int64 request_deadline = 1;
}
message EchoResponse {
optional string message = 1;
optional ResponseParams param = 2;
}
package grpc.cpp.test.util;
service TestService {
rpc Echo(EchoRequest) returns (EchoResponse);

@ -0,0 +1,12 @@
// This is a partial copy of echo.proto with a different package name.
syntax = "proto2";
import "test/cpp/util/messages.proto";
package grpc.cpp.test.util.duplicate;
service TestService {
rpc Echo(grpc.cpp.test.util.EchoRequest)
returns (grpc.cpp.test.util.EchoResponse);
}

@ -0,0 +1,21 @@
syntax = "proto2";
package grpc.cpp.test.util;
message RequestParams {
optional bool echo_deadline = 1;
}
message EchoRequest {
optional string message = 1;
optional RequestParams param = 2;
}
message ResponseParams {
optional int64 request_deadline = 1;
}
message EchoResponse {
optional string message = 1;
optional ResponseParams param = 2;
}
Loading…
Cancel
Save