blob: 05440a6f56cc2c9f201fd1271e76abd2dbbb77d8 [file] [log] [blame]
ctillerc6d61c42014-12-15 14:52:08 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
ctillerc6d61c42014-12-15 14:52:08 -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>
ctillerc6d61c42014-12-15 14:52:08 -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"
45
Craig Tiller7536af02015-12-22 13:49:30 -080046static void *tag(intptr_t t) { return (void *)t; }
ctillerc6d61c42014-12-15 14:52:08 -080047
48static gpr_timespec n_seconds_time(int n) {
Robbie Shadeca7effc2017-01-17 09:14:29 -050049 return grpc_timeout_seconds_to_deadline(n);
ctillerc6d61c42014-12-15 14:52:08 -080050}
51
Craig Tiller32946d32015-01-15 11:37:30 -080052static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); }
ctillerc6d61c42014-12-15 14:52:08 -080053
54static void drain_cq(grpc_completion_queue *cq) {
Craig Tiller64be9f72015-05-04 14:53:51 -070055 grpc_event ev;
ctillerc6d61c42014-12-15 14:52:08 -080056 do {
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +020057 ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL);
Craig Tiller64be9f72015-05-04 14:53:51 -070058 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
ctillerc6d61c42014-12-15 14:52:08 -080059}
60
61static void shutdown_server(grpc_end2end_test_fixture *f) {
62 if (!f->server) return;
ctillerc6d61c42014-12-15 14:52:08 -080063 grpc_server_destroy(f->server);
64 f->server = NULL;
65}
66
67static 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
73static void end_test(grpc_end2end_test_fixture *f) {
74 shutdown_server(f);
75 shutdown_client(f);
76
Craig Tillerbc0ec332015-05-11 12:11:32 -070077 grpc_completion_queue_shutdown(f->cq);
78 drain_cq(f->cq);
79 grpc_completion_queue_destroy(f->cq);
ctillerc6d61c42014-12-15 14:52:08 -080080}
81
Muxi Yan56456c32016-10-30 23:11:11 -070082static void do_request_and_shutdown_server(grpc_end2end_test_config config,
83 grpc_end2end_test_fixture *f,
Craig Tillerbc0ec332015-05-11 12:11:32 -070084 cq_verifier *cqv) {
ctillerc6d61c42014-12-15 14:52:08 -080085 grpc_call *c;
86 grpc_call *s;
ctillerc6d61c42014-12-15 14:52:08 -080087 gpr_timespec deadline = five_seconds_time();
Craig Tiller8b1d9892015-04-28 14:28:19 -070088 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" Noble9a123df2015-07-29 23:45:08 +020095 grpc_call_error error;
Craig Tiller7c70b6c2017-01-23 07:48:42 -080096 grpc_slice details;
Craig Tiller8b1d9892015-04-28 14:28:19 -070097 int was_cancelled = 2;
ctillerc6d61c42014-12-15 14:52:08 -080098
Muxi Yan5ebd3272016-10-31 07:27:07 -070099 c = grpc_channel_create_call(
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800100 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 Yan5ebd3272016-10-31 07:27:07 -0700103 NULL);
ctillerc6d61c42014-12-15 14:52:08 -0800104 GPR_ASSERT(c);
105
Craig Tiller8b1d9892015-04-28 14:28:19 -0700106 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);
ctillerc6d61c42014-12-15 14:52:08 -0800110
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700111 memset(ops, 0, sizeof(ops));
Craig Tiller8b1d9892015-04-28 14:28:19 -0700112 op = ops;
113 op->op = GRPC_OP_SEND_INITIAL_METADATA;
114 op->data.send_initial_metadata.count = 0;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700115 op->flags = 0;
Nicolas "Pixel" Noble49605162015-08-01 00:12:01 +0200116 op->reserved = NULL;
Craig Tiller8b1d9892015-04-28 14:28:19 -0700117 op++;
118 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700119 op->flags = 0;
Nicolas "Pixel" Noble49605162015-08-01 00:12:01 +0200120 op->reserved = NULL;
Craig Tiller8b1d9892015-04-28 14:28:19 -0700121 op++;
122 op->op = GRPC_OP_RECV_INITIAL_METADATA;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800123 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700124 op->flags = 0;
Nicolas "Pixel" Noble49605162015-08-01 00:12:01 +0200125 op->reserved = NULL;
Craig Tiller8b1d9892015-04-28 14:28:19 -0700126 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 Quintas1d5aca52015-06-14 14:42:04 -0700131 op->flags = 0;
Nicolas "Pixel" Noble49605162015-08-01 00:12:01 +0200132 op->reserved = NULL;
Craig Tiller8b1d9892015-04-28 14:28:19 -0700133 op++;
Craig Tiller3121fd42015-09-10 09:56:20 -0700134 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL);
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200135 GPR_ASSERT(GRPC_CALL_OK == error);
ctillerc6d61c42014-12-15 14:52:08 -0800136
Craig Tillerd6c98df2015-08-18 09:33:44 -0700137 error =
138 grpc_server_request_call(f->server, &s, &call_details,
139 &request_metadata_recv, f->cq, f->cq, tag(101));
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200140 GPR_ASSERT(GRPC_CALL_OK == error);
Mark D. Roth7187ab92016-08-24 13:49:22 -0700141 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700142 cq_verify(cqv);
ctillerc6d61c42014-12-15 14:52:08 -0800143
ctillerc6d61c42014-12-15 14:52:08 -0800144 /* should be able to shut down the server early
145 - and still complete the request */
Craig Tillerba2e7552015-05-28 12:53:54 -0700146 grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
ctillerc6d61c42014-12-15 14:52:08 -0800147
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700148 memset(ops, 0, sizeof(ops));
Craig Tiller8b1d9892015-04-28 14:28:19 -0700149 op = ops;
150 op->op = GRPC_OP_SEND_INITIAL_METADATA;
151 op->data.send_initial_metadata.count = 0;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700152 op->flags = 0;
Nicolas "Pixel" Noble49605162015-08-01 00:12:01 +0200153 op->reserved = NULL;
Craig Tiller8b1d9892015-04-28 14:28:19 -0700154 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 Tiller7c70b6c2017-01-23 07:48:42 -0800158 grpc_slice status_details = grpc_slice_from_static_string("xyz");
159 op->data.send_status_from_server.status_details = &status_details;
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 Tiller8b1d9892015-04-28 14:28:19 -0700162 op++;
163 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
164 op->data.recv_close_on_server.cancelled = &was_cancelled;
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 Tiller8b1d9892015-04-28 14:28:19 -0700167 op++;
Craig Tiller3121fd42015-09-10 09:56:20 -0700168 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL);
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200169 GPR_ASSERT(GRPC_CALL_OK == error);
Craig Tiller8b1d9892015-04-28 14:28:19 -0700170
Mark D. Roth7187ab92016-08-24 13:49:22 -0700171 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
172 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
173 CQ_EXPECT_COMPLETION(cqv, tag(1000), 1);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700174 cq_verify(cqv);
ctillerc6d61c42014-12-15 14:52:08 -0800175
Craig Tiller8b1d9892015-04-28 14:28:19 -0700176 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800177 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
178 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
Muxi Yan5ebd3272016-10-31 07:27:07 -0700179 validate_host_override_string("foo.test.google.fr:1234", call_details.host,
180 config);
Craig Tilleraea081f2015-06-11 14:19:33 -0700181 GPR_ASSERT(was_cancelled == 1);
Craig Tiller8b1d9892015-04-28 14:28:19 -0700182
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800183 grpc_slice_unref(details);
Craig Tiller8b1d9892015-04-28 14:28:19 -0700184 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);
ctillerc6d61c42014-12-15 14:52:08 -0800188
189 grpc_call_destroy(c);
190 grpc_call_destroy(s);
191}
192
193static void disappearing_server_test(grpc_end2end_test_config config) {
194 grpc_end2end_test_fixture f = config.create_fixture(NULL, NULL);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700195 cq_verifier *cqv = cq_verifier_create(f.cq);
ctillerc6d61c42014-12-15 14:52:08 -0800196
Robbie Shade55a046a2017-01-25 15:14:28 -0500197 gpr_log(GPR_INFO, "Running test: %s/%s", "disappearing_server_test",
198 config.name);
ctillerc6d61c42014-12-15 14:52:08 -0800199
Mark D. Rothe127a392016-10-27 08:27:15 -0700200 config.init_client(&f, NULL);
ctillerc6d61c42014-12-15 14:52:08 -0800201 config.init_server(&f, NULL);
202
Muxi Yan56456c32016-10-30 23:11:11 -0700203 do_request_and_shutdown_server(config, &f, cqv);
ctillerc6d61c42014-12-15 14:52:08 -0800204
205 /* now destroy and recreate the server */
206 config.init_server(&f, NULL);
207
Muxi Yan56456c32016-10-30 23:11:11 -0700208 do_request_and_shutdown_server(config, &f, cqv);
ctillerc6d61c42014-12-15 14:52:08 -0800209
Craig Tillerbc0ec332015-05-11 12:11:32 -0700210 cq_verifier_destroy(cqv);
ctillerc6d61c42014-12-15 14:52:08 -0800211
212 end_test(&f);
213 config.tear_down_data(&f);
214}
215
Craig Tiller0fe5ee72015-12-22 12:50:36 -0800216void disappearing_server(grpc_end2end_test_config config) {
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700217 GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION);
218 disappearing_server_test(config);
Craig Tiller190d3602015-02-18 09:23:38 -0800219}
Craig Tiller9e9edbc2016-04-04 10:38:49 -0700220
221void disappearing_server_pre_init(void) {}