xref: /aosp_15_r20/external/grpc-grpc/test/core/surface/lame_client_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 <memory>
22 
23 #include "absl/status/status.h"
24 #include "gtest/gtest.h"
25 
26 #include <grpc/grpc.h>
27 #include <grpc/impl/propagation_bits.h>
28 #include <grpc/slice.h>
29 #include <grpc/status.h>
30 #include <grpc/support/alloc.h>
31 
32 #include "src/core/lib/channel/channel_fwd.h"
33 #include "src/core/lib/channel/channel_stack.h"
34 #include "src/core/lib/experiments/experiments.h"
35 #include "src/core/lib/gprpp/orphanable.h"
36 #include "src/core/lib/iomgr/closure.h"
37 #include "src/core/lib/iomgr/error.h"
38 #include "src/core/lib/iomgr/exec_ctx.h"
39 #include "src/core/lib/surface/channel.h"
40 #include "src/core/lib/transport/connectivity_state.h"
41 #include "src/core/lib/transport/transport.h"
42 #include "test/core/end2end/cq_verifier.h"
43 #include "test/core/util/test_config.h"
44 
45 class Watcher : public grpc_core::ConnectivityStateWatcherInterface {
46  public:
Notify(grpc_connectivity_state new_state,const absl::Status &)47   void Notify(grpc_connectivity_state new_state,
48               const absl::Status& /* status */) override {
49     ASSERT_EQ(new_state, GRPC_CHANNEL_SHUTDOWN);
50   }
51 };
52 
53 static grpc_closure transport_op_cb;
54 
do_nothing(void *,grpc_error_handle)55 static void do_nothing(void* /*arg*/, grpc_error_handle /*error*/) {}
56 
test_transport_op(grpc_channel * channel)57 void test_transport_op(grpc_channel* channel) {
58   grpc_core::ExecCtx exec_ctx;
59   grpc_transport_op* op = grpc_make_transport_op(nullptr);
60   op->start_connectivity_watch = grpc_core::MakeOrphanable<Watcher>();
61   grpc_channel_element* elem = grpc_channel_stack_element(
62       grpc_core::Channel::FromC(channel)->channel_stack(), 0);
63   elem->filter->start_transport_op(elem, op);
64 
65   GRPC_CLOSURE_INIT(&transport_op_cb, do_nothing, nullptr,
66                     grpc_schedule_on_exec_ctx);
67   op = grpc_make_transport_op(&transport_op_cb);
68   elem->filter->start_transport_op(elem, op);
69 }
70 
TEST(LameClientTest,MainTest)71 TEST(LameClientTest, MainTest) {
72   grpc_channel* chan;
73   grpc_call* call;
74   grpc_completion_queue* cq;
75   grpc_op ops[6];
76   grpc_op* op;
77   grpc_metadata_array initial_metadata_recv;
78   grpc_metadata_array trailing_metadata_recv;
79   grpc_status_code status;
80   grpc_call_error error;
81   grpc_slice details;
82   char* peer;
83 
84   grpc_init();
85 
86   grpc_metadata_array_init(&initial_metadata_recv);
87   grpc_metadata_array_init(&trailing_metadata_recv);
88 
89   const char* error_message = "Rpc sent on a lame channel.";
90   grpc_status_code error_code = GRPC_STATUS_ABORTED;
91   chan = grpc_lame_client_channel_create("lampoon:national", error_code,
92                                          error_message);
93   ASSERT_TRUE(chan);
94 
95   test_transport_op(chan);
96 
97   ASSERT_EQ(GRPC_CHANNEL_TRANSIENT_FAILURE,
98             grpc_channel_check_connectivity_state(chan, 0));
99 
100   cq = grpc_completion_queue_create_for_next(nullptr);
101 
102   grpc_slice host = grpc_slice_from_static_string("anywhere");
103   call =
104       grpc_channel_create_call(chan, nullptr, GRPC_PROPAGATE_DEFAULTS, cq,
105                                grpc_slice_from_static_string("/Foo"), &host,
106                                grpc_timeout_seconds_to_deadline(100), nullptr);
107   ASSERT_TRUE(call);
108   grpc_core::CqVerifier cqv(cq);
109 
110   memset(ops, 0, sizeof(ops));
111   op = ops;
112   op->op = GRPC_OP_SEND_INITIAL_METADATA;
113   op->data.send_initial_metadata.count = 0;
114   op->flags = 0;
115   op->reserved = nullptr;
116   op++;
117   op->op = GRPC_OP_RECV_INITIAL_METADATA;
118   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
119   op->flags = 0;
120   op->reserved = nullptr;
121   op++;
122   error = grpc_call_start_batch(call, ops, static_cast<size_t>(op - ops),
123                                 grpc_core::CqVerifier::tag(1), nullptr);
124   ASSERT_EQ(GRPC_CALL_OK, error);
125 
126   // Filter stack code considers this a failed to receive initial metadata
127   // result, where as promise based code interprets this as a trailers only
128   // failed request. Both are rational interpretations, so we accept the one
129   // that is implemented for each stack.
130   cqv.Expect(grpc_core::CqVerifier::tag(1),
131              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                                 grpc_core::CqVerifier::tag(2), nullptr);
145   ASSERT_EQ(GRPC_CALL_OK, error);
146 
147   // the call should immediately fail
148   cqv.Expect(grpc_core::CqVerifier::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 
main(int argc,char ** argv)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 }
174