blob: 440d817cf13786d3cc598299cb39a48369094e3f [file] [log] [blame]
Craig Tiller08453372015-04-10 16:05:38 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Craig Tiller08453372015-04-10 16:05:38 -07004 *
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 Tiller08453372015-04-10 16:05:38 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller08453372015-04-10 16:05:38 -070010 *
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 Tiller08453372015-04-10 16:05:38 -070016 *
17 */
18
19#include "test/core/end2end/end2end_tests.h"
20
21#include <stdio.h>
22#include <string.h>
Craig Tiller08453372015-04-10 16:05:38 -070023
Craig Tiller08453372015-04-10 16:05:38 -070024#include <grpc/byte_buffer.h>
25#include <grpc/grpc.h>
26#include <grpc/support/alloc.h>
27#include <grpc/support/log.h>
28#include <grpc/support/time.h>
29#include <grpc/support/useful.h>
Craig Tiller9533d042016-03-25 17:11:06 -070030#include "src/core/lib/support/string.h"
Craig Tiller08453372015-04-10 16:05:38 -070031#include "test/core/end2end/cq_verifier.h"
32
Craig Tillerbaa14a92017-11-03 09:09:36 -070033static void* tag(intptr_t t) { return (void*)t; }
Craig Tiller08453372015-04-10 16:05:38 -070034
35static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
Craig Tillerbaa14a92017-11-03 09:09:36 -070036 const char* test_name,
37 grpc_channel_args* client_args,
38 grpc_channel_args* server_args) {
Craig Tiller08453372015-04-10 16:05:38 -070039 grpc_end2end_test_fixture f;
Robbie Shade55a046a2017-01-25 15:14:28 -050040 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
Craig Tiller08453372015-04-10 16:05:38 -070041 f = config.create_fixture(client_args, server_args);
Craig Tiller08453372015-04-10 16:05:38 -070042 config.init_server(&f, server_args);
Mark D. Rothe127a392016-10-27 08:27:15 -070043 config.init_client(&f, client_args);
Craig Tiller08453372015-04-10 16:05:38 -070044 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);
Craig Tiller08453372015-04-10 16:05:38 -070049}
50
Chris Evansed2a5472017-03-27 17:34:51 -050051static gpr_timespec five_seconds_from_now(void) {
52 return n_seconds_from_now(5);
53}
Craig Tiller08453372015-04-10 16:05:38 -070054
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;
Craig Tiller08453372015-04-10 16:05:38 -070057 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);
Craig Tiller08453372015-04-10 16:05:38 -070060}
61
Craig Tillerbaa14a92017-11-03 09:09:36 -070062static void shutdown_server(grpc_end2end_test_fixture* f) {
Craig Tiller08453372015-04-10 16:05:38 -070063 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);
Craig Tiller08453372015-04-10 16:05:38 -070069 grpc_server_destroy(f->server);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080070 f->server = nullptr;
Craig Tiller08453372015-04-10 16:05:38 -070071}
72
Craig Tillerbaa14a92017-11-03 09:09:36 -070073static void shutdown_client(grpc_end2end_test_fixture* f) {
Craig Tiller08453372015-04-10 16:05:38 -070074 if (!f->client) return;
75 grpc_channel_destroy(f->client);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080076 f->client = nullptr;
Craig Tiller08453372015-04-10 16:05:38 -070077}
78
Craig Tillerbaa14a92017-11-03 09:09:36 -070079static void end_test(grpc_end2end_test_fixture* f) {
Craig Tiller08453372015-04-10 16:05:38 -070080 shutdown_server(f);
81 shutdown_client(f);
82
Craig Tillerbc0ec332015-05-11 12:11:32 -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);
Craig Tiller08453372015-04-10 16:05:38 -070087}
88
Muxi Yan5ebd3272016-10-31 07:27:07 -070089static void simple_request_body(grpc_end2end_test_config config,
Craig Tillerbaa14a92017-11-03 09:09:36 -070090 grpc_end2end_test_fixture f, void* rc) {
91 grpc_call* c;
92 grpc_call* s;
93 cq_verifier* cqv = cq_verifier_create(f.cq);
Craig Tiller08453372015-04-10 16:05:38 -070094 grpc_op ops[6];
Craig Tillerbaa14a92017-11-03 09:09:36 -070095 grpc_op* op;
Craig Tiller08453372015-04-10 16:05:38 -070096 grpc_metadata_array initial_metadata_recv;
97 grpc_metadata_array trailing_metadata_recv;
98 grpc_metadata_array request_metadata_recv;
99 grpc_call_details call_details;
100 grpc_status_code status;
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200101 grpc_call_error error;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800102 grpc_slice details;
Craig Tiller08453372015-04-10 16:05:38 -0700103 int was_cancelled = 2;
104
Chris Evansed2a5472017-03-27 17:34:51 -0500105 gpr_timespec deadline = five_seconds_from_now();
Craig Tillere1b0e6e2015-07-31 17:07:31 -0700106 c = grpc_channel_create_registered_call(
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800107 f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, rc, deadline, nullptr);
Craig Tiller08453372015-04-10 16:05:38 -0700108 GPR_ASSERT(c);
109
110 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);
114
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700115 memset(ops, 0, sizeof(ops));
Craig Tiller08453372015-04-10 16:05:38 -0700116 op = ops;
117 op->op = GRPC_OP_SEND_INITIAL_METADATA;
118 op->data.send_initial_metadata.count = 0;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700119 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800120 op->reserved = nullptr;
Craig Tiller08453372015-04-10 16:05:38 -0700121 op++;
122 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700123 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800124 op->reserved = nullptr;
Craig Tiller08453372015-04-10 16:05:38 -0700125 op++;
126 op->op = GRPC_OP_RECV_INITIAL_METADATA;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800127 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700128 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800129 op->reserved = nullptr;
Craig Tiller08453372015-04-10 16:05:38 -0700130 op++;
131 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
132 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
133 op->data.recv_status_on_client.status = &status;
134 op->data.recv_status_on_client.status_details = &details;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700135 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800136 op->reserved = nullptr;
Craig Tiller08453372015-04-10 16:05:38 -0700137 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800138 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr);
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200139 GPR_ASSERT(GRPC_CALL_OK == error);
Craig Tiller08453372015-04-10 16:05:38 -0700140
Craig Tillerd6c98df2015-08-18 09:33:44 -0700141 error =
142 grpc_server_request_call(f.server, &s, &call_details,
143 &request_metadata_recv, f.cq, f.cq, tag(101));
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200144 GPR_ASSERT(GRPC_CALL_OK == error);
Mark D. Roth7187ab92016-08-24 13:49:22 -0700145 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700146 cq_verify(cqv);
Craig Tiller08453372015-04-10 16:05:38 -0700147
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700148 memset(ops, 0, sizeof(ops));
Craig Tiller08453372015-04-10 16:05:38 -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;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800153 op->reserved = nullptr;
Craig Tiller08453372015-04-10 16:05:38 -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;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800161 op->reserved = nullptr;
Craig Tiller08453372015-04-10 16:05:38 -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;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800166 op->reserved = nullptr;
Craig Tiller08453372015-04-10 16:05:38 -0700167 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800168 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr);
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200169 GPR_ASSERT(GRPC_CALL_OK == error);
Craig Tiller08453372015-04-10 16:05:38 -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);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700173 cq_verify(cqv);
Craig Tiller08453372015-04-10 16:05:38 -0700174
175 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800176 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
177 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
Muxi Yan5ebd3272016-10-31 07:27:07 -0700178 validate_host_override_string("foo.test.google.fr:1234", call_details.host,
179 config);
Craig Tilleraea081f2015-06-11 14:19:33 -0700180 GPR_ASSERT(was_cancelled == 1);
Craig Tiller08453372015-04-10 16:05:38 -0700181
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800182 grpc_slice_unref(details);
Craig Tiller08453372015-04-10 16:05:38 -0700183 grpc_metadata_array_destroy(&initial_metadata_recv);
184 grpc_metadata_array_destroy(&trailing_metadata_recv);
185 grpc_metadata_array_destroy(&request_metadata_recv);
186 grpc_call_details_destroy(&call_details);
187
Craig Tillerdd36b152017-03-31 08:27:28 -0700188 grpc_call_unref(c);
189 grpc_call_unref(s);
Craig Tiller08453372015-04-10 16:05:38 -0700190
Craig Tillerbc0ec332015-05-11 12:11:32 -0700191 cq_verifier_destroy(cqv);
Craig Tiller08453372015-04-10 16:05:38 -0700192}
193
194static void test_invoke_simple_request(grpc_end2end_test_config config) {
Craig Tiller9a576332015-06-17 10:21:49 -0700195 grpc_end2end_test_fixture f =
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800196 begin_test(config, "test_invoke_simple_request", nullptr, nullptr);
Craig Tillerbaa14a92017-11-03 09:09:36 -0700197 void* rc = grpc_channel_register_call(
Muxi Yan5ebd3272016-10-31 07:27:07 -0700198 f.client, "/foo",
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800199 get_host_override_string("foo.test.google.fr:1234", config), nullptr);
Craig Tiller08453372015-04-10 16:05:38 -0700200
Muxi Yanea3b5682016-10-30 23:39:41 -0700201 simple_request_body(config, f, rc);
Craig Tiller08453372015-04-10 16:05:38 -0700202 end_test(&f);
203 config.tear_down_data(&f);
204}
205
206static void test_invoke_10_simple_requests(grpc_end2end_test_config config) {
207 int i;
Craig Tiller9a576332015-06-17 10:21:49 -0700208 grpc_end2end_test_fixture f =
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800209 begin_test(config, "test_invoke_10_simple_requests", nullptr, nullptr);
Craig Tillerbaa14a92017-11-03 09:09:36 -0700210 void* rc = grpc_channel_register_call(
Muxi Yan5ebd3272016-10-31 07:27:07 -0700211 f.client, "/foo",
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800212 get_host_override_string("foo.test.google.fr:1234", config), nullptr);
Craig Tiller08453372015-04-10 16:05:38 -0700213
214 for (i = 0; i < 10; i++) {
Muxi Yanea3b5682016-10-30 23:39:41 -0700215 simple_request_body(config, f, rc);
Craig Tiller08453372015-04-10 16:05:38 -0700216 gpr_log(GPR_INFO, "Passed simple request %d", i);
217 }
218 end_test(&f);
219 config.tear_down_data(&f);
220}
221
Craig Tiller0fe5ee72015-12-22 12:50:36 -0800222void registered_call(grpc_end2end_test_config config) {
Craig Tiller08453372015-04-10 16:05:38 -0700223 test_invoke_simple_request(config);
224 test_invoke_10_simple_requests(config);
225}
Craig Tiller9e9edbc2016-04-04 10:38:49 -0700226
227void registered_call_pre_init(void) {}