• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "test/core/end2end/end2end_tests.h"
20 
21 #include <stdio.h>
22 #include <string.h>
23 
24 #include <grpc/byte_buffer.h>
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 #include <grpc/support/time.h>
28 
29 #include "src/core/lib/channel/channel_args.h"
30 #include "src/core/lib/iomgr/exec_ctx.h"
31 #include "src/core/lib/slice/slice_internal.h"
32 #include "src/core/lib/transport/metadata.h"
33 #include "src/core/lib/transport/service_config.h"
34 
35 #include "test/core/end2end/cq_verifier.h"
36 #include "test/core/end2end/tests/cancel_test_helpers.h"
37 
tag(intptr_t t)38 static void* tag(intptr_t t) { return (void*)t; }
39 
begin_test(grpc_end2end_test_config config,const char * test_name,cancellation_mode mode,bool use_service_config,grpc_channel_args * client_args,grpc_channel_args * server_args)40 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
41                                             const char* test_name,
42                                             cancellation_mode mode,
43                                             bool use_service_config,
44                                             grpc_channel_args* client_args,
45                                             grpc_channel_args* server_args) {
46   grpc_end2end_test_fixture f;
47   gpr_log(GPR_INFO, "Running test: %s/%s/%s/%s", test_name, config.name,
48           mode.name, use_service_config ? "service_config" : "client_api");
49   f = config.create_fixture(client_args, server_args);
50   config.init_server(&f, server_args);
51   config.init_client(&f, client_args);
52   return f;
53 }
54 
n_seconds_from_now(int n)55 static gpr_timespec n_seconds_from_now(int n) {
56   return grpc_timeout_seconds_to_deadline(n);
57 }
58 
five_seconds_from_now(void)59 static gpr_timespec five_seconds_from_now(void) {
60   return n_seconds_from_now(5);
61 }
62 
drain_cq(grpc_completion_queue * cq)63 static void drain_cq(grpc_completion_queue* cq) {
64   grpc_event ev;
65   do {
66     ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
67   } while (ev.type != GRPC_QUEUE_SHUTDOWN);
68 }
69 
shutdown_server(grpc_end2end_test_fixture * f)70 static void shutdown_server(grpc_end2end_test_fixture* f) {
71   if (!f->server) return;
72   grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
73   GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
74                                          grpc_timeout_seconds_to_deadline(5),
75                                          nullptr)
76                  .type == GRPC_OP_COMPLETE);
77   grpc_server_destroy(f->server);
78   f->server = nullptr;
79 }
80 
shutdown_client(grpc_end2end_test_fixture * f)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 
end_test(grpc_end2end_test_fixture * f)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   grpc_completion_queue_destroy(f->shutdown_cq);
95 }
96 
97 /* Cancel after accept, no payload */
test_cancel_after_accept(grpc_end2end_test_config config,cancellation_mode mode,bool use_service_config)98 static void test_cancel_after_accept(grpc_end2end_test_config config,
99                                      cancellation_mode mode,
100                                      bool use_service_config) {
101   grpc_op ops[6];
102   grpc_op* op;
103   grpc_call* c;
104   grpc_call* s;
105   grpc_metadata_array initial_metadata_recv;
106   grpc_metadata_array trailing_metadata_recv;
107   grpc_metadata_array request_metadata_recv;
108   grpc_call_details call_details;
109   grpc_status_code status;
110   grpc_call_error error;
111   grpc_slice details;
112   grpc_byte_buffer* request_payload_recv = nullptr;
113   grpc_byte_buffer* response_payload_recv = nullptr;
114   grpc_slice request_payload_slice =
115       grpc_slice_from_copied_string("hello world");
116   grpc_slice response_payload_slice =
117       grpc_slice_from_copied_string("hello you");
118   grpc_byte_buffer* request_payload =
119       grpc_raw_byte_buffer_create(&request_payload_slice, 1);
120   grpc_byte_buffer* response_payload =
121       grpc_raw_byte_buffer_create(&response_payload_slice, 1);
122   int was_cancelled = 2;
123 
124   grpc_channel_args* args = nullptr;
125   if (use_service_config) {
126     grpc_arg arg;
127     arg.type = GRPC_ARG_STRING;
128     arg.key = const_cast<char*>(GRPC_ARG_SERVICE_CONFIG);
129     arg.value.string = const_cast<char*>(
130         "{\n"
131         "  \"methodConfig\": [ {\n"
132         "    \"name\": [\n"
133         "      { \"service\": \"service\", \"method\": \"method\" },\n"
134         "      { \"service\": \"unused\" }\n"
135         "    ],\n"
136         "    \"timeout\": \"5s\"\n"
137         "  } ]\n"
138         "}");
139     args = grpc_channel_args_copy_and_add(args, &arg, 1);
140   }
141 
142   grpc_end2end_test_fixture f = begin_test(config, "cancel_after_accept", mode,
143                                            use_service_config, args, nullptr);
144   cq_verifier* cqv = cq_verifier_create(f.cq);
145 
146   gpr_timespec deadline = use_service_config
147                               ? gpr_inf_future(GPR_CLOCK_MONOTONIC)
148                               : five_seconds_from_now();
149   c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
150                                grpc_slice_from_static_string("/service/method"),
151                                nullptr, deadline, nullptr);
152   GPR_ASSERT(c);
153 
154   grpc_metadata_array_init(&initial_metadata_recv);
155   grpc_metadata_array_init(&trailing_metadata_recv);
156   grpc_metadata_array_init(&request_metadata_recv);
157   grpc_call_details_init(&call_details);
158 
159   memset(ops, 0, sizeof(ops));
160   op = ops;
161   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
162   op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
163   op->data.recv_status_on_client.status = &status;
164   op->data.recv_status_on_client.status_details = &details;
165   op->flags = 0;
166   op->reserved = nullptr;
167   op++;
168   op->op = GRPC_OP_SEND_INITIAL_METADATA;
169   op->data.send_initial_metadata.count = 0;
170   op->flags = 0;
171   op->reserved = nullptr;
172   op++;
173   op->op = GRPC_OP_SEND_MESSAGE;
174   op->data.send_message.send_message = request_payload;
175   op->flags = 0;
176   op->reserved = nullptr;
177   op++;
178   op->op = GRPC_OP_RECV_INITIAL_METADATA;
179   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
180   op->flags = 0;
181   op->reserved = nullptr;
182   op++;
183   op->op = GRPC_OP_RECV_MESSAGE;
184   op->data.recv_message.recv_message = &response_payload_recv;
185   op->flags = 0;
186   op->reserved = nullptr;
187   op++;
188   error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
189                                 nullptr);
190   GPR_ASSERT(GRPC_CALL_OK == error);
191 
192   error = grpc_server_request_call(f.server, &s, &call_details,
193                                    &request_metadata_recv, f.cq, f.cq, tag(2));
194   GPR_ASSERT(GRPC_CALL_OK == error);
195   CQ_EXPECT_COMPLETION(cqv, tag(2), 1);
196   cq_verify(cqv);
197 
198   memset(ops, 0, sizeof(ops));
199   op = ops;
200   op->op = GRPC_OP_RECV_MESSAGE;
201   op->data.recv_message.recv_message = &request_payload_recv;
202   op->flags = 0;
203   op->reserved = nullptr;
204   op++;
205   op->op = GRPC_OP_SEND_INITIAL_METADATA;
206   op->data.send_initial_metadata.count = 0;
207   op->flags = 0;
208   op->reserved = nullptr;
209   op++;
210   op->op = GRPC_OP_SEND_MESSAGE;
211   op->data.send_message.send_message = response_payload;
212   op->flags = 0;
213   op->reserved = nullptr;
214   op++;
215   op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
216   op->data.recv_close_on_server.cancelled = &was_cancelled;
217   op->flags = 0;
218   op->reserved = nullptr;
219   op++;
220   error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(3),
221                                 nullptr);
222   GPR_ASSERT(GRPC_CALL_OK == error);
223 
224   GPR_ASSERT(GRPC_CALL_OK == mode.initiate_cancel(c, nullptr));
225 
226   CQ_EXPECT_COMPLETION(cqv, tag(3), 1);
227   CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
228   cq_verify(cqv);
229 
230   GPR_ASSERT(status == mode.expect_status || status == GRPC_STATUS_INTERNAL);
231   GPR_ASSERT(was_cancelled == 1);
232 
233   grpc_metadata_array_destroy(&initial_metadata_recv);
234   grpc_metadata_array_destroy(&trailing_metadata_recv);
235   grpc_metadata_array_destroy(&request_metadata_recv);
236   grpc_call_details_destroy(&call_details);
237 
238   grpc_byte_buffer_destroy(request_payload);
239   grpc_byte_buffer_destroy(response_payload);
240   grpc_byte_buffer_destroy(request_payload_recv);
241   grpc_byte_buffer_destroy(response_payload_recv);
242   grpc_slice_unref(details);
243 
244   grpc_call_unref(c);
245   grpc_call_unref(s);
246 
247   if (args != nullptr) {
248     grpc_core::ExecCtx exec_ctx;
249     grpc_channel_args_destroy(args);
250   }
251 
252   cq_verifier_destroy(cqv);
253   end_test(&f);
254   config.tear_down_data(&f);
255 }
256 
cancel_after_accept(grpc_end2end_test_config config)257 void cancel_after_accept(grpc_end2end_test_config config) {
258   unsigned i;
259 
260   for (i = 0; i < GPR_ARRAY_SIZE(cancellation_modes); i++) {
261     test_cancel_after_accept(config, cancellation_modes[i],
262                              false /* use_service_config */);
263     if (config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL &&
264         cancellation_modes[i].expect_status == GRPC_STATUS_DEADLINE_EXCEEDED) {
265       test_cancel_after_accept(config, cancellation_modes[i],
266                                true /* use_service_config */);
267     }
268   }
269 }
270 
cancel_after_accept_pre_init(void)271 void cancel_after_accept_pre_init(void) {}
272