blob: bf8651221a621ea68b9bbd0ccfa43401960b9b85 [file] [log] [blame]
Yuchen Zeng916079d2016-08-30 15:52:09 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2016 gRPC authors.
Yuchen Zeng916079d2016-08-30 15:52:09 -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
Yuchen Zeng916079d2016-08-30 15:52:09 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Yuchen Zeng916079d2016-08-30 15:52:09 -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.
Yuchen Zeng916079d2016-08-30 15:52:09 -070016 *
17 */
18
19#include "test/core/end2end/end2end_tests.h"
20
21#include <stdio.h>
22#include <string.h>
23
24#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/string_util.h>
29#include <grpc/support/time.h>
30#include <grpc/support/useful.h>
31#include "src/core/lib/iomgr/error.h"
32#include "src/core/lib/support/string.h"
33#include "test/core/end2end/cq_verifier.h"
34
35enum { TIMEOUT = 200000 };
36
Craig Tillerbaa14a92017-11-03 09:09:36 -070037static void* tag(intptr_t t) { return (void*)t; }
Yuchen Zeng916079d2016-08-30 15:52:09 -070038
ncteisenadbfbd52017-11-16 15:35:45 -080039void gpr_default_log(gpr_log_func_args* args);
Yuchen Zeng916079d2016-08-30 15:52:09 -070040
Craig Tillerbaa14a92017-11-03 09:09:36 -070041static void test_no_log(gpr_log_func_args* args) {
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080042 char* message = nullptr;
Yuchen Zeng916079d2016-08-30 15:52:09 -070043 gpr_asprintf(&message, "Unwanted log: %s", args->message);
44 args->message = message;
45 gpr_default_log(args);
46 gpr_free(message);
47 abort();
48}
49
Craig Tillerbaa14a92017-11-03 09:09:36 -070050static void test_no_error_log(gpr_log_func_args* args) {
Yuchen Zeng916079d2016-08-30 15:52:09 -070051 if (args->severity == GPR_LOG_SEVERITY_ERROR) {
52 test_no_log(args);
53 }
54}
55
Mark D. Rothcf218fb2016-09-16 17:46:07 +000056static gpr_atm g_log_func = (gpr_atm)gpr_default_log;
57
Craig Tillerbaa14a92017-11-03 09:09:36 -070058static void log_dispatcher_func(gpr_log_func_args* args) {
Mark D. Rothcf218fb2016-09-16 17:46:07 +000059 gpr_log_func log_func = (gpr_log_func)gpr_atm_no_barrier_load(&g_log_func);
60 log_func(args);
61}
62
Yuchen Zeng916079d2016-08-30 15:52:09 -070063static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
Craig Tillerbaa14a92017-11-03 09:09:36 -070064 const char* test_name,
65 grpc_channel_args* client_args,
66 grpc_channel_args* server_args) {
Yuchen Zeng916079d2016-08-30 15:52:09 -070067 grpc_end2end_test_fixture f;
Robbie Shade55a046a2017-01-25 15:14:28 -050068 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
Yuchen Zeng916079d2016-08-30 15:52:09 -070069 f = config.create_fixture(client_args, server_args);
70 config.init_server(&f, server_args);
Mark D. Rothe127a392016-10-27 08:27:15 -070071 config.init_client(&f, client_args);
Yuchen Zeng916079d2016-08-30 15:52:09 -070072 return f;
73}
74
Chris Evansed2a5472017-03-27 17:34:51 -050075static gpr_timespec n_seconds_from_now(int n) {
Robbie Shadeca7effc2017-01-17 09:14:29 -050076 return grpc_timeout_seconds_to_deadline(n);
Yuchen Zeng916079d2016-08-30 15:52:09 -070077}
78
Chris Evansed2a5472017-03-27 17:34:51 -050079static gpr_timespec five_seconds_from_now(void) {
80 return n_seconds_from_now(5);
81}
Yuchen Zeng916079d2016-08-30 15:52:09 -070082
Craig Tillerbaa14a92017-11-03 09:09:36 -070083static void drain_cq(grpc_completion_queue* cq) {
Yuchen Zeng916079d2016-08-30 15:52:09 -070084 grpc_event ev;
85 do {
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080086 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
Yuchen Zeng916079d2016-08-30 15:52:09 -070087 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
88}
89
Craig Tillerbaa14a92017-11-03 09:09:36 -070090static void shutdown_server(grpc_end2end_test_fixture* f) {
Yuchen Zeng916079d2016-08-30 15:52:09 -070091 if (!f->server) return;
Sree Kuchibhotla321881d2017-02-27 11:25:28 -080092 grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
93 GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
94 grpc_timeout_seconds_to_deadline(5),
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080095 nullptr)
Yuchen Zeng916079d2016-08-30 15:52:09 -070096 .type == GRPC_OP_COMPLETE);
97 grpc_server_destroy(f->server);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080098 f->server = nullptr;
Yuchen Zeng916079d2016-08-30 15:52:09 -070099}
100
Craig Tillerbaa14a92017-11-03 09:09:36 -0700101static void shutdown_client(grpc_end2end_test_fixture* f) {
Yuchen Zeng916079d2016-08-30 15:52:09 -0700102 if (!f->client) return;
103 grpc_channel_destroy(f->client);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800104 f->client = nullptr;
Yuchen Zeng916079d2016-08-30 15:52:09 -0700105}
106
Craig Tillerbaa14a92017-11-03 09:09:36 -0700107static void end_test(grpc_end2end_test_fixture* f) {
Yuchen Zeng916079d2016-08-30 15:52:09 -0700108 shutdown_server(f);
109 shutdown_client(f);
110
111 grpc_completion_queue_shutdown(f->cq);
112 drain_cq(f->cq);
113 grpc_completion_queue_destroy(f->cq);
Sree Kuchibhotla321881d2017-02-27 11:25:28 -0800114 grpc_completion_queue_destroy(f->shutdown_cq);
Yuchen Zeng916079d2016-08-30 15:52:09 -0700115}
116
Muxi Yan5ebd3272016-10-31 07:27:07 -0700117static void simple_request_body(grpc_end2end_test_config config,
118 grpc_end2end_test_fixture f) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700119 grpc_call* c;
120 grpc_call* s;
121 cq_verifier* cqv = cq_verifier_create(f.cq);
Yuchen Zeng916079d2016-08-30 15:52:09 -0700122 grpc_op ops[6];
Craig Tillerbaa14a92017-11-03 09:09:36 -0700123 grpc_op* op;
Yuchen Zeng916079d2016-08-30 15:52:09 -0700124 grpc_metadata_array initial_metadata_recv;
125 grpc_metadata_array trailing_metadata_recv;
126 grpc_metadata_array request_metadata_recv;
127 grpc_call_details call_details;
128 grpc_status_code status;
129 grpc_call_error error;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800130 grpc_slice details;
Yuchen Zeng916079d2016-08-30 15:52:09 -0700131 int was_cancelled = 2;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700132 char* peer;
Yuchen Zeng916079d2016-08-30 15:52:09 -0700133
Chris Evansed2a5472017-03-27 17:34:51 -0500134 gpr_timespec deadline = five_seconds_from_now();
Muxi Yan5ebd3272016-10-31 07:27:07 -0700135 c = grpc_channel_create_call(
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800136 f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800137 grpc_slice_from_static_string("/foo"),
138 get_host_override_slice("foo.test.google.fr:1234", config), deadline,
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800139 nullptr);
Yuchen Zeng916079d2016-08-30 15:52:09 -0700140 GPR_ASSERT(c);
141
142 peer = grpc_call_get_peer(c);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800143 GPR_ASSERT(peer != nullptr);
Yuchen Zeng916079d2016-08-30 15:52:09 -0700144 gpr_free(peer);
145
146 grpc_metadata_array_init(&initial_metadata_recv);
147 grpc_metadata_array_init(&trailing_metadata_recv);
148 grpc_metadata_array_init(&request_metadata_recv);
149 grpc_call_details_init(&call_details);
150
151 memset(ops, 0, sizeof(ops));
152 op = ops;
153 op->op = GRPC_OP_SEND_INITIAL_METADATA;
154 op->data.send_initial_metadata.count = 0;
155 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800156 op->reserved = nullptr;
Yuchen Zeng916079d2016-08-30 15:52:09 -0700157 op++;
158 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
159 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800160 op->reserved = nullptr;
Yuchen Zeng916079d2016-08-30 15:52:09 -0700161 op++;
162 op->op = GRPC_OP_RECV_INITIAL_METADATA;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800163 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
Yuchen Zeng916079d2016-08-30 15:52:09 -0700164 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800165 op->reserved = nullptr;
Yuchen Zeng916079d2016-08-30 15:52:09 -0700166 op++;
167 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
168 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
169 op->data.recv_status_on_client.status = &status;
170 op->data.recv_status_on_client.status_details = &details;
Yuchen Zeng916079d2016-08-30 15:52:09 -0700171 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800172 op->reserved = nullptr;
Yuchen Zeng916079d2016-08-30 15:52:09 -0700173 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800174 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr);
Yuchen Zeng916079d2016-08-30 15:52:09 -0700175 GPR_ASSERT(GRPC_CALL_OK == error);
176
177 error =
178 grpc_server_request_call(f.server, &s, &call_details,
179 &request_metadata_recv, f.cq, f.cq, tag(101));
180 GPR_ASSERT(GRPC_CALL_OK == error);
Yuchen Zeng5c51aad2016-08-31 18:18:14 -0700181 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
Yuchen Zeng916079d2016-08-30 15:52:09 -0700182 cq_verify(cqv);
183
184 peer = grpc_call_get_peer(s);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800185 GPR_ASSERT(peer != nullptr);
Yuchen Zeng916079d2016-08-30 15:52:09 -0700186 gpr_free(peer);
187 peer = grpc_call_get_peer(c);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800188 GPR_ASSERT(peer != nullptr);
Yuchen Zeng916079d2016-08-30 15:52:09 -0700189 gpr_free(peer);
190
191 memset(ops, 0, sizeof(ops));
192 op = ops;
193 op->op = GRPC_OP_SEND_INITIAL_METADATA;
194 op->data.send_initial_metadata.count = 0;
195 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800196 op->reserved = nullptr;
Yuchen Zeng916079d2016-08-30 15:52:09 -0700197 op++;
198 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
199 op->data.send_status_from_server.trailing_metadata_count = 0;
200 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800201 grpc_slice status_details = grpc_slice_from_static_string("xyz");
202 op->data.send_status_from_server.status_details = &status_details;
Yuchen Zeng916079d2016-08-30 15:52:09 -0700203 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800204 op->reserved = nullptr;
Yuchen Zeng916079d2016-08-30 15:52:09 -0700205 op++;
206 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
207 op->data.recv_close_on_server.cancelled = &was_cancelled;
208 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800209 op->reserved = nullptr;
Yuchen Zeng916079d2016-08-30 15:52:09 -0700210 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800211 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr);
Yuchen Zeng916079d2016-08-30 15:52:09 -0700212 GPR_ASSERT(GRPC_CALL_OK == error);
213
Yuchen Zeng5c51aad2016-08-31 18:18:14 -0700214 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
215 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
Yuchen Zeng916079d2016-08-30 15:52:09 -0700216 cq_verify(cqv);
217
218 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800219 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
220 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
Muxi Yan5ebd3272016-10-31 07:27:07 -0700221 validate_host_override_string("foo.test.google.fr:1234", call_details.host,
222 config);
Yuchen Zeng916079d2016-08-30 15:52:09 -0700223 GPR_ASSERT(0 == call_details.flags);
224 GPR_ASSERT(was_cancelled == 1);
225
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800226 grpc_slice_unref(details);
Yuchen Zeng916079d2016-08-30 15:52:09 -0700227 grpc_metadata_array_destroy(&initial_metadata_recv);
228 grpc_metadata_array_destroy(&trailing_metadata_recv);
229 grpc_metadata_array_destroy(&request_metadata_recv);
230 grpc_call_details_destroy(&call_details);
231
Craig Tillerdd36b152017-03-31 08:27:28 -0700232 grpc_call_unref(c);
233 grpc_call_unref(s);
Yuchen Zeng916079d2016-08-30 15:52:09 -0700234
235 cq_verifier_destroy(cqv);
236}
237
238static void test_invoke_simple_request(grpc_end2end_test_config config) {
239 grpc_end2end_test_fixture f;
240
241 f = begin_test(config, "test_invoke_simple_request_with_no_error_logging",
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800242 nullptr, nullptr);
Muxi Yan56456c32016-10-30 23:11:11 -0700243 simple_request_body(config, f);
Yuchen Zeng916079d2016-08-30 15:52:09 -0700244 end_test(&f);
245 config.tear_down_data(&f);
246}
247
248static void test_invoke_10_simple_requests(grpc_end2end_test_config config) {
249 int i;
250 grpc_end2end_test_fixture f =
251 begin_test(config, "test_invoke_10_simple_requests_with_no_error_logging",
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800252 nullptr, nullptr);
Yuchen Zeng916079d2016-08-30 15:52:09 -0700253 for (i = 0; i < 10; i++) {
Muxi Yan56456c32016-10-30 23:11:11 -0700254 simple_request_body(config, f);
Yuchen Zeng916079d2016-08-30 15:52:09 -0700255 gpr_log(GPR_INFO, "Passed simple request %d", i);
256 }
Muxi Yan56456c32016-10-30 23:11:11 -0700257 simple_request_body(config, f);
Yuchen Zeng916079d2016-08-30 15:52:09 -0700258 end_test(&f);
259 config.tear_down_data(&f);
260}
261
262static void test_no_error_logging_in_entire_process(
263 grpc_end2end_test_config config) {
264 int i;
Mark D. Roth8ce02af2016-09-16 15:12:00 -0700265 gpr_atm_no_barrier_store(&g_log_func, (gpr_atm)test_no_error_log);
Yuchen Zeng916079d2016-08-30 15:52:09 -0700266 for (i = 0; i < 10; i++) {
267 test_invoke_simple_request(config);
268 }
269 test_invoke_10_simple_requests(config);
Mark D. Roth8ce02af2016-09-16 15:12:00 -0700270 gpr_atm_no_barrier_store(&g_log_func, (gpr_atm)gpr_default_log);
Yuchen Zeng916079d2016-08-30 15:52:09 -0700271}
272
273static void test_no_logging_in_one_request(grpc_end2end_test_config config) {
274 int i;
275 grpc_end2end_test_fixture f =
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800276 begin_test(config, "test_no_logging_in_last_request", nullptr, nullptr);
Yuchen Zeng916079d2016-08-30 15:52:09 -0700277 for (i = 0; i < 10; i++) {
Muxi Yan56456c32016-10-30 23:11:11 -0700278 simple_request_body(config, f);
Yuchen Zeng916079d2016-08-30 15:52:09 -0700279 }
Mark D. Roth8ce02af2016-09-16 15:12:00 -0700280 gpr_atm_no_barrier_store(&g_log_func, (gpr_atm)test_no_log);
Muxi Yan56456c32016-10-30 23:11:11 -0700281 simple_request_body(config, f);
Mark D. Roth8ce02af2016-09-16 15:12:00 -0700282 gpr_atm_no_barrier_store(&g_log_func, (gpr_atm)gpr_default_log);
Yuchen Zeng916079d2016-08-30 15:52:09 -0700283 end_test(&f);
284 config.tear_down_data(&f);
285}
286
287void no_logging(grpc_end2end_test_config config) {
Craig Tillercd6add32016-09-01 10:56:01 -0700288 gpr_set_log_verbosity(GPR_LOG_SEVERITY_DEBUG);
Craig Tiller02b87cd2016-09-02 09:50:08 -0700289 grpc_tracer_set_enabled("all", 0);
Mark D. Rothcf218fb2016-09-16 17:46:07 +0000290 gpr_set_log_function(log_dispatcher_func);
Yuchen Zeng916079d2016-08-30 15:52:09 -0700291 test_no_logging_in_one_request(config);
292 test_no_error_logging_in_entire_process(config);
Mark D. Rothcf218fb2016-09-16 17:46:07 +0000293 gpr_set_log_function(gpr_default_log);
Yuchen Zeng916079d2016-08-30 15:52:09 -0700294}
295
296void no_logging_pre_init(void) {}