• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpc/support/port_platform.h>
20 #include <grpcpp/channel.h>
21 #include <grpcpp/client_context.h>
22 #include <grpcpp/server.h>
23 #include <grpcpp/server_builder.h>
24 #include <grpcpp/server_context.h>
25 #include <gtest/gtest.h>
26 
27 #include <exception>
28 #include <memory>
29 
30 #include "src/proto/grpc/testing/echo.grpc.pb.h"
31 #include "test/core/test_util/test_config.h"
32 
33 namespace grpc {
34 namespace testing {
35 
36 const char* kErrorMessage = "This service caused an exception";
37 
38 #if GRPC_ALLOW_EXCEPTIONS
39 class ExceptingServiceImpl : public grpc::testing::EchoTestService::Service {
40  public:
Echo(ServerContext *,const EchoRequest *,EchoResponse *)41   Status Echo(ServerContext* /*server_context*/, const EchoRequest* /*request*/,
42               EchoResponse* /*response*/) override {
43     throw -1;
44   }
RequestStream(ServerContext *,ServerReader<EchoRequest> *,EchoResponse *)45   Status RequestStream(ServerContext* /*context*/,
46                        ServerReader<EchoRequest>* /*reader*/,
47                        EchoResponse* /*response*/) override {
48     throw ServiceException();
49   }
50 
51  private:
52   class ServiceException final : public std::exception {
53    public:
ServiceException()54     ServiceException() {}
55 
56    private:
what() const57     const char* what() const noexcept override { return kErrorMessage; }
58   };
59 };
60 
61 class ExceptionTest : public ::testing::Test {
62  protected:
ExceptionTest()63   ExceptionTest() {}
64 
SetUp()65   void SetUp() override {
66     ServerBuilder builder;
67     builder.RegisterService(&service_);
68     server_ = builder.BuildAndStart();
69   }
70 
TearDown()71   void TearDown() override { server_->Shutdown(); }
72 
ResetStub()73   void ResetStub() {
74     channel_ = server_->InProcessChannel(ChannelArguments());
75     stub_ = grpc::testing::EchoTestService::NewStub(channel_);
76   }
77 
78   std::shared_ptr<Channel> channel_;
79   std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
80   std::unique_ptr<Server> server_;
81   ExceptingServiceImpl service_;
82 };
83 
TEST_F(ExceptionTest,Unary)84 TEST_F(ExceptionTest, Unary) {
85   ResetStub();
86   EchoRequest request;
87   EchoResponse response;
88   request.set_message("test");
89 
90   for (int i = 0; i < 10; i++) {
91     ClientContext context;
92     Status s = stub_->Echo(&context, request, &response);
93     EXPECT_FALSE(s.ok());
94     EXPECT_EQ(s.error_code(), StatusCode::UNKNOWN);
95   }
96 }
97 
TEST_F(ExceptionTest,RequestStream)98 TEST_F(ExceptionTest, RequestStream) {
99   ResetStub();
100   EchoResponse response;
101 
102   for (int i = 0; i < 10; i++) {
103     ClientContext context;
104     auto stream = stub_->RequestStream(&context, &response);
105     stream->WritesDone();
106     Status s = stream->Finish();
107 
108     EXPECT_FALSE(s.ok());
109     EXPECT_EQ(s.error_code(), StatusCode::UNKNOWN);
110   }
111 }
112 
113 #endif  // GRPC_ALLOW_EXCEPTIONS
114 
115 }  // namespace testing
116 }  // namespace grpc
117 
main(int argc,char ** argv)118 int main(int argc, char** argv) {
119   grpc::testing::TestEnvironment env(&argc, argv);
120   ::testing::InitGoogleTest(&argc, argv);
121   return RUN_ALL_TESTS();
122 }
123