blob: 2428c92a42bd3994cf10a3b6eee56355da137626 [file] [log] [blame]
Mark D. Roth3d883412016-11-07 13:42:54 -08001/*
2 *
3 * Copyright 2016, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include "test/core/end2end/end2end_tests.h"
35
36#include <limits.h>
37#include <stdbool.h>
38#include <stdio.h>
39#include <string.h>
40
41#include <grpc/byte_buffer.h>
42#include <grpc/support/alloc.h>
43#include <grpc/support/log.h>
44#include <grpc/support/time.h>
45#include <grpc/support/useful.h>
46
47#include "src/core/lib/channel/channel_stack_builder.h"
48#include "src/core/lib/surface/channel_init.h"
49#include "test/core/end2end/cq_verifier.h"
50
51enum { TIMEOUT = 200000 };
52
53static bool g_enable_filter = false;
Mark D. Roth6cf61532016-11-14 22:19:22 +000054static gpr_mu g_mu;
Mark D. Roth3d883412016-11-07 13:42:54 -080055static gpr_timespec g_client_latency;
56static gpr_timespec g_server_latency;
57
58static void *tag(intptr_t t) { return (void *)t; }
59
60static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
61 const char *test_name,
62 grpc_channel_args *client_args,
63 grpc_channel_args *server_args) {
64 grpc_end2end_test_fixture f;
Robbie Shade55a046a2017-01-25 15:14:28 -050065 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
Mark D. Roth3d883412016-11-07 13:42:54 -080066 f = config.create_fixture(client_args, server_args);
67 config.init_server(&f, server_args);
68 config.init_client(&f, client_args);
69 return f;
70}
71
72static gpr_timespec n_seconds_time(int n) {
Robbie Shadeca7effc2017-01-17 09:14:29 -050073 return grpc_timeout_seconds_to_deadline(n);
Mark D. Roth3d883412016-11-07 13:42:54 -080074}
75
76static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); }
77
78static void drain_cq(grpc_completion_queue *cq) {
79 grpc_event ev;
80 do {
81 ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL);
82 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
83}
84
85static void shutdown_server(grpc_end2end_test_fixture *f) {
86 if (!f->server) return;
87 grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
88 GPR_ASSERT(grpc_completion_queue_pluck(
Robbie Shadeca7effc2017-01-17 09:14:29 -050089 f->cq, tag(1000), grpc_timeout_seconds_to_deadline(5), NULL)
Mark D. Roth3d883412016-11-07 13:42:54 -080090 .type == GRPC_OP_COMPLETE);
91 grpc_server_destroy(f->server);
92 f->server = NULL;
93}
94
95static void shutdown_client(grpc_end2end_test_fixture *f) {
96 if (!f->client) return;
97 grpc_channel_destroy(f->client);
98 f->client = NULL;
99}
100
101static void end_test(grpc_end2end_test_fixture *f) {
102 shutdown_server(f);
103 shutdown_client(f);
104
105 grpc_completion_queue_shutdown(f->cq);
106 drain_cq(f->cq);
107 grpc_completion_queue_destroy(f->cq);
108}
109
Mark D. Roth3d1fe582016-11-08 10:29:08 -0800110// Simple request via a server filter that saves the reported latency value.
Mark D. Roth3d883412016-11-07 13:42:54 -0800111static void test_request(grpc_end2end_test_config config) {
112 grpc_call *c;
113 grpc_call *s;
Mark D. Rothc7d24672016-11-09 13:22:49 -0800114 grpc_slice request_payload_slice =
115 grpc_slice_from_copied_string("hello world");
Mark D. Roth3d883412016-11-07 13:42:54 -0800116 grpc_byte_buffer *request_payload =
117 grpc_raw_byte_buffer_create(&request_payload_slice, 1);
118 gpr_timespec deadline = five_seconds_time();
119 grpc_end2end_test_fixture f =
120 begin_test(config, "filter_latency", NULL, NULL);
121 cq_verifier *cqv = cq_verifier_create(f.cq);
122 grpc_op ops[6];
123 grpc_op *op;
124 grpc_metadata_array initial_metadata_recv;
125 grpc_metadata_array trailing_metadata_recv;
126 grpc_metadata_array request_metadata_recv;
127 grpc_byte_buffer *request_payload_recv = NULL;
128 grpc_call_details call_details;
129 grpc_status_code status;
130 grpc_call_error error;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800131 grpc_slice details;
Mark D. Roth3d883412016-11-07 13:42:54 -0800132 int was_cancelled = 2;
133
Mark D. Roth6cf61532016-11-14 22:19:22 +0000134 gpr_mu_lock(&g_mu);
Mark D. Roth3d883412016-11-07 13:42:54 -0800135 g_client_latency = gpr_time_0(GPR_TIMESPAN);
136 g_server_latency = gpr_time_0(GPR_TIMESPAN);
Mark D. Roth6cf61532016-11-14 22:19:22 +0000137 gpr_mu_unlock(&g_mu);
Mark D. Roth859cce42016-11-07 13:44:35 -0800138 const gpr_timespec start_time = gpr_now(GPR_CLOCK_MONOTONIC);
Mark D. Roth3d883412016-11-07 13:42:54 -0800139
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800140 c = grpc_channel_create_call(
141 f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq,
142 grpc_slice_from_static_string("/foo"),
143 get_host_override_slice("foo.test.google.fr", config), deadline, NULL);
Mark D. Roth3d883412016-11-07 13:42:54 -0800144 GPR_ASSERT(c);
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->data.send_initial_metadata.metadata = NULL;
156 op->flags = 0;
157 op->reserved = NULL;
158 op++;
159 op->op = GRPC_OP_SEND_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800160 op->data.send_message.send_message = request_payload;
Mark D. Roth3d883412016-11-07 13:42:54 -0800161 op->flags = 0;
162 op->reserved = NULL;
163 op++;
164 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
165 op->flags = 0;
166 op->reserved = NULL;
167 op++;
168 op->op = GRPC_OP_RECV_INITIAL_METADATA;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800169 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
Mark D. Roth3d883412016-11-07 13:42:54 -0800170 op->flags = 0;
171 op->reserved = NULL;
172 op++;
173 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
174 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
175 op->data.recv_status_on_client.status = &status;
176 op->data.recv_status_on_client.status_details = &details;
Mark D. Roth3d883412016-11-07 13:42:54 -0800177 op->flags = 0;
178 op->reserved = NULL;
179 op++;
180 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL);
181 GPR_ASSERT(GRPC_CALL_OK == error);
182
183 error =
184 grpc_server_request_call(f.server, &s, &call_details,
185 &request_metadata_recv, f.cq, f.cq, tag(101));
186 GPR_ASSERT(GRPC_CALL_OK == error);
187
188 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
189 cq_verify(cqv);
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;
196 op->reserved = NULL;
197 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_string = grpc_slice_from_static_string("xyz");
202 op->data.send_status_from_server.status_details = &status_string;
Mark D. Roth3d883412016-11-07 13:42:54 -0800203 op->flags = 0;
204 op->reserved = NULL;
205 op++;
206 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
207 op->data.recv_close_on_server.cancelled = &was_cancelled;
208 op->flags = 0;
209 op->reserved = NULL;
210 op++;
211 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL);
212 GPR_ASSERT(GRPC_CALL_OK == error);
213
214 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
215 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
216 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"));
Mark D. Roth3d883412016-11-07 13:42:54 -0800220
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800221 grpc_slice_unref(details);
Mark D. Roth3d883412016-11-07 13:42:54 -0800222 grpc_metadata_array_destroy(&initial_metadata_recv);
223 grpc_metadata_array_destroy(&trailing_metadata_recv);
224 grpc_metadata_array_destroy(&request_metadata_recv);
225 grpc_call_details_destroy(&call_details);
226
Mark D. Roth3d883412016-11-07 13:42:54 -0800227 grpc_call_destroy(s);
Mark D. Roth3d1fe582016-11-08 10:29:08 -0800228 grpc_call_destroy(c);
Mark D. Roth3d883412016-11-07 13:42:54 -0800229
Mark D. Roth3d883412016-11-07 13:42:54 -0800230 cq_verifier_destroy(cqv);
231
232 grpc_byte_buffer_destroy(request_payload);
233 grpc_byte_buffer_destroy(request_payload_recv);
234
235 end_test(&f);
236 config.tear_down_data(&f);
Craig Tiller25dba292016-11-28 09:24:01 -0800237
238 const gpr_timespec end_time = gpr_now(GPR_CLOCK_MONOTONIC);
239 const gpr_timespec max_latency = gpr_time_sub(end_time, start_time);
240
241 // Perform checks after test tear-down
242 // Guards against the case that there's outstanding channel-related work on a
243 // call prior to verification
244 gpr_mu_lock(&g_mu);
245 GPR_ASSERT(gpr_time_cmp(max_latency, g_client_latency) >= 0);
246 GPR_ASSERT(gpr_time_cmp(gpr_time_0(GPR_TIMESPAN), g_client_latency) <= 0);
247 GPR_ASSERT(gpr_time_cmp(max_latency, g_server_latency) >= 0);
248 GPR_ASSERT(gpr_time_cmp(gpr_time_0(GPR_TIMESPAN), g_server_latency) <= 0);
249 // Server latency should always be smaller than client latency, however since
250 // we only calculate latency at destruction time, and that might mean that we
Craig Tiller797bc832016-11-28 09:52:31 -0800251 // need to wait for outstanding channel-related work, this isn't verifiable
Craig Tillera45a5e42016-11-28 09:45:06 -0800252 // right now (the server MAY hold on to the call for longer than the client).
Craig Tiller25dba292016-11-28 09:24:01 -0800253 // GPR_ASSERT(gpr_time_cmp(g_server_latency, g_client_latency) < 0);
254 gpr_mu_unlock(&g_mu);
Mark D. Roth3d883412016-11-07 13:42:54 -0800255}
256
257/*******************************************************************************
Mark D. Roth3d1fe582016-11-08 10:29:08 -0800258 * Test latency filter
Mark D. Roth3d883412016-11-07 13:42:54 -0800259 */
260
261static grpc_error *init_call_elem(grpc_exec_ctx *exec_ctx,
262 grpc_call_element *elem,
Craig Tillereb022be2017-02-22 08:08:53 -0800263 const grpc_call_element_args *args) {
Mark D. Roth3d883412016-11-07 13:42:54 -0800264 return GRPC_ERROR_NONE;
265}
266
Mark D. Roth859cce42016-11-07 13:44:35 -0800267static void client_destroy_call_elem(grpc_exec_ctx *exec_ctx,
268 grpc_call_element *elem,
269 const grpc_call_final_info *final_info,
Craig Tillerd426cac2017-03-13 12:30:45 -0700270 grpc_closure *ignored) {
Mark D. Roth6cf61532016-11-14 22:19:22 +0000271 gpr_mu_lock(&g_mu);
Mark D. Roth3d883412016-11-07 13:42:54 -0800272 g_client_latency = final_info->stats.latency;
Mark D. Roth6cf61532016-11-14 22:19:22 +0000273 gpr_mu_unlock(&g_mu);
Mark D. Roth3d883412016-11-07 13:42:54 -0800274}
275
Mark D. Roth859cce42016-11-07 13:44:35 -0800276static void server_destroy_call_elem(grpc_exec_ctx *exec_ctx,
277 grpc_call_element *elem,
278 const grpc_call_final_info *final_info,
Craig Tillerd426cac2017-03-13 12:30:45 -0700279 grpc_closure *ignored) {
Mark D. Roth6cf61532016-11-14 22:19:22 +0000280 gpr_mu_lock(&g_mu);
Mark D. Roth3d883412016-11-07 13:42:54 -0800281 g_server_latency = final_info->stats.latency;
Mark D. Roth6cf61532016-11-14 22:19:22 +0000282 gpr_mu_unlock(&g_mu);
Mark D. Roth3d883412016-11-07 13:42:54 -0800283}
284
Mark D. Rothc1087882016-11-18 10:54:45 -0800285static grpc_error *init_channel_elem(grpc_exec_ctx *exec_ctx,
Mark D. Roth5e2566e2016-11-18 10:53:13 -0800286 grpc_channel_element *elem,
287 grpc_channel_element_args *args) {
288 return GRPC_ERROR_NONE;
289}
Mark D. Roth3d883412016-11-07 13:42:54 -0800290
291static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
292 grpc_channel_element *elem) {}
293
294static const grpc_channel_filter test_client_filter = {
295 grpc_call_next_op,
296 grpc_channel_next_op,
297 0,
298 init_call_elem,
299 grpc_call_stack_ignore_set_pollset_or_pollset_set,
300 client_destroy_call_elem,
301 0,
302 init_channel_elem,
303 destroy_channel_elem,
304 grpc_call_next_get_peer,
Mark D. Rothc7d24672016-11-09 13:22:49 -0800305 grpc_channel_next_get_info,
Mark D. Roth3d1fe582016-11-08 10:29:08 -0800306 "client_filter_latency"};
Mark D. Roth3d883412016-11-07 13:42:54 -0800307
308static const grpc_channel_filter test_server_filter = {
309 grpc_call_next_op,
310 grpc_channel_next_op,
311 0,
312 init_call_elem,
313 grpc_call_stack_ignore_set_pollset_or_pollset_set,
314 server_destroy_call_elem,
315 0,
316 init_channel_elem,
317 destroy_channel_elem,
318 grpc_call_next_get_peer,
Mark D. Rothc7d24672016-11-09 13:22:49 -0800319 grpc_channel_next_get_info,
Mark D. Roth3d1fe582016-11-08 10:29:08 -0800320 "server_filter_latency"};
Mark D. Roth3d883412016-11-07 13:42:54 -0800321
322/*******************************************************************************
323 * Registration
324 */
325
Craig Tiller7851ea32016-11-16 15:41:56 -0800326static bool maybe_add_filter(grpc_exec_ctx *exec_ctx,
327 grpc_channel_stack_builder *builder, void *arg) {
Mark D. Roth859cce42016-11-07 13:44:35 -0800328 grpc_channel_filter *filter = arg;
Mark D. Roth3d883412016-11-07 13:42:54 -0800329 if (g_enable_filter) {
330 // Want to add the filter as close to the end as possible, to make
331 // sure that all of the filters work well together. However, we
332 // can't add it at the very end, because the connected channel filter
333 // must be the last one. So we add it right before the last one.
334 grpc_channel_stack_builder_iterator *it =
335 grpc_channel_stack_builder_create_iterator_at_last(builder);
336 GPR_ASSERT(grpc_channel_stack_builder_move_prev(it));
Mark D. Roth859cce42016-11-07 13:44:35 -0800337 const bool retval =
338 grpc_channel_stack_builder_add_filter_before(it, filter, NULL, NULL);
Mark D. Roth3d883412016-11-07 13:42:54 -0800339 grpc_channel_stack_builder_iterator_destroy(it);
340 return retval;
341 } else {
342 return true;
343 }
344}
345
346static void init_plugin(void) {
Mark D. Roth6cf61532016-11-14 22:19:22 +0000347 gpr_mu_init(&g_mu);
Mark D. Roth3d883412016-11-07 13:42:54 -0800348 grpc_channel_init_register_stage(GRPC_CLIENT_CHANNEL, INT_MAX,
Mark D. Roth859cce42016-11-07 13:44:35 -0800349 maybe_add_filter,
350 (void *)&test_client_filter);
Mark D. Roth1dd1ce72016-11-08 10:32:24 -0800351 grpc_channel_init_register_stage(GRPC_CLIENT_DIRECT_CHANNEL, INT_MAX,
352 maybe_add_filter,
353 (void *)&test_client_filter);
Mark D. Roth3d883412016-11-07 13:42:54 -0800354 grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL, INT_MAX,
Mark D. Roth859cce42016-11-07 13:44:35 -0800355 maybe_add_filter,
356 (void *)&test_server_filter);
Mark D. Roth3d883412016-11-07 13:42:54 -0800357}
358
Mark D. Roth9b3f7402016-11-15 07:27:50 -0800359static void destroy_plugin(void) { gpr_mu_destroy(&g_mu); }
Mark D. Roth3d883412016-11-07 13:42:54 -0800360
361void filter_latency(grpc_end2end_test_config config) {
362 g_enable_filter = true;
363 test_request(config);
364 g_enable_filter = false;
365}
366
367void filter_latency_pre_init(void) {
368 grpc_register_plugin(init_plugin, destroy_plugin);
369}