lame_client_test.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/support/alloc.h>
  21. #include <grpc/support/log.h>
  22. #include "src/core/lib/channel/channel_stack.h"
  23. #include "src/core/lib/iomgr/closure.h"
  24. #include "src/core/lib/surface/channel.h"
  25. #include "src/core/lib/transport/transport.h"
  26. #include "test/core/end2end/cq_verifier.h"
  27. #include "test/core/util/test_config.h"
  28. class Watcher : public grpc_core::ConnectivityStateWatcherInterface {
  29. public:
  30. void Notify(grpc_connectivity_state new_state,
  31. const absl::Status& /* status */) override {
  32. GPR_ASSERT(new_state == GRPC_CHANNEL_SHUTDOWN);
  33. }
  34. };
  35. static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
  36. static grpc_closure transport_op_cb;
  37. static void do_nothing(void* /*arg*/, grpc_error_handle /*error*/) {}
  38. void test_transport_op(grpc_channel* channel) {
  39. grpc_core::ExecCtx exec_ctx;
  40. grpc_transport_op* op = grpc_make_transport_op(nullptr);
  41. op->start_connectivity_watch = grpc_core::MakeOrphanable<Watcher>();
  42. grpc_channel_element* elem =
  43. grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0);
  44. elem->filter->start_transport_op(elem, op);
  45. GRPC_CLOSURE_INIT(&transport_op_cb, do_nothing, nullptr,
  46. grpc_schedule_on_exec_ctx);
  47. op = grpc_make_transport_op(&transport_op_cb);
  48. elem->filter->start_transport_op(elem, op);
  49. }
  50. int main(int argc, char** argv) {
  51. grpc_channel* chan;
  52. grpc_call* call;
  53. grpc_completion_queue* cq;
  54. cq_verifier* cqv;
  55. grpc_op ops[6];
  56. grpc_op* op;
  57. grpc_metadata_array initial_metadata_recv;
  58. grpc_metadata_array trailing_metadata_recv;
  59. grpc_status_code status;
  60. grpc_call_error error;
  61. grpc_slice details;
  62. char* peer;
  63. grpc::testing::TestEnvironment env(argc, argv);
  64. grpc_init();
  65. grpc_metadata_array_init(&initial_metadata_recv);
  66. grpc_metadata_array_init(&trailing_metadata_recv);
  67. const char* error_message = "Rpc sent on a lame channel.";
  68. grpc_status_code error_code = GRPC_STATUS_ABORTED;
  69. chan = grpc_lame_client_channel_create("lampoon:national", error_code,
  70. error_message);
  71. GPR_ASSERT(chan);
  72. test_transport_op(chan);
  73. GPR_ASSERT(GRPC_CHANNEL_TRANSIENT_FAILURE ==
  74. grpc_channel_check_connectivity_state(chan, 0));
  75. cq = grpc_completion_queue_create_for_next(nullptr);
  76. grpc_slice host = grpc_slice_from_static_string("anywhere");
  77. call =
  78. grpc_channel_create_call(chan, nullptr, GRPC_PROPAGATE_DEFAULTS, cq,
  79. grpc_slice_from_static_string("/Foo"), &host,
  80. grpc_timeout_seconds_to_deadline(100), nullptr);
  81. GPR_ASSERT(call);
  82. cqv = cq_verifier_create(cq);
  83. memset(ops, 0, sizeof(ops));
  84. op = ops;
  85. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  86. op->data.send_initial_metadata.count = 0;
  87. op->flags = 0;
  88. op->reserved = nullptr;
  89. op++;
  90. op->op = GRPC_OP_RECV_INITIAL_METADATA;
  91. op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
  92. op->flags = 0;
  93. op->reserved = nullptr;
  94. op++;
  95. error = grpc_call_start_batch(call, ops, static_cast<size_t>(op - ops),
  96. tag(1), nullptr);
  97. GPR_ASSERT(GRPC_CALL_OK == error);
  98. /* the call should immediately fail */
  99. CQ_EXPECT_COMPLETION(cqv, tag(1), 0);
  100. cq_verify(cqv);
  101. memset(ops, 0, sizeof(ops));
  102. op = ops;
  103. op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  104. op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
  105. op->data.recv_status_on_client.status = &status;
  106. op->data.recv_status_on_client.status_details = &details;
  107. op->flags = 0;
  108. op->reserved = nullptr;
  109. op++;
  110. error = grpc_call_start_batch(call, ops, static_cast<size_t>(op - ops),
  111. tag(2), nullptr);
  112. GPR_ASSERT(GRPC_CALL_OK == error);
  113. /* the call should immediately fail */
  114. CQ_EXPECT_COMPLETION(cqv, tag(2), 1);
  115. cq_verify(cqv);
  116. peer = grpc_call_get_peer(call);
  117. GPR_ASSERT(strcmp(peer, "lampoon:national") == 0);
  118. gpr_free(peer);
  119. GPR_ASSERT(status == error_code);
  120. GPR_ASSERT(grpc_slice_str_cmp(details, error_message) == 0);
  121. grpc_call_unref(call);
  122. grpc_channel_destroy(chan);
  123. cq_verifier_destroy(cqv);
  124. grpc_completion_queue_destroy(cq);
  125. grpc_metadata_array_destroy(&initial_metadata_recv);
  126. grpc_metadata_array_destroy(&trailing_metadata_recv);
  127. grpc_slice_unref(details);
  128. grpc_shutdown();
  129. return 0;
  130. }