Debian Codegraph @

test/cpp/server/server_request_call_test.cc

raw
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 <thread>
20
21 #include <gtest/gtest.h>
22
23 #include <grpc/support/log.h>
24 #include <grpcpp/create_channel.h>
25 #include <grpcpp/security/credentials.h>
26 #include <grpcpp/server.h>
27 #include <grpcpp/server_builder.h>
28 #include <grpcpp/support/config.h>
29
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
34 namespace grpc {
35 namespace {
36
37 TEST(ServerRequestCallTest, ShortDeadlineDoesNotCauseOkayFalse) {
38 std::mutex mu;
39 bool shutting_down = false;
40
41 // grpc server config.
42 std::ostringstream s;
43 int p = grpc_pick_unused_port_or_die();
44 s << "[::1]:" << p;
45 const string address = s.str();
46 testing::EchoTestService::AsyncService service;
47 ServerBuilder builder;
48 builder.AddListeningPort(address, InsecureServerCredentials());
49 auto cq = builder.AddCompletionQueue();
50 builder.RegisterService(&service);
51 auto server = builder.BuildAndStart();
52
53 // server thread.
54 std::thread t([address, &service, &cq, &mu, &shutting_down] {
55 for (int n = 0; true; n++) {
56 ServerContext ctx;
57 testing::EchoRequest req;
58 ServerAsyncResponseWriter<testing::EchoResponse> responder(&ctx);
59
60 // if shutting down, don't enqueue a new request.
61 {
62 std::lock_guard<std::mutex> lock(mu);
63 if (!shutting_down) {
64 service.RequestEcho(&ctx, &req, &responder, cq.get(), cq.get(),
65 reinterpret_cast<void*>(1));
66 }
67 }
68
69 bool ok;
70 void* tag;
71 if (!cq->Next(&tag, &ok)) {
72 break;
73 }
74
75 EXPECT_EQ((void*)1, tag);
76 // If not shutting down, ok must be true for new requests.
77 {
78 std::lock_guard<std::mutex> lock(mu);
79 if (!shutting_down && !ok) {
80 gpr_log(GPR_INFO, "!ok on request %d", n);
81 abort();
82 }
83 if (shutting_down && !ok) {
84 // Failed connection due to shutdown, continue flushing the CQ.
85 continue;
86 }
87 }
88
89 // Send a simple response after a small delay that would ensure the client
90 // deadline is exceeded.
91 gpr_log(GPR_INFO, "Got request %d", n);
92 testing::EchoResponse response;
93 response.set_message("foobar");
94 // A bit of sleep to make sure the deadline elapses.
95 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC),
96 gpr_time_from_seconds(1, GPR_TIMESPAN)));
97 {
98 std::lock_guard<std::mutex> lock(mu);
99 if (shutting_down) {
100 gpr_log(GPR_INFO,
101 "shut down while processing call, not calling Finish()");
102 // Continue flushing the CQ.
103 continue;
104 }
105 gpr_log(GPR_INFO, "Finishing request %d", n);
106 responder.Finish(response, grpc::Status::OK,
107 reinterpret_cast<void*>(2));
108 if (!cq->Next(&tag, &ok)) {
109 break;
110 }
111 EXPECT_EQ((void*)2, tag);
112 }
113 }
114 });
115
116 auto stub = testing::EchoTestService::NewStub(
117 grpc::CreateChannel(address, InsecureChannelCredentials()));
118
119 for (int i = 0; i < 100; i++) {
120 gpr_log(GPR_INFO, "Sending %d.", i);
121 testing::EchoRequest request;
122
123 /////////
124 // Comment out the following line to get ok=false due to invalid request.
125 // Otherwise, ok=false due to deadline being exceeded.
126 /////////
127 request.set_message("foobar");
128
129 // A simple request with a short deadline. The server will always exceed the
130 // deadline, whether due to the sleep or because the server was unable to
131 // even fetch the request from the CQ before the deadline elapsed.
132 testing::EchoResponse response;
133 grpc::ClientContext ctx;
134 ctx.set_fail_fast(false);
135 ctx.set_deadline(std::chrono::system_clock::now() +
136 std::chrono::milliseconds(1));
137 grpc::Status status = stub->Echo(&ctx, request, &response);
138 EXPECT_EQ(StatusCode::DEADLINE_EXCEEDED, status.error_code());
139 gpr_log(GPR_INFO, "Success.");
140 }
141 gpr_log(GPR_INFO, "Done sending RPCs.");
142
143 // Shut down everything properly.
144 gpr_log(GPR_INFO, "Shutting down.");
145 {
146 std::lock_guard<std::mutex> lock(mu);
147 shutting_down = true;
148 }
149 server->Shutdown();
150 cq->Shutdown();
151 server->Wait();
152
153 t.join();
154 }
155
156 } // namespace
157 } // namespace grpc
158
159 int main(int argc, char** argv) {
160 grpc::testing::TestEnvironment env(&argc, argv);
161 ::testing::InitGoogleTest(&argc, argv);
162 return RUN_ALL_TESTS();
163 }