• 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/gpr/useful.h"
30 #include "test/core/end2end/cq_verifier.h"
31 #include "test/core/end2end/tests/cancel_test_helpers.h"
32 
tag(intptr_t t)33 static void* tag(intptr_t t) { return (void*)t; }
34 
begin_test(grpc_end2end_test_config config,const char * test_name,cancellation_mode mode,grpc_channel_args * client_args,grpc_channel_args * server_args)35 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
36                                             const char* test_name,
37                                             cancellation_mode mode,
38                                             grpc_channel_args* client_args,
39                                             grpc_channel_args* server_args) {
40   grpc_end2end_test_fixture f;
41   gpr_log(GPR_INFO, "Running test: %s/%s/%s", test_name, config.name,
42           mode.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 
n_seconds_from_now(int n)49 static gpr_timespec n_seconds_from_now(int n) {
50   return grpc_timeout_seconds_to_deadline(n);
51 }
52 
five_seconds_from_now(void)53 static gpr_timespec five_seconds_from_now(void) {
54   return n_seconds_from_now(5);
55 }
56 
drain_cq(grpc_completion_queue * cq)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 
shutdown_server(grpc_end2end_test_fixture * f)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 
shutdown_client(grpc_end2end_test_fixture * f)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 
end_test(grpc_end2end_test_fixture * f)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 /* Cancel after accept with a writes closed, no payload */
test_cancel_after_accept_and_writes_closed(grpc_end2end_test_config config,cancellation_mode mode)92 static void test_cancel_after_accept_and_writes_closed(
93     grpc_end2end_test_config config, cancellation_mode mode) {
94   grpc_op ops[6];
95   grpc_op* op;
96   grpc_call* c;
97   grpc_call* s;
98   grpc_end2end_test_fixture f =
99       begin_test(config, "test_cancel_after_accept_and_writes_closed", mode,
100                  nullptr, nullptr);
101   cq_verifier* cqv = cq_verifier_create(f.cq);
102   grpc_metadata_array initial_metadata_recv;
103   grpc_metadata_array trailing_metadata_recv;
104   grpc_metadata_array request_metadata_recv;
105   grpc_call_details call_details;
106   grpc_status_code status;
107   grpc_call_error error;
108   grpc_slice details;
109   grpc_byte_buffer* request_payload_recv = nullptr;
110   grpc_byte_buffer* response_payload_recv = nullptr;
111   grpc_slice request_payload_slice =
112       grpc_slice_from_copied_string("hello world");
113   grpc_slice response_payload_slice =
114       grpc_slice_from_copied_string("hello you");
115   grpc_byte_buffer* request_payload =
116       grpc_raw_byte_buffer_create(&request_payload_slice, 1);
117   grpc_byte_buffer* response_payload =
118       grpc_raw_byte_buffer_create(&response_payload_slice, 1);
119   int was_cancelled = 2;
120 
121   gpr_timespec deadline = five_seconds_from_now();
122   c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
123                                grpc_slice_from_static_string("/foo"), nullptr,
124                                deadline, nullptr);
125   GPR_ASSERT(c);
126 
127   grpc_metadata_array_init(&initial_metadata_recv);
128   grpc_metadata_array_init(&trailing_metadata_recv);
129   grpc_metadata_array_init(&request_metadata_recv);
130   grpc_call_details_init(&call_details);
131 
132   memset(ops, 0, sizeof(ops));
133   op = ops;
134   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
135   op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
136   op->data.recv_status_on_client.status = &status;
137   op->data.recv_status_on_client.status_details = &details;
138   op->flags = 0;
139   op->reserved = nullptr;
140   op++;
141   op->op = GRPC_OP_SEND_INITIAL_METADATA;
142   op->data.send_initial_metadata.count = 0;
143   op->flags = 0;
144   op->reserved = nullptr;
145   op++;
146   op->op = GRPC_OP_SEND_MESSAGE;
147   op->data.send_message.send_message = request_payload;
148   op->flags = 0;
149   op->reserved = nullptr;
150   op++;
151   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
152   op->flags = 0;
153   op->reserved = nullptr;
154   op++;
155   op->op = GRPC_OP_RECV_INITIAL_METADATA;
156   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
157   op->flags = 0;
158   op->reserved = nullptr;
159   op++;
160   op->op = GRPC_OP_RECV_MESSAGE;
161   op->data.recv_message.recv_message = &response_payload_recv;
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 = grpc_server_request_call(f.server, &s, &call_details,
170                                    &request_metadata_recv, f.cq, f.cq, tag(2));
171   GPR_ASSERT(GRPC_CALL_OK == error);
172   CQ_EXPECT_COMPLETION(cqv, tag(2), 1);
173   cq_verify(cqv);
174 
175   memset(ops, 0, sizeof(ops));
176   op = ops;
177   op->op = GRPC_OP_RECV_MESSAGE;
178   op->data.recv_message.recv_message = &request_payload_recv;
179   op->flags = 0;
180   op->reserved = nullptr;
181   op++;
182   op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
183   op->data.recv_close_on_server.cancelled = &was_cancelled;
184   op->flags = 0;
185   op->reserved = nullptr;
186   op++;
187   op->op = GRPC_OP_SEND_INITIAL_METADATA;
188   op->data.send_initial_metadata.count = 0;
189   op->flags = 0;
190   op->reserved = nullptr;
191   op++;
192   op->op = GRPC_OP_SEND_MESSAGE;
193   op->data.send_message.send_message = response_payload;
194   op->flags = 0;
195   op->reserved = nullptr;
196   op++;
197   error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(3),
198                                 nullptr);
199   GPR_ASSERT(GRPC_CALL_OK == error);
200 
201   GPR_ASSERT(GRPC_CALL_OK == mode.initiate_cancel(c, nullptr));
202 
203   CQ_EXPECT_COMPLETION(cqv, tag(3), 1);
204   CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
205   cq_verify(cqv);
206 
207   GPR_ASSERT(status == mode.expect_status || status == GRPC_STATUS_INTERNAL);
208   GPR_ASSERT(was_cancelled == 1);
209 
210   grpc_metadata_array_destroy(&initial_metadata_recv);
211   grpc_metadata_array_destroy(&trailing_metadata_recv);
212   grpc_metadata_array_destroy(&request_metadata_recv);
213   grpc_call_details_destroy(&call_details);
214 
215   grpc_byte_buffer_destroy(request_payload);
216   grpc_byte_buffer_destroy(response_payload);
217   grpc_byte_buffer_destroy(request_payload_recv);
218   grpc_byte_buffer_destroy(response_payload_recv);
219   grpc_slice_unref(details);
220 
221   grpc_call_unref(c);
222   grpc_call_unref(s);
223 
224   cq_verifier_destroy(cqv);
225   end_test(&f);
226   config.tear_down_data(&f);
227 }
228 
cancel_after_client_done(grpc_end2end_test_config config)229 void cancel_after_client_done(grpc_end2end_test_config config) {
230   unsigned i;
231 
232   for (i = 0; i < GPR_ARRAY_SIZE(cancellation_modes); i++) {
233     test_cancel_after_accept_and_writes_closed(config, cancellation_modes[i]);
234   }
235 }
236 
cancel_after_client_done_pre_init(void)237 void cancel_after_client_done_pre_init(void) {}
238