• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2015 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/grpc.h>
20 #include <grpc/support/log.h>
21 #include <grpc/support/time.h>
22 #include <grpcpp/channel.h>
23 #include <grpcpp/client_context.h>
24 #include <grpcpp/create_channel.h>
25 #include <grpcpp/server.h>
26 #include <grpcpp/server_builder.h>
27 #include <grpcpp/server_context.h>
28 
29 #include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h"
30 #include "src/proto/grpc/testing/echo.grpc.pb.h"
31 #include "test/core/util/port.h"
32 #include "test/core/util/test_config.h"
33 #include "test/cpp/util/subprocess.h"
34 
35 #include <gtest/gtest.h>
36 
37 using grpc::testing::EchoRequest;
38 using grpc::testing::EchoResponse;
39 using std::chrono::system_clock;
40 
41 static std::string g_root;
42 
43 namespace grpc {
44 namespace testing {
45 
46 namespace {
47 
48 class ServiceImpl final : public ::grpc::testing::EchoTestService::Service {
49  public:
ServiceImpl()50   ServiceImpl() : bidi_stream_count_(0), response_stream_count_(0) {}
51 
BidiStream(ServerContext * context,ServerReaderWriter<EchoResponse,EchoRequest> * stream)52   Status BidiStream(
53       ServerContext* context,
54       ServerReaderWriter<EchoResponse, EchoRequest>* stream) override {
55     bidi_stream_count_++;
56     EchoRequest request;
57     EchoResponse response;
58     while (stream->Read(&request)) {
59       gpr_log(GPR_INFO, "recv msg %s", request.message().c_str());
60       response.set_message(request.message());
61       stream->Write(response);
62       gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
63                                    gpr_time_from_seconds(1, GPR_TIMESPAN)));
64     }
65     return Status::OK;
66   }
67 
ResponseStream(ServerContext * context,const EchoRequest * request,ServerWriter<EchoResponse> * writer)68   Status ResponseStream(ServerContext* context, const EchoRequest* request,
69                         ServerWriter<EchoResponse>* writer) override {
70     EchoResponse response;
71     response_stream_count_++;
72     for (int i = 0;; i++) {
73       std::ostringstream msg;
74       msg << "Hello " << i;
75       response.set_message(msg.str());
76       if (!writer->Write(response)) break;
77       gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
78                                    gpr_time_from_seconds(1, GPR_TIMESPAN)));
79     }
80     return Status::OK;
81   }
82 
bidi_stream_count()83   int bidi_stream_count() { return bidi_stream_count_; }
84 
response_stream_count()85   int response_stream_count() { return response_stream_count_; }
86 
87  private:
88   int bidi_stream_count_;
89   int response_stream_count_;
90 };
91 
92 class CrashTest : public ::testing::Test {
93  protected:
CrashTest()94   CrashTest() {}
95 
CreateServerAndClient(const std::string & mode)96   std::unique_ptr<Server> CreateServerAndClient(const std::string& mode) {
97     auto port = grpc_pick_unused_port_or_die();
98     std::ostringstream addr_stream;
99     addr_stream << "localhost:" << port;
100     auto addr = addr_stream.str();
101     client_.reset(new SubProcess({g_root + "/server_crash_test_client",
102                                   "--address=" + addr, "--mode=" + mode}));
103     GPR_ASSERT(client_);
104 
105     ServerBuilder builder;
106     builder.AddListeningPort(addr, grpc::InsecureServerCredentials());
107     builder.RegisterService(&service_);
108     return builder.BuildAndStart();
109   }
110 
KillClient()111   void KillClient() { client_.reset(); }
112 
HadOneBidiStream()113   bool HadOneBidiStream() { return service_.bidi_stream_count() == 1; }
114 
HadOneResponseStream()115   bool HadOneResponseStream() { return service_.response_stream_count() == 1; }
116 
117  private:
118   std::unique_ptr<SubProcess> client_;
119   ServiceImpl service_;
120 };
121 
TEST_F(CrashTest,ResponseStream)122 TEST_F(CrashTest, ResponseStream) {
123   auto server = CreateServerAndClient("response");
124 
125   gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
126                                gpr_time_from_seconds(60, GPR_TIMESPAN)));
127   KillClient();
128   server->Shutdown();
129   GPR_ASSERT(HadOneResponseStream());
130 }
131 
TEST_F(CrashTest,BidiStream)132 TEST_F(CrashTest, BidiStream) {
133   auto server = CreateServerAndClient("bidi");
134 
135   gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
136                                gpr_time_from_seconds(60, GPR_TIMESPAN)));
137   KillClient();
138   server->Shutdown();
139   GPR_ASSERT(HadOneBidiStream());
140 }
141 
142 }  // namespace
143 
144 }  // namespace testing
145 }  // namespace grpc
146 
main(int argc,char ** argv)147 int main(int argc, char** argv) {
148   std::string me = argv[0];
149   auto lslash = me.rfind('/');
150   if (lslash != std::string::npos) {
151     g_root = me.substr(0, lslash);
152   } else {
153     g_root = ".";
154   }
155 
156   grpc_test_init(argc, argv);
157   ::testing::InitGoogleTest(&argc, argv);
158   return RUN_ALL_TESTS();
159 }
160