exception_test.cc
Go to the documentation of this file.
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 
20 
21 #include <exception>
22 #include <memory>
23 
24 #include <gtest/gtest.h>
25 
26 #include <grpcpp/channel.h>
27 #include <grpcpp/client_context.h>
28 #include <grpcpp/server.h>
29 #include <grpcpp/server_builder.h>
30 #include <grpcpp/server_context.h>
31 
32 #include "src/proto/grpc/testing/echo.grpc.pb.h"
34 
35 namespace grpc {
36 namespace testing {
37 
38 const char* kErrorMessage = "This service caused an exception";
39 
40 #if GRPC_ALLOW_EXCEPTIONS
41 class ExceptingServiceImpl : public grpc::testing::EchoTestService::Service {
42  public:
43  Status Echo(ServerContext* /*server_context*/, const EchoRequest* /*request*/,
44  EchoResponse* /*response*/) override {
45  throw -1;
46  }
47  Status RequestStream(ServerContext* /*context*/,
48  ServerReader<EchoRequest>* /*reader*/,
49  EchoResponse* /*response*/) override {
50  throw ServiceException();
51  }
52 
53  private:
54  class ServiceException final : public std::exception {
55  public:
56  ServiceException() {}
57 
58  private:
59  const char* what() const noexcept override { return kErrorMessage; }
60  };
61 };
62 
63 class ExceptionTest : public ::testing::Test {
64  protected:
65  ExceptionTest() {}
66 
67  void SetUp() override {
68  ServerBuilder builder;
69  builder.RegisterService(&service_);
70  server_ = builder.BuildAndStart();
71  }
72 
73  void TearDown() override { server_->Shutdown(); }
74 
75  void ResetStub() {
76  channel_ = server_->InProcessChannel(ChannelArguments());
77  stub_ = grpc::testing::EchoTestService::NewStub(channel_);
78  }
79 
80  std::shared_ptr<Channel> channel_;
81  std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
82  std::unique_ptr<Server> server_;
83  ExceptingServiceImpl service_;
84 };
85 
86 TEST_F(ExceptionTest, Unary) {
87  ResetStub();
88  EchoRequest request;
89  EchoResponse response;
90  request.set_message("test");
91 
92  for (int i = 0; i < 10; i++) {
93  ClientContext context;
94  Status s = stub_->Echo(&context, request, &response);
95  EXPECT_FALSE(s.ok());
96  EXPECT_EQ(s.error_code(), StatusCode::UNKNOWN);
97  }
98 }
99 
100 TEST_F(ExceptionTest, RequestStream) {
101  ResetStub();
102  EchoResponse response;
103 
104  for (int i = 0; i < 10; i++) {
105  ClientContext context;
106  auto stream = stub_->RequestStream(&context, &response);
107  stream->WritesDone();
108  Status s = stream->Finish();
109 
110  EXPECT_FALSE(s.ok());
111  EXPECT_EQ(s.error_code(), StatusCode::UNKNOWN);
112  }
113 }
114 
115 #endif // GRPC_ALLOW_EXCEPTIONS
116 
117 } // namespace testing
118 } // namespace grpc
119 
120 int main(int argc, char** argv) {
121  grpc::testing::TestEnvironment env(&argc, argv);
122  ::testing::InitGoogleTest(&argc, argv);
123  return RUN_ALL_TESTS();
124 }
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
testing
Definition: aws_request_signer_test.cc:25
grpc::ServerContext
Definition: grpcpp/impl/codegen/server_context.h:566
stub_
std::unique_ptr< grpc::testing::EchoTestService::Stub > stub_
Definition: client_channel_stress_test.cc:331
generate.env
env
Definition: generate.py:37
grpc
Definition: grpcpp/alarm.h:33
benchmark.request
request
Definition: benchmark.py:77
main
int main(int argc, char **argv)
Definition: exception_test.cc:120
testing::gmock_more_actions_test::Unary
bool Unary(int x)
Definition: bloaty/third_party/googletest/googlemock/test/gmock-more-actions_test.cc:85
grpc::testing::kErrorMessage
const char * kErrorMessage
Definition: exception_test.cc:38
absl::FormatConversionChar::s
@ s
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
server_
Server *const server_
Definition: chttp2_server.cc:260
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
grpc.StatusCode.UNKNOWN
tuple UNKNOWN
Definition: src/python/grpcio/grpc/__init__.py:262
channel_
RefCountedPtr< Channel > channel_
Definition: channel_connectivity.cc:209
channel.h
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
test_config.h
client_context.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc::testing::TEST_F
TEST_F(ChannelArgumentsTest, SetInt)
Definition: channel_arguments_test.cc:134
port_platform.h
server_context.h
asyncio_get_stats.response
response
Definition: asyncio_get_stats.py:28
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc::protobuf::util::Status
GRPC_CUSTOM_UTIL_STATUS Status
Definition: include/grpcpp/impl/codegen/config_protobuf.h:93
grpc::ServerReader
Definition: include/grpcpp/impl/codegen/completion_queue.h:58
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
grpc::testing::EXPECT_EQ
EXPECT_EQ(options.token_exchange_service_uri, "https://foo/exchange")
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
server.h
server_builder.h
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
service_
std::unique_ptr< grpc::testing::TestServiceImpl > service_
Definition: end2end_binder_transport_test.cc:71
stream
voidpf stream
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:16