connection_refused_test.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. *
  3. * Copyright 2016 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 <string.h>
  19. #include <grpc/grpc.h>
  20. #include <grpc/grpc_security.h>
  21. #include <grpc/support/alloc.h>
  22. #include <grpc/support/log.h>
  23. #include <grpc/support/string_util.h>
  24. #include "src/core/lib/channel/channel_args.h"
  25. #include "src/core/lib/gprpp/host_port.h"
  26. #include "src/core/lib/iomgr/exec_ctx.h"
  27. #include "src/core/lib/slice/slice_internal.h"
  28. #include "test/core/end2end/cq_verifier.h"
  29. #include "test/core/util/port.h"
  30. #include "test/core/util/test_config.h"
  31. static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
  32. static void run_test(bool wait_for_ready, bool use_service_config) {
  33. grpc_channel* chan;
  34. grpc_call* call;
  35. grpc_completion_queue* cq;
  36. cq_verifier* cqv;
  37. grpc_op ops[6];
  38. grpc_op* op;
  39. grpc_metadata_array trailing_metadata_recv;
  40. grpc_status_code status;
  41. grpc_slice details;
  42. gpr_log(GPR_INFO, "TEST: wait_for_ready=%d use_service_config=%d",
  43. wait_for_ready, use_service_config);
  44. grpc_init();
  45. grpc_metadata_array_init(&trailing_metadata_recv);
  46. cq = grpc_completion_queue_create_for_next(nullptr);
  47. cqv = cq_verifier_create(cq);
  48. /* if using service config, create channel args */
  49. grpc_channel_args* args = nullptr;
  50. if (use_service_config) {
  51. GPR_ASSERT(wait_for_ready);
  52. grpc_arg arg;
  53. arg.type = GRPC_ARG_STRING;
  54. arg.key = const_cast<char*>(GRPC_ARG_SERVICE_CONFIG);
  55. arg.value.string = const_cast<char*>(
  56. "{\n"
  57. " \"methodConfig\": [ {\n"
  58. " \"name\": [\n"
  59. " { \"service\": \"service\", \"method\": \"method\" }\n"
  60. " ],\n"
  61. " \"waitForReady\": true\n"
  62. " } ]\n"
  63. "}");
  64. args = grpc_channel_args_copy_and_add(args, &arg, 1);
  65. }
  66. /* create a call, channel to a port which will refuse connection */
  67. int port = grpc_pick_unused_port_or_die();
  68. std::string addr = grpc_core::JoinHostPort("127.0.0.1", port);
  69. gpr_log(GPR_INFO, "server: %s", addr.c_str());
  70. grpc_channel_credentials* creds = grpc_insecure_credentials_create();
  71. chan = grpc_channel_create(addr.c_str(), creds, args);
  72. grpc_channel_credentials_release(creds);
  73. grpc_slice host = grpc_slice_from_static_string("nonexistant");
  74. gpr_timespec deadline = grpc_timeout_seconds_to_deadline(2);
  75. call =
  76. grpc_channel_create_call(chan, nullptr, GRPC_PROPAGATE_DEFAULTS, cq,
  77. grpc_slice_from_static_string("/service/method"),
  78. &host, deadline, nullptr);
  79. memset(ops, 0, sizeof(ops));
  80. op = ops;
  81. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  82. op->data.send_initial_metadata.count = 0;
  83. op->flags = (wait_for_ready && !use_service_config)
  84. ? GRPC_INITIAL_METADATA_WAIT_FOR_READY
  85. : 0;
  86. op->reserved = nullptr;
  87. op++;
  88. op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  89. op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
  90. op->data.recv_status_on_client.status = &status;
  91. op->data.recv_status_on_client.status_details = &details;
  92. op->flags = 0;
  93. op->reserved = nullptr;
  94. op++;
  95. GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(call, ops,
  96. (size_t)(op - ops), tag(1),
  97. nullptr));
  98. /* verify that all tags get completed */
  99. CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
  100. cq_verify(cqv);
  101. if (wait_for_ready) {
  102. GPR_ASSERT(status == GRPC_STATUS_DEADLINE_EXCEEDED);
  103. } else {
  104. GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE);
  105. }
  106. grpc_completion_queue_shutdown(cq);
  107. while (grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
  108. nullptr)
  109. .type != GRPC_QUEUE_SHUTDOWN) {
  110. }
  111. grpc_completion_queue_destroy(cq);
  112. grpc_call_unref(call);
  113. grpc_channel_destroy(chan);
  114. cq_verifier_destroy(cqv);
  115. grpc_slice_unref(details);
  116. grpc_metadata_array_destroy(&trailing_metadata_recv);
  117. {
  118. grpc_core::ExecCtx exec_ctx;
  119. if (args != nullptr) grpc_channel_args_destroy(args);
  120. }
  121. grpc_shutdown();
  122. }
  123. int main(int argc, char** argv) {
  124. grpc::testing::TestEnvironment env(argc, argv);
  125. run_test(false /* wait_for_ready */, false /* use_service_config */);
  126. run_test(true /* wait_for_ready */, false /* use_service_config */);
  127. run_test(true /* wait_for_ready */, true /* use_service_config */);
  128. return 0;
  129. }