ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -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> |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -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" |
| 45 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 46 | static void *tag(intptr_t t) { return (void *)t; } |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 47 | |
| 48 | static gpr_timespec n_seconds_time(int n) { |
Robbie Shade | ca7effc | 2017-01-17 09:14:29 -0500 | [diff] [blame^] | 49 | return grpc_timeout_seconds_to_deadline(n); |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 52 | static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 53 | |
| 54 | static void drain_cq(grpc_completion_queue *cq) { |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 55 | grpc_event ev; |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 56 | do { |
Nicolas "Pixel" Noble | 9a123df | 2015-07-29 23:45:08 +0200 | [diff] [blame] | 57 | ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 58 | } while (ev.type != GRPC_QUEUE_SHUTDOWN); |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static void shutdown_server(grpc_end2end_test_fixture *f) { |
| 62 | if (!f->server) return; |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 63 | grpc_server_destroy(f->server); |
| 64 | f->server = NULL; |
| 65 | } |
| 66 | |
| 67 | static void shutdown_client(grpc_end2end_test_fixture *f) { |
| 68 | if (!f->client) return; |
| 69 | grpc_channel_destroy(f->client); |
| 70 | f->client = NULL; |
| 71 | } |
| 72 | |
| 73 | static void end_test(grpc_end2end_test_fixture *f) { |
| 74 | shutdown_server(f); |
| 75 | shutdown_client(f); |
| 76 | |
Craig Tiller | bc0ec33 | 2015-05-11 12:11:32 -0700 | [diff] [blame] | 77 | grpc_completion_queue_shutdown(f->cq); |
| 78 | drain_cq(f->cq); |
| 79 | grpc_completion_queue_destroy(f->cq); |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Muxi Yan | 56456c3 | 2016-10-30 23:11:11 -0700 | [diff] [blame] | 82 | static void do_request_and_shutdown_server(grpc_end2end_test_config config, |
| 83 | grpc_end2end_test_fixture *f, |
Craig Tiller | bc0ec33 | 2015-05-11 12:11:32 -0700 | [diff] [blame] | 84 | cq_verifier *cqv) { |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 85 | grpc_call *c; |
| 86 | grpc_call *s; |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 87 | gpr_timespec deadline = five_seconds_time(); |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 88 | grpc_op ops[6]; |
| 89 | grpc_op *op; |
| 90 | grpc_metadata_array initial_metadata_recv; |
| 91 | grpc_metadata_array trailing_metadata_recv; |
| 92 | grpc_metadata_array request_metadata_recv; |
| 93 | grpc_call_details call_details; |
| 94 | grpc_status_code status; |
Nicolas "Pixel" Noble | 9a123df | 2015-07-29 23:45:08 +0200 | [diff] [blame] | 95 | grpc_call_error error; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 96 | grpc_slice details; |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 97 | int was_cancelled = 2; |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 98 | |
Muxi Yan | 5ebd327 | 2016-10-31 07:27:07 -0700 | [diff] [blame] | 99 | c = grpc_channel_create_call( |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 100 | f->client, NULL, GRPC_PROPAGATE_DEFAULTS, f->cq, |
| 101 | grpc_slice_from_static_string("/foo"), |
| 102 | get_host_override_slice("foo.test.google.fr:1234", config), deadline, |
Muxi Yan | 5ebd327 | 2016-10-31 07:27:07 -0700 | [diff] [blame] | 103 | NULL); |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 104 | GPR_ASSERT(c); |
| 105 | |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 106 | grpc_metadata_array_init(&initial_metadata_recv); |
| 107 | grpc_metadata_array_init(&trailing_metadata_recv); |
| 108 | grpc_metadata_array_init(&request_metadata_recv); |
| 109 | grpc_call_details_init(&call_details); |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 110 | |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 111 | memset(ops, 0, sizeof(ops)); |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 112 | op = ops; |
| 113 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 114 | op->data.send_initial_metadata.count = 0; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 115 | op->flags = 0; |
Nicolas "Pixel" Noble | 4960516 | 2015-08-01 00:12:01 +0200 | [diff] [blame] | 116 | op->reserved = NULL; |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 117 | op++; |
| 118 | op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 119 | op->flags = 0; |
Nicolas "Pixel" Noble | 4960516 | 2015-08-01 00:12:01 +0200 | [diff] [blame] | 120 | op->reserved = NULL; |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 121 | op++; |
| 122 | op->op = GRPC_OP_RECV_INITIAL_METADATA; |
Mark D. Roth | 435f9f2 | 2017-01-25 12:53:54 -0800 | [diff] [blame] | 123 | op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 124 | op->flags = 0; |
Nicolas "Pixel" Noble | 4960516 | 2015-08-01 00:12:01 +0200 | [diff] [blame] | 125 | op->reserved = NULL; |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 126 | op++; |
| 127 | op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 128 | op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 129 | op->data.recv_status_on_client.status = &status; |
| 130 | op->data.recv_status_on_client.status_details = &details; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 131 | op->flags = 0; |
Nicolas "Pixel" Noble | 4960516 | 2015-08-01 00:12:01 +0200 | [diff] [blame] | 132 | op->reserved = NULL; |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 133 | op++; |
Craig Tiller | 3121fd4 | 2015-09-10 09:56:20 -0700 | [diff] [blame] | 134 | error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); |
Nicolas "Pixel" Noble | 9a123df | 2015-07-29 23:45:08 +0200 | [diff] [blame] | 135 | GPR_ASSERT(GRPC_CALL_OK == error); |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 136 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 137 | error = |
| 138 | grpc_server_request_call(f->server, &s, &call_details, |
| 139 | &request_metadata_recv, f->cq, f->cq, tag(101)); |
Nicolas "Pixel" Noble | 9a123df | 2015-07-29 23:45:08 +0200 | [diff] [blame] | 140 | GPR_ASSERT(GRPC_CALL_OK == error); |
Mark D. Roth | 7187ab9 | 2016-08-24 13:49:22 -0700 | [diff] [blame] | 141 | CQ_EXPECT_COMPLETION(cqv, tag(101), 1); |
Craig Tiller | bc0ec33 | 2015-05-11 12:11:32 -0700 | [diff] [blame] | 142 | cq_verify(cqv); |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 143 | |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 144 | /* should be able to shut down the server early |
| 145 | - and still complete the request */ |
Craig Tiller | ba2e755 | 2015-05-28 12:53:54 -0700 | [diff] [blame] | 146 | grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 147 | |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 148 | memset(ops, 0, sizeof(ops)); |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 149 | op = ops; |
| 150 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 151 | op->data.send_initial_metadata.count = 0; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 152 | op->flags = 0; |
Nicolas "Pixel" Noble | 4960516 | 2015-08-01 00:12:01 +0200 | [diff] [blame] | 153 | op->reserved = NULL; |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 154 | op++; |
| 155 | op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
| 156 | op->data.send_status_from_server.trailing_metadata_count = 0; |
| 157 | op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 158 | grpc_slice status_details = grpc_slice_from_static_string("xyz"); |
| 159 | op->data.send_status_from_server.status_details = &status_details; |
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 | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 162 | op++; |
| 163 | op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
| 164 | op->data.recv_close_on_server.cancelled = &was_cancelled; |
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 | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 167 | op++; |
Craig Tiller | 3121fd4 | 2015-09-10 09:56:20 -0700 | [diff] [blame] | 168 | error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); |
Nicolas "Pixel" Noble | 9a123df | 2015-07-29 23:45:08 +0200 | [diff] [blame] | 169 | GPR_ASSERT(GRPC_CALL_OK == error); |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 170 | |
Mark D. Roth | 7187ab9 | 2016-08-24 13:49:22 -0700 | [diff] [blame] | 171 | CQ_EXPECT_COMPLETION(cqv, tag(102), 1); |
| 172 | CQ_EXPECT_COMPLETION(cqv, tag(1), 1); |
| 173 | CQ_EXPECT_COMPLETION(cqv, tag(1000), 1); |
Craig Tiller | bc0ec33 | 2015-05-11 12:11:32 -0700 | [diff] [blame] | 174 | cq_verify(cqv); |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 175 | |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 176 | GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 177 | GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz")); |
| 178 | GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo")); |
Muxi Yan | 5ebd327 | 2016-10-31 07:27:07 -0700 | [diff] [blame] | 179 | validate_host_override_string("foo.test.google.fr:1234", call_details.host, |
| 180 | config); |
Craig Tiller | aea081f | 2015-06-11 14:19:33 -0700 | [diff] [blame] | 181 | GPR_ASSERT(was_cancelled == 1); |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 182 | |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 183 | grpc_slice_unref(details); |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 184 | grpc_metadata_array_destroy(&initial_metadata_recv); |
| 185 | grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 186 | grpc_metadata_array_destroy(&request_metadata_recv); |
| 187 | grpc_call_details_destroy(&call_details); |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 188 | |
| 189 | grpc_call_destroy(c); |
| 190 | grpc_call_destroy(s); |
| 191 | } |
| 192 | |
| 193 | static void disappearing_server_test(grpc_end2end_test_config config) { |
| 194 | grpc_end2end_test_fixture f = config.create_fixture(NULL, NULL); |
Craig Tiller | bc0ec33 | 2015-05-11 12:11:32 -0700 | [diff] [blame] | 195 | cq_verifier *cqv = cq_verifier_create(f.cq); |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 196 | |
Robbie Shade | 55a046a | 2017-01-25 15:14:28 -0500 | [diff] [blame] | 197 | gpr_log(GPR_INFO, "Running test: %s/%s", "disappearing_server_test", |
| 198 | config.name); |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 199 | |
Mark D. Roth | e127a39 | 2016-10-27 08:27:15 -0700 | [diff] [blame] | 200 | config.init_client(&f, NULL); |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 201 | config.init_server(&f, NULL); |
| 202 | |
Muxi Yan | 56456c3 | 2016-10-30 23:11:11 -0700 | [diff] [blame] | 203 | do_request_and_shutdown_server(config, &f, cqv); |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 204 | |
| 205 | /* now destroy and recreate the server */ |
| 206 | config.init_server(&f, NULL); |
| 207 | |
Muxi Yan | 56456c3 | 2016-10-30 23:11:11 -0700 | [diff] [blame] | 208 | do_request_and_shutdown_server(config, &f, cqv); |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 209 | |
Craig Tiller | bc0ec33 | 2015-05-11 12:11:32 -0700 | [diff] [blame] | 210 | cq_verifier_destroy(cqv); |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 211 | |
| 212 | end_test(&f); |
| 213 | config.tear_down_data(&f); |
| 214 | } |
| 215 | |
Craig Tiller | 0fe5ee7 | 2015-12-22 12:50:36 -0800 | [diff] [blame] | 216 | void disappearing_server(grpc_end2end_test_config config) { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 217 | GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION); |
| 218 | disappearing_server_test(config); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 219 | } |
Craig Tiller | 9e9edbc | 2016-04-04 10:38:49 -0700 | [diff] [blame] | 220 | |
| 221 | void disappearing_server_pre_init(void) {} |