| 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/impl/codegen/port_platform.h> |
| 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" |
| 33 |
#include "test/core/util/test_config.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 |
} |