blob: bf11b49fa4a7a27e64c84008fe3d2b17ee5c2b48 [file] [log] [blame]
Craig Tiller4ffdcd52015-01-16 11:34:55 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Craig Tiller4ffdcd52015-01-16 11:34:55 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
Craig Tiller4ffdcd52015-01-16 11:34:55 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller4ffdcd52015-01-16 11:34:55 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Craig Tiller4ffdcd52015-01-16 11:34:55 -080016 *
17 */
18
19#include "test/core/end2end/end2end_tests.h"
20
21#include <stdio.h>
22#include <string.h>
Craig Tiller4ffdcd52015-01-16 11:34:55 -080023
24#include <grpc/byte_buffer.h>
25#include <grpc/support/alloc.h>
26#include <grpc/support/log.h>
27#include <grpc/support/time.h>
28#include <grpc/support/useful.h>
29#include "test/core/end2end/cq_verifier.h"
30
Craig Tillerbaa14a92017-11-03 09:09:36 -070031static void* tag(intptr_t t) { return (void*)t; }
Craig Tiller4ffdcd52015-01-16 11:34:55 -080032
33static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
Craig Tillerbaa14a92017-11-03 09:09:36 -070034 const char* test_name,
35 grpc_channel_args* client_args,
36 grpc_channel_args* server_args) {
Craig Tiller4ffdcd52015-01-16 11:34:55 -080037 grpc_end2end_test_fixture f;
Robbie Shade55a046a2017-01-25 15:14:28 -050038 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
Craig Tiller4ffdcd52015-01-16 11:34:55 -080039 f = config.create_fixture(client_args, server_args);
Craig Tiller4ffdcd52015-01-16 11:34:55 -080040 config.init_server(&f, server_args);
Mark D. Rothe127a392016-10-27 08:27:15 -070041 config.init_client(&f, client_args);
Craig Tiller4ffdcd52015-01-16 11:34:55 -080042 return f;
43}
44
Chris Evansed2a5472017-03-27 17:34:51 -050045static gpr_timespec n_seconds_from_now(int n) {
Robbie Shadeca7effc2017-01-17 09:14:29 -050046 return grpc_timeout_seconds_to_deadline(n);
Craig Tiller4ffdcd52015-01-16 11:34:55 -080047}
48
Chris Evansed2a5472017-03-27 17:34:51 -050049static gpr_timespec five_seconds_from_now(void) {
50 return n_seconds_from_now(5);
51}
Craig Tiller4ffdcd52015-01-16 11:34:55 -080052
Craig Tillerbaa14a92017-11-03 09:09:36 -070053static void drain_cq(grpc_completion_queue* cq) {
Craig Tiller64be9f72015-05-04 14:53:51 -070054 grpc_event ev;
Craig Tiller4ffdcd52015-01-16 11:34:55 -080055 do {
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080056 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
Craig Tiller64be9f72015-05-04 14:53:51 -070057 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
Craig Tiller4ffdcd52015-01-16 11:34:55 -080058}
59
Craig Tillerbaa14a92017-11-03 09:09:36 -070060static void shutdown_server(grpc_end2end_test_fixture* f) {
Craig Tiller4ffdcd52015-01-16 11:34:55 -080061 if (!f->server) return;
62 grpc_server_destroy(f->server);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080063 f->server = nullptr;
Craig Tiller4ffdcd52015-01-16 11:34:55 -080064}
65
Craig Tillerbaa14a92017-11-03 09:09:36 -070066static void shutdown_client(grpc_end2end_test_fixture* f) {
Craig Tiller4ffdcd52015-01-16 11:34:55 -080067 if (!f->client) return;
68 grpc_channel_destroy(f->client);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080069 f->client = nullptr;
Craig Tiller4ffdcd52015-01-16 11:34:55 -080070}
71
Craig Tillerbaa14a92017-11-03 09:09:36 -070072static void end_test(grpc_end2end_test_fixture* f) {
Craig Tiller4ffdcd52015-01-16 11:34:55 -080073 shutdown_server(f);
74 shutdown_client(f);
75
Craig Tillerbc0ec332015-05-11 12:11:32 -070076 grpc_completion_queue_shutdown(f->cq);
77 drain_cq(f->cq);
78 grpc_completion_queue_destroy(f->cq);
Sree Kuchibhotla321881d2017-02-27 11:25:28 -080079 /* Note: shutdown_cq is not used in this test */
80 grpc_completion_queue_destroy(f->shutdown_cq);
Craig Tiller4ffdcd52015-01-16 11:34:55 -080081}
82
83static void test_early_server_shutdown_finishes_inflight_calls(
84 grpc_end2end_test_config config) {
Craig Tillerbaa14a92017-11-03 09:09:36 -070085 grpc_call* c;
86 grpc_call* s;
Craig Tillerbe98d242017-11-10 15:26:57 -080087 grpc_end2end_test_fixture f =
88 begin_test(config, "test_early_server_shutdown_finishes_inflight_calls",
89 nullptr, nullptr);
Craig Tillerbaa14a92017-11-03 09:09:36 -070090 cq_verifier* cqv = cq_verifier_create(f.cq);
Craig Tiller8b1d9892015-04-28 14:28:19 -070091 grpc_op ops[6];
Craig Tillerbaa14a92017-11-03 09:09:36 -070092 grpc_op* op;
Craig Tiller8b1d9892015-04-28 14:28:19 -070093 grpc_metadata_array initial_metadata_recv;
94 grpc_metadata_array trailing_metadata_recv;
95 grpc_metadata_array request_metadata_recv;
96 grpc_call_details call_details;
97 grpc_status_code status;
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +020098 grpc_call_error error;
Craig Tiller7c70b6c2017-01-23 07:48:42 -080099 grpc_slice details;
Craig Tiller8b1d9892015-04-28 14:28:19 -0700100 int was_cancelled = 2;
Craig Tiller4ffdcd52015-01-16 11:34:55 -0800101
Chris Evansed2a5472017-03-27 17:34:51 -0500102 gpr_timespec deadline = n_seconds_from_now(10);
Muxi Yan5ebd3272016-10-31 07:27:07 -0700103 c = grpc_channel_create_call(
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800104 f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800105 grpc_slice_from_static_string("/foo"),
106 get_host_override_slice("foo.test.google.fr:1234", config), deadline,
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800107 nullptr);
Craig Tiller4ffdcd52015-01-16 11:34:55 -0800108 GPR_ASSERT(c);
109
Craig Tiller8b1d9892015-04-28 14:28:19 -0700110 grpc_metadata_array_init(&initial_metadata_recv);
111 grpc_metadata_array_init(&trailing_metadata_recv);
112 grpc_metadata_array_init(&request_metadata_recv);
113 grpc_call_details_init(&call_details);
Craig Tiller4ffdcd52015-01-16 11:34:55 -0800114
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700115 memset(ops, 0, sizeof(ops));
Craig Tiller8b1d9892015-04-28 14:28:19 -0700116 op = ops;
117 op->op = GRPC_OP_SEND_INITIAL_METADATA;
118 op->data.send_initial_metadata.count = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800119 op->data.send_initial_metadata.metadata = nullptr;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700120 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800121 op->reserved = nullptr;
Craig Tiller8b1d9892015-04-28 14:28:19 -0700122 op++;
123 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700124 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800125 op->reserved = nullptr;
Craig Tiller8b1d9892015-04-28 14:28:19 -0700126 op++;
127 op->op = GRPC_OP_RECV_INITIAL_METADATA;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800128 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700129 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800130 op->reserved = nullptr;
Craig Tiller8b1d9892015-04-28 14:28:19 -0700131 op++;
132 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
133 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
134 op->data.recv_status_on_client.status = &status;
135 op->data.recv_status_on_client.status_details = &details;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700136 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800137 op->reserved = nullptr;
Craig Tiller8b1d9892015-04-28 14:28:19 -0700138 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800139 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr);
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200140 GPR_ASSERT(GRPC_CALL_OK == error);
Craig Tiller4ffdcd52015-01-16 11:34:55 -0800141
Craig Tillerd6c98df2015-08-18 09:33:44 -0700142 error =
143 grpc_server_request_call(f.server, &s, &call_details,
144 &request_metadata_recv, f.cq, f.cq, tag(101));
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200145 GPR_ASSERT(GRPC_CALL_OK == error);
Mark D. Roth7187ab92016-08-24 13:49:22 -0700146 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700147 cq_verify(cqv);
Craig Tiller4ffdcd52015-01-16 11:34:55 -0800148
Craig Tiller8b1d9892015-04-28 14:28:19 -0700149 /* shutdown and destroy the server */
Craig Tillerba2e7552015-05-28 12:53:54 -0700150 grpc_server_shutdown_and_notify(f.server, f.cq, tag(0xdead));
Craig Tillerbc0ec332015-05-11 12:11:32 -0700151 cq_verify_empty(cqv);
Craig Tiller4ffdcd52015-01-16 11:34:55 -0800152
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700153 memset(ops, 0, sizeof(ops));
Craig Tiller8b1d9892015-04-28 14:28:19 -0700154 op = ops;
155 op->op = GRPC_OP_SEND_INITIAL_METADATA;
156 op->data.send_initial_metadata.count = 0;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700157 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800158 op->reserved = nullptr;
Craig Tiller8b1d9892015-04-28 14:28:19 -0700159 op++;
160 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
161 op->data.send_status_from_server.trailing_metadata_count = 0;
162 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800163 grpc_slice status_details = grpc_slice_from_static_string("xyz");
164 op->data.send_status_from_server.status_details = &status_details;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700165 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800166 op->reserved = nullptr;
Craig Tiller8b1d9892015-04-28 14:28:19 -0700167 op++;
168 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
169 op->data.recv_close_on_server.cancelled = &was_cancelled;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700170 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800171 op->reserved = nullptr;
Craig Tiller8b1d9892015-04-28 14:28:19 -0700172 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800173 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr);
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200174 GPR_ASSERT(GRPC_CALL_OK == error);
Craig Tiller8b1d9892015-04-28 14:28:19 -0700175
Mark D. Roth7187ab92016-08-24 13:49:22 -0700176 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
177 CQ_EXPECT_COMPLETION(cqv, tag(0xdead), 1);
178 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700179 cq_verify(cqv);
Craig Tiller4ffdcd52015-01-16 11:34:55 -0800180
Craig Tillerdd36b152017-03-31 08:27:28 -0700181 grpc_call_unref(s);
Craig Tiller4ffdcd52015-01-16 11:34:55 -0800182
Craig Tillerf1071202015-04-30 13:29:48 -0700183 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800184 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
Muxi Yan5ebd3272016-10-31 07:27:07 -0700185 validate_host_override_string("foo.test.google.fr:1234", call_details.host,
186 config);
Craig Tilleraea081f2015-06-11 14:19:33 -0700187 GPR_ASSERT(was_cancelled == 1);
Craig Tiller8b1d9892015-04-28 14:28:19 -0700188
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800189 grpc_slice_unref(details);
Craig Tiller8b1d9892015-04-28 14:28:19 -0700190 grpc_metadata_array_destroy(&initial_metadata_recv);
191 grpc_metadata_array_destroy(&trailing_metadata_recv);
192 grpc_metadata_array_destroy(&request_metadata_recv);
193 grpc_call_details_destroy(&call_details);
194
Craig Tillerdd36b152017-03-31 08:27:28 -0700195 grpc_call_unref(c);
Craig Tiller4ffdcd52015-01-16 11:34:55 -0800196
Craig Tillerbc0ec332015-05-11 12:11:32 -0700197 cq_verifier_destroy(cqv);
Craig Tiller4ffdcd52015-01-16 11:34:55 -0800198
199 end_test(&f);
200 config.tear_down_data(&f);
201}
202
Craig Tiller0fe5ee72015-12-22 12:50:36 -0800203void graceful_server_shutdown(grpc_end2end_test_config config) {
Craig Tiller4ffdcd52015-01-16 11:34:55 -0800204 test_early_server_shutdown_finishes_inflight_calls(config);
Craig Tiller190d3602015-02-18 09:23:38 -0800205}
Craig Tiller9e9edbc2016-04-04 10:38:49 -0700206
207void graceful_server_shutdown_pre_init(void) {}