blob: 1112375176e6324a6e5a23bdca09e7a7fbb9877e [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -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
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -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.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080016 *
17 */
18
19#include "test/core/end2end/end2end_tests.h"
20
21#include <stdio.h>
22#include <string.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -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; }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -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,
Mark D. Rothd4ac3a12017-09-05 13:47:15 -070035 size_t num_ops,
Craig Tillerbaa14a92017-11-03 09:09:36 -070036 grpc_channel_args* client_args,
37 grpc_channel_args* server_args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080038 grpc_end2end_test_fixture f;
Mark D. Rothd4ac3a12017-09-05 13:47:15 -070039 gpr_log(GPR_INFO, "Running test: %s/%s [%" PRIdPTR " ops]", test_name,
40 config.name, num_ops);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041 f = config.create_fixture(client_args, server_args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080042 config.init_server(&f, server_args);
Mark D. Rothe127a392016-10-27 08:27:15 -070043 config.init_client(&f, client_args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080044 return f;
45}
46
Chris Evansed2a5472017-03-27 17:34:51 -050047static gpr_timespec n_seconds_from_now(int n) {
Robbie Shadeca7effc2017-01-17 09:14:29 -050048 return grpc_timeout_seconds_to_deadline(n);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080049}
50
Chris Evansed2a5472017-03-27 17:34:51 -050051static gpr_timespec five_seconds_from_now(void) {
52 return n_seconds_from_now(5);
53}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080054
Craig Tillerbaa14a92017-11-03 09:09:36 -070055static void drain_cq(grpc_completion_queue* cq) {
Craig Tiller64be9f72015-05-04 14:53:51 -070056 grpc_event ev;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080057 do {
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080058 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
Craig Tiller64be9f72015-05-04 14:53:51 -070059 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080060}
61
Craig Tillerbaa14a92017-11-03 09:09:36 -070062static void shutdown_server(grpc_end2end_test_fixture* f) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080063 if (!f->server) return;
Sree Kuchibhotla321881d2017-02-27 11:25:28 -080064 grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
65 GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
66 grpc_timeout_seconds_to_deadline(5),
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080067 nullptr)
Craig Tillerf40df232016-03-25 13:38:14 -070068 .type == GRPC_OP_COMPLETE);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080069 grpc_server_destroy(f->server);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080070 f->server = nullptr;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080071}
72
Craig Tillerbaa14a92017-11-03 09:09:36 -070073static void shutdown_client(grpc_end2end_test_fixture* f) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080074 if (!f->client) return;
75 grpc_channel_destroy(f->client);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080076 f->client = nullptr;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080077}
78
Craig Tillerbaa14a92017-11-03 09:09:36 -070079static void end_test(grpc_end2end_test_fixture* f) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080080 shutdown_server(f);
81 shutdown_client(f);
82
Craig Tiller5cef1b32015-05-11 11:00:42 -070083 grpc_completion_queue_shutdown(f->cq);
84 drain_cq(f->cq);
85 grpc_completion_queue_destroy(f->cq);
Sree Kuchibhotla321881d2017-02-27 11:25:28 -080086 grpc_completion_queue_destroy(f->shutdown_cq);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080087}
88
89/* Cancel before invoke */
Craig Tiller8ad8a412015-02-25 08:36:40 -080090static void test_cancel_before_invoke(grpc_end2end_test_config config,
Craig Tiller3121fd42015-09-10 09:56:20 -070091 size_t test_ops) {
Craig Tillerfee91b42015-02-05 16:03:13 -080092 grpc_op ops[6];
Craig Tillerbaa14a92017-11-03 09:09:36 -070093 grpc_op* op;
94 grpc_call* c;
Craig Tiller9a576332015-06-17 10:21:49 -070095 grpc_end2end_test_fixture f =
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080096 begin_test(config, "cancel_before_invoke", test_ops, nullptr, nullptr);
Craig Tillerbaa14a92017-11-03 09:09:36 -070097 cq_verifier* cqv = cq_verifier_create(f.cq);
Craig Tillerfee91b42015-02-05 16:03:13 -080098 grpc_metadata_array initial_metadata_recv;
99 grpc_metadata_array trailing_metadata_recv;
100 grpc_metadata_array request_metadata_recv;
101 grpc_call_details call_details;
102 grpc_status_code status;
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200103 grpc_call_error error;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800104 grpc_slice details;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800105 grpc_byte_buffer* response_payload_recv = nullptr;
Craig Tiller28b72422016-10-26 21:15:29 -0700106 grpc_slice request_payload_slice =
107 grpc_slice_from_copied_string("hello world");
Craig Tillerbaa14a92017-11-03 09:09:36 -0700108 grpc_byte_buffer* request_payload =
David Garcia Quintas59f905d2015-06-08 16:31:19 -0700109 grpc_raw_byte_buffer_create(&request_payload_slice, 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800110
Chris Evansed2a5472017-03-27 17:34:51 -0500111 gpr_timespec deadline = five_seconds_from_now();
Muxi Yan5ebd3272016-10-31 07:27:07 -0700112 c = grpc_channel_create_call(
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800113 f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800114 grpc_slice_from_static_string("/foo"),
115 get_host_override_slice("foo.test.google.fr:1234", config), deadline,
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800116 nullptr);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800117 GPR_ASSERT(c);
118
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800119 GPR_ASSERT(GRPC_CALL_OK == grpc_call_cancel(c, nullptr));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800120
Craig Tillerfee91b42015-02-05 16:03:13 -0800121 grpc_metadata_array_init(&initial_metadata_recv);
122 grpc_metadata_array_init(&trailing_metadata_recv);
123 grpc_metadata_array_init(&request_metadata_recv);
124 grpc_call_details_init(&call_details);
125
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700126 memset(ops, 0, sizeof(ops));
Craig Tillerfee91b42015-02-05 16:03:13 -0800127 op = ops;
128 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
129 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
130 op->data.recv_status_on_client.status = &status;
131 op->data.recv_status_on_client.status_details = &details;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700132 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800133 op->reserved = nullptr;
Craig Tillerfee91b42015-02-05 16:03:13 -0800134 op++;
135 op->op = GRPC_OP_SEND_INITIAL_METADATA;
136 op->data.send_initial_metadata.count = 0;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700137 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800138 op->reserved = nullptr;
Craig Tillerfee91b42015-02-05 16:03:13 -0800139 op++;
140 op->op = GRPC_OP_SEND_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800141 op->data.send_message.send_message = request_payload;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700142 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800143 op->reserved = nullptr;
Craig Tillerfee91b42015-02-05 16:03:13 -0800144 op++;
145 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700146 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800147 op->reserved = nullptr;
Craig Tillerfee91b42015-02-05 16:03:13 -0800148 op++;
149 op->op = GRPC_OP_RECV_INITIAL_METADATA;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800150 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700151 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800152 op->reserved = nullptr;
Craig Tillerfee91b42015-02-05 16:03:13 -0800153 op++;
154 op->op = GRPC_OP_RECV_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800155 op->data.recv_message.recv_message = &response_payload_recv;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700156 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800157 op->reserved = nullptr;
Craig Tillerfee91b42015-02-05 16:03:13 -0800158 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800159 error = grpc_call_start_batch(c, ops, test_ops, tag(1), nullptr);
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200160 GPR_ASSERT(GRPC_CALL_OK == error);
Craig Tillerfee91b42015-02-05 16:03:13 -0800161
Mark D. Roth7187ab92016-08-24 13:49:22 -0700162 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
Craig Tiller5cef1b32015-05-11 11:00:42 -0700163 cq_verify(cqv);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800164
Craig Tillerfee91b42015-02-05 16:03:13 -0800165 GPR_ASSERT(status == GRPC_STATUS_CANCELLED);
166
167 grpc_metadata_array_destroy(&initial_metadata_recv);
168 grpc_metadata_array_destroy(&trailing_metadata_recv);
169 grpc_metadata_array_destroy(&request_metadata_recv);
170 grpc_call_details_destroy(&call_details);
171
172 grpc_byte_buffer_destroy(request_payload);
173 grpc_byte_buffer_destroy(response_payload_recv);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800174 grpc_slice_unref(details);
Craig Tillerfee91b42015-02-05 16:03:13 -0800175
Craig Tillerdd36b152017-03-31 08:27:28 -0700176 grpc_call_unref(c);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800177
Craig Tiller5cef1b32015-05-11 11:00:42 -0700178 cq_verifier_destroy(cqv);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800179 end_test(&f);
180 config.tear_down_data(&f);
181}
182
Craig Tiller0fe5ee72015-12-22 12:50:36 -0800183void cancel_before_invoke(grpc_end2end_test_config config) {
Craig Tiller3121fd42015-09-10 09:56:20 -0700184 size_t i;
Craig Tillerfee91b42015-02-05 16:03:13 -0800185 for (i = 1; i <= 6; i++) {
186 test_cancel_before_invoke(config, i);
187 }
Craig Tiller190d3602015-02-18 09:23:38 -0800188}
Craig Tiller9e9edbc2016-04-04 10:38:49 -0700189
190void cancel_before_invoke_pre_init(void) {}