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