aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/cpp/util/grpc_tool_test.cc
diff options
context:
space:
mode:
authorGravatar Yuchen Zeng <zyc@google.com>2016-09-16 15:42:57 -0700
committerGravatar Yuchen Zeng <zyc@google.com>2017-01-30 14:31:24 -0800
commit8d2d70ca99451623ec48175671a81f9927000983 (patch)
tree1e3968a107f695bb9990c13106309dbea908bd21 /test/cpp/util/grpc_tool_test.cc
parentd37f642f359cb7fd7405831e675abb93fd4704e2 (diff)
Support bidirectional stream RPC
Add bidistream test Add bad request tests Replace deprecated DynamicMessageFactory constructor
Diffstat (limited to 'test/cpp/util/grpc_tool_test.cc')
-rw-r--r--test/cpp/util/grpc_tool_test.cc178
1 files changed, 138 insertions, 40 deletions
diff --git a/test/cpp/util/grpc_tool_test.cc b/test/cpp/util/grpc_tool_test.cc
index e2eebd4089..26e2b1f502 100644
--- a/test/cpp/util/grpc_tool_test.cc
+++ b/test/cpp/util/grpc_tool_test.cc
@@ -142,7 +142,7 @@ class TestServiceImpl : public ::grpc::testing::EchoTestService::Service {
Status RequestStream(ServerContext* context,
ServerReader<EchoRequest>* reader,
- EchoResponse* response) GRPC_OVERRIDE {
+ EchoResponse* response) override {
EchoRequest request;
response->set_message("");
if (!context->client_metadata().empty()) {
@@ -162,7 +162,7 @@ class TestServiceImpl : public ::grpc::testing::EchoTestService::Service {
}
Status ResponseStream(ServerContext* context, const EchoRequest* request,
- ServerWriter<EchoResponse>* writer) GRPC_OVERRIDE {
+ ServerWriter<EchoResponse>* writer) override {
if (!context->client_metadata().empty()) {
for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator
iter = context->client_metadata().begin();
@@ -181,6 +181,29 @@ class TestServiceImpl : public ::grpc::testing::EchoTestService::Service {
return Status::OK;
}
+
+ Status BidiStream(
+ ServerContext* context,
+ ServerReaderWriter<EchoResponse, EchoRequest>* stream) override {
+ EchoRequest request;
+ EchoResponse response;
+ if (!context->client_metadata().empty()) {
+ for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator
+ iter = context->client_metadata().begin();
+ iter != context->client_metadata().end(); ++iter) {
+ context->AddInitialMetadata(ToString(iter->first),
+ ToString(iter->second));
+ }
+ }
+ context->AddTrailingMetadata("trailing_key", "trailing_value");
+
+ while (stream->Read(&request)) {
+ response.set_message(request.message());
+ stream->Write(response);
+ }
+
+ return Status::OK;
+ }
};
} // namespace
@@ -391,48 +414,32 @@ TEST_F(GrpcToolTest, CallCommand) {
ShutdownServer();
}
-TEST_F(GrpcToolTest, ParseCommand) {
- // Test input "grpc_cli parse localhost:<port> grpc.testing.EchoResponse
- // ECHO_RESPONSE_MESSAGE"
+TEST_F(GrpcToolTest, CallCommandRequestStream) {
+ // Test input: grpc_cli call localhost:<port> RequestStream "message:
+ // 'Hello0'"
std::stringstream output_stream;
- std::stringstream binary_output_stream;
const grpc::string server_address = SetUpServer();
- const char* argv[] = {"grpc_cli", "parse", server_address.c_str(),
- "grpc.testing.EchoResponse", ECHO_RESPONSE_MESSAGE};
+ const char* argv[] = {"grpc_cli", "call", server_address.c_str(),
+ "RequestStream", "message: 'Hello0'"};
- FLAGS_binary_input = false;
- FLAGS_binary_output = false;
- EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
- std::bind(PrintStream, &output_stream,
- std::placeholders::_1)));
- // Expected output: ECHO_RESPONSE_MESSAGE
- EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(), ECHO_RESPONSE_MESSAGE));
+ // Mock std::cin input "message: 'Hello1'\n\n message: 'Hello2'\n\n"
+ std::streambuf* orig = std::cin.rdbuf();
+ std::istringstream ss("message: 'Hello1'\n\n message: 'Hello2'\n\n");
+ std::cin.rdbuf(ss.rdbuf());
- // Parse text message to binary message and then parse it back to text message
- output_stream.str(grpc::string());
- output_stream.clear();
- FLAGS_binary_output = true;
EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
std::bind(PrintStream, &output_stream,
std::placeholders::_1)));
- grpc::string binary_data = output_stream.str();
- output_stream.str(grpc::string());
- output_stream.clear();
- argv[4] = binary_data.c_str();
- FLAGS_binary_input = true;
- FLAGS_binary_output = false;
- EXPECT_TRUE(0 == GrpcToolMainLib(5, argv, TestCliCredentials(),
- std::bind(PrintStream, &output_stream,
- std::placeholders::_1)));
-
- // Expected output: ECHO_RESPONSE_MESSAGE
- EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(), ECHO_RESPONSE_MESSAGE));
+ // Expected output: "message: \"Hello0Hello1Hello2\""
+ EXPECT_TRUE(NULL != strstr(output_stream.str().c_str(),
+ "message: \"Hello0Hello1Hello2\""));
+ std::cin.rdbuf(orig);
ShutdownServer();
}
-TEST_F(GrpcToolTest, CallCommandRequestStream) {
+TEST_F(GrpcToolTest, CallCommandRequestStreamWithBadRequest) {
// Test input: grpc_cli call localhost:<port> RequestStream "message:
// 'Hello0'"
std::stringstream output_stream;
@@ -441,18 +448,18 @@ TEST_F(GrpcToolTest, CallCommandRequestStream) {
const char* argv[] = {"grpc_cli", "call", server_address.c_str(),
"RequestStream", "message: 'Hello0'"};
- // Mock std::cin input "message: 'Hello1'\n\n message: 'Hello2'\n\n"
+ // Mock std::cin input "bad_field: 'Hello1'\n\n message: 'Hello2'\n\n"
std::streambuf* orig = std::cin.rdbuf();
- std::istringstream ss("message: 'Hello1'\n\n message: 'Hello2'\n\n");
+ std::istringstream ss("bad_field: 'Hello1'\n\n message: 'Hello2'\n\n");
std::cin.rdbuf(ss.rdbuf());
EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
std::bind(PrintStream, &output_stream,
std::placeholders::_1)));
- // Expected output: "message: \"Hello0Hello1Hello2\""
- EXPECT_TRUE(NULL != strstr(output_stream.str().c_str(),
- "message: \"Hello0Hello1Hello2\""));
+ // Expected output: "message: \"Hello0Hello2\""
+ EXPECT_TRUE(NULL !=
+ strstr(output_stream.str().c_str(), "message: \"Hello0Hello2\""));
std::cin.rdbuf(orig);
ShutdownServer();
}
@@ -470,12 +477,10 @@ TEST_F(GrpcToolTest, CallCommandResponseStream) {
std::bind(PrintStream, &output_stream,
std::placeholders::_1)));
- fprintf(stderr, "%s\n", output_stream.str().c_str());
// Expected output: "message: \"Hello{n}\""
-
for (int i = 0; i < kNumResponseStreamsMsgs; i++) {
grpc::string expected_response_text =
- "message: \"Hello" + grpc::to_string(i) + "\"\n\n";
+ "message: \"Hello" + grpc::to_string(i) + "\"\n";
EXPECT_TRUE(NULL != strstr(output_stream.str().c_str(),
expected_response_text.c_str()));
}
@@ -483,6 +488,99 @@ TEST_F(GrpcToolTest, CallCommandResponseStream) {
ShutdownServer();
}
+TEST_F(GrpcToolTest, CallCommandBidiStream) {
+ // Test input: grpc_cli call localhost:<port> BidiStream "message: 'Hello0'"
+ std::stringstream output_stream;
+
+ const grpc::string server_address = SetUpServer();
+ const char* argv[] = {"grpc_cli", "call", server_address.c_str(),
+ "BidiStream", "message: 'Hello0'"};
+
+ // Mock std::cin input "message: 'Hello1'\n\n message: 'Hello2'\n\n"
+ std::streambuf* orig = std::cin.rdbuf();
+ std::istringstream ss("message: 'Hello1'\n\n message: 'Hello2'\n\n");
+ std::cin.rdbuf(ss.rdbuf());
+
+ EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
+ std::bind(PrintStream, &output_stream,
+ std::placeholders::_1)));
+
+ // Expected output: "message: \"Hello0\"\nmessage: \"Hello1\"\nmessage:
+ // \"Hello2\"\n\n"
+ EXPECT_TRUE(NULL != strstr(output_stream.str().c_str(),
+ "message: \"Hello0\"\nmessage: "
+ "\"Hello1\"\nmessage: \"Hello2\"\n"));
+ std::cin.rdbuf(orig);
+ ShutdownServer();
+}
+
+TEST_F(GrpcToolTest, CallCommandBidiStreamWithBadRequest) {
+ // Test input: grpc_cli call localhost:<port> BidiStream "message: 'Hello0'"
+ std::stringstream output_stream;
+
+ const grpc::string server_address = SetUpServer();
+ const char* argv[] = {"grpc_cli", "call", server_address.c_str(),
+ "BidiStream", "message: 'Hello0'"};
+
+ // Mock std::cin input "message: 'Hello1'\n\n message: 'Hello2'\n\n"
+ std::streambuf* orig = std::cin.rdbuf();
+ std::istringstream ss("message: 1.0\n\n message: 'Hello2'\n\n");
+ std::cin.rdbuf(ss.rdbuf());
+
+ EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
+ std::bind(PrintStream, &output_stream,
+ std::placeholders::_1)));
+
+ // Expected output: "message: \"Hello0\"\nmessage: \"Hello1\"\nmessage:
+ // \"Hello2\"\n\n"
+ EXPECT_TRUE(NULL != strstr(output_stream.str().c_str(),
+ "message: \"Hello0\"\nmessage: \"Hello2\"\n"));
+ std::cin.rdbuf(orig);
+
+ ShutdownServer();
+}
+
+TEST_F(GrpcToolTest, ParseCommand) {
+ // Test input "grpc_cli parse localhost:<port> grpc.testing.EchoResponse
+ // ECHO_RESPONSE_MESSAGE"
+ std::stringstream output_stream;
+ std::stringstream binary_output_stream;
+
+ const grpc::string server_address = SetUpServer();
+ const char* argv[] = {"grpc_cli", "parse", server_address.c_str(),
+ "grpc.testing.EchoResponse", ECHO_RESPONSE_MESSAGE};
+
+ FLAGS_binary_input = false;
+ FLAGS_binary_output = false;
+ EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
+ std::bind(PrintStream, &output_stream,
+ std::placeholders::_1)));
+ // Expected output: ECHO_RESPONSE_MESSAGE
+ EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(), ECHO_RESPONSE_MESSAGE));
+
+ // Parse text message to binary message and then parse it back to text message
+ output_stream.str(grpc::string());
+ output_stream.clear();
+ FLAGS_binary_output = true;
+ EXPECT_TRUE(0 == GrpcToolMainLib(ArraySize(argv), argv, TestCliCredentials(),
+ std::bind(PrintStream, &output_stream,
+ std::placeholders::_1)));
+ grpc::string binary_data = output_stream.str();
+ output_stream.str(grpc::string());
+ output_stream.clear();
+ argv[4] = binary_data.c_str();
+ FLAGS_binary_input = true;
+ FLAGS_binary_output = false;
+ EXPECT_TRUE(0 == GrpcToolMainLib(5, argv, TestCliCredentials(),
+ std::bind(PrintStream, &output_stream,
+ std::placeholders::_1)));
+
+ // Expected output: ECHO_RESPONSE_MESSAGE
+ EXPECT_TRUE(0 == strcmp(output_stream.str().c_str(), ECHO_RESPONSE_MESSAGE));
+
+ ShutdownServer();
+}
+
TEST_F(GrpcToolTest, TooFewArguments) {
// Test input "grpc_cli call Echo"
std::stringstream output_stream;