Debian Codegraph @

test/core/end2end/tests/simple_request.cc

raw
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 <algorithm>
23 #include <memory>
24 #include <string>
25
26 #include <grpc/grpc.h>
27 #include <grpc/impl/codegen/propagation_bits.h>
28 #include <grpc/slice.h>
29 #include <grpc/status.h>
30 #include <grpc/support/alloc.h>
31 #include <grpc/support/log.h>
32
33 #include "src/core/lib/debug/stats.h"
34 #include "src/core/lib/debug/stats_data.h"
35 #include "test/core/end2end/cq_verifier.h"
36 #include "test/core/end2end/end2end_tests.h"
37 #include "test/core/util/test_config.h"
38
39 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
40
41 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
42 const char* test_name,
43 grpc_channel_args* client_args,
44 grpc_channel_args* server_args) {
45 grpc_end2end_test_fixture f;
46 gpr_log(GPR_INFO, "%s", std::string(100, '*').c_str());
47 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
48 f = config.create_fixture(client_args, server_args);
49 config.init_server(&f, server_args);
50 config.init_client(&f, client_args);
51 return f;
52 }
53
54 static gpr_timespec n_seconds_from_now(int n) {
55 return grpc_timeout_seconds_to_deadline(n);
56 }
57
58 static gpr_timespec five_seconds_from_now(void) {
59 return n_seconds_from_now(5);
60 }
61
62 static void drain_cq(grpc_completion_queue* cq) {
63 grpc_event ev;
64 do {
65 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
66 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
67 }
68
69 static void shutdown_server(grpc_end2end_test_fixture* f) {
70 if (!f->server) return;
71 grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
72 grpc_event ev;
73 do {
74 ev = grpc_completion_queue_next(f->cq, grpc_timeout_seconds_to_deadline(5),
75 nullptr);
76 } while (ev.type != GRPC_OP_COMPLETE || ev.tag != tag(1000));
77 grpc_server_destroy(f->server);
78 f->server = nullptr;
79 }
80
81 static void shutdown_client(grpc_end2end_test_fixture* f) {
82 if (!f->client) return;
83 grpc_channel_destroy(f->client);
84 f->client = nullptr;
85 }
86
87 static void end_test(grpc_end2end_test_fixture* f) {
88 shutdown_server(f);
89 shutdown_client(f);
90
91 grpc_completion_queue_shutdown(f->cq);
92 drain_cq(f->cq);
93 grpc_completion_queue_destroy(f->cq);
94 }
95
96 static void check_peer(char* peer_name) {
97 // If the peer name is a uds path, then check if it is filled
98 if (strncmp(peer_name, "unix:/", strlen("unix:/")) == 0) {
99 GPR_ASSERT(strncmp(peer_name, "unix:/tmp/grpc_fullstack_test.",
100 strlen("unix:/tmp/grpc_fullstack_test.")) == 0);
101 }
102 }
103
104 static void simple_request_body(grpc_end2end_test_config config,
105 grpc_end2end_test_fixture f) {
106 grpc_call* c;
107 grpc_call* s;
108 grpc_core::CqVerifier cqv(f.cq);
109 grpc_op ops[6];
110 grpc_op* op;
111 grpc_metadata_array initial_metadata_recv;
112 grpc_metadata_array trailing_metadata_recv;
113 grpc_metadata_array request_metadata_recv;
114 grpc_call_details call_details;
115 grpc_status_code status;
116 const char* error_string;
117 grpc_call_error error;
118 grpc_slice details;
119 int was_cancelled = 2;
120 char* peer;
121
122 auto before = grpc_core::global_stats().Collect();
123
124 gpr_timespec deadline = five_seconds_from_now();
125 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
126 grpc_slice_from_static_string("/foo"), nullptr,
127 deadline, nullptr);
128 GPR_ASSERT(c);
129
130 peer = grpc_call_get_peer(c);
131 GPR_ASSERT(peer != nullptr);
132 gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer);
133 gpr_free(peer);
134
135 grpc_metadata_array_init(&initial_metadata_recv);
136 grpc_metadata_array_init(&trailing_metadata_recv);
137 grpc_metadata_array_init(&request_metadata_recv);
138 grpc_call_details_init(&call_details);
139
140 memset(ops, 0, sizeof(ops));
141 op = ops;
142 op->op = GRPC_OP_SEND_INITIAL_METADATA;
143 op->data.send_initial_metadata.count = 0;
144 op->flags = 0;
145 op->reserved = nullptr;
146 op++;
147 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
148 op->flags = 0;
149 op->reserved = nullptr;
150 op++;
151 op->op = GRPC_OP_RECV_INITIAL_METADATA;
152 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
153 op->flags = 0;
154 op->reserved = nullptr;
155 op++;
156 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
157 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
158 op->data.recv_status_on_client.status = &status;
159 op->data.recv_status_on_client.status_details = &details;
160 op->data.recv_status_on_client.error_string = &error_string;
161 op->flags = 0;
162 op->reserved = nullptr;
163 op++;
164 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
165 nullptr);
166 GPR_ASSERT(GRPC_CALL_OK == error);
167
168 error =
169 grpc_server_request_call(f.server, &s, &call_details,
170 &request_metadata_recv, f.cq, f.cq, tag(101));
171 GPR_ASSERT(GRPC_CALL_OK == error);
172 cqv.Expect(tag(101), true);
173 cqv.Verify();
174
175 peer = grpc_call_get_peer(s);
176 GPR_ASSERT(peer != nullptr);
177 gpr_log(GPR_DEBUG, "server_peer=%s", peer);
178 check_peer(peer);
179 gpr_free(peer);
180 peer = grpc_call_get_peer(c);
181 GPR_ASSERT(peer != nullptr);
182 gpr_log(GPR_DEBUG, "client_peer=%s", peer);
183 check_peer(peer);
184 gpr_free(peer);
185
186 memset(ops, 0, sizeof(ops));
187 op = ops;
188 op->op = GRPC_OP_SEND_INITIAL_METADATA;
189 op->data.send_initial_metadata.count = 0;
190 op->flags = 0;
191 op->reserved = nullptr;
192 op++;
193 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
194 op->data.send_status_from_server.trailing_metadata_count = 0;
195 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
196 grpc_slice status_details = grpc_slice_from_static_string("xyz");
197 op->data.send_status_from_server.status_details = &status_details;
198 op->flags = 0;
199 op->reserved = nullptr;
200 op++;
201 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
202 op->data.recv_close_on_server.cancelled = &was_cancelled;
203 op->flags = 0;
204 op->reserved = nullptr;
205 op++;
206 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
207 nullptr);
208 GPR_ASSERT(GRPC_CALL_OK == error);
209
210 cqv.Expect(tag(102), true);
211 cqv.Expect(tag(1), true);
212 cqv.Verify();
213
214 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
215 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
216 // the following sanity check makes sure that the requested error string is
217 // correctly populated by the core. It looks for certain substrings that are
218 // not likely to change much. Some parts of the error, like time created,
219 // obviously are not checked.
220 GPR_ASSERT(nullptr != strstr(error_string, "xyz"));
221 GPR_ASSERT(nullptr != strstr(error_string, "Error received from peer"));
222 GPR_ASSERT(nullptr != strstr(error_string, "grpc_message"));
223 GPR_ASSERT(nullptr != strstr(error_string, "grpc_status"));
224 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
225 GPR_ASSERT(was_cancelled == 0);
226
227 grpc_slice_unref(details);
228 gpr_free(const_cast<char*>(error_string));
229 grpc_metadata_array_destroy(&initial_metadata_recv);
230 grpc_metadata_array_destroy(&trailing_metadata_recv);
231 grpc_metadata_array_destroy(&request_metadata_recv);
232 grpc_call_details_destroy(&call_details);
233
234 grpc_call_unref(c);
235 grpc_call_unref(s);
236
237 uint64_t expected_calls = 1;
238 if (config.feature_mask & FEATURE_MASK_SUPPORTS_REQUEST_PROXYING) {
239 expected_calls *= 2;
240 }
241
242 auto after = grpc_core::global_stats().Collect();
243
244 gpr_log(GPR_DEBUG, "%s", grpc_core::StatsAsJson(after.get()).c_str());
245
246 GPR_ASSERT(after->client_calls_created - before->client_calls_created ==
247 expected_calls);
248 GPR_ASSERT(after->server_calls_created - before->server_calls_created ==
249 expected_calls);
250 }
251
252 static void test_invoke_simple_request(grpc_end2end_test_config config) {
253 grpc_end2end_test_fixture f;
254
255 f = begin_test(config, "test_invoke_simple_request", nullptr, nullptr);
256 simple_request_body(config, f);
257 end_test(&f);
258 config.tear_down_data(&f);
259 }
260
261 static void test_invoke_10_simple_requests(grpc_end2end_test_config config) {
262 int i;
263 grpc_end2end_test_fixture f =
264 begin_test(config, "test_invoke_10_simple_requests", nullptr, nullptr);
265 for (i = 0; i < 10; i++) {
266 simple_request_body(config, f);
267 gpr_log(GPR_INFO, "Running test: Passed simple request %d", i);
268 }
269 end_test(&f);
270 config.tear_down_data(&f);
271 }
272
273 void simple_request(grpc_end2end_test_config config) {
274 int i;
275 for (i = 0; i < 10; i++) {
276 test_invoke_simple_request(config);
277 }
278 test_invoke_10_simple_requests(config);
279 }
280
281 void simple_request_pre_init(void) {}