blob: 9b832d435ded6ff360e78347e1bc969f5a05afbe [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080016 *
17 */
18
19#include "test/core/end2end/end2end_tests.h"
20
21#include <stdio.h>
22#include <string.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080023
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#include <grpc/support/useful.h>
29#include "test/core/end2end/cq_verifier.h"
Craig Tiller7b018782015-01-16 09:53:39 -080030#include "test/core/end2end/tests/cancel_test_helpers.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080031
Craig Tillerbaa14a92017-11-03 09:09:36 -070032static void* tag(intptr_t t) { return (void*)t; }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080033
34static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
Craig Tillerbaa14a92017-11-03 09:09:36 -070035 const char* test_name,
Mark D. Rothd4ac3a12017-09-05 13:47:15 -070036 cancellation_mode mode,
Craig Tillerbaa14a92017-11-03 09:09:36 -070037 grpc_channel_args* client_args,
38 grpc_channel_args* server_args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039 grpc_end2end_test_fixture f;
Mark D. Rothd4ac3a12017-09-05 13:47:15 -070040 gpr_log(GPR_INFO, "Running test: %s/%s/%s", test_name, config.name,
41 mode.name);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080042 f = config.create_fixture(client_args, server_args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043 config.init_server(&f, server_args);
Mark D. Rothe127a392016-10-27 08:27:15 -070044 config.init_client(&f, client_args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080045 return f;
46}
47
Chris Evansed2a5472017-03-27 17:34:51 -050048static gpr_timespec n_seconds_from_now(int n) {
Robbie Shadeca7effc2017-01-17 09:14:29 -050049 return grpc_timeout_seconds_to_deadline(n);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080050}
51
Chris Evansed2a5472017-03-27 17:34:51 -050052static gpr_timespec five_seconds_from_now(void) {
53 return n_seconds_from_now(5);
54}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080055
Craig Tillerbaa14a92017-11-03 09:09:36 -070056static void drain_cq(grpc_completion_queue* cq) {
Craig Tiller64be9f72015-05-04 14:53:51 -070057 grpc_event ev;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080058 do {
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080059 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
Craig Tiller64be9f72015-05-04 14:53:51 -070060 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080061}
62
Craig Tillerbaa14a92017-11-03 09:09:36 -070063static void shutdown_server(grpc_end2end_test_fixture* f) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080064 if (!f->server) return;
Sree Kuchibhotla321881d2017-02-27 11:25:28 -080065 grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
66 GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
67 grpc_timeout_seconds_to_deadline(5),
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080068 nullptr)
Craig Tillerf40df232016-03-25 13:38:14 -070069 .type == GRPC_OP_COMPLETE);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080070 grpc_server_destroy(f->server);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080071 f->server = nullptr;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080072}
73
Craig Tillerbaa14a92017-11-03 09:09:36 -070074static void shutdown_client(grpc_end2end_test_fixture* f) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080075 if (!f->client) return;
76 grpc_channel_destroy(f->client);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080077 f->client = nullptr;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080078}
79
Craig Tillerbaa14a92017-11-03 09:09:36 -070080static void end_test(grpc_end2end_test_fixture* f) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080081 shutdown_server(f);
82 shutdown_client(f);
83
Craig Tillerbc0ec332015-05-11 12:11:32 -070084 grpc_completion_queue_shutdown(f->cq);
85 drain_cq(f->cq);
86 grpc_completion_queue_destroy(f->cq);
Sree Kuchibhotla321881d2017-02-27 11:25:28 -080087 grpc_completion_queue_destroy(f->shutdown_cq);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080088}
89
90/* Cancel after accept with a writes closed, no payload */
91static void test_cancel_after_accept_and_writes_closed(
Craig Tiller7b018782015-01-16 09:53:39 -080092 grpc_end2end_test_config config, cancellation_mode mode) {
Craig Tiller8b1d9892015-04-28 14:28:19 -070093 grpc_op ops[6];
Craig Tillerbaa14a92017-11-03 09:09:36 -070094 grpc_op* op;
95 grpc_call* c;
96 grpc_call* s;
Craig Tillerbe98d242017-11-10 15:26:57 -080097 grpc_end2end_test_fixture f =
98 begin_test(config, "test_cancel_after_accept_and_writes_closed", mode,
99 nullptr, nullptr);
Craig Tillerbaa14a92017-11-03 09:09:36 -0700100 cq_verifier* cqv = cq_verifier_create(f.cq);
Craig Tiller8b1d9892015-04-28 14:28:19 -0700101 grpc_metadata_array initial_metadata_recv;
102 grpc_metadata_array trailing_metadata_recv;
103 grpc_metadata_array request_metadata_recv;
104 grpc_call_details call_details;
105 grpc_status_code status;
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200106 grpc_call_error error;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800107 grpc_slice details;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800108 grpc_byte_buffer* request_payload_recv = nullptr;
109 grpc_byte_buffer* response_payload_recv = nullptr;
Craig Tiller28b72422016-10-26 21:15:29 -0700110 grpc_slice request_payload_slice =
111 grpc_slice_from_copied_string("hello world");
112 grpc_slice response_payload_slice =
113 grpc_slice_from_copied_string("hello you");
Craig Tillerbaa14a92017-11-03 09:09:36 -0700114 grpc_byte_buffer* request_payload =
David Garcia Quintas59f905d2015-06-08 16:31:19 -0700115 grpc_raw_byte_buffer_create(&request_payload_slice, 1);
Craig Tillerbaa14a92017-11-03 09:09:36 -0700116 grpc_byte_buffer* response_payload =
David Garcia Quintas59f905d2015-06-08 16:31:19 -0700117 grpc_raw_byte_buffer_create(&response_payload_slice, 1);
Craig Tiller8b1d9892015-04-28 14:28:19 -0700118 int was_cancelled = 2;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800119
Chris Evansed2a5472017-03-27 17:34:51 -0500120 gpr_timespec deadline = five_seconds_from_now();
Muxi Yan5ebd3272016-10-31 07:27:07 -0700121 c = grpc_channel_create_call(
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800122 f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800123 grpc_slice_from_static_string("/foo"),
124 get_host_override_slice("foo.test.google.fr:1234", config), deadline,
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800125 nullptr);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800126 GPR_ASSERT(c);
127
Craig Tiller8b1d9892015-04-28 14:28:19 -0700128 grpc_metadata_array_init(&initial_metadata_recv);
129 grpc_metadata_array_init(&trailing_metadata_recv);
130 grpc_metadata_array_init(&request_metadata_recv);
131 grpc_call_details_init(&call_details);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800132
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700133 memset(ops, 0, sizeof(ops));
Craig Tiller8b1d9892015-04-28 14:28:19 -0700134 op = ops;
135 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
136 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
137 op->data.recv_status_on_client.status = &status;
138 op->data.recv_status_on_client.status_details = &details;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700139 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800140 op->reserved = nullptr;
Craig Tiller8b1d9892015-04-28 14:28:19 -0700141 op++;
142 op->op = GRPC_OP_SEND_INITIAL_METADATA;
143 op->data.send_initial_metadata.count = 0;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700144 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800145 op->reserved = nullptr;
Craig Tiller8b1d9892015-04-28 14:28:19 -0700146 op++;
147 op->op = GRPC_OP_SEND_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800148 op->data.send_message.send_message = request_payload;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700149 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800150 op->reserved = nullptr;
Craig Tiller8b1d9892015-04-28 14:28:19 -0700151 op++;
152 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700153 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800154 op->reserved = nullptr;
Craig Tiller8b1d9892015-04-28 14:28:19 -0700155 op++;
156 op->op = GRPC_OP_RECV_INITIAL_METADATA;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800157 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700158 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800159 op->reserved = nullptr;
Craig Tiller8b1d9892015-04-28 14:28:19 -0700160 op++;
161 op->op = GRPC_OP_RECV_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800162 op->data.recv_message.recv_message = &response_payload_recv;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700163 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800164 op->reserved = nullptr;
Craig Tiller8b1d9892015-04-28 14:28:19 -0700165 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800166 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr);
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200167 GPR_ASSERT(GRPC_CALL_OK == error);
Craig Tiller8b1d9892015-04-28 14:28:19 -0700168
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200169 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);
Mark D. Roth7187ab92016-08-24 13:49:22 -0700172 CQ_EXPECT_COMPLETION(cqv, tag(2), 1);
Craig Tillere6aabbb2015-05-11 10:44:11 -0700173 cq_verify(cqv);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800174
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700175 memset(ops, 0, sizeof(ops));
Craig Tiller8b1d9892015-04-28 14:28:19 -0700176 op = ops;
177 op->op = GRPC_OP_RECV_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800178 op->data.recv_message.recv_message = &request_payload_recv;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700179 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800180 op->reserved = nullptr;
Craig Tiller8b1d9892015-04-28 14:28:19 -0700181 op++;
182 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
183 op->data.recv_close_on_server.cancelled = &was_cancelled;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700184 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800185 op->reserved = nullptr;
Craig Tiller8b1d9892015-04-28 14:28:19 -0700186 op++;
187 op->op = GRPC_OP_SEND_INITIAL_METADATA;
188 op->data.send_initial_metadata.count = 0;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700189 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800190 op->reserved = nullptr;
Craig Tiller8b1d9892015-04-28 14:28:19 -0700191 op++;
192 op->op = GRPC_OP_SEND_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800193 op->data.send_message.send_message = response_payload;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700194 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800195 op->reserved = nullptr;
Craig Tiller8b1d9892015-04-28 14:28:19 -0700196 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800197 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(3), nullptr);
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200198 GPR_ASSERT(GRPC_CALL_OK == error);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800199
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800200 GPR_ASSERT(GRPC_CALL_OK == mode.initiate_cancel(c, nullptr));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800201
Mark D. Roth7187ab92016-08-24 13:49:22 -0700202 CQ_EXPECT_COMPLETION(cqv, tag(3), 1);
203 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700204 cq_verify(cqv);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800205
Craig Tiller402acf62015-08-05 10:43:10 -0700206 GPR_ASSERT(status == mode.expect_status || status == GRPC_STATUS_INTERNAL);
Craig Tiller8b1d9892015-04-28 14:28:19 -0700207 GPR_ASSERT(was_cancelled == 1);
208
209 grpc_metadata_array_destroy(&initial_metadata_recv);
210 grpc_metadata_array_destroy(&trailing_metadata_recv);
211 grpc_metadata_array_destroy(&request_metadata_recv);
212 grpc_call_details_destroy(&call_details);
213
214 grpc_byte_buffer_destroy(request_payload);
215 grpc_byte_buffer_destroy(response_payload);
216 grpc_byte_buffer_destroy(request_payload_recv);
217 grpc_byte_buffer_destroy(response_payload_recv);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800218 grpc_slice_unref(details);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800219
Craig Tillerdd36b152017-03-31 08:27:28 -0700220 grpc_call_unref(c);
221 grpc_call_unref(s);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800222
Craig Tillere6aabbb2015-05-11 10:44:11 -0700223 cq_verifier_destroy(cqv);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800224 end_test(&f);
225 config.tear_down_data(&f);
226}
227
Craig Tiller0fe5ee72015-12-22 12:50:36 -0800228void cancel_after_client_done(grpc_end2end_test_config config) {
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +0100229 unsigned i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800230
Craig Tiller7b018782015-01-16 09:53:39 -0800231 for (i = 0; i < GPR_ARRAY_SIZE(cancellation_modes); i++) {
232 test_cancel_after_accept_and_writes_closed(config, cancellation_modes[i]);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800233 }
Craig Tiller190d3602015-02-18 09:23:38 -0800234}
Craig Tiller9e9edbc2016-04-04 10:38:49 -0700235
236void cancel_after_client_done_pre_init(void) {}