blob: f29012b86dc72f0f5d55fadda41fcc8a4d501e20 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include "test/core/end2end/end2end_tests.h"
35
36#include <stdio.h>
37#include <string.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080038
39#include <grpc/byte_buffer.h>
40#include <grpc/support/alloc.h>
41#include <grpc/support/log.h>
42#include <grpc/support/time.h>
43#include <grpc/support/useful.h>
44#include "test/core/end2end/cq_verifier.h"
Craig Tiller7b018782015-01-16 09:53:39 -080045#include "test/core/end2end/tests/cancel_test_helpers.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080046
Craig Tiller7536af02015-12-22 13:49:30 -080047static void *tag(intptr_t t) { return (void *)t; }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080048
49static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
50 const char *test_name,
Craig Tilleraec96aa2015-04-07 14:32:15 -070051 cancellation_mode mode,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080052 grpc_channel_args *client_args,
53 grpc_channel_args *server_args) {
54 grpc_end2end_test_fixture f;
Robbie Shade55a046a2017-01-25 15:14:28 -050055 gpr_log(GPR_INFO, "Running test: %s/%s/%s", test_name, config.name,
56 mode.name);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080057 f = config.create_fixture(client_args, server_args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080058 config.init_server(&f, server_args);
Mark D. Rothe127a392016-10-27 08:27:15 -070059 config.init_client(&f, client_args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080060 return f;
61}
62
Chris Evansed2a5472017-03-27 17:34:51 -050063static gpr_timespec n_seconds_from_now(int n) {
Robbie Shadeca7effc2017-01-17 09:14:29 -050064 return grpc_timeout_seconds_to_deadline(n);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080065}
66
Chris Evansed2a5472017-03-27 17:34:51 -050067static gpr_timespec five_seconds_from_now(void) {
68 return n_seconds_from_now(5);
69}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080070
71static void drain_cq(grpc_completion_queue *cq) {
Craig Tiller64be9f72015-05-04 14:53:51 -070072 grpc_event ev;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080073 do {
Chris Evansed2a5472017-03-27 17:34:51 -050074 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), NULL);
Craig Tiller64be9f72015-05-04 14:53:51 -070075 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080076}
77
78static void shutdown_server(grpc_end2end_test_fixture *f) {
79 if (!f->server) return;
Sree Kuchibhotla2eeedad2017-04-13 14:25:05 -070080 grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
81 grpc_event ev = grpc_completion_queue_next(
82 f->cq, grpc_timeout_seconds_to_deadline(5), NULL);
83 GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
84 GPR_ASSERT(ev.tag == tag(1000));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080085 grpc_server_destroy(f->server);
86 f->server = NULL;
87}
88
89static void shutdown_client(grpc_end2end_test_fixture *f) {
90 if (!f->client) return;
91 grpc_channel_destroy(f->client);
92 f->client = NULL;
93}
94
95static void end_test(grpc_end2end_test_fixture *f) {
96 shutdown_server(f);
97 shutdown_client(f);
98
Craig Tillerbc0ec332015-05-11 12:11:32 -070099 grpc_completion_queue_shutdown(f->cq);
100 drain_cq(f->cq);
101 grpc_completion_queue_destroy(f->cq);
Sree Kuchibhotla321881d2017-02-27 11:25:28 -0800102 grpc_completion_queue_destroy(f->shutdown_cq);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800103}
104
105/* Cancel after invoke, no payload */
106static void test_cancel_after_invoke(grpc_end2end_test_config config,
Craig Tiller3121fd42015-09-10 09:56:20 -0700107 cancellation_mode mode, size_t test_ops) {
Craig Tillerfee91b42015-02-05 16:03:13 -0800108 grpc_op ops[6];
109 grpc_op *op;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800110 grpc_call *c;
Craig Tillerc02c1d82015-04-07 16:21:55 -0700111 grpc_end2end_test_fixture f =
Craig Tiller35696192015-05-24 15:00:37 -0700112 begin_test(config, "test_cancel_after_invoke", mode, NULL, NULL);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700113 cq_verifier *cqv = cq_verifier_create(f.cq);
Craig Tillerfee91b42015-02-05 16:03:13 -0800114 grpc_metadata_array initial_metadata_recv;
115 grpc_metadata_array trailing_metadata_recv;
116 grpc_metadata_array request_metadata_recv;
117 grpc_call_details call_details;
118 grpc_status_code status;
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200119 grpc_call_error error;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800120 grpc_slice details;
Craig Tillerfee91b42015-02-05 16:03:13 -0800121 grpc_byte_buffer *response_payload_recv = NULL;
Craig Tiller28b72422016-10-26 21:15:29 -0700122 grpc_slice request_payload_slice =
123 grpc_slice_from_copied_string("hello world");
Craig Tillerfee91b42015-02-05 16:03:13 -0800124 grpc_byte_buffer *request_payload =
David Garcia Quintas59f905d2015-06-08 16:31:19 -0700125 grpc_raw_byte_buffer_create(&request_payload_slice, 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800126
Chris Evansed2a5472017-03-27 17:34:51 -0500127 gpr_timespec deadline = five_seconds_from_now();
Muxi Yan5ebd3272016-10-31 07:27:07 -0700128 c = grpc_channel_create_call(
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800129 f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq,
130 grpc_slice_from_static_string("/foo"),
131 get_host_override_slice("foo.test.google.fr:1234", config), deadline,
Muxi Yan5ebd3272016-10-31 07:27:07 -0700132 NULL);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800133 GPR_ASSERT(c);
134
Craig Tillerfee91b42015-02-05 16:03:13 -0800135 grpc_metadata_array_init(&initial_metadata_recv);
136 grpc_metadata_array_init(&trailing_metadata_recv);
137 grpc_metadata_array_init(&request_metadata_recv);
138 grpc_call_details_init(&call_details);
139
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700140 memset(ops, 0, sizeof(ops));
Craig Tillerfee91b42015-02-05 16:03:13 -0800141 op = ops;
142 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
143 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
144 op->data.recv_status_on_client.status = &status;
145 op->data.recv_status_on_client.status_details = &details;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700146 op->flags = 0;
Nicolas "Pixel" Noble49605162015-08-01 00:12:01 +0200147 op->reserved = NULL;
Craig Tillerfee91b42015-02-05 16:03:13 -0800148 op++;
149 op->op = GRPC_OP_SEND_INITIAL_METADATA;
150 op->data.send_initial_metadata.count = 0;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700151 op->flags = 0;
Nicolas "Pixel" Noble49605162015-08-01 00:12:01 +0200152 op->reserved = NULL;
Craig Tillerfee91b42015-02-05 16:03:13 -0800153 op++;
154 op->op = GRPC_OP_SEND_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800155 op->data.send_message.send_message = request_payload;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700156 op->flags = 0;
Nicolas "Pixel" Noble49605162015-08-01 00:12:01 +0200157 op->reserved = NULL;
Craig Tillerfee91b42015-02-05 16:03:13 -0800158 op++;
159 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700160 op->flags = 0;
Nicolas "Pixel" Noble49605162015-08-01 00:12:01 +0200161 op->reserved = NULL;
Craig Tillerfee91b42015-02-05 16:03:13 -0800162 op++;
163 op->op = GRPC_OP_RECV_INITIAL_METADATA;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800164 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700165 op->flags = 0;
Nicolas "Pixel" Noble49605162015-08-01 00:12:01 +0200166 op->reserved = NULL;
Craig Tillerfee91b42015-02-05 16:03:13 -0800167 op++;
168 op->op = GRPC_OP_RECV_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800169 op->data.recv_message.recv_message = &response_payload_recv;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700170 op->flags = 0;
Nicolas "Pixel" Noble49605162015-08-01 00:12:01 +0200171 op->reserved = NULL;
Craig Tillerfee91b42015-02-05 16:03:13 -0800172 op++;
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200173 error = grpc_call_start_batch(c, ops, test_ops, tag(1), NULL);
174 GPR_ASSERT(GRPC_CALL_OK == error);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800175
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200176 GPR_ASSERT(GRPC_CALL_OK == mode.initiate_cancel(c, NULL));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800177
Mark D. Roth7187ab92016-08-24 13:49:22 -0700178 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700179 cq_verify(cqv);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800180
Craig Tiller402acf62015-08-05 10:43:10 -0700181 GPR_ASSERT(status == mode.expect_status || status == GRPC_STATUS_INTERNAL);
Craig Tillerfee91b42015-02-05 16:03:13 -0800182
183 grpc_metadata_array_destroy(&initial_metadata_recv);
184 grpc_metadata_array_destroy(&trailing_metadata_recv);
185 grpc_metadata_array_destroy(&request_metadata_recv);
186 grpc_call_details_destroy(&call_details);
187
188 grpc_byte_buffer_destroy(request_payload);
189 grpc_byte_buffer_destroy(response_payload_recv);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800190 grpc_slice_unref(details);
Craig Tillerfee91b42015-02-05 16:03:13 -0800191
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800192 grpc_call_destroy(c);
193
Craig Tillerbc0ec332015-05-11 12:11:32 -0700194 cq_verifier_destroy(cqv);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800195 end_test(&f);
196 config.tear_down_data(&f);
197}
198
Craig Tiller0fe5ee72015-12-22 12:50:36 -0800199void cancel_after_invoke(grpc_end2end_test_config config) {
Craig Tillerfee91b42015-02-05 16:03:13 -0800200 unsigned i, j;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800201
Craig Tiller8b976d02015-02-05 21:41:23 -0800202 for (j = 2; j < 6; j++) {
Craig Tillerfee91b42015-02-05 16:03:13 -0800203 for (i = 0; i < GPR_ARRAY_SIZE(cancellation_modes); i++) {
204 test_cancel_after_invoke(config, cancellation_modes[i], j);
205 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800206 }
Craig Tiller190d3602015-02-18 09:23:38 -0800207}
Craig Tiller9e9edbc2016-04-04 10:38:49 -0700208
209void cancel_after_invoke_pre_init(void) {}