|
|
|
@ -41,23 +41,68 @@ using proto::EchoTestService; |
|
|
|
|
using proto::ForwardEchoRequest; |
|
|
|
|
using proto::ForwardEchoResponse; |
|
|
|
|
|
|
|
|
|
// A very simple EchoTestService implementation that just echoes back the
|
|
|
|
|
// message without handling any other expectations for ForwardEcho.
|
|
|
|
|
class SimpleEchoTestServerImpl : public proto::EchoTestService::Service { |
|
|
|
|
public: |
|
|
|
|
explicit SimpleEchoTestServerImpl() {} |
|
|
|
|
|
|
|
|
|
grpc::Status Echo(grpc::ServerContext* context, |
|
|
|
|
const proto::EchoRequest* request, |
|
|
|
|
proto::EchoResponse* response) override { |
|
|
|
|
GPR_ASSERT(false); |
|
|
|
|
return Status(StatusCode::INVALID_ARGUMENT, "Unexpected"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
grpc::Status ForwardEcho(grpc::ServerContext* /*context*/, |
|
|
|
|
const proto::ForwardEchoRequest* request, |
|
|
|
|
proto::ForwardEchoResponse* response) override { |
|
|
|
|
response->add_output(request->message()); |
|
|
|
|
return Status::OK; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
std::string hostname_; |
|
|
|
|
std::string forwarding_address_; |
|
|
|
|
// The following fields are not set yet. But we may need them later.
|
|
|
|
|
// int port_;
|
|
|
|
|
// std::string version_;
|
|
|
|
|
// std::string cluster_;
|
|
|
|
|
// std::string istio_version_;
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
class EchoTest : public ::testing::Test { |
|
|
|
|
protected: |
|
|
|
|
EchoTest() : echo_test_service_impl_("hostname") { |
|
|
|
|
EchoTest() { |
|
|
|
|
// Start the simple server which will handle protocols that
|
|
|
|
|
// EchoTestServiceImpl does not handle.
|
|
|
|
|
int forwarding_port = grpc_pick_unused_port_or_die(); |
|
|
|
|
forwarding_address_ = grpc_core::JoinHostPort("localhost", forwarding_port); |
|
|
|
|
ServerBuilder simple_builder; |
|
|
|
|
simple_builder.RegisterService(&simple_test_service_impl_); |
|
|
|
|
simple_builder.AddListeningPort(forwarding_address_, |
|
|
|
|
InsecureServerCredentials()); |
|
|
|
|
simple_server_ = simple_builder.BuildAndStart(); |
|
|
|
|
// Start the EchoTestServiceImpl server
|
|
|
|
|
ServerBuilder builder; |
|
|
|
|
builder.RegisterService(&echo_test_service_impl_); |
|
|
|
|
echo_test_service_impl_ = |
|
|
|
|
std::make_unique<EchoTestServiceImpl>("hostname", forwarding_address_); |
|
|
|
|
builder.RegisterService(echo_test_service_impl_.get()); |
|
|
|
|
int port = grpc_pick_unused_port_or_die(); |
|
|
|
|
server_address_ = grpc_core::JoinHostPort("localhost", port); |
|
|
|
|
builder.AddListeningPort(grpc_core::JoinHostPort("localhost", port), |
|
|
|
|
InsecureServerCredentials()); |
|
|
|
|
builder.AddListeningPort(server_address_, InsecureServerCredentials()); |
|
|
|
|
server_ = builder.BuildAndStart(); |
|
|
|
|
|
|
|
|
|
auto channel = CreateChannel(server_address_, InsecureChannelCredentials()); |
|
|
|
|
stub_ = EchoTestService::NewStub(channel); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
EchoTestServiceImpl echo_test_service_impl_; |
|
|
|
|
std::string forwarding_address_; |
|
|
|
|
SimpleEchoTestServerImpl simple_test_service_impl_; |
|
|
|
|
std::unique_ptr<EchoTestServiceImpl> echo_test_service_impl_; |
|
|
|
|
std::string server_address_; |
|
|
|
|
std::unique_ptr<Server> server_; |
|
|
|
|
std::unique_ptr<Server> simple_server_; |
|
|
|
|
std::unique_ptr<EchoTestService::Stub> stub_; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
@ -67,7 +112,7 @@ TEST_F(EchoTest, SimpleEchoTest) { |
|
|
|
|
EchoResponse response; |
|
|
|
|
request.set_message("hello"); |
|
|
|
|
auto status = stub_->Echo(&context, request, &response); |
|
|
|
|
EXPECT_TRUE(status.ok()); |
|
|
|
|
ASSERT_TRUE(status.ok()); |
|
|
|
|
EXPECT_THAT(response.message(), |
|
|
|
|
::testing::AllOf(::testing::HasSubstr("StatusCode=200\n"), |
|
|
|
|
::testing::HasSubstr("Hostname=hostname\n"), |
|
|
|
@ -86,7 +131,7 @@ TEST_F(EchoTest, ForwardEchoTest) { |
|
|
|
|
request.set_url(absl::StrCat("grpc://", server_address_)); |
|
|
|
|
request.set_message("hello"); |
|
|
|
|
auto status = stub_->ForwardEcho(&context, request, &response); |
|
|
|
|
EXPECT_TRUE(status.ok()); |
|
|
|
|
ASSERT_TRUE(status.ok()); |
|
|
|
|
for (int i = 0; i < 3; ++i) { |
|
|
|
|
EXPECT_THAT( |
|
|
|
|
response.output()[i], |
|
|
|
@ -101,6 +146,24 @@ TEST_F(EchoTest, ForwardEchoTest) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TEST_F(EchoTest, ForwardEchoTestUnhandledProtocols) { |
|
|
|
|
ClientContext context; |
|
|
|
|
ForwardEchoRequest request; |
|
|
|
|
ForwardEchoResponse response; |
|
|
|
|
request.set_count(3); |
|
|
|
|
request.set_qps(1); |
|
|
|
|
request.set_timeout_micros(20 * 1000 * 1000); // 20 seconds
|
|
|
|
|
// http protocol is unhandled by EchoTestServiceImpl and should be forwarded
|
|
|
|
|
// to SimpleEchoTestServiceImpl
|
|
|
|
|
request.set_url(absl::StrCat("http://", server_address_)); |
|
|
|
|
request.set_message("hello"); |
|
|
|
|
auto status = stub_->ForwardEcho(&context, request, &response); |
|
|
|
|
ASSERT_TRUE(status.ok()) << "Code = " << status.error_code() |
|
|
|
|
<< " Message = " << status.error_message(); |
|
|
|
|
ASSERT_FALSE(response.output().empty()); |
|
|
|
|
EXPECT_EQ(response.output()[0], "hello"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
} // namespace testing
|
|
|
|
|
} // namespace grpc
|
|
|
|
|