Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2017 gRPC authors. |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 16 | * |
| 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/support/alloc.h> |
| 26 | #include <grpc/support/log.h> |
| 27 | #include <grpc/support/time.h> |
| 28 | #include <grpc/support/useful.h> |
Yuchen Zeng | 3d43da7 | 2017-03-27 11:33:21 -0700 | [diff] [blame] | 29 | #include "src/core/ext/transport/chttp2/transport/frame_ping.h" |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 30 | #include "src/core/lib/channel/channel_args.h" |
| 31 | #include "src/core/lib/iomgr/exec_ctx.h" |
| 32 | #include "src/core/lib/support/env.h" |
| 33 | #include "test/core/end2end/cq_verifier.h" |
| 34 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 35 | static void* tag(intptr_t t) { return (void*)t; } |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 36 | |
| 37 | static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 38 | const char* test_name, |
| 39 | grpc_channel_args* client_args, |
| 40 | grpc_channel_args* server_args) { |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 41 | grpc_end2end_test_fixture f; |
| 42 | gpr_log(GPR_INFO, "%s/%s", test_name, config.name); |
| 43 | f = config.create_fixture(client_args, server_args); |
| 44 | config.init_server(&f, server_args); |
| 45 | config.init_client(&f, client_args); |
| 46 | return f; |
| 47 | } |
| 48 | |
Chris Evans | ed2a547 | 2017-03-27 17:34:51 -0500 | [diff] [blame] | 49 | static gpr_timespec n_seconds_from_now(int n) { |
Yuchen Zeng | efdf5a3 | 2017-02-26 23:29:54 -0800 | [diff] [blame] | 50 | return grpc_timeout_seconds_to_deadline(n); |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Chris Evans | ed2a547 | 2017-03-27 17:34:51 -0500 | [diff] [blame] | 53 | static gpr_timespec five_seconds_from_now(void) { |
| 54 | return n_seconds_from_now(5); |
| 55 | } |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 56 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 57 | static void drain_cq(grpc_completion_queue* cq) { |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 58 | grpc_event ev; |
| 59 | do { |
Craig Tiller | 4ac2b8e | 2017-11-10 14:14:17 -0800 | [diff] [blame] | 60 | ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr); |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 61 | } while (ev.type != GRPC_QUEUE_SHUTDOWN); |
| 62 | } |
| 63 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 64 | static void shutdown_server(grpc_end2end_test_fixture* f) { |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 65 | if (!f->server) return; |
Sree Kuchibhotla | a6ff103 | 2017-04-06 13:09:17 -0700 | [diff] [blame] | 66 | |
Sree Kuchibhotla | 6b45d01 | 2017-03-02 16:37:59 -0800 | [diff] [blame] | 67 | grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000)); |
| 68 | GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000), |
Craig Tiller | 4ac2b8e | 2017-11-10 14:14:17 -0800 | [diff] [blame] | 69 | five_seconds_from_now(), nullptr) |
Chris Evans | ed2a547 | 2017-03-27 17:34:51 -0500 | [diff] [blame] | 70 | .type == GRPC_OP_COMPLETE); |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 71 | grpc_server_destroy(f->server); |
Craig Tiller | 4ac2b8e | 2017-11-10 14:14:17 -0800 | [diff] [blame] | 72 | f->server = nullptr; |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 75 | static void shutdown_client(grpc_end2end_test_fixture* f) { |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 76 | if (!f->client) return; |
| 77 | grpc_channel_destroy(f->client); |
Craig Tiller | 4ac2b8e | 2017-11-10 14:14:17 -0800 | [diff] [blame] | 78 | f->client = nullptr; |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 81 | static void end_test(grpc_end2end_test_fixture* f) { |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 82 | shutdown_server(f); |
| 83 | shutdown_client(f); |
| 84 | |
| 85 | grpc_completion_queue_shutdown(f->cq); |
| 86 | drain_cq(f->cq); |
| 87 | grpc_completion_queue_destroy(f->cq); |
Sree Kuchibhotla | 6b45d01 | 2017-03-02 16:37:59 -0800 | [diff] [blame] | 88 | grpc_completion_queue_destroy(f->shutdown_cq); |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* Client sends a request, server replies with a payload, then waits for the |
| 92 | keepalive watchdog timeouts before returning status. */ |
| 93 | static void test_keepalive_timeout(grpc_end2end_test_config config) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 94 | grpc_call* c; |
| 95 | grpc_call* s; |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 96 | grpc_slice response_payload_slice = |
| 97 | grpc_slice_from_copied_string("hello world"); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 98 | grpc_byte_buffer* response_payload = |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 99 | grpc_raw_byte_buffer_create(&response_payload_slice, 1); |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 100 | |
Yash Tibrewal | 34a57d0 | 2017-10-23 15:33:21 -0700 | [diff] [blame] | 101 | grpc_arg keepalive_arg_elems[3]; |
| 102 | keepalive_arg_elems[0].type = GRPC_ARG_INTEGER; |
Yash Tibrewal | 40422d5 | 2017-11-06 14:39:17 -0800 | [diff] [blame] | 103 | keepalive_arg_elems[0].key = const_cast<char*>(GRPC_ARG_KEEPALIVE_TIME_MS); |
Yash Tibrewal | 34a57d0 | 2017-10-23 15:33:21 -0700 | [diff] [blame] | 104 | keepalive_arg_elems[0].value.integer = 1500; |
| 105 | keepalive_arg_elems[1].type = GRPC_ARG_INTEGER; |
Yash Tibrewal | 40422d5 | 2017-11-06 14:39:17 -0800 | [diff] [blame] | 106 | keepalive_arg_elems[1].key = const_cast<char*>(GRPC_ARG_KEEPALIVE_TIMEOUT_MS); |
Yash Tibrewal | 34a57d0 | 2017-10-23 15:33:21 -0700 | [diff] [blame] | 107 | keepalive_arg_elems[1].value.integer = 0; |
| 108 | keepalive_arg_elems[2].type = GRPC_ARG_INTEGER; |
Yash Tibrewal | 40422d5 | 2017-11-06 14:39:17 -0800 | [diff] [blame] | 109 | keepalive_arg_elems[2].key = const_cast<char*>(GRPC_ARG_HTTP2_BDP_PROBE); |
Yash Tibrewal | 34a57d0 | 2017-10-23 15:33:21 -0700 | [diff] [blame] | 110 | keepalive_arg_elems[2].value.integer = 0; |
| 111 | grpc_channel_args keepalive_args = {GPR_ARRAY_SIZE(keepalive_arg_elems), |
| 112 | keepalive_arg_elems}; |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 113 | |
| 114 | grpc_end2end_test_fixture f = |
Craig Tiller | 4ac2b8e | 2017-11-10 14:14:17 -0800 | [diff] [blame] | 115 | begin_test(config, "keepalive_timeout", &keepalive_args, nullptr); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 116 | cq_verifier* cqv = cq_verifier_create(f.cq); |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 117 | grpc_op ops[6]; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 118 | grpc_op* op; |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 119 | grpc_metadata_array initial_metadata_recv; |
| 120 | grpc_metadata_array trailing_metadata_recv; |
| 121 | grpc_metadata_array request_metadata_recv; |
Craig Tiller | 4ac2b8e | 2017-11-10 14:14:17 -0800 | [diff] [blame] | 122 | grpc_byte_buffer* response_payload_recv = nullptr; |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 123 | grpc_call_details call_details; |
| 124 | grpc_status_code status; |
| 125 | grpc_call_error error; |
| 126 | grpc_slice details; |
| 127 | |
Yuchen Zeng | 3d43da7 | 2017-03-27 11:33:21 -0700 | [diff] [blame] | 128 | /* Disable ping ack to trigger the keepalive timeout */ |
| 129 | grpc_set_disable_ping_ack(true); |
| 130 | |
Chris Evans | ed2a547 | 2017-03-27 17:34:51 -0500 | [diff] [blame] | 131 | gpr_timespec deadline = five_seconds_from_now(); |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 132 | c = grpc_channel_create_call( |
Craig Tiller | 4ac2b8e | 2017-11-10 14:14:17 -0800 | [diff] [blame] | 133 | f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 134 | grpc_slice_from_static_string("/foo"), |
| 135 | get_host_override_slice("foo.test.google.fr:1234", config), deadline, |
Craig Tiller | 4ac2b8e | 2017-11-10 14:14:17 -0800 | [diff] [blame] | 136 | nullptr); |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 137 | GPR_ASSERT(c); |
| 138 | |
| 139 | grpc_metadata_array_init(&initial_metadata_recv); |
| 140 | grpc_metadata_array_init(&trailing_metadata_recv); |
| 141 | grpc_metadata_array_init(&request_metadata_recv); |
| 142 | grpc_call_details_init(&call_details); |
| 143 | |
| 144 | memset(ops, 0, sizeof(ops)); |
| 145 | op = ops; |
| 146 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 147 | op->data.send_initial_metadata.count = 0; |
| 148 | op++; |
| 149 | op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 150 | op++; |
| 151 | op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 152 | op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
| 153 | op++; |
| 154 | op->op = GRPC_OP_RECV_MESSAGE; |
| 155 | op->data.recv_message.recv_message = &response_payload_recv; |
| 156 | op++; |
Craig Tiller | 4ac2b8e | 2017-11-10 14:14:17 -0800 | [diff] [blame] | 157 | error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr); |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 158 | GPR_ASSERT(GRPC_CALL_OK == error); |
| 159 | |
| 160 | GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call( |
| 161 | f.server, &s, &call_details, |
| 162 | &request_metadata_recv, f.cq, f.cq, tag(101))); |
| 163 | CQ_EXPECT_COMPLETION(cqv, tag(101), 1); |
| 164 | cq_verify(cqv); |
| 165 | |
| 166 | memset(ops, 0, sizeof(ops)); |
| 167 | op = ops; |
| 168 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 169 | op->data.send_initial_metadata.count = 0; |
| 170 | op++; |
| 171 | op->op = GRPC_OP_SEND_MESSAGE; |
| 172 | op->data.send_message.send_message = response_payload; |
| 173 | op++; |
Craig Tiller | 4ac2b8e | 2017-11-10 14:14:17 -0800 | [diff] [blame] | 174 | error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr); |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 175 | GPR_ASSERT(GRPC_CALL_OK == error); |
| 176 | |
| 177 | CQ_EXPECT_COMPLETION(cqv, tag(102), 1); |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 178 | CQ_EXPECT_COMPLETION(cqv, tag(1), 1); |
| 179 | cq_verify(cqv); |
| 180 | |
| 181 | memset(ops, 0, sizeof(ops)); |
| 182 | op = ops; |
| 183 | op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 184 | op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 185 | op->data.recv_status_on_client.status = &status; |
| 186 | op->data.recv_status_on_client.status_details = &details; |
| 187 | op++; |
Craig Tiller | 4ac2b8e | 2017-11-10 14:14:17 -0800 | [diff] [blame] | 188 | error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(3), nullptr); |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 189 | GPR_ASSERT(GRPC_CALL_OK == error); |
| 190 | |
| 191 | CQ_EXPECT_COMPLETION(cqv, tag(3), 1); |
| 192 | cq_verify(cqv); |
| 193 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 194 | char* details_str = grpc_slice_to_c_string(details); |
| 195 | char* method_str = grpc_slice_to_c_string(call_details.method); |
Craig Tiller | 85516af | 2017-10-11 18:16:21 -0700 | [diff] [blame] | 196 | GPR_ASSERT(status == GRPC_STATUS_INTERNAL); |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 197 | GPR_ASSERT(0 == grpc_slice_str_cmp(details, "keepalive watchdog timeout")); |
| 198 | GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo")); |
| 199 | validate_host_override_string("foo.test.google.fr:1234", call_details.host, |
| 200 | config); |
| 201 | |
| 202 | gpr_free(details_str); |
| 203 | gpr_free(method_str); |
| 204 | |
| 205 | grpc_slice_unref(details); |
| 206 | grpc_metadata_array_destroy(&initial_metadata_recv); |
| 207 | grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 208 | grpc_metadata_array_destroy(&request_metadata_recv); |
| 209 | grpc_call_details_destroy(&call_details); |
| 210 | |
Craig Tiller | dd36b15 | 2017-03-31 08:27:28 -0700 | [diff] [blame] | 211 | grpc_call_unref(c); |
| 212 | grpc_call_unref(s); |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 213 | |
| 214 | cq_verifier_destroy(cqv); |
| 215 | |
| 216 | grpc_byte_buffer_destroy(response_payload); |
| 217 | grpc_byte_buffer_destroy(response_payload_recv); |
| 218 | |
Yuchen Zeng | 990d9fe | 2017-02-26 16:54:18 -0800 | [diff] [blame] | 219 | end_test(&f); |
| 220 | config.tear_down_data(&f); |
| 221 | } |
| 222 | |
| 223 | void keepalive_timeout(grpc_end2end_test_config config) { |
| 224 | test_keepalive_timeout(config); |
| 225 | } |
| 226 | |
| 227 | void keepalive_timeout_pre_init(void) {} |