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