Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 38 | |
| 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 Tiller | 7b01878 | 2015-01-16 09:53:39 -0800 | [diff] [blame] | 45 | #include "test/core/end2end/tests/cancel_test_helpers.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 46 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 47 | static void *tag(intptr_t t) { return (void *)t; } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 48 | |
| 49 | static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
| 50 | const char *test_name, |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 51 | cancellation_mode mode, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 52 | grpc_channel_args *client_args, |
| 53 | grpc_channel_args *server_args) { |
| 54 | grpc_end2end_test_fixture f; |
Robbie Shade | 55a046a | 2017-01-25 15:14:28 -0500 | [diff] [blame] | 55 | gpr_log(GPR_INFO, "Running test: %s/%s/%s", test_name, config.name, |
| 56 | mode.name); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 57 | f = config.create_fixture(client_args, server_args); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 58 | config.init_server(&f, server_args); |
Mark D. Roth | e127a39 | 2016-10-27 08:27:15 -0700 | [diff] [blame] | 59 | config.init_client(&f, client_args); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 60 | return f; |
| 61 | } |
| 62 | |
Chris Evans | ed2a547 | 2017-03-27 17:34:51 -0500 | [diff] [blame] | 63 | static gpr_timespec n_seconds_from_now(int n) { |
Robbie Shade | ca7effc | 2017-01-17 09:14:29 -0500 | [diff] [blame] | 64 | return grpc_timeout_seconds_to_deadline(n); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Chris Evans | ed2a547 | 2017-03-27 17:34:51 -0500 | [diff] [blame] | 67 | static gpr_timespec five_seconds_from_now(void) { |
| 68 | return n_seconds_from_now(5); |
| 69 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 70 | |
| 71 | static void drain_cq(grpc_completion_queue *cq) { |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 72 | grpc_event ev; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 73 | do { |
Chris Evans | ed2a547 | 2017-03-27 17:34:51 -0500 | [diff] [blame] | 74 | ev = grpc_completion_queue_next(cq, five_seconds_from_now(), NULL); |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 75 | } while (ev.type != GRPC_QUEUE_SHUTDOWN); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static void shutdown_server(grpc_end2end_test_fixture *f) { |
| 79 | if (!f->server) return; |
Sree Kuchibhotla | 2eeedad | 2017-04-13 14:25:05 -0700 | [diff] [blame^] | 80 | 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 85 | grpc_server_destroy(f->server); |
| 86 | f->server = NULL; |
| 87 | } |
| 88 | |
| 89 | static 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 | |
| 95 | static void end_test(grpc_end2end_test_fixture *f) { |
| 96 | shutdown_server(f); |
| 97 | shutdown_client(f); |
| 98 | |
Craig Tiller | bc0ec33 | 2015-05-11 12:11:32 -0700 | [diff] [blame] | 99 | grpc_completion_queue_shutdown(f->cq); |
| 100 | drain_cq(f->cq); |
| 101 | grpc_completion_queue_destroy(f->cq); |
Sree Kuchibhotla | 321881d | 2017-02-27 11:25:28 -0800 | [diff] [blame] | 102 | grpc_completion_queue_destroy(f->shutdown_cq); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* Cancel after invoke, no payload */ |
| 106 | static void test_cancel_after_invoke(grpc_end2end_test_config config, |
Craig Tiller | 3121fd4 | 2015-09-10 09:56:20 -0700 | [diff] [blame] | 107 | cancellation_mode mode, size_t test_ops) { |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 108 | grpc_op ops[6]; |
| 109 | grpc_op *op; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 110 | grpc_call *c; |
Craig Tiller | c02c1d8 | 2015-04-07 16:21:55 -0700 | [diff] [blame] | 111 | grpc_end2end_test_fixture f = |
Craig Tiller | 3569619 | 2015-05-24 15:00:37 -0700 | [diff] [blame] | 112 | begin_test(config, "test_cancel_after_invoke", mode, NULL, NULL); |
Craig Tiller | bc0ec33 | 2015-05-11 12:11:32 -0700 | [diff] [blame] | 113 | cq_verifier *cqv = cq_verifier_create(f.cq); |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 114 | 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" Noble | 9a123df | 2015-07-29 23:45:08 +0200 | [diff] [blame] | 119 | grpc_call_error error; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 120 | grpc_slice details; |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 121 | grpc_byte_buffer *response_payload_recv = NULL; |
Craig Tiller | 28b7242 | 2016-10-26 21:15:29 -0700 | [diff] [blame] | 122 | grpc_slice request_payload_slice = |
| 123 | grpc_slice_from_copied_string("hello world"); |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 124 | grpc_byte_buffer *request_payload = |
David Garcia Quintas | 59f905d | 2015-06-08 16:31:19 -0700 | [diff] [blame] | 125 | grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 126 | |
Chris Evans | ed2a547 | 2017-03-27 17:34:51 -0500 | [diff] [blame] | 127 | gpr_timespec deadline = five_seconds_from_now(); |
Muxi Yan | 5ebd327 | 2016-10-31 07:27:07 -0700 | [diff] [blame] | 128 | c = grpc_channel_create_call( |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 129 | 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 Yan | 5ebd327 | 2016-10-31 07:27:07 -0700 | [diff] [blame] | 132 | NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 133 | GPR_ASSERT(c); |
| 134 | |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 135 | 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 Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 140 | memset(ops, 0, sizeof(ops)); |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 141 | 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 Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 146 | op->flags = 0; |
Nicolas "Pixel" Noble | 4960516 | 2015-08-01 00:12:01 +0200 | [diff] [blame] | 147 | op->reserved = NULL; |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 148 | op++; |
| 149 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 150 | op->data.send_initial_metadata.count = 0; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 151 | op->flags = 0; |
Nicolas "Pixel" Noble | 4960516 | 2015-08-01 00:12:01 +0200 | [diff] [blame] | 152 | op->reserved = NULL; |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 153 | op++; |
| 154 | op->op = GRPC_OP_SEND_MESSAGE; |
Mark D. Roth | 435f9f2 | 2017-01-25 12:53:54 -0800 | [diff] [blame] | 155 | op->data.send_message.send_message = request_payload; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 156 | op->flags = 0; |
Nicolas "Pixel" Noble | 4960516 | 2015-08-01 00:12:01 +0200 | [diff] [blame] | 157 | op->reserved = NULL; |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 158 | op++; |
| 159 | op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 160 | op->flags = 0; |
Nicolas "Pixel" Noble | 4960516 | 2015-08-01 00:12:01 +0200 | [diff] [blame] | 161 | op->reserved = NULL; |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 162 | op++; |
| 163 | op->op = GRPC_OP_RECV_INITIAL_METADATA; |
Mark D. Roth | 435f9f2 | 2017-01-25 12:53:54 -0800 | [diff] [blame] | 164 | op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 165 | op->flags = 0; |
Nicolas "Pixel" Noble | 4960516 | 2015-08-01 00:12:01 +0200 | [diff] [blame] | 166 | op->reserved = NULL; |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 167 | op++; |
| 168 | op->op = GRPC_OP_RECV_MESSAGE; |
Mark D. Roth | 435f9f2 | 2017-01-25 12:53:54 -0800 | [diff] [blame] | 169 | op->data.recv_message.recv_message = &response_payload_recv; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 170 | op->flags = 0; |
Nicolas "Pixel" Noble | 4960516 | 2015-08-01 00:12:01 +0200 | [diff] [blame] | 171 | op->reserved = NULL; |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 172 | op++; |
Nicolas "Pixel" Noble | 9a123df | 2015-07-29 23:45:08 +0200 | [diff] [blame] | 173 | error = grpc_call_start_batch(c, ops, test_ops, tag(1), NULL); |
| 174 | GPR_ASSERT(GRPC_CALL_OK == error); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 175 | |
Nicolas "Pixel" Noble | 9a123df | 2015-07-29 23:45:08 +0200 | [diff] [blame] | 176 | GPR_ASSERT(GRPC_CALL_OK == mode.initiate_cancel(c, NULL)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 177 | |
Mark D. Roth | 7187ab9 | 2016-08-24 13:49:22 -0700 | [diff] [blame] | 178 | CQ_EXPECT_COMPLETION(cqv, tag(1), 1); |
Craig Tiller | bc0ec33 | 2015-05-11 12:11:32 -0700 | [diff] [blame] | 179 | cq_verify(cqv); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 180 | |
Craig Tiller | 402acf6 | 2015-08-05 10:43:10 -0700 | [diff] [blame] | 181 | GPR_ASSERT(status == mode.expect_status || status == GRPC_STATUS_INTERNAL); |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 182 | |
| 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 Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 190 | grpc_slice_unref(details); |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 191 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 192 | grpc_call_destroy(c); |
| 193 | |
Craig Tiller | bc0ec33 | 2015-05-11 12:11:32 -0700 | [diff] [blame] | 194 | cq_verifier_destroy(cqv); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 195 | end_test(&f); |
| 196 | config.tear_down_data(&f); |
| 197 | } |
| 198 | |
Craig Tiller | 0fe5ee7 | 2015-12-22 12:50:36 -0800 | [diff] [blame] | 199 | void cancel_after_invoke(grpc_end2end_test_config config) { |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 200 | unsigned i, j; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 201 | |
Craig Tiller | 8b976d0 | 2015-02-05 21:41:23 -0800 | [diff] [blame] | 202 | for (j = 2; j < 6; j++) { |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 203 | for (i = 0; i < GPR_ARRAY_SIZE(cancellation_modes); i++) { |
| 204 | test_cancel_after_invoke(config, cancellation_modes[i], j); |
| 205 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 206 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 207 | } |
Craig Tiller | 9e9edbc | 2016-04-04 10:38:49 -0700 | [diff] [blame] | 208 | |
| 209 | void cancel_after_invoke_pre_init(void) {} |