• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "test/core/end2end/end2end_tests.h"
20 
21 #include <string.h>
22 
23 #include <grpc/support/alloc.h>
24 #include <grpc/support/log.h>
25 #include <grpc/support/sync.h>
26 
27 #include "src/core/ext/transport/inproc/inproc_transport.h"
28 #include "src/core/lib/surface/channel.h"
29 #include "src/core/lib/surface/completion_queue.h"
30 #include "src/core/lib/surface/server.h"
31 #include "test/core/util/port.h"
32 #include "test/core/util/test_config.h"
33 
34 typedef struct inproc_fixture_data {
35   bool dummy;  // reserved for future expansion. Struct can't be empty
36 } inproc_fixture_data;
37 
38 namespace {
39 template <typename F>
40 class CQDeletingCallback : public grpc_experimental_completion_queue_functor {
41  public:
CQDeletingCallback(F f)42   explicit CQDeletingCallback(F f) : func_(f) {
43     functor_run = &CQDeletingCallback::Run;
44   }
~CQDeletingCallback()45   ~CQDeletingCallback() {}
Run(grpc_experimental_completion_queue_functor * cb,int ok)46   static void Run(grpc_experimental_completion_queue_functor* cb, int ok) {
47     auto* callback = static_cast<CQDeletingCallback*>(cb);
48     callback->func_(static_cast<bool>(ok));
49     grpc_core::Delete(callback);
50   }
51 
52  private:
53   F func_;
54 };
55 
56 template <typename F>
NewDeletingCallback(F f)57 grpc_experimental_completion_queue_functor* NewDeletingCallback(F f) {
58   return grpc_core::New<CQDeletingCallback<F>>(f);
59 }
60 
61 class ShutdownCallback : public grpc_experimental_completion_queue_functor {
62  public:
ShutdownCallback()63   ShutdownCallback() : done_(false) {
64     functor_run = &ShutdownCallback::StaticRun;
65     gpr_mu_init(&mu_);
66     gpr_cv_init(&cv_);
67   }
~ShutdownCallback()68   ~ShutdownCallback() {}
StaticRun(grpc_experimental_completion_queue_functor * cb,int ok)69   static void StaticRun(grpc_experimental_completion_queue_functor* cb,
70                         int ok) {
71     auto* callback = static_cast<ShutdownCallback*>(cb);
72     callback->Run(static_cast<bool>(ok));
73   }
Run(bool ok)74   void Run(bool ok) {
75     gpr_log(GPR_DEBUG, "CQ shutdown notification invoked");
76     gpr_mu_lock(&mu_);
77     done_ = true;
78     gpr_cv_broadcast(&cv_);
79     gpr_mu_unlock(&mu_);
80   }
81   // The Wait function waits for a specified amount of
82   // time for the completion of the shutdown and returns
83   // whether it was successfully shut down
Wait(gpr_timespec deadline)84   bool Wait(gpr_timespec deadline) {
85     gpr_mu_lock(&mu_);
86     while (!done_ && !gpr_cv_wait(&cv_, &mu_, deadline)) {
87     }
88     bool ret = done_;
89     gpr_mu_unlock(&mu_);
90     return ret;
91   }
92 
93  private:
94   bool done_;
95   gpr_mu mu_;
96   gpr_cv cv_;
97 };
98 
99 ShutdownCallback* g_shutdown_callback;
100 }  // namespace
101 
102 // The following global structure is the tag collection. It holds
103 // all information related to tags expected and tags received
104 // during the execution, with each callback setting a tag.
105 // The tag sets are implemented and checked using arrays and
106 // linear lookups (rather than maps) so that this test doesn't
107 // need the C++ standard library.
108 static gpr_mu tags_mu;
109 static gpr_cv tags_cv;
110 const size_t kAvailableTags = 4;
111 bool tags[kAvailableTags];
112 bool tags_valid[kAvailableTags];
113 bool tags_expected[kAvailableTags];
114 bool tags_needed[kAvailableTags];
115 
116 // Mark that a tag is expected; this function must be executed in the
117 // main thread only while there are no other threads altering the
118 // expectation set (e.g., by calling expect_tag or verify_tags)
expect_tag(intptr_t tag,bool ok)119 static void expect_tag(intptr_t tag, bool ok) {
120   size_t idx = static_cast<size_t>(tag);
121   GPR_ASSERT(idx < kAvailableTags);
122   tags_needed[idx] = true;
123   tags_expected[idx] = ok;
124 }
125 
126 // Check that the expected tags have reached, within a certain
127 // deadline. This must also be executed only on the main thread while
128 // there are no other threads altering the expectation set (e.g., by
129 // calling expect_tag or verify_tags). The tag verifier doesn't have
130 // to drive the CQ at all (unlike the next-based end2end tests)
131 // because the tags will get set when the callbacks are executed,
132 // which happens when a particular batch related to a callback is
133 // complete.
verify_tags(gpr_timespec deadline)134 static void verify_tags(gpr_timespec deadline) {
135   bool done = false;
136 
137   gpr_mu_lock(&tags_mu);
138   while (!done) {
139     done = gpr_time_cmp(gpr_now(GPR_CLOCK_MONOTONIC), deadline) > 0;
140     for (size_t i = 0; i < kAvailableTags; i++) {
141       if (tags_needed[i]) {
142         if (tags_valid[i]) {
143           gpr_log(GPR_DEBUG, "Verifying tag %d", static_cast<int>(i));
144           if (tags[i] != tags_expected[i]) {
145             gpr_log(GPR_ERROR, "Got wrong result (%d instead of %d) for tag %d",
146                     tags[i], tags_expected[i], static_cast<int>(i));
147             GPR_ASSERT(false);
148           }
149           tags_valid[i] = false;
150           tags_needed[i] = false;
151         } else if (done) {
152           gpr_log(GPR_ERROR, "Didn't get tag %d", static_cast<int>(i));
153           GPR_ASSERT(false);
154         }
155       }
156     }
157     bool empty = true;
158     for (size_t i = 0; i < kAvailableTags; i++) {
159       if (tags_needed[i]) {
160         empty = false;
161       }
162     }
163     done = done || empty;
164     if (done) {
165       for (size_t i = 0; i < kAvailableTags; i++) {
166         if (tags_valid[i]) {
167           gpr_log(GPR_ERROR, "Got unexpected tag %d and result %d",
168                   static_cast<int>(i), tags[i]);
169           GPR_ASSERT(false);
170         }
171         tags_valid[i] = false;
172       }
173     } else {
174       gpr_cv_wait(&tags_cv, &tags_mu, deadline);
175     }
176   }
177   gpr_mu_unlock(&tags_mu);
178 }
179 
180 // This function creates a callback functor that emits the
181 // desired tag into the global tag set
tag(intptr_t t)182 static grpc_experimental_completion_queue_functor* tag(intptr_t t) {
183   auto func = [t](bool ok) {
184     gpr_mu_lock(&tags_mu);
185     gpr_log(GPR_DEBUG, "Completing operation %" PRIdPTR, t);
186     bool was_empty = true;
187     for (size_t i = 0; i < kAvailableTags; i++) {
188       if (tags_valid[i]) {
189         was_empty = false;
190       }
191     }
192     size_t idx = static_cast<size_t>(t);
193     tags[idx] = ok;
194     tags_valid[idx] = true;
195     if (was_empty) {
196       gpr_cv_signal(&tags_cv);
197     }
198     gpr_mu_unlock(&tags_mu);
199   };
200   auto cb = NewDeletingCallback(func);
201   return cb;
202 }
203 
inproc_create_fixture(grpc_channel_args * client_args,grpc_channel_args * server_args)204 static grpc_end2end_test_fixture inproc_create_fixture(
205     grpc_channel_args* client_args, grpc_channel_args* server_args) {
206   grpc_end2end_test_fixture f;
207   inproc_fixture_data* ffd = static_cast<inproc_fixture_data*>(
208       gpr_malloc(sizeof(inproc_fixture_data)));
209   memset(&f, 0, sizeof(f));
210 
211   f.fixture_data = ffd;
212   g_shutdown_callback = grpc_core::New<ShutdownCallback>();
213   f.cq =
214       grpc_completion_queue_create_for_callback(g_shutdown_callback, nullptr);
215   f.shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr);
216 
217   return f;
218 }
219 
inproc_init_client(grpc_end2end_test_fixture * f,grpc_channel_args * client_args)220 void inproc_init_client(grpc_end2end_test_fixture* f,
221                         grpc_channel_args* client_args) {
222   f->client = grpc_inproc_channel_create(f->server, client_args, nullptr);
223   GPR_ASSERT(f->client);
224 }
225 
inproc_init_server(grpc_end2end_test_fixture * f,grpc_channel_args * server_args)226 void inproc_init_server(grpc_end2end_test_fixture* f,
227                         grpc_channel_args* server_args) {
228   if (f->server) {
229     grpc_server_destroy(f->server);
230   }
231   f->server = grpc_server_create(server_args, nullptr);
232   grpc_server_register_completion_queue(f->server, f->cq, nullptr);
233   grpc_server_start(f->server);
234 }
235 
inproc_tear_down(grpc_end2end_test_fixture * f)236 void inproc_tear_down(grpc_end2end_test_fixture* f) {
237   inproc_fixture_data* ffd = static_cast<inproc_fixture_data*>(f->fixture_data);
238   gpr_free(ffd);
239 }
240 
begin_test(grpc_end2end_test_config config,const char * test_name,grpc_channel_args * client_args,grpc_channel_args * server_args)241 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
242                                             const char* test_name,
243                                             grpc_channel_args* client_args,
244                                             grpc_channel_args* server_args) {
245   grpc_end2end_test_fixture f;
246   gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
247   f = config.create_fixture(client_args, server_args);
248   config.init_server(&f, server_args);
249   config.init_client(&f, client_args);
250   return f;
251 }
252 
n_seconds_from_now(int n)253 static gpr_timespec n_seconds_from_now(int n) {
254   return grpc_timeout_seconds_to_deadline(n);
255 }
256 
five_seconds_from_now()257 static gpr_timespec five_seconds_from_now() { return n_seconds_from_now(5); }
258 
drain_cq(grpc_completion_queue * cq)259 static void drain_cq(grpc_completion_queue* cq) {
260   // Wait for the shutdown callback to arrive, or fail the test
261   GPR_ASSERT(g_shutdown_callback->Wait(five_seconds_from_now()));
262   gpr_log(GPR_DEBUG, "CQ shutdown wait complete");
263   grpc_core::Delete(g_shutdown_callback);
264 }
265 
shutdown_server(grpc_end2end_test_fixture * f)266 static void shutdown_server(grpc_end2end_test_fixture* f) {
267   if (!f->server) return;
268   grpc_server_shutdown_and_notify(
269       f->server, f->shutdown_cq,
270       reinterpret_cast<void*>(static_cast<intptr_t>(1000)));
271   GPR_ASSERT(
272       grpc_completion_queue_pluck(f->shutdown_cq, (void*)((intptr_t)1000),
273                                   grpc_timeout_seconds_to_deadline(5), nullptr)
274           .type == GRPC_OP_COMPLETE);
275   grpc_server_destroy(f->server);
276   f->server = nullptr;
277 }
278 
shutdown_client(grpc_end2end_test_fixture * f)279 static void shutdown_client(grpc_end2end_test_fixture* f) {
280   if (!f->client) return;
281   grpc_channel_destroy(f->client);
282   f->client = nullptr;
283 }
284 
end_test(grpc_end2end_test_fixture * f)285 static void end_test(grpc_end2end_test_fixture* f) {
286   shutdown_server(f);
287   shutdown_client(f);
288 
289   grpc_completion_queue_shutdown(f->cq);
290   drain_cq(f->cq);
291   grpc_completion_queue_destroy(f->cq);
292   grpc_completion_queue_destroy(f->shutdown_cq);
293 }
294 
simple_request_body(grpc_end2end_test_config config,grpc_end2end_test_fixture f)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, "description"));
415   GPR_ASSERT(nullptr != strstr(error_string, "Error received from peer"));
416   GPR_ASSERT(nullptr != strstr(error_string, "grpc_message"));
417   GPR_ASSERT(nullptr != strstr(error_string, "grpc_status"));
418   GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
419   GPR_ASSERT(0 == call_details.flags);
420   GPR_ASSERT(was_cancelled == 1);
421 
422   grpc_slice_unref(details);
423   gpr_free(static_cast<void*>(const_cast<char*>(error_string)));
424   grpc_metadata_array_destroy(&initial_metadata_recv);
425   grpc_metadata_array_destroy(&trailing_metadata_recv);
426   grpc_metadata_array_destroy(&request_metadata_recv);
427   grpc_call_details_destroy(&call_details);
428 
429   grpc_call_unref(c);
430   grpc_call_unref(s);
431 
432   int expected_calls = 1;
433   if (config.feature_mask & FEATURE_MASK_SUPPORTS_REQUEST_PROXYING) {
434     expected_calls *= 2;
435   }
436 }
437 
test_invoke_simple_request(grpc_end2end_test_config config)438 static void test_invoke_simple_request(grpc_end2end_test_config config) {
439   grpc_end2end_test_fixture f;
440 
441   f = begin_test(config, "test_invoke_simple_request", nullptr, nullptr);
442   simple_request_body(config, f);
443   end_test(&f);
444   config.tear_down_data(&f);
445 }
446 
test_invoke_10_simple_requests(grpc_end2end_test_config config)447 static void test_invoke_10_simple_requests(grpc_end2end_test_config config) {
448   int i;
449   grpc_end2end_test_fixture f =
450       begin_test(config, "test_invoke_10_simple_requests", nullptr, nullptr);
451   for (i = 0; i < 10; i++) {
452     simple_request_body(config, f);
453     gpr_log(GPR_INFO, "Running test: Passed simple request %d", i);
454   }
455   end_test(&f);
456   config.tear_down_data(&f);
457 }
458 
test_invoke_many_simple_requests(grpc_end2end_test_config config)459 static void test_invoke_many_simple_requests(grpc_end2end_test_config config) {
460   int i;
461   const int many = 1000;
462   grpc_end2end_test_fixture f =
463       begin_test(config, "test_invoke_many_simple_requests", nullptr, nullptr);
464   gpr_timespec t1 = gpr_now(GPR_CLOCK_MONOTONIC);
465   for (i = 0; i < many; i++) {
466     simple_request_body(config, f);
467   }
468   double us =
469       gpr_timespec_to_micros(gpr_time_sub(gpr_now(GPR_CLOCK_MONOTONIC), t1)) /
470       many;
471   gpr_log(GPR_INFO, "Time per ping %f us", us);
472   end_test(&f);
473   config.tear_down_data(&f);
474 }
475 
simple_request(grpc_end2end_test_config config)476 static void simple_request(grpc_end2end_test_config config) {
477   int i;
478   for (i = 0; i < 10; i++) {
479     test_invoke_simple_request(config);
480   }
481   test_invoke_10_simple_requests(config);
482   test_invoke_many_simple_requests(config);
483 }
484 
simple_request_pre_init()485 static void simple_request_pre_init() {
486   gpr_mu_init(&tags_mu);
487   gpr_cv_init(&tags_cv);
488 }
489 
490 /* All test configurations */
491 static grpc_end2end_test_config configs[] = {
492     {"inproc-callback", FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER, nullptr,
493      inproc_create_fixture, inproc_init_client, inproc_init_server,
494      inproc_tear_down},
495 };
496 
main(int argc,char ** argv)497 int main(int argc, char** argv) {
498   grpc_test_init(argc, argv);
499   grpc_init();
500 
501   simple_request_pre_init();
502   simple_request(configs[0]);
503 
504   grpc_shutdown();
505 
506   return 0;
507 }
508