blob: 7eb74679816656b5843b01f56b0bd39325b810a5 [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>
Craig Tillerea61b072015-02-03 19:19:27 -080025#include <grpc/grpc.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080026#include <grpc/support/alloc.h>
27#include <grpc/support/log.h>
28#include <grpc/support/time.h>
29#include <grpc/support/useful.h>
Craig Tiller28086682017-07-18 14:22:19 -070030#include "src/core/lib/debug/stats.h"
Craig Tiller9533d042016-03-25 17:11:06 -070031#include "src/core/lib/support/string.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080032#include "test/core/end2end/cq_verifier.h"
33
Craig Tillerbaa14a92017-11-03 09:09:36 -070034static void* tag(intptr_t t) { return (void*)t; }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080035
36static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
Craig Tillerbaa14a92017-11-03 09:09:36 -070037 const char* test_name,
38 grpc_channel_args* client_args,
39 grpc_channel_args* server_args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080040 grpc_end2end_test_fixture f;
Robbie Shade55a046a2017-01-25 15:14:28 -050041 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080042 f = config.create_fixture(client_args, server_args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043 config.init_server(&f, server_args);
Mark D. Rothe127a392016-10-27 08:27:15 -070044 config.init_client(&f, client_args);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080045 return f;
46}
47
Chris Evansed2a5472017-03-27 17:34:51 -050048static gpr_timespec n_seconds_from_now(int n) {
Robbie Shadeca7effc2017-01-17 09:14:29 -050049 return grpc_timeout_seconds_to_deadline(n);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080050}
51
Chris Evansed2a5472017-03-27 17:34:51 -050052static gpr_timespec five_seconds_from_now(void) {
53 return n_seconds_from_now(5);
54}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080055
Craig Tillerbaa14a92017-11-03 09:09:36 -070056static void drain_cq(grpc_completion_queue* cq) {
Craig Tiller64be9f72015-05-04 14:53:51 -070057 grpc_event ev;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080058 do {
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080059 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
Craig Tiller64be9f72015-05-04 14:53:51 -070060 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080061}
62
Craig Tillerbaa14a92017-11-03 09:09:36 -070063static void shutdown_server(grpc_end2end_test_fixture* f) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080064 if (!f->server) return;
Sree Kuchibhotla321881d2017-02-27 11:25:28 -080065 grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
66 GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
67 grpc_timeout_seconds_to_deadline(5),
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080068 nullptr)
Craig Tillerf40df232016-03-25 13:38:14 -070069 .type == GRPC_OP_COMPLETE);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080070 grpc_server_destroy(f->server);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080071 f->server = nullptr;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080072}
73
Craig Tillerbaa14a92017-11-03 09:09:36 -070074static void shutdown_client(grpc_end2end_test_fixture* f) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080075 if (!f->client) return;
76 grpc_channel_destroy(f->client);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080077 f->client = nullptr;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080078}
79
Craig Tillerbaa14a92017-11-03 09:09:36 -070080static void end_test(grpc_end2end_test_fixture* f) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080081 shutdown_server(f);
82 shutdown_client(f);
83
Craig Tillerbc0ec332015-05-11 12:11:32 -070084 grpc_completion_queue_shutdown(f->cq);
85 drain_cq(f->cq);
86 grpc_completion_queue_destroy(f->cq);
Sree Kuchibhotla321881d2017-02-27 11:25:28 -080087 grpc_completion_queue_destroy(f->shutdown_cq);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080088}
89
Muxi Yan5ebd3272016-10-31 07:27:07 -070090static void simple_request_body(grpc_end2end_test_config config,
91 grpc_end2end_test_fixture f) {
Craig Tillerbaa14a92017-11-03 09:09:36 -070092 grpc_call* c;
93 grpc_call* s;
94 cq_verifier* cqv = cq_verifier_create(f.cq);
Craig Tillerea61b072015-02-03 19:19:27 -080095 grpc_op ops[6];
Craig Tillerbaa14a92017-11-03 09:09:36 -070096 grpc_op* op;
Craig Tillerea61b072015-02-03 19:19:27 -080097 grpc_metadata_array initial_metadata_recv;
Craig Tiller5a34d452015-02-03 14:29:22 -080098 grpc_metadata_array trailing_metadata_recv;
99 grpc_metadata_array request_metadata_recv;
100 grpc_call_details call_details;
101 grpc_status_code status;
ncteisen65dbb9d2017-11-29 10:06:30 -0800102 const char* error_string;
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 Tillerea61b072015-02-03 19:19:27 -0800105 int was_cancelled = 2;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700106 char* peer;
Yash Tibrewal40422d52017-11-06 14:39:17 -0800107 grpc_stats_data* before =
108 static_cast<grpc_stats_data*>(gpr_malloc(sizeof(grpc_stats_data)));
109 grpc_stats_data* after =
110 static_cast<grpc_stats_data*>(gpr_malloc(sizeof(grpc_stats_data)));
Craig Tiller28086682017-07-18 14:22:19 -0700111
Craig Tiller604a7ca2017-09-22 09:28:32 -0700112 grpc_stats_collect(before);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800113
Chris Evansed2a5472017-03-27 17:34:51 -0500114 gpr_timespec deadline = five_seconds_from_now();
Muxi Yan5ebd3272016-10-31 07:27:07 -0700115 c = grpc_channel_create_call(
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800116 f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800117 grpc_slice_from_static_string("/foo"),
118 get_host_override_slice("foo.test.google.fr:1234", config), deadline,
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800119 nullptr);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800120 GPR_ASSERT(c);
121
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700122 peer = grpc_call_get_peer(c);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800123 GPR_ASSERT(peer != nullptr);
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700124 gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer);
125 gpr_free(peer);
126
Craig Tillerea61b072015-02-03 19:19:27 -0800127 grpc_metadata_array_init(&initial_metadata_recv);
Craig Tiller5a34d452015-02-03 14:29:22 -0800128 grpc_metadata_array_init(&trailing_metadata_recv);
129 grpc_metadata_array_init(&request_metadata_recv);
130 grpc_call_details_init(&call_details);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800131
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700132 memset(ops, 0, sizeof(ops));
Craig Tiller5a34d452015-02-03 14:29:22 -0800133 op = ops;
134 op->op = GRPC_OP_SEND_INITIAL_METADATA;
135 op->data.send_initial_metadata.count = 0;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700136 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800137 op->reserved = nullptr;
Craig Tiller5a34d452015-02-03 14:29:22 -0800138 op++;
Craig Tillerea61b072015-02-03 19:19:27 -0800139 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700140 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800141 op->reserved = nullptr;
Craig Tillerea61b072015-02-03 19:19:27 -0800142 op++;
143 op->op = GRPC_OP_RECV_INITIAL_METADATA;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800144 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700145 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800146 op->reserved = nullptr;
Craig Tillerea61b072015-02-03 19:19:27 -0800147 op++;
Craig Tiller5a34d452015-02-03 14:29:22 -0800148 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
Craig Tillerea61b072015-02-03 19:19:27 -0800149 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
Craig Tiller5a34d452015-02-03 14:29:22 -0800150 op->data.recv_status_on_client.status = &status;
151 op->data.recv_status_on_client.status_details = &details;
ncteisen65dbb9d2017-11-29 10:06:30 -0800152 op->data.recv_status_on_client.error_string = &error_string;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700153 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800154 op->reserved = nullptr;
Craig Tiller5a34d452015-02-03 14:29:22 -0800155 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800156 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr);
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200157 GPR_ASSERT(GRPC_CALL_OK == error);
Craig Tillerea61b072015-02-03 19:19:27 -0800158
Craig Tillerd6c98df2015-08-18 09:33:44 -0700159 error =
160 grpc_server_request_call(f.server, &s, &call_details,
161 &request_metadata_recv, f.cq, f.cq, tag(101));
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200162 GPR_ASSERT(GRPC_CALL_OK == error);
Mark D. Roth7187ab92016-08-24 13:49:22 -0700163 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700164 cq_verify(cqv);
Craig Tillerea61b072015-02-03 19:19:27 -0800165
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700166 peer = grpc_call_get_peer(s);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800167 GPR_ASSERT(peer != nullptr);
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700168 gpr_log(GPR_DEBUG, "server_peer=%s", peer);
169 gpr_free(peer);
170 peer = grpc_call_get_peer(c);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800171 GPR_ASSERT(peer != nullptr);
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700172 gpr_log(GPR_DEBUG, "client_peer=%s", peer);
173 gpr_free(peer);
174
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700175 memset(ops, 0, sizeof(ops));
Craig Tillerea61b072015-02-03 19:19:27 -0800176 op = ops;
177 op->op = GRPC_OP_SEND_INITIAL_METADATA;
178 op->data.send_initial_metadata.count = 0;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700179 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800180 op->reserved = nullptr;
Craig Tiller5a34d452015-02-03 14:29:22 -0800181 op++;
Craig Tillerea61b072015-02-03 19:19:27 -0800182 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
183 op->data.send_status_from_server.trailing_metadata_count = 0;
184 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800185 grpc_slice status_details = grpc_slice_from_static_string("xyz");
186 op->data.send_status_from_server.status_details = &status_details;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700187 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800188 op->reserved = nullptr;
Craig Tillerea61b072015-02-03 19:19:27 -0800189 op++;
190 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
191 op->data.recv_close_on_server.cancelled = &was_cancelled;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700192 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800193 op->reserved = nullptr;
Craig Tillerea61b072015-02-03 19:19:27 -0800194 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800195 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr);
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200196 GPR_ASSERT(GRPC_CALL_OK == error);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800197
Mark D. Roth7187ab92016-08-24 13:49:22 -0700198 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
199 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700200 cq_verify(cqv);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800201
Craig Tillerea61b072015-02-03 19:19:27 -0800202 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800203 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
ncteisen6ab0ba82017-11-29 13:32:46 -0800204 // the following sanity check makes sure that the requested error string is
ncteisen65dbb9d2017-11-29 10:06:30 -0800205 // correctly populated by the core. It looks for certain substrings that are
206 // not likely to change much. Some parts of the error, like time created,
207 // obviously are not checked.
208 GPR_ASSERT(nullptr != strstr(error_string, "xyz"));
209 GPR_ASSERT(nullptr != strstr(error_string, "description"));
210 GPR_ASSERT(nullptr != strstr(error_string, "Error received from peer"));
ncteisen65dbb9d2017-11-29 10:06:30 -0800211 GPR_ASSERT(nullptr != strstr(error_string, "grpc_message"));
ncteisen6ab0ba82017-11-29 13:32:46 -0800212 GPR_ASSERT(nullptr != strstr(error_string, "grpc_status"));
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800213 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
Muxi Yan5ebd3272016-10-31 07:27:07 -0700214 validate_host_override_string("foo.test.google.fr:1234", call_details.host,
215 config);
Craig Tillerdf5231e2016-03-10 07:00:47 -0800216 GPR_ASSERT(0 == call_details.flags);
Craig Tilleraea081f2015-06-11 14:19:33 -0700217 GPR_ASSERT(was_cancelled == 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800218
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800219 grpc_slice_unref(details);
ncteisen65dbb9d2017-11-29 10:06:30 -0800220 gpr_free((void*)error_string);
Craig Tiller4f972732015-02-05 12:40:20 -0800221 grpc_metadata_array_destroy(&initial_metadata_recv);
222 grpc_metadata_array_destroy(&trailing_metadata_recv);
223 grpc_metadata_array_destroy(&request_metadata_recv);
224 grpc_call_details_destroy(&call_details);
225
Craig Tillerdd36b152017-03-31 08:27:28 -0700226 grpc_call_unref(c);
227 grpc_call_unref(s);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800228
Craig Tillerbc0ec332015-05-11 12:11:32 -0700229 cq_verifier_destroy(cqv);
Craig Tiller28086682017-07-18 14:22:19 -0700230
Craig Tiller604a7ca2017-09-22 09:28:32 -0700231 grpc_stats_collect(after);
Craig Tiller28086682017-07-18 14:22:19 -0700232
Craig Tillerbaa14a92017-11-03 09:09:36 -0700233 char* stats = grpc_stats_data_as_json(after);
Craig Tiller28086682017-07-18 14:22:19 -0700234 gpr_log(GPR_DEBUG, "%s", stats);
235 gpr_free(stats);
236
237 int expected_calls = 1;
238 if (config.feature_mask & FEATURE_MASK_SUPPORTS_REQUEST_PROXYING) {
239 expected_calls *= 2;
240 }
Craig Tiller604a7ca2017-09-22 09:28:32 -0700241 GPR_ASSERT(after->counters[GRPC_STATS_COUNTER_CLIENT_CALLS_CREATED] -
242 before->counters[GRPC_STATS_COUNTER_CLIENT_CALLS_CREATED] ==
Craig Tiller28086682017-07-18 14:22:19 -0700243 expected_calls);
Craig Tiller604a7ca2017-09-22 09:28:32 -0700244 GPR_ASSERT(after->counters[GRPC_STATS_COUNTER_SERVER_CALLS_CREATED] -
245 before->counters[GRPC_STATS_COUNTER_SERVER_CALLS_CREATED] ==
Craig Tiller28086682017-07-18 14:22:19 -0700246 expected_calls);
Craig Tiller3c520382017-09-22 09:31:03 -0700247 gpr_free(before);
248 gpr_free(after);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800249}
250
Craig Tillerea61b072015-02-03 19:19:27 -0800251static void test_invoke_simple_request(grpc_end2end_test_config config) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800252 grpc_end2end_test_fixture f;
253
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800254 f = begin_test(config, "test_invoke_simple_request", nullptr, nullptr);
Muxi Yan56456c32016-10-30 23:11:11 -0700255 simple_request_body(config, f);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800256 end_test(&f);
257 config.tear_down_data(&f);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800258}
259
260static void test_invoke_10_simple_requests(grpc_end2end_test_config config) {
261 int i;
Craig Tiller9a576332015-06-17 10:21:49 -0700262 grpc_end2end_test_fixture f =
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800263 begin_test(config, "test_invoke_10_simple_requests", nullptr, nullptr);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800264 for (i = 0; i < 10; i++) {
Muxi Yan56456c32016-10-30 23:11:11 -0700265 simple_request_body(config, f);
Robbie Shade55a046a2017-01-25 15:14:28 -0500266 gpr_log(GPR_INFO, "Running test: Passed simple request %d", i);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800267 }
268 end_test(&f);
269 config.tear_down_data(&f);
270}
271
Craig Tiller0fe5ee72015-12-22 12:50:36 -0800272void simple_request(grpc_end2end_test_config config) {
Craig Tillere398fa22015-06-02 12:51:53 -0700273 int i;
274 for (i = 0; i < 10; i++) {
275 test_invoke_simple_request(config);
276 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800277 test_invoke_10_simple_requests(config);
Craig Tiller190d3602015-02-18 09:23:38 -0800278}
Craig Tiller9e9edbc2016-04-04 10:38:49 -0700279
280void simple_request_pre_init(void) {}