Debian Codegraph @

test/core/end2end/inproc_callback_test.cc

raw
1 /*
2 *
3 * Copyright 2018 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 <inttypes.h>
20 #include <string.h>
21
22 #include <grpc/grpc.h>
23 #include <grpc/impl/codegen/propagation_bits.h>
24 #include <grpc/slice.h>
25 #include <grpc/status.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/sync.h>
29 #include <grpc/support/time.h>
30
31 #include "src/core/ext/transport/inproc/inproc_transport.h"
32 #include "test/core/end2end/end2end_tests.h"
33 #include "test/core/util/test_config.h"
34
35 typedef struct inproc_fixture_data {
36 bool phony; // reserved for future expansion. Struct can't be empty
37 } inproc_fixture_data;
38
39 namespace {
40 template <typename F>
41 class CQDeletingCallback : public grpc_completion_queue_functor {
42 public:
43 explicit CQDeletingCallback(F f) : func_(f) {
44 functor_run = &CQDeletingCallback::Run;
45 inlineable = false;
46 }
47 ~CQDeletingCallback() {}
48 static void Run(grpc_completion_queue_functor* cb, int ok) {
49 auto* callback = static_cast<CQDeletingCallback*>(cb);
50 callback->func_(static_cast<bool>(ok));
51 delete callback;
52 }
53
54 private:
55 F func_;
56 };
57
58 template <typename F>
59 grpc_completion_queue_functor* NewDeletingCallback(F f) {
60 return new CQDeletingCallback<F>(f);
61 }
62
63 class ShutdownCallback : public grpc_completion_queue_functor {
64 public:
65 ShutdownCallback() : done_(false) {
66 functor_run = &ShutdownCallback::StaticRun;
67 inlineable = false;
68 gpr_mu_init(&mu_);
69 gpr_cv_init(&cv_);
70 }
71 ~ShutdownCallback() {
72 gpr_mu_destroy(&mu_);
73 gpr_cv_destroy(&cv_);
74 }
75 static void StaticRun(grpc_completion_queue_functor* cb, int ok) {
76 auto* callback = static_cast<ShutdownCallback*>(cb);
77 callback->Run(static_cast<bool>(ok));
78 }
79 void Run(bool /*ok*/) {
80 gpr_log(GPR_DEBUG, "CQ shutdown notification invoked");
81 gpr_mu_lock(&mu_);
82 done_ = true;
83 gpr_cv_broadcast(&cv_);
84 gpr_mu_unlock(&mu_);
85 }
86 // The Wait function waits for a specified amount of
87 // time for the completion of the shutdown and returns
88 // whether it was successfully shut down
89 bool Wait(gpr_timespec deadline) {
90 gpr_mu_lock(&mu_);
91 while (!done_ && !gpr_cv_wait(&cv_, &mu_, deadline)) {
92 }
93 bool ret = done_;
94 gpr_mu_unlock(&mu_);
95 return ret;
96 }
97
98 private:
99 bool done_;
100 gpr_mu mu_;
101 gpr_cv cv_;
102 };
103
104 ShutdownCallback* g_shutdown_callback;
105 } // namespace
106
107 // The following global structure is the tag collection. It holds
108 // all information related to tags expected and tags received
109 // during the execution, with each callback setting a tag.
110 // The tag sets are implemented and checked using arrays and
111 // linear lookups (rather than maps) so that this test doesn't
112 // need the C++ standard library.
113 static gpr_mu tags_mu;
114 static gpr_cv tags_cv;
115 const size_t kAvailableTags = 4;
116 bool tags[kAvailableTags];
117 bool tags_valid[kAvailableTags];
118 bool tags_expected[kAvailableTags];
119 bool tags_needed[kAvailableTags];
120
121 // Mark that a tag is expected; this function must be executed in the
122 // main thread only while there are no other threads altering the
123 // expectation set (e.g., by calling expect_tag or verify_tags)
124 static void expect_tag(intptr_t tag, bool ok) {
125 size_t idx = static_cast<size_t>(tag);
126 GPR_ASSERT(idx < kAvailableTags);
127 tags_needed[idx] = true;
128 tags_expected[idx] = ok;
129 }
130
131 // Check that the expected tags have reached, within a certain
132 // deadline. This must also be executed only on the main thread while
133 // there are no other threads altering the expectation set (e.g., by
134 // calling expect_tag or verify_tags). The tag verifier doesn't have
135 // to drive the CQ at all (unlike the next-based end2end tests)
136 // because the tags will get set when the callbacks are executed,
137 // which happens when a particular batch related to a callback is
138 // complete.
139 static void verify_tags(gpr_timespec deadline) {
140 bool done = false;
141
142 gpr_mu_lock(&tags_mu);
143 while (!done) {
144 done = gpr_time_cmp(gpr_now(GPR_CLOCK_MONOTONIC), deadline) > 0;
145 for (size_t i = 0; i < kAvailableTags; i++) {
146 if (tags_needed[i]) {
147 if (tags_valid[i]) {
148 gpr_log(GPR_DEBUG, "Verifying tag %d", static_cast<int>(i));
149 if (tags[i] != tags_expected[i]) {
150 gpr_log(GPR_ERROR, "Got wrong result (%d instead of %d) for tag %d",
151 tags[i], tags_expected[i], static_cast<int>(i));
152 GPR_ASSERT(false);
153 }
154 tags_valid[i] = false;
155 tags_needed[i] = false;
156 } else if (done) {
157 gpr_log(GPR_ERROR, "Didn't get tag %d", static_cast<int>(i));
158 GPR_ASSERT(false);
159 }
160 }
161 }
162 bool empty = true;
163 for (size_t i = 0; i < kAvailableTags; i++) {
164 if (tags_needed[i]) {
165 empty = false;
166 }
167 }
168 done = done || empty;
169 if (done) {
170 for (size_t i = 0; i < kAvailableTags; i++) {
171 if (tags_valid[i]) {
172 gpr_log(GPR_ERROR, "Got unexpected tag %d and result %d",
173 static_cast<int>(i), tags[i]);
174 GPR_ASSERT(false);
175 }
176 tags_valid[i] = false;
177 }
178 } else {
179 gpr_cv_wait(&tags_cv, &tags_mu, deadline);
180 }
181 }
182 gpr_mu_unlock(&tags_mu);
183 }
184
185 // This function creates a callback functor that emits the
186 // desired tag into the global tag set
187 static grpc_completion_queue_functor* tag(intptr_t t) {
188 auto func = [t](bool ok) {
189 gpr_mu_lock(&tags_mu);
190 gpr_log(GPR_DEBUG, "Completing operation %" PRIdPTR, t);
191 bool was_empty = true;
192 for (size_t i = 0; i < kAvailableTags; i++) {
193 if (tags_valid[i]) {
194 was_empty = false;
195 }
196 }
197 size_t idx = static_cast<size_t>(t);
198 tags[idx] = ok;
199 tags_valid[idx] = true;
200 if (was_empty) {
201 gpr_cv_signal(&tags_cv);
202 }
203 gpr_mu_unlock(&tags_mu);
204 };
205 auto cb = NewDeletingCallback(func);
206 return cb;
207 }
208
209 static grpc_end2end_test_fixture inproc_create_fixture(
210 const grpc_channel_args* /*client_args*/,
211 const grpc_channel_args* /*server_args*/) {
212 grpc_end2end_test_fixture f;
213 inproc_fixture_data* ffd = static_cast<inproc_fixture_data*>(
214 gpr_malloc(sizeof(inproc_fixture_data)));
215 memset(&f, 0, sizeof(f));
216
217 f.fixture_data = ffd;
218 g_shutdown_callback = new ShutdownCallback();
219 f.cq =
220 grpc_completion_queue_create_for_callback(g_shutdown_callback, nullptr);
221
222 return f;
223 }
224
225 void inproc_init_client(grpc_end2end_test_fixture* f,
226 const grpc_channel_args* client_args) {
227 f->client = grpc_inproc_channel_create(f->server, client_args, nullptr);
228 GPR_ASSERT(f->client);
229 }
230
231 void inproc_init_server(grpc_end2end_test_fixture* f,
232 const grpc_channel_args* server_args) {
233 if (f->server) {
234 grpc_server_destroy(f->server);
235 }
236 f->server = grpc_server_create(server_args, nullptr);
237 grpc_server_register_completion_queue(f->server, f->cq, nullptr);
238 grpc_server_start(f->server);
239 }
240
241 void inproc_tear_down(grpc_end2end_test_fixture* f) {
242 inproc_fixture_data* ffd = static_cast<inproc_fixture_data*>(f->fixture_data);
243 gpr_free(ffd);
244 }
245
246 static grpc_end2end_test_fixture begin_test(
247 grpc_end2end_test_config config, const char* test_name,
248 const grpc_channel_args* client_args,
249 const grpc_channel_args* server_args) {
250 grpc_end2end_test_fixture f;
251 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
252 f = config.create_fixture(client_args, server_args);
253 config.init_server(&f, server_args);
254 config.init_client(&f, client_args);
255 return f;
256 }
257
258 static gpr_timespec n_seconds_from_now(int n) {
259 return grpc_timeout_seconds_to_deadline(n);
260 }
261
262 static gpr_timespec five_seconds_from_now() { return n_seconds_from_now(5); }
263
264 static void drain_cq(grpc_completion_queue* /*cq*/) {
265 // Wait for the shutdown callback to arrive, or fail the test
266 GPR_ASSERT(g_shutdown_callback->Wait(five_seconds_from_now()));
267 gpr_log(GPR_DEBUG, "CQ shutdown wait complete");
268 delete g_shutdown_callback;
269 }
270
271 static void shutdown_server(grpc_end2end_test_fixture* f) {
272 if (!f->server) return;
273 grpc_server_shutdown_and_notify(f->server, f->cq, tag(1));
274 expect_tag(1, true);
275 verify_tags(five_seconds_from_now());
276 grpc_server_destroy(f->server);
277 f->server = nullptr;
278 }
279
280 static void shutdown_client(grpc_end2end_test_fixture* f) {
281 if (!f->client) return;
282 grpc_channel_destroy(f->client);
283 f->client = nullptr;
284 }
285
286 static void end_test(grpc_end2end_test_fixture* f) {
287 shutdown_server(f);
288 shutdown_client(f);
289
290 grpc_completion_queue_shutdown(f->cq);
291 drain_cq(f->cq);
292 grpc_completion_queue_destroy(f->cq);
293 }
294
295 static void simple_request_body(grpc_end2end_test_config /* config */,
296 grpc_end2end_test_fixture f) {
297 grpc_call* c;
298 grpc_call* s;
299 grpc_op ops[6];
300 grpc_op* op;
301 grpc_metadata_array initial_metadata_recv;
302 grpc_metadata_array trailing_metadata_recv;
303 grpc_metadata_array request_metadata_recv;
304 grpc_call_details call_details;
305 grpc_status_code status;
306 const char* error_string;
307 grpc_call_error error;
308 grpc_slice details;
309 int was_cancelled = 2;
310 char* peer;
311 gpr_timespec deadline = five_seconds_from_now();
312
313 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
314 grpc_slice_from_static_string("/foo"), nullptr,
315 deadline, nullptr);
316 GPR_ASSERT(c);
317
318 peer = grpc_call_get_peer(c);
319 GPR_ASSERT(peer != nullptr);
320 gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer);
321 gpr_free(peer);
322
323 grpc_metadata_array_init(&initial_metadata_recv);
324 grpc_metadata_array_init(&trailing_metadata_recv);
325 grpc_metadata_array_init(&request_metadata_recv);
326 grpc_call_details_init(&call_details);
327
328 // Create a basic client unary request batch (no payload)
329 memset(ops, 0, sizeof(ops));
330 op = ops;
331 op->op = GRPC_OP_SEND_INITIAL_METADATA;
332 op->data.send_initial_metadata.count = 0;
333 op->flags = 0;
334 op->reserved = nullptr;
335 op++;
336 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
337 op->flags = 0;
338 op->reserved = nullptr;
339 op++;
340 op->op = GRPC_OP_RECV_INITIAL_METADATA;
341 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
342 op->flags = 0;
343 op->reserved = nullptr;
344 op++;
345 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
346 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
347 op->data.recv_status_on_client.status = &status;
348 op->data.recv_status_on_client.status_details = &details;
349 op->data.recv_status_on_client.error_string = &error_string;
350 op->flags = 0;
351 op->reserved = nullptr;
352 op++;
353 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
354 nullptr);
355 GPR_ASSERT(GRPC_CALL_OK == error);
356
357 // Register a call at the server-side to match the incoming client call
358 error = grpc_server_request_call(f.server, &s, &call_details,
359 &request_metadata_recv, f.cq, f.cq, tag(2));
360 GPR_ASSERT(GRPC_CALL_OK == error);
361
362 // We expect that the server call creation callback (and no others) will
363 // execute now since no other batch should be complete.
364 expect_tag(2, true);
365 verify_tags(deadline);
366
367 peer = grpc_call_get_peer(s);
368 GPR_ASSERT(peer != nullptr);
369 gpr_log(GPR_DEBUG, "server_peer=%s", peer);
370 gpr_free(peer);
371 peer = grpc_call_get_peer(c);
372 GPR_ASSERT(peer != nullptr);
373 gpr_log(GPR_DEBUG, "client_peer=%s", peer);
374 gpr_free(peer);
375
376 // Create the server response batch (no payload)
377 memset(ops, 0, sizeof(ops));
378 op = ops;
379 op->op = GRPC_OP_SEND_INITIAL_METADATA;
380 op->data.send_initial_metadata.count = 0;
381 op->flags = 0;
382 op->reserved = nullptr;
383 op++;
384 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
385 op->data.send_status_from_server.trailing_metadata_count = 0;
386 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
387 grpc_slice status_details = grpc_slice_from_static_string("xyz");
388 op->data.send_status_from_server.status_details = &status_details;
389 op->flags = 0;
390 op->reserved = nullptr;
391 op++;
392 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
393 op->data.recv_close_on_server.cancelled = &was_cancelled;
394 op->flags = 0;
395 op->reserved = nullptr;
396 op++;
397 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(3),
398 nullptr);
399 GPR_ASSERT(GRPC_CALL_OK == error);
400
401 // Both the client request and server response batches should get complete
402 // now and we should see that their callbacks have been executed
403 expect_tag(3, true);
404 expect_tag(1, true);
405 verify_tags(deadline);
406
407 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
408 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
409 // the following sanity check makes sure that the requested error string is
410 // correctly populated by the core. It looks for certain substrings that are
411 // not likely to change much. Some parts of the error, like time created,
412 // obviously are not checked.
413 GPR_ASSERT(nullptr != strstr(error_string, "xyz"));
414 GPR_ASSERT(nullptr != strstr(error_string, "Error received from peer"));
415 GPR_ASSERT(nullptr != strstr(error_string, "grpc_message"));
416 GPR_ASSERT(nullptr != strstr(error_string, "grpc_status"));
417 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
418 GPR_ASSERT(was_cancelled == 0);
419
420 grpc_slice_unref(details);
421 gpr_free(static_cast<void*>(const_cast<char*>(error_string)));
422 grpc_metadata_array_destroy(&initial_metadata_recv);
423 grpc_metadata_array_destroy(&trailing_metadata_recv);
424 grpc_metadata_array_destroy(&request_metadata_recv);
425 grpc_call_details_destroy(&call_details);
426
427 grpc_call_unref(c);
428 grpc_call_unref(s);
429 }
430
431 static void test_invoke_simple_request(grpc_end2end_test_config config) {
432 grpc_end2end_test_fixture f;
433
434 f = begin_test(config, "test_invoke_simple_request", nullptr, nullptr);
435 simple_request_body(config, f);
436 end_test(&f);
437 config.tear_down_data(&f);
438 }
439
440 static void test_invoke_10_simple_requests(grpc_end2end_test_config config) {
441 int i;
442 grpc_end2end_test_fixture f =
443 begin_test(config, "test_invoke_10_simple_requests", nullptr, nullptr);
444 for (i = 0; i < 10; i++) {
445 simple_request_body(config, f);
446 gpr_log(GPR_INFO, "Running test: Passed simple request %d", i);
447 }
448 end_test(&f);
449 config.tear_down_data(&f);
450 }
451
452 static void test_invoke_many_simple_requests(grpc_end2end_test_config config) {
453 int i;
454 const int many = 1000;
455 grpc_end2end_test_fixture f =
456 begin_test(config, "test_invoke_many_simple_requests", nullptr, nullptr);
457 gpr_timespec t1 = gpr_now(GPR_CLOCK_MONOTONIC);
458 for (i = 0; i < many; i++) {
459 simple_request_body(config, f);
460 }
461 double us =
462 gpr_timespec_to_micros(gpr_time_sub(gpr_now(GPR_CLOCK_MONOTONIC), t1)) /
463 many;
464 gpr_log(GPR_INFO, "Time per ping %f us", us);
465 end_test(&f);
466 config.tear_down_data(&f);
467 }
468
469 static void simple_request(grpc_end2end_test_config config) {
470 int i;
471 for (i = 0; i < 10; i++) {
472 test_invoke_simple_request(config);
473 }
474 test_invoke_10_simple_requests(config);
475 test_invoke_many_simple_requests(config);
476 }
477
478 static void simple_request_pre_init() {
479 gpr_mu_init(&tags_mu);
480 gpr_cv_init(&tags_cv);
481 }
482
483 /* All test configurations */
484 static grpc_end2end_test_config configs[] = {
485 {"inproc-callback", FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER, nullptr,
486 inproc_create_fixture, inproc_init_client, inproc_init_server,
487 inproc_tear_down},
488 };
489
490 int main(int argc, char** argv) {
491 grpc::testing::TestEnvironment env(&argc, argv);
492 grpc_init();
493
494 simple_request_pre_init();
495 simple_request(configs[0]);
496
497 grpc_shutdown();
498
499 return 0;
500 }