Debian Codegraph @

test/core/end2end/tests/no_logging.cc

raw
1 /*
2 *
3 * Copyright 2016 gRPC authors.
4 *
5 * 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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * 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.
16 *
17 */
18
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include <string>
24
25 #include "absl/strings/str_cat.h"
26
27 #include <grpc/grpc.h>
28 #include <grpc/impl/codegen/propagation_bits.h>
29 #include <grpc/slice.h>
30 #include <grpc/status.h>
31 #include <grpc/support/alloc.h>
32 #include <grpc/support/atm.h>
33 #include <grpc/support/log.h>
34
35 #include "test/core/end2end/cq_verifier.h"
36 #include "test/core/end2end/end2end_tests.h"
37 #include "test/core/util/test_config.h"
38
39 enum { TIMEOUT = 200000 };
40
41 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
42
43 void gpr_default_log(gpr_log_func_args* args);
44
45 static void test_no_log(gpr_log_func_args* args) {
46 std::string message = absl::StrCat("Unwanted log: ", args->message);
47 args->message = message.c_str();
48 gpr_default_log(args);
49 abort();
50 }
51
52 static void test_no_error_log(gpr_log_func_args* args) {
53 if (args->severity == GPR_LOG_SEVERITY_ERROR) {
54 test_no_log(args);
55 }
56 }
57
58 static gpr_atm g_log_func = reinterpret_cast<gpr_atm>(gpr_default_log);
59
60 static void log_dispatcher_func(gpr_log_func_args* args) {
61 gpr_log_func log_func =
62 reinterpret_cast<gpr_log_func>(gpr_atm_no_barrier_load(&g_log_func));
63 log_func(args);
64 }
65
66 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
67 const char* test_name,
68 grpc_channel_args* client_args,
69 grpc_channel_args* server_args) {
70 grpc_end2end_test_fixture f;
71 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
72 f = config.create_fixture(client_args, server_args);
73 config.init_server(&f, server_args);
74 config.init_client(&f, client_args);
75 return f;
76 }
77
78 static gpr_timespec n_seconds_from_now(int n) {
79 return grpc_timeout_seconds_to_deadline(n);
80 }
81
82 static gpr_timespec five_seconds_from_now(void) {
83 return n_seconds_from_now(5);
84 }
85
86 static void drain_cq(grpc_completion_queue* cq) {
87 grpc_event ev;
88 do {
89 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
90 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
91 }
92
93 static void shutdown_server(grpc_end2end_test_fixture* f) {
94 if (!f->server) return;
95 grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
96 grpc_event ev;
97 do {
98 ev = grpc_completion_queue_next(f->cq, grpc_timeout_seconds_to_deadline(5),
99 nullptr);
100 } while (ev.type != GRPC_OP_COMPLETE || ev.tag != tag(1000));
101 grpc_server_destroy(f->server);
102 f->server = nullptr;
103 }
104
105 static void shutdown_client(grpc_end2end_test_fixture* f) {
106 if (!f->client) return;
107 grpc_channel_destroy(f->client);
108 f->client = nullptr;
109 }
110
111 static void end_test(grpc_end2end_test_fixture* f) {
112 shutdown_server(f);
113 shutdown_client(f);
114
115 grpc_completion_queue_shutdown(f->cq);
116 drain_cq(f->cq);
117 grpc_completion_queue_destroy(f->cq);
118 }
119
120 static void simple_request_body(grpc_end2end_test_config /*config*/,
121 grpc_end2end_test_fixture f) {
122 grpc_call* c;
123 grpc_call* s;
124 grpc_core::CqVerifier cqv(f.cq);
125 grpc_op ops[6];
126 grpc_op* op;
127 grpc_metadata_array initial_metadata_recv;
128 grpc_metadata_array trailing_metadata_recv;
129 grpc_metadata_array request_metadata_recv;
130 grpc_call_details call_details;
131 grpc_status_code status;
132 grpc_call_error error;
133 grpc_slice details;
134 int was_cancelled = 2;
135 char* peer;
136
137 gpr_timespec deadline = five_seconds_from_now();
138 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
139 grpc_slice_from_static_string("/foo"), nullptr,
140 deadline, nullptr);
141 GPR_ASSERT(c);
142
143 peer = grpc_call_get_peer(c);
144 GPR_ASSERT(peer != nullptr);
145 gpr_free(peer);
146
147 grpc_metadata_array_init(&initial_metadata_recv);
148 grpc_metadata_array_init(&trailing_metadata_recv);
149 grpc_metadata_array_init(&request_metadata_recv);
150 grpc_call_details_init(&call_details);
151
152 memset(ops, 0, sizeof(ops));
153 op = ops;
154 op->op = GRPC_OP_SEND_INITIAL_METADATA;
155 op->data.send_initial_metadata.count = 0;
156 op->flags = 0;
157 op->reserved = nullptr;
158 op++;
159 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
160 op->flags = 0;
161 op->reserved = nullptr;
162 op++;
163 op->op = GRPC_OP_RECV_INITIAL_METADATA;
164 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
165 op->flags = 0;
166 op->reserved = nullptr;
167 op++;
168 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
169 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
170 op->data.recv_status_on_client.status = &status;
171 op->data.recv_status_on_client.status_details = &details;
172 op->flags = 0;
173 op->reserved = nullptr;
174 op++;
175 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
176 nullptr);
177 GPR_ASSERT(GRPC_CALL_OK == error);
178
179 error =
180 grpc_server_request_call(f.server, &s, &call_details,
181 &request_metadata_recv, f.cq, f.cq, tag(101));
182 GPR_ASSERT(GRPC_CALL_OK == error);
183 cqv.Expect(tag(101), true);
184 cqv.Verify();
185
186 peer = grpc_call_get_peer(s);
187 GPR_ASSERT(peer != nullptr);
188 gpr_free(peer);
189 peer = grpc_call_get_peer(c);
190 GPR_ASSERT(peer != nullptr);
191 gpr_free(peer);
192
193 memset(ops, 0, sizeof(ops));
194 op = ops;
195 op->op = GRPC_OP_SEND_INITIAL_METADATA;
196 op->data.send_initial_metadata.count = 0;
197 op->flags = 0;
198 op->reserved = nullptr;
199 op++;
200 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
201 op->data.send_status_from_server.trailing_metadata_count = 0;
202 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
203 grpc_slice status_details = grpc_slice_from_static_string("xyz");
204 op->data.send_status_from_server.status_details = &status_details;
205 op->flags = 0;
206 op->reserved = nullptr;
207 op++;
208 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
209 op->data.recv_close_on_server.cancelled = &was_cancelled;
210 op->flags = 0;
211 op->reserved = nullptr;
212 op++;
213 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
214 nullptr);
215 GPR_ASSERT(GRPC_CALL_OK == error);
216
217 cqv.Expect(tag(102), true);
218 cqv.Expect(tag(1), true);
219 cqv.Verify();
220
221 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
222 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
223 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
224 GPR_ASSERT(was_cancelled == 0);
225
226 grpc_slice_unref(details);
227 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
232 grpc_call_unref(c);
233 grpc_call_unref(s);
234 }
235
236 static void test_invoke_simple_request(grpc_end2end_test_config config) {
237 grpc_end2end_test_fixture f;
238
239 f = begin_test(config, "test_invoke_simple_request_with_no_error_logging",
240 nullptr, nullptr);
241 simple_request_body(config, f);
242 end_test(&f);
243 config.tear_down_data(&f);
244 }
245
246 static void test_invoke_10_simple_requests(grpc_end2end_test_config config) {
247 int i;
248 grpc_end2end_test_fixture f =
249 begin_test(config, "test_invoke_10_simple_requests_with_no_error_logging",
250 nullptr, nullptr);
251 for (i = 0; i < 10; i++) {
252 simple_request_body(config, f);
253 gpr_log(GPR_INFO, "Passed simple request %d", i);
254 }
255 simple_request_body(config, f);
256 end_test(&f);
257 config.tear_down_data(&f);
258 }
259
260 static void test_no_error_logging_in_entire_process(
261 grpc_end2end_test_config config) {
262 int i;
263 gpr_atm_no_barrier_store(&g_log_func, (gpr_atm)test_no_error_log);
264 for (i = 0; i < 10; i++) {
265 test_invoke_simple_request(config);
266 }
267 test_invoke_10_simple_requests(config);
268 gpr_atm_no_barrier_store(&g_log_func, (gpr_atm)gpr_default_log);
269 }
270
271 static void test_no_logging_in_one_request(grpc_end2end_test_config config) {
272 int i;
273 grpc_end2end_test_fixture f =
274 begin_test(config, "test_no_logging_in_last_request", nullptr, nullptr);
275 for (i = 0; i < 10; i++) {
276 simple_request_body(config, f);
277 }
278 gpr_atm_no_barrier_store(&g_log_func, (gpr_atm)test_no_log);
279 simple_request_body(config, f);
280 gpr_atm_no_barrier_store(&g_log_func, (gpr_atm)gpr_default_log);
281 end_test(&f);
282 config.tear_down_data(&f);
283 }
284
285 void no_logging(grpc_end2end_test_config config) {
286 gpr_set_log_verbosity(GPR_LOG_SEVERITY_DEBUG);
287 grpc_tracer_set_enabled("all", 0);
288 gpr_set_log_function(log_dispatcher_func);
289 test_no_logging_in_one_request(config);
290 test_no_error_logging_in_entire_process(config);
291 gpr_set_log_function(gpr_default_log);
292 }
293
294 void no_logging_pre_init(void) {}