Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [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 | |
| 47 | enum { TIMEOUT = 200000 }; |
| 48 | |
| 49 | static void *tag(gpr_intptr t) { return (void *)t; } |
| 50 | |
| 51 | static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
| 52 | const char *test_name, |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 53 | cancellation_mode mode, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 54 | grpc_channel_args *client_args, |
| 55 | grpc_channel_args *server_args) { |
| 56 | grpc_end2end_test_fixture f; |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 57 | gpr_log(GPR_INFO, "%s/%s/%s", test_name, config.name, mode.name); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 58 | f = config.create_fixture(client_args, server_args); |
| 59 | config.init_client(&f, client_args); |
| 60 | config.init_server(&f, server_args); |
| 61 | return f; |
| 62 | } |
| 63 | |
| 64 | static gpr_timespec n_seconds_time(int n) { |
Craig Tiller | 8ad8a41 | 2015-02-25 08:36:40 -0800 | [diff] [blame] | 65 | return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 68 | static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 69 | |
| 70 | static void drain_cq(grpc_completion_queue *cq) { |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 71 | grpc_event ev; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 72 | do { |
| 73 | ev = grpc_completion_queue_next(cq, five_seconds_time()); |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 74 | } while (ev.type != GRPC_QUEUE_SHUTDOWN); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static void shutdown_server(grpc_end2end_test_fixture *f) { |
| 78 | if (!f->server) return; |
| 79 | grpc_server_shutdown(f->server); |
| 80 | grpc_server_destroy(f->server); |
| 81 | f->server = NULL; |
| 82 | } |
| 83 | |
| 84 | static void shutdown_client(grpc_end2end_test_fixture *f) { |
| 85 | if (!f->client) return; |
| 86 | grpc_channel_destroy(f->client); |
| 87 | f->client = NULL; |
| 88 | } |
| 89 | |
| 90 | static void end_test(grpc_end2end_test_fixture *f) { |
| 91 | shutdown_server(f); |
| 92 | shutdown_client(f); |
| 93 | |
| 94 | grpc_completion_queue_shutdown(f->server_cq); |
| 95 | drain_cq(f->server_cq); |
| 96 | grpc_completion_queue_destroy(f->server_cq); |
| 97 | grpc_completion_queue_shutdown(f->client_cq); |
| 98 | drain_cq(f->client_cq); |
| 99 | grpc_completion_queue_destroy(f->client_cq); |
| 100 | } |
| 101 | |
| 102 | /* Cancel after invoke, no payload */ |
| 103 | static void test_cancel_after_invoke(grpc_end2end_test_config config, |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 104 | cancellation_mode mode, int test_ops) { |
| 105 | grpc_op ops[6]; |
| 106 | grpc_op *op; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 107 | grpc_call *c; |
Craig Tiller | c02c1d8 | 2015-04-07 16:21:55 -0700 | [diff] [blame] | 108 | grpc_end2end_test_fixture f = |
Craig Tiller | 3569619 | 2015-05-24 15:00:37 -0700 | [diff] [blame] | 109 | begin_test(config, "test_cancel_after_invoke", mode, NULL, NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 110 | gpr_timespec deadline = five_seconds_time(); |
| 111 | cq_verifier *v_client = cq_verifier_create(f.client_cq); |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 112 | grpc_metadata_array initial_metadata_recv; |
| 113 | grpc_metadata_array trailing_metadata_recv; |
| 114 | grpc_metadata_array request_metadata_recv; |
| 115 | grpc_call_details call_details; |
| 116 | grpc_status_code status; |
| 117 | char *details = NULL; |
| 118 | size_t details_capacity = 0; |
| 119 | grpc_byte_buffer *response_payload_recv = NULL; |
| 120 | gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world"); |
| 121 | grpc_byte_buffer *request_payload = |
| 122 | grpc_byte_buffer_create(&request_payload_slice, 1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 123 | |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 124 | c = grpc_channel_create_call(f.client, f.client_cq, "/foo", |
Julien Boeuf | 597a4f2 | 2015-02-23 15:57:14 -0800 | [diff] [blame] | 125 | "foo.test.google.fr", deadline); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 126 | GPR_ASSERT(c); |
| 127 | |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 128 | 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); |
| 132 | |
| 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->data.recv_status_on_client.status_details_capacity = &details_capacity; |
| 139 | op++; |
| 140 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 141 | op->data.send_initial_metadata.count = 0; |
| 142 | op++; |
| 143 | op->op = GRPC_OP_SEND_MESSAGE; |
| 144 | op->data.send_message = request_payload; |
| 145 | op++; |
| 146 | op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 147 | op++; |
| 148 | op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 149 | op->data.recv_initial_metadata = &initial_metadata_recv; |
| 150 | op++; |
| 151 | op->op = GRPC_OP_RECV_MESSAGE; |
| 152 | op->data.recv_message = &response_payload_recv; |
| 153 | op++; |
| 154 | GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(c, ops, test_ops, tag(1))); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 155 | |
Craig Tiller | 7b01878 | 2015-01-16 09:53:39 -0800 | [diff] [blame] | 156 | GPR_ASSERT(GRPC_CALL_OK == mode.initiate_cancel(c)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 157 | |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 158 | cq_expect_completion(v_client, tag(1), 1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 159 | cq_verify(v_client); |
| 160 | |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 161 | GPR_ASSERT(status == mode.expect_status); |
| 162 | GPR_ASSERT(0 == strcmp(details, mode.expect_details)); |
| 163 | |
| 164 | grpc_metadata_array_destroy(&initial_metadata_recv); |
| 165 | grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 166 | grpc_metadata_array_destroy(&request_metadata_recv); |
| 167 | grpc_call_details_destroy(&call_details); |
| 168 | |
| 169 | grpc_byte_buffer_destroy(request_payload); |
| 170 | grpc_byte_buffer_destroy(response_payload_recv); |
| 171 | gpr_free(details); |
| 172 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 173 | grpc_call_destroy(c); |
| 174 | |
| 175 | cq_verifier_destroy(v_client); |
| 176 | end_test(&f); |
| 177 | config.tear_down_data(&f); |
| 178 | } |
| 179 | |
| 180 | void grpc_end2end_tests(grpc_end2end_test_config config) { |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 181 | unsigned i, j; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 182 | |
Craig Tiller | 8b976d0 | 2015-02-05 21:41:23 -0800 | [diff] [blame] | 183 | for (j = 2; j < 6; j++) { |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 184 | for (i = 0; i < GPR_ARRAY_SIZE(cancellation_modes); i++) { |
| 185 | test_cancel_after_invoke(config, cancellation_modes[i], j); |
| 186 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 187 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 188 | } |