Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 51 | enum { TIMEOUT = 200000 }; |
| 52 | |
| 53 | static bool g_enable_filter = false; |
Mark D. Roth | 6cf6153 | 2016-11-14 22:19:22 +0000 | [diff] [blame] | 54 | static gpr_mu g_mu; |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 55 | static gpr_timespec g_client_latency; |
| 56 | static gpr_timespec g_server_latency; |
| 57 | |
| 58 | static void *tag(intptr_t t) { return (void *)t; } |
| 59 | |
| 60 | static 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 Shade | 55a046a | 2017-01-25 15:14:28 -0500 | [diff] [blame] | 65 | gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name); |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 66 | 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 | |
| 72 | static gpr_timespec n_seconds_time(int n) { |
Robbie Shade | ca7effc | 2017-01-17 09:14:29 -0500 | [diff] [blame] | 73 | return grpc_timeout_seconds_to_deadline(n); |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } |
| 77 | |
| 78 | static 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 | |
| 85 | static 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 Shade | ca7effc | 2017-01-17 09:14:29 -0500 | [diff] [blame] | 89 | f->cq, tag(1000), grpc_timeout_seconds_to_deadline(5), NULL) |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 90 | .type == GRPC_OP_COMPLETE); |
| 91 | grpc_server_destroy(f->server); |
| 92 | f->server = NULL; |
| 93 | } |
| 94 | |
| 95 | static 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 | |
| 101 | static 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. Roth | 3d1fe58 | 2016-11-08 10:29:08 -0800 | [diff] [blame] | 110 | // Simple request via a server filter that saves the reported latency value. |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 111 | static void test_request(grpc_end2end_test_config config) { |
| 112 | grpc_call *c; |
| 113 | grpc_call *s; |
Mark D. Roth | c7d2467 | 2016-11-09 13:22:49 -0800 | [diff] [blame] | 114 | grpc_slice request_payload_slice = |
| 115 | grpc_slice_from_copied_string("hello world"); |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 116 | 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 Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 131 | grpc_slice details; |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 132 | int was_cancelled = 2; |
| 133 | |
Mark D. Roth | 6cf6153 | 2016-11-14 22:19:22 +0000 | [diff] [blame] | 134 | gpr_mu_lock(&g_mu); |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 135 | g_client_latency = gpr_time_0(GPR_TIMESPAN); |
| 136 | g_server_latency = gpr_time_0(GPR_TIMESPAN); |
Mark D. Roth | 6cf6153 | 2016-11-14 22:19:22 +0000 | [diff] [blame] | 137 | gpr_mu_unlock(&g_mu); |
Mark D. Roth | 859cce4 | 2016-11-07 13:44:35 -0800 | [diff] [blame] | 138 | const gpr_timespec start_time = gpr_now(GPR_CLOCK_MONOTONIC); |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 139 | |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 140 | 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. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 144 | 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. Roth | 435f9f2 | 2017-01-25 12:53:54 -0800 | [diff] [blame] | 160 | op->data.send_message.send_message = request_payload; |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 161 | 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. Roth | 435f9f2 | 2017-01-25 12:53:54 -0800 | [diff] [blame] | 169 | op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 170 | 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. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 177 | 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 Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 201 | grpc_slice status_string = grpc_slice_from_static_string("xyz"); |
| 202 | op->data.send_status_from_server.status_details = &status_string; |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 203 | 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 Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 219 | GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz")); |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 220 | |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 221 | grpc_slice_unref(details); |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 222 | 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. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 227 | grpc_call_destroy(s); |
Mark D. Roth | 3d1fe58 | 2016-11-08 10:29:08 -0800 | [diff] [blame] | 228 | grpc_call_destroy(c); |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 229 | |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 230 | 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 Tiller | 25dba29 | 2016-11-28 09:24:01 -0800 | [diff] [blame] | 237 | |
| 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 Tiller | 797bc83 | 2016-11-28 09:52:31 -0800 | [diff] [blame] | 251 | // need to wait for outstanding channel-related work, this isn't verifiable |
Craig Tiller | a45a5e4 | 2016-11-28 09:45:06 -0800 | [diff] [blame] | 252 | // right now (the server MAY hold on to the call for longer than the client). |
Craig Tiller | 25dba29 | 2016-11-28 09:24:01 -0800 | [diff] [blame] | 253 | // GPR_ASSERT(gpr_time_cmp(g_server_latency, g_client_latency) < 0); |
| 254 | gpr_mu_unlock(&g_mu); |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /******************************************************************************* |
Mark D. Roth | 3d1fe58 | 2016-11-08 10:29:08 -0800 | [diff] [blame] | 258 | * Test latency filter |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 259 | */ |
| 260 | |
| 261 | static grpc_error *init_call_elem(grpc_exec_ctx *exec_ctx, |
| 262 | grpc_call_element *elem, |
Craig Tiller | eb022be | 2017-02-22 08:08:53 -0800 | [diff] [blame] | 263 | const grpc_call_element_args *args) { |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 264 | return GRPC_ERROR_NONE; |
| 265 | } |
| 266 | |
Mark D. Roth | 859cce4 | 2016-11-07 13:44:35 -0800 | [diff] [blame] | 267 | static void client_destroy_call_elem(grpc_exec_ctx *exec_ctx, |
| 268 | grpc_call_element *elem, |
| 269 | const grpc_call_final_info *final_info, |
Craig Tiller | d426cac | 2017-03-13 12:30:45 -0700 | [diff] [blame^] | 270 | grpc_closure *ignored) { |
Mark D. Roth | 6cf6153 | 2016-11-14 22:19:22 +0000 | [diff] [blame] | 271 | gpr_mu_lock(&g_mu); |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 272 | g_client_latency = final_info->stats.latency; |
Mark D. Roth | 6cf6153 | 2016-11-14 22:19:22 +0000 | [diff] [blame] | 273 | gpr_mu_unlock(&g_mu); |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Mark D. Roth | 859cce4 | 2016-11-07 13:44:35 -0800 | [diff] [blame] | 276 | static void server_destroy_call_elem(grpc_exec_ctx *exec_ctx, |
| 277 | grpc_call_element *elem, |
| 278 | const grpc_call_final_info *final_info, |
Craig Tiller | d426cac | 2017-03-13 12:30:45 -0700 | [diff] [blame^] | 279 | grpc_closure *ignored) { |
Mark D. Roth | 6cf6153 | 2016-11-14 22:19:22 +0000 | [diff] [blame] | 280 | gpr_mu_lock(&g_mu); |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 281 | g_server_latency = final_info->stats.latency; |
Mark D. Roth | 6cf6153 | 2016-11-14 22:19:22 +0000 | [diff] [blame] | 282 | gpr_mu_unlock(&g_mu); |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Mark D. Roth | c108788 | 2016-11-18 10:54:45 -0800 | [diff] [blame] | 285 | static grpc_error *init_channel_elem(grpc_exec_ctx *exec_ctx, |
Mark D. Roth | 5e2566e | 2016-11-18 10:53:13 -0800 | [diff] [blame] | 286 | grpc_channel_element *elem, |
| 287 | grpc_channel_element_args *args) { |
| 288 | return GRPC_ERROR_NONE; |
| 289 | } |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 290 | |
| 291 | static void destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
| 292 | grpc_channel_element *elem) {} |
| 293 | |
| 294 | static 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. Roth | c7d2467 | 2016-11-09 13:22:49 -0800 | [diff] [blame] | 305 | grpc_channel_next_get_info, |
Mark D. Roth | 3d1fe58 | 2016-11-08 10:29:08 -0800 | [diff] [blame] | 306 | "client_filter_latency"}; |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 307 | |
| 308 | static 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. Roth | c7d2467 | 2016-11-09 13:22:49 -0800 | [diff] [blame] | 319 | grpc_channel_next_get_info, |
Mark D. Roth | 3d1fe58 | 2016-11-08 10:29:08 -0800 | [diff] [blame] | 320 | "server_filter_latency"}; |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 321 | |
| 322 | /******************************************************************************* |
| 323 | * Registration |
| 324 | */ |
| 325 | |
Craig Tiller | 7851ea3 | 2016-11-16 15:41:56 -0800 | [diff] [blame] | 326 | static bool maybe_add_filter(grpc_exec_ctx *exec_ctx, |
| 327 | grpc_channel_stack_builder *builder, void *arg) { |
Mark D. Roth | 859cce4 | 2016-11-07 13:44:35 -0800 | [diff] [blame] | 328 | grpc_channel_filter *filter = arg; |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 329 | 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. Roth | 859cce4 | 2016-11-07 13:44:35 -0800 | [diff] [blame] | 337 | const bool retval = |
| 338 | grpc_channel_stack_builder_add_filter_before(it, filter, NULL, NULL); |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 339 | grpc_channel_stack_builder_iterator_destroy(it); |
| 340 | return retval; |
| 341 | } else { |
| 342 | return true; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | static void init_plugin(void) { |
Mark D. Roth | 6cf6153 | 2016-11-14 22:19:22 +0000 | [diff] [blame] | 347 | gpr_mu_init(&g_mu); |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 348 | grpc_channel_init_register_stage(GRPC_CLIENT_CHANNEL, INT_MAX, |
Mark D. Roth | 859cce4 | 2016-11-07 13:44:35 -0800 | [diff] [blame] | 349 | maybe_add_filter, |
| 350 | (void *)&test_client_filter); |
Mark D. Roth | 1dd1ce7 | 2016-11-08 10:32:24 -0800 | [diff] [blame] | 351 | grpc_channel_init_register_stage(GRPC_CLIENT_DIRECT_CHANNEL, INT_MAX, |
| 352 | maybe_add_filter, |
| 353 | (void *)&test_client_filter); |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 354 | grpc_channel_init_register_stage(GRPC_SERVER_CHANNEL, INT_MAX, |
Mark D. Roth | 859cce4 | 2016-11-07 13:44:35 -0800 | [diff] [blame] | 355 | maybe_add_filter, |
| 356 | (void *)&test_server_filter); |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Mark D. Roth | 9b3f740 | 2016-11-15 07:27:50 -0800 | [diff] [blame] | 359 | static void destroy_plugin(void) { gpr_mu_destroy(&g_mu); } |
Mark D. Roth | 3d88341 | 2016-11-07 13:42:54 -0800 | [diff] [blame] | 360 | |
| 361 | void filter_latency(grpc_end2end_test_config config) { |
| 362 | g_enable_filter = true; |
| 363 | test_request(config); |
| 364 | g_enable_filter = false; |
| 365 | } |
| 366 | |
| 367 | void filter_latency_pre_init(void) { |
| 368 | grpc_register_plugin(init_plugin, destroy_plugin); |
| 369 | } |