1 //
2 //
3 // Copyright 2017 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #include <exception>
20 #include <memory>
21
22 #include <gtest/gtest.h>
23
24 #include <grpc/support/port_platform.h>
25 #include <grpcpp/channel.h>
26 #include <grpcpp/client_context.h>
27 #include <grpcpp/server.h>
28 #include <grpcpp/server_builder.h>
29 #include <grpcpp/server_context.h>
30
31 #include "src/proto/grpc/testing/echo.grpc.pb.h"
32 #include "test/core/util/test_config.h"
33
34 namespace grpc {
35 namespace testing {
36
37 const char* kErrorMessage = "This service caused an exception";
38
39 #if GRPC_ALLOW_EXCEPTIONS
40 class ExceptingServiceImpl : public grpc::testing::EchoTestService::Service {
41 public:
Echo(ServerContext *,const EchoRequest *,EchoResponse *)42 Status Echo(ServerContext* /*server_context*/, const EchoRequest* /*request*/,
43 EchoResponse* /*response*/) override {
44 throw -1;
45 }
RequestStream(ServerContext *,ServerReader<EchoRequest> *,EchoResponse *)46 Status RequestStream(ServerContext* /*context*/,
47 ServerReader<EchoRequest>* /*reader*/,
48 EchoResponse* /*response*/) override {
49 throw ServiceException();
50 }
51
52 private:
53 class ServiceException final : public std::exception {
54 public:
ServiceException()55 ServiceException() {}
56
57 private:
what() const58 const char* what() const noexcept override { return kErrorMessage; }
59 };
60 };
61
62 class ExceptionTest : public ::testing::Test {
63 protected:
ExceptionTest()64 ExceptionTest() {}
65
SetUp()66 void SetUp() override {
67 ServerBuilder builder;
68 builder.RegisterService(&service_);
69 server_ = builder.BuildAndStart();
70 }
71
TearDown()72 void TearDown() override { server_->Shutdown(); }
73
ResetStub()74 void ResetStub() {
75 channel_ = server_->InProcessChannel(ChannelArguments());
76 stub_ = grpc::testing::EchoTestService::NewStub(channel_);
77 }
78
79 std::shared_ptr<Channel> channel_;
80 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
81 std::unique_ptr<Server> server_;
82 ExceptingServiceImpl service_;
83 };
84
TEST_F(ExceptionTest,Unary)85 TEST_F(ExceptionTest, Unary) {
86 ResetStub();
87 EchoRequest request;
88 EchoResponse response;
89 request.set_message("test");
90
91 for (int i = 0; i < 10; i++) {
92 ClientContext context;
93 Status s = stub_->Echo(&context, request, &response);
94 EXPECT_FALSE(s.ok());
95 EXPECT_EQ(s.error_code(), StatusCode::UNKNOWN);
96 }
97 }
98
TEST_F(ExceptionTest,RequestStream)99 TEST_F(ExceptionTest, RequestStream) {
100 ResetStub();
101 EchoResponse response;
102
103 for (int i = 0; i < 10; i++) {
104 ClientContext context;
105 auto stream = stub_->RequestStream(&context, &response);
106 stream->WritesDone();
107 Status s = stream->Finish();
108
109 EXPECT_FALSE(s.ok());
110 EXPECT_EQ(s.error_code(), StatusCode::UNKNOWN);
111 }
112 }
113
114 #endif // GRPC_ALLOW_EXCEPTIONS
115
116 } // namespace testing
117 } // namespace grpc
118
main(int argc,char ** argv)119 int main(int argc, char** argv) {
120 grpc::testing::TestEnvironment env(&argc, argv);
121 ::testing::InitGoogleTest(&argc, argv);
122 return RUN_ALL_TESTS();
123 }
124