server_request_call_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 
19 #include <thread>
20 
21 #include <gtest/gtest.h>
22 
23 #include <grpc/support/log.h>
24 #include <grpcpp/create_channel.h>
27 #include <grpcpp/server.h>
28 #include <grpcpp/server_builder.h>
29 
30 #include "src/proto/grpc/testing/echo.grpc.pb.h"
31 #include "test/core/util/port.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;
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.
97  {
98  std::lock_guard<std::mutex> lock(mu);
99  if (shutting_down) {
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(
118 
119  for (int i = 0; i < 100; i++) {
120  gpr_log(GPR_INFO, "Sending %d.", i);
121  testing::EchoRequest request;
122 
124  // Comment out the following line to get ok=false due to invalid request.
125  // Otherwise, ok=false due to deadline being exceeded.
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;
134  ctx.set_fail_fast(false);
135  ctx.set_deadline(std::chrono::system_clock::now() +
136  std::chrono::milliseconds(1));
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 }
GPR_TIMESPAN
@ GPR_TIMESPAN
Definition: gpr_types.h:45
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
grpc::status
auto status
Definition: cpp/client/credentials_test.cc:200
now
static double now(void)
Definition: test/core/fling/client.cc:130
log.h
port.h
ctx
Definition: benchmark-async.c:30
generate.env
env
Definition: generate.py:37
grpc
Definition: grpcpp/alarm.h:33
mutex
static uv_mutex_t mutex
Definition: threadpool.c:34
benchmark.request
request
Definition: benchmark.py:77
ctx
static struct test_ctx ctx
Definition: test-ipc-send-recv.c:65
absl::FormatConversionChar::s
@ s
xds_manager.p
p
Definition: xds_manager.py:60
async_greeter_client.stub
stub
Definition: hellostreamingworld/async_greeter_client.py:26
config.h
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
tag
static void * tag(intptr_t t)
Definition: bad_client.cc:318
mu
Mutex mu
Definition: server_config_selector_filter.cc:74
req
static uv_connect_t req
Definition: test-connection-fail.c:30
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
gpr_sleep_until
GPRAPI void gpr_sleep_until(gpr_timespec until)
GPR_CLOCK_MONOTONIC
@ GPR_CLOCK_MONOTONIC
Definition: gpr_types.h:36
grpc::Status::OK
static const Status & OK
An OK pre-defined instance.
Definition: include/grpcpp/impl/codegen/status.h:113
grpc::CreateChannel
std::shared_ptr< Channel > CreateChannel(const grpc::string &target, const std::shared_ptr< ChannelCredentials > &creds)
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc_pick_unused_port_or_die
int grpc_pick_unused_port_or_die(void)
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
grpc::ClientContext
Definition: grpcpp/impl/codegen/client_context.h:195
test_config.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
credentials.h
gpr_time_add
GPRAPI gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:135
server
Definition: examples/python/async_streaming/server.py:1
main
int main(int argc, char **argv)
Definition: server_request_call_test.cc:159
absl::str_format_internal::LengthMod::t
@ t
grpc::TEST
TEST(CredentialsTest, StsCredentialsOptionsFromEnv)
Definition: cpp/client/credentials_test.cc:229
asyncio_get_stats.response
response
Definition: asyncio_get_stats.py:28
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
ok
bool ok
Definition: async_end2end_test.cc:197
grpc::InsecureServerCredentials
std::shared_ptr< ServerCredentials > InsecureServerCredentials()
Definition: insecure_server_credentials.cc:52
grpc::EXPECT_EQ
EXPECT_EQ(grpc::StatusCode::INVALID_ARGUMENT, status.error_code())
server.h
grpc::InsecureChannelCredentials
std::shared_ptr< ChannelCredentials > InsecureChannelCredentials()
Credentials for an unencrypted, unauthenticated channel.
Definition: cpp/client/insecure_credentials.cc:69
service
__attribute__((deprecated("Please use GRPCProtoMethod."))) @interface ProtoMethod NSString * service
Definition: ProtoMethod.h:25
grpc.StatusCode.DEADLINE_EXCEEDED
tuple DEADLINE_EXCEEDED
Definition: src/python/grpcio/grpc/__init__.py:264
thread
static uv_thread_t thread
Definition: test-async-null-cb.c:29
server_builder.h
cq
static grpc_completion_queue * cq
Definition: test/core/fling/client.cc:37
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
create_channel.h
gpr_time_from_seconds
GPRAPI gpr_timespec gpr_time_from_seconds(int64_t s, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:123


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:17