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