Skip to content
This repository has been archived by the owner on Mar 28, 2022. It is now read-only.

Commit

Permalink
Added a test for validating the RPC signature.
Browse files Browse the repository at this point in the history
  • Loading branch information
benh committed Jul 10, 2020
1 parent 9a77ac2 commit 999ad11
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 4 deletions.
4 changes: 4 additions & 0 deletions stout/grpc/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,10 @@ class Client
case CallType::SERVER_STREAMING:
return call->WriteAndDone(*request);
default:
// NOTE: because 'Client' is a friend of
// 'ClientCallBase' we won't get a compile time
// error even if the type of 'call' doesn't allow us
// to call 'Write()'.
return call->Write(*request);
}
}();
Expand Down
14 changes: 11 additions & 3 deletions stout/grpc/traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,19 @@ struct RequestResponseTraits
static absl::optional<Error> Validate(const google::protobuf::MethodDescriptor* method)
{
if (Details<Request>::streaming && !method->client_streaming()) {
return Error { "Method DOES NOT have streaming requests" };
return Error { "Method does not have streaming requests" };
}

if (!Details<Request>::streaming && method->client_streaming()) {
return Error { "Method has streaming requests" };
}

if (Details<Response>::streaming && !method->server_streaming()) {
return Error { "Method DOES NOT have streaming responses" };
return Error { "Method does not have streaming responses" };
}

if (!Details<Response>::streaming && method->server_streaming()) {
return Error { "Method has streaming responses" };
}

if (Details<Request>::name() != method->input_type()->full_name()) {
Expand All @@ -108,7 +116,7 @@ struct RequestResponseTraits

if (Details<Response>::name() != method->output_type()->full_name()) {
return Error {
"Method does not have reponses of type "
"Method does not have responses of type "
+ Details<Response>::name()
};
}
Expand Down
2 changes: 1 addition & 1 deletion test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ cc_test(
"ClientDeathTest.cc",
"ServerDeathTest.cc",
"MultipleHosts.cc",
"ServeValidate.cc",
],
copts = ["-ftime-trace"],
deps = [
"//:grpc",
"@com_github_grpc_grpc//examples:helloworld_cc_grpc",
Expand Down
86 changes: 86 additions & 0 deletions test/ServeValidate.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include "gtest/gtest.h"

// https://github.com/grpc/grpc/blob/master/examples/protos/helloworld.proto
#include "examples/protos/helloworld.grpc.pb.h"

// https://github.com/grpc/grpc/blob/master/examples/protos/keyvaluestore.proto
#include "examples/protos/keyvaluestore.grpc.pb.h"

#include "stout/grpc/server.h"

using helloworld::HelloRequest;
using helloworld::HelloReply;
using helloworld::Greeter;

using stout::grpc::ServerBuilder;
using stout::grpc::Stream;


TEST(GrpcTest, ServeValidate)
{
ServerBuilder builder;

builder.AddListeningPort("0.0.0.0:0", grpc::InsecureServerCredentials());

auto build = builder.BuildAndStart();

ASSERT_TRUE(build.status.ok());

auto server = std::move(build.server);

ASSERT_TRUE(server);

auto serve = server->Serve<
keyvaluestore::Request,
Stream<keyvaluestore::Response>>(
"keyvaluestore.KeyValueStore.GetValues",
[](auto&& call) {});

ASSERT_FALSE(serve.ok());
EXPECT_EQ("Method has streaming requests", serve.error());

serve = server->Serve<
Stream<keyvaluestore::Request>,
keyvaluestore::Response>(
"keyvaluestore.KeyValueStore.GetValues",
[](auto&& call) {});

ASSERT_FALSE(serve.ok());
EXPECT_EQ("Method has streaming responses", serve.error());

serve = server->Serve<Greeter, Stream<HelloRequest>, HelloReply>(
"SayHello",
[](auto&& call) {});

ASSERT_FALSE(serve.ok());
EXPECT_EQ("Method does not have streaming requests", serve.error());

serve = server->Serve<Greeter, HelloRequest, Stream<HelloReply>>(
"SayHello",
[](auto&& call) {});

ASSERT_FALSE(serve.ok());
EXPECT_EQ("Method does not have streaming responses", serve.error());

serve = server->Serve<
Stream<HelloRequest>,
Stream<keyvaluestore::Response>>(
"keyvaluestore.KeyValueStore.GetValues",
[](auto&& call) {});

ASSERT_FALSE(serve.ok());
EXPECT_EQ(
"Method does not have requests of type helloworld.HelloRequest",
serve.error());

serve = server->Serve<
Stream<keyvaluestore::Request>,
Stream<HelloReply>>(
"keyvaluestore.KeyValueStore.GetValues",
[](auto&& call) {});

ASSERT_FALSE(serve.ok());
EXPECT_EQ(
"Method does not have responses of type helloworld.HelloReply",
serve.error());
}

0 comments on commit 999ad11

Please sign in to comment.