server_request_call_test.cc 4.9 KB

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