3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <grpc/grpc.h>
22 #include <grpc/support/alloc.h>
23 #include <grpc/support/log.h>
25 #include "src/core/lib/channel/channel_stack.h"
26 #include "src/core/lib/iomgr/closure.h"
27 #include "src/core/lib/surface/channel.h"
28 #include "src/core/lib/transport/transport.h"
29 #include "test/core/end2end/cq_verifier.h"
30 #include "test/core/util/test_config.h"
32 class Watcher : public grpc_core::ConnectivityStateWatcherInterface {
34 void Notify(grpc_connectivity_state new_state,
35 const absl::Status& /* status */) override {
36 GPR_ASSERT(new_state == GRPC_CHANNEL_SHUTDOWN);
40 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
42 static grpc_closure transport_op_cb;
44 static void do_nothing(void* /*arg*/, grpc_error_handle /*error*/) {}
46 void test_transport_op(grpc_channel* channel) {
47 grpc_core::ExecCtx exec_ctx;
48 grpc_transport_op* op = grpc_make_transport_op(nullptr);
49 op->start_connectivity_watch = grpc_core::MakeOrphanable<Watcher>();
50 grpc_channel_element* elem =
51 grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0);
52 elem->filter->start_transport_op(elem, op);
54 GRPC_CLOSURE_INIT(&transport_op_cb, do_nothing, nullptr,
55 grpc_schedule_on_exec_ctx);
56 op = grpc_make_transport_op(&transport_op_cb);
57 elem->filter->start_transport_op(elem, op);
60 int main(int argc, char** argv) {
63 grpc_completion_queue* cq;
67 grpc_metadata_array initial_metadata_recv;
68 grpc_metadata_array trailing_metadata_recv;
69 grpc_status_code status;
70 grpc_call_error error;
74 grpc::testing::TestEnvironment env(argc, argv);
77 grpc_metadata_array_init(&initial_metadata_recv);
78 grpc_metadata_array_init(&trailing_metadata_recv);
80 const char* error_message = "Rpc sent on a lame channel.";
81 grpc_status_code error_code = GRPC_STATUS_ABORTED;
82 chan = grpc_lame_client_channel_create("lampoon:national", error_code,
86 test_transport_op(chan);
88 GPR_ASSERT(GRPC_CHANNEL_SHUTDOWN ==
89 grpc_channel_check_connectivity_state(chan, 0));
91 cq = grpc_completion_queue_create_for_next(nullptr);
93 grpc_slice host = grpc_slice_from_static_string("anywhere");
95 grpc_channel_create_call(chan, nullptr, GRPC_PROPAGATE_DEFAULTS, cq,
96 grpc_slice_from_static_string("/Foo"), &host,
97 grpc_timeout_seconds_to_deadline(100), nullptr);
99 cqv = cq_verifier_create(cq);
101 memset(ops, 0, sizeof(ops));
103 op->op = GRPC_OP_SEND_INITIAL_METADATA;
104 op->data.send_initial_metadata.count = 0;
106 op->reserved = nullptr;
108 op->op = GRPC_OP_RECV_INITIAL_METADATA;
109 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
111 op->reserved = nullptr;
113 error = grpc_call_start_batch(call, ops, static_cast<size_t>(op - ops),
115 GPR_ASSERT(GRPC_CALL_OK == error);
117 /* the call should immediately fail */
118 CQ_EXPECT_COMPLETION(cqv, tag(1), 0);
121 memset(ops, 0, sizeof(ops));
123 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
124 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
125 op->data.recv_status_on_client.status = &status;
126 op->data.recv_status_on_client.status_details = &details;
128 op->reserved = nullptr;
130 error = grpc_call_start_batch(call, ops, static_cast<size_t>(op - ops),
132 GPR_ASSERT(GRPC_CALL_OK == error);
134 /* the call should immediately fail */
135 CQ_EXPECT_COMPLETION(cqv, tag(2), 1);
138 peer = grpc_call_get_peer(call);
139 GPR_ASSERT(strcmp(peer, "lampoon:national") == 0);
142 GPR_ASSERT(status == error_code);
143 GPR_ASSERT(grpc_slice_str_cmp(details, error_message) == 0);
145 grpc_call_unref(call);
146 grpc_channel_destroy(chan);
147 cq_verifier_destroy(cqv);
148 grpc_completion_queue_destroy(cq);
150 grpc_metadata_array_destroy(&initial_metadata_recv);
151 grpc_metadata_array_destroy(&trailing_metadata_recv);
152 grpc_slice_unref(details);