| 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 |
|
| 19 |
#include <stdint.h> |
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#include "absl/status/status.h" |
| 23 |
#include "gtest/gtest.h" |
| 24 |
|
| 25 |
#include <grpc/grpc.h> |
| 26 |
#include <grpc/impl/codegen/propagation_bits.h> |
| 27 |
#include <grpc/slice.h> |
| 28 |
#include <grpc/status.h> |
| 29 |
#include <grpc/support/alloc.h> |
| 30 |
|
| 31 |
#include "src/core/lib/channel/channel_fwd.h" |
| 32 |
#include "src/core/lib/channel/channel_stack.h" |
| 33 |
#include "src/core/lib/experiments/experiments.h" |
| 34 |
#include "src/core/lib/gprpp/orphanable.h" |
| 35 |
#include "src/core/lib/iomgr/closure.h" |
| 36 |
#include "src/core/lib/iomgr/error.h" |
| 37 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 38 |
#include "src/core/lib/surface/channel.h" |
| 39 |
#include "src/core/lib/transport/connectivity_state.h" |
| 40 |
#include "src/core/lib/transport/transport.h" |
| 41 |
#include "test/core/end2end/cq_verifier.h" |
| 42 |
#include "test/core/util/test_config.h" |
| 43 |
|
| 44 |
class Watcher : public grpc_core::ConnectivityStateWatcherInterface { |
| 45 |
public: |
| 46 |
void Notify(grpc_connectivity_state new_state, |
| 47 |
const absl::Status& /* status */) override { |
| 48 |
ASSERT_EQ(new_state, GRPC_CHANNEL_SHUTDOWN); |
| 49 |
} |
| 50 |
}; |
| 51 |
|
| 52 |
static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); } |
| 53 |
|
| 54 |
static grpc_closure transport_op_cb; |
| 55 |
|
| 56 |
static void do_nothing(void* /*arg*/, grpc_error_handle /*error*/) {} |
| 57 |
|
| 58 |
void test_transport_op(grpc_channel* channel) { |
| 59 |
grpc_core::ExecCtx exec_ctx; |
| 60 |
grpc_transport_op* op = grpc_make_transport_op(nullptr); |
| 61 |
op->start_connectivity_watch = grpc_core::MakeOrphanable<Watcher>(); |
| 62 |
grpc_channel_element* elem = |
| 63 |
grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0); |
| 64 |
elem->filter->start_transport_op(elem, op); |
| 65 |
|
| 66 |
GRPC_CLOSURE_INIT(&transport_op_cb, do_nothing, nullptr, |
| 67 |
grpc_schedule_on_exec_ctx); |
| 68 |
op = grpc_make_transport_op(&transport_op_cb); |
| 69 |
elem->filter->start_transport_op(elem, op); |
| 70 |
} |
| 71 |
|
| 72 |
TEST(LameClientTest, MainTest) { |
| 73 |
grpc_channel* chan; |
| 74 |
grpc_call* call; |
| 75 |
grpc_completion_queue* cq; |
| 76 |
grpc_op ops[6]; |
| 77 |
grpc_op* op; |
| 78 |
grpc_metadata_array initial_metadata_recv; |
| 79 |
grpc_metadata_array trailing_metadata_recv; |
| 80 |
grpc_status_code status; |
| 81 |
grpc_call_error error; |
| 82 |
grpc_slice details; |
| 83 |
char* peer; |
| 84 |
|
| 85 |
grpc_init(); |
| 86 |
|
| 87 |
grpc_metadata_array_init(&initial_metadata_recv); |
| 88 |
grpc_metadata_array_init(&trailing_metadata_recv); |
| 89 |
|
| 90 |
const char* error_message = "Rpc sent on a lame channel."; |
| 91 |
grpc_status_code error_code = GRPC_STATUS_ABORTED; |
| 92 |
chan = grpc_lame_client_channel_create("lampoon:national", error_code, |
| 93 |
error_message); |
| 94 |
ASSERT_TRUE(chan); |
| 95 |
|
| 96 |
test_transport_op(chan); |
| 97 |
|
| 98 |
ASSERT_EQ(GRPC_CHANNEL_TRANSIENT_FAILURE, |
| 99 |
grpc_channel_check_connectivity_state(chan, 0)); |
| 100 |
|
| 101 |
cq = grpc_completion_queue_create_for_next(nullptr); |
| 102 |
|
| 103 |
grpc_slice host = grpc_slice_from_static_string("anywhere"); |
| 104 |
call = |
| 105 |
grpc_channel_create_call(chan, nullptr, GRPC_PROPAGATE_DEFAULTS, cq, |
| 106 |
grpc_slice_from_static_string("/Foo"), &host, |
| 107 |
grpc_timeout_seconds_to_deadline(100), nullptr); |
| 108 |
ASSERT_TRUE(call); |
| 109 |
grpc_core::CqVerifier cqv(cq); |
| 110 |
|
| 111 |
memset(ops, 0, sizeof(ops)); |
| 112 |
op = ops; |
| 113 |
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 114 |
op->data.send_initial_metadata.count = 0; |
| 115 |
op->flags = 0; |
| 116 |
op->reserved = nullptr; |
| 117 |
op++; |
| 118 |
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 119 |
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
| 120 |
op->flags = 0; |
| 121 |
op->reserved = nullptr; |
| 122 |
op++; |
| 123 |
error = grpc_call_start_batch(call, ops, static_cast<size_t>(op - ops), |
| 124 |
tag(1), nullptr); |
| 125 |
ASSERT_EQ(GRPC_CALL_OK, error); |
| 126 |
|
| 127 |
// Filter stack code considers this a failed to receive initial metadata |
| 128 |
// result, where as promise based code interprets this as a trailers only |
| 129 |
// failed request. Both are rational interpretations, so we accept the one |
| 130 |
// that is implemented for each stack. |
| 131 |
cqv.Expect(tag(1), grpc_core::IsPromiseBasedClientCallEnabled()); |
| 132 |
cqv.Verify(); |
| 133 |
|
| 134 |
memset(ops, 0, sizeof(ops)); |
| 135 |
op = ops; |
| 136 |
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 137 |
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 138 |
op->data.recv_status_on_client.status = &status; |
| 139 |
op->data.recv_status_on_client.status_details = &details; |
| 140 |
op->flags = 0; |
| 141 |
op->reserved = nullptr; |
| 142 |
op++; |
| 143 |
error = grpc_call_start_batch(call, ops, static_cast<size_t>(op - ops), |
| 144 |
tag(2), nullptr); |
| 145 |
ASSERT_EQ(GRPC_CALL_OK, error); |
| 146 |
|
| 147 |
/* the call should immediately fail */ |
| 148 |
cqv.Expect(tag(2), true); |
| 149 |
cqv.Verify(); |
| 150 |
|
| 151 |
peer = grpc_call_get_peer(call); |
| 152 |
ASSERT_STREQ(peer, "lampoon:national"); |
| 153 |
gpr_free(peer); |
| 154 |
|
| 155 |
ASSERT_EQ(status, error_code); |
| 156 |
ASSERT_EQ(grpc_slice_str_cmp(details, error_message), 0); |
| 157 |
|
| 158 |
grpc_call_unref(call); |
| 159 |
grpc_channel_destroy(chan); |
| 160 |
grpc_completion_queue_destroy(cq); |
| 161 |
|
| 162 |
grpc_metadata_array_destroy(&initial_metadata_recv); |
| 163 |
grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 164 |
grpc_slice_unref(details); |
| 165 |
|
| 166 |
grpc_shutdown(); |
| 167 |
} |
| 168 |
|
| 169 |
int main(int argc, char** argv) { |
| 170 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 171 |
::testing::InitGoogleTest(&argc, argv); |
| 172 |
return RUN_ALL_TESTS(); |
| 173 |
} |