no_server_test.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #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 "src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h"
  24. #include "src/core/lib/iomgr/exec_ctx.h"
  25. #include "test/core/end2end/cq_verifier.h"
  26. #include "test/core/util/test_config.h"
  27. static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
  28. void run_test(bool wait_for_ready) {
  29. gpr_log(GPR_INFO, "TEST: wait_for_ready=%d", wait_for_ready);
  30. grpc_init();
  31. grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
  32. cq_verifier* cqv = cq_verifier_create(cq);
  33. grpc_core::RefCountedPtr<grpc_core::FakeResolverResponseGenerator>
  34. response_generator =
  35. grpc_core::MakeRefCounted<grpc_core::FakeResolverResponseGenerator>();
  36. grpc_arg arg = grpc_core::FakeResolverResponseGenerator::MakeChannelArg(
  37. response_generator.get());
  38. grpc_channel_args args = {1, &arg};
  39. /* create a call, channel to a non existant server */
  40. grpc_channel_credentials* creds = grpc_insecure_credentials_create();
  41. grpc_channel* chan = grpc_channel_create("fake:nonexistant", creds, &args);
  42. grpc_channel_credentials_release(creds);
  43. gpr_timespec deadline = grpc_timeout_seconds_to_deadline(2);
  44. grpc_call* call = grpc_channel_create_call(
  45. chan, nullptr, GRPC_PROPAGATE_DEFAULTS, cq,
  46. grpc_slice_from_static_string("/Foo"), nullptr, deadline, nullptr);
  47. grpc_op ops[6];
  48. memset(ops, 0, sizeof(ops));
  49. grpc_op* op = ops;
  50. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  51. op->data.send_initial_metadata.count = 0;
  52. op->flags = wait_for_ready ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 0;
  53. op->reserved = nullptr;
  54. op++;
  55. grpc_metadata_array trailing_metadata_recv;
  56. grpc_metadata_array_init(&trailing_metadata_recv);
  57. grpc_status_code status;
  58. grpc_slice details;
  59. op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  60. op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
  61. op->data.recv_status_on_client.status = &status;
  62. op->data.recv_status_on_client.status_details = &details;
  63. op->flags = 0;
  64. op->reserved = nullptr;
  65. op++;
  66. GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(call, ops,
  67. (size_t)(op - ops), tag(1),
  68. nullptr));
  69. {
  70. grpc_core::ExecCtx exec_ctx;
  71. response_generator->SetFailure();
  72. }
  73. /* verify that all tags get completed */
  74. CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
  75. cq_verify(cqv);
  76. gpr_log(GPR_INFO, "call status: %d", status);
  77. if (wait_for_ready) {
  78. GPR_ASSERT(status == GRPC_STATUS_DEADLINE_EXCEEDED);
  79. } else {
  80. GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE);
  81. }
  82. grpc_slice_unref(details);
  83. grpc_metadata_array_destroy(&trailing_metadata_recv);
  84. grpc_completion_queue_shutdown(cq);
  85. while (grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
  86. nullptr)
  87. .type != GRPC_QUEUE_SHUTDOWN) {
  88. }
  89. grpc_completion_queue_destroy(cq);
  90. grpc_call_unref(call);
  91. grpc_channel_destroy(chan);
  92. cq_verifier_destroy(cqv);
  93. grpc_shutdown();
  94. }
  95. int main(int argc, char** argv) {
  96. grpc::testing::TestEnvironment env(argc, argv);
  97. run_test(true /* wait_for_ready */);
  98. run_test(false /* wait_for_ready */);
  99. return 0;
  100. }