Debian Codegraph @

test/core/end2end/tests/max_connection_idle.cc

raw
1 /*
2 *
3 * Copyright 2017 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 <string.h>
21
22 #include <grpc/grpc.h>
23 #include <grpc/impl/codegen/propagation_bits.h>
24 #include <grpc/slice.h>
25 #include <grpc/status.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/time.h>
29
30 #include "src/core/lib/channel/channel_args.h"
31 #include "src/core/lib/gpr/useful.h"
32 #include "test/core/end2end/cq_verifier.h"
33 #include "test/core/end2end/end2end_tests.h"
34 #include "test/core/util/test_config.h"
35
36 #define MAX_CONNECTION_IDLE_MS 2000
37 #define MAX_CONNECTION_AGE_MS 9999
38
39 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
40
41 static void drain_cq(grpc_completion_queue* cq) {
42 grpc_event ev;
43 do {
44 ev = grpc_completion_queue_next(cq, grpc_timeout_seconds_to_deadline(5),
45 nullptr);
46 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
47 }
48
49 static void simple_request_body(grpc_end2end_test_config /*config*/,
50 grpc_end2end_test_fixture* f) {
51 grpc_call* c;
52 grpc_call* s;
53 grpc_core::CqVerifier cqv(f->cq);
54 grpc_op ops[6];
55 grpc_op* op;
56 grpc_metadata_array initial_metadata_recv;
57 grpc_metadata_array trailing_metadata_recv;
58 grpc_metadata_array request_metadata_recv;
59 grpc_call_details call_details;
60 grpc_status_code status;
61 grpc_call_error error;
62 grpc_slice details;
63 int was_cancelled = 2;
64 char* peer;
65
66 gpr_timespec deadline = grpc_timeout_seconds_to_deadline(30);
67 c = grpc_channel_create_call(f->client, nullptr, GRPC_PROPAGATE_DEFAULTS,
68 f->cq, grpc_slice_from_static_string("/foo"),
69 nullptr, deadline, nullptr);
70 GPR_ASSERT(c);
71
72 peer = grpc_call_get_peer(c);
73 GPR_ASSERT(peer != nullptr);
74 gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer);
75 gpr_free(peer);
76
77 grpc_metadata_array_init(&initial_metadata_recv);
78 grpc_metadata_array_init(&trailing_metadata_recv);
79 grpc_metadata_array_init(&request_metadata_recv);
80 grpc_call_details_init(&call_details);
81
82 memset(ops, 0, sizeof(ops));
83 op = ops;
84 op->op = GRPC_OP_SEND_INITIAL_METADATA;
85 op->data.send_initial_metadata.count = 0;
86 op->flags = GRPC_INITIAL_METADATA_WAIT_FOR_READY |
87 GRPC_INITIAL_METADATA_WAIT_FOR_READY_EXPLICITLY_SET;
88 op->reserved = nullptr;
89 op++;
90 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
91 op->flags = 0;
92 op->reserved = nullptr;
93 op++;
94 op->op = GRPC_OP_RECV_INITIAL_METADATA;
95 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
96 op->flags = 0;
97 op->reserved = nullptr;
98 op++;
99 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
100 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
101 op->data.recv_status_on_client.status = &status;
102 op->data.recv_status_on_client.status_details = &details;
103 op->flags = 0;
104 op->reserved = nullptr;
105 op++;
106 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
107 nullptr);
108 GPR_ASSERT(GRPC_CALL_OK == error);
109
110 error =
111 grpc_server_request_call(f->server, &s, &call_details,
112 &request_metadata_recv, f->cq, f->cq, tag(101));
113 GPR_ASSERT(GRPC_CALL_OK == error);
114 cqv.Expect(tag(101), true);
115 cqv.Verify();
116
117 peer = grpc_call_get_peer(s);
118 GPR_ASSERT(peer != nullptr);
119 gpr_log(GPR_DEBUG, "server_peer=%s", peer);
120 gpr_free(peer);
121 peer = grpc_call_get_peer(c);
122 GPR_ASSERT(peer != nullptr);
123 gpr_log(GPR_DEBUG, "client_peer=%s", peer);
124 gpr_free(peer);
125
126 memset(ops, 0, sizeof(ops));
127 op = ops;
128 op->op = GRPC_OP_SEND_INITIAL_METADATA;
129 op->data.send_initial_metadata.count = 0;
130 op->flags = 0;
131 op->reserved = nullptr;
132 op++;
133 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
134 op->data.send_status_from_server.trailing_metadata_count = 0;
135 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
136 grpc_slice status_details = grpc_slice_from_static_string("xyz");
137 op->data.send_status_from_server.status_details = &status_details;
138 op->flags = 0;
139 op->reserved = nullptr;
140 op++;
141 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
142 op->data.recv_close_on_server.cancelled = &was_cancelled;
143 op->flags = 0;
144 op->reserved = nullptr;
145 op++;
146 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
147 nullptr);
148 GPR_ASSERT(GRPC_CALL_OK == error);
149
150 cqv.Expect(tag(102), true);
151 cqv.Expect(tag(1), true);
152 cqv.Verify();
153
154 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
155 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
156 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
157 GPR_ASSERT(was_cancelled == 0);
158
159 grpc_slice_unref(details);
160 grpc_metadata_array_destroy(&initial_metadata_recv);
161 grpc_metadata_array_destroy(&trailing_metadata_recv);
162 grpc_metadata_array_destroy(&request_metadata_recv);
163 grpc_call_details_destroy(&call_details);
164
165 grpc_call_unref(c);
166 grpc_call_unref(s);
167 }
168
169 static void test_max_connection_idle(grpc_end2end_test_config config) {
170 grpc_end2end_test_fixture f = config.create_fixture(nullptr, nullptr);
171 grpc_connectivity_state state = GRPC_CHANNEL_IDLE;
172 grpc_core::CqVerifier cqv(f.cq);
173
174 auto client_args = grpc_core::ChannelArgs()
175 .Set(GRPC_ARG_INITIAL_RECONNECT_BACKOFF_MS, 1000)
176 .Set(GRPC_ARG_MAX_RECONNECT_BACKOFF_MS, 1000)
177 .Set(GRPC_ARG_MIN_RECONNECT_BACKOFF_MS, 5000)
178 .ToC();
179 grpc_arg server_a[2];
180 server_a[0].type = GRPC_ARG_INTEGER;
181 server_a[0].key = const_cast<char*>(GRPC_ARG_MAX_CONNECTION_IDLE_MS);
182 server_a[0].value.integer = MAX_CONNECTION_IDLE_MS;
183 server_a[1].type = GRPC_ARG_INTEGER;
184 server_a[1].key = const_cast<char*>(GRPC_ARG_MAX_CONNECTION_AGE_MS);
185 server_a[1].value.integer = MAX_CONNECTION_AGE_MS;
186 grpc_channel_args server_args = {GPR_ARRAY_SIZE(server_a), server_a};
187
188 config.init_client(&f, client_args.get());
189 config.init_server(&f, &server_args);
190
191 /* check that we're still in idle, and start connecting */
192 GPR_ASSERT(grpc_channel_check_connectivity_state(f.client, 1) ==
193 GRPC_CHANNEL_IDLE);
194 /* we'll go through some set of transitions (some might be missed), until
195 READY is reached */
196 while (state != GRPC_CHANNEL_READY) {
197 grpc_channel_watch_connectivity_state(
198 f.client, state, grpc_timeout_seconds_to_deadline(10), f.cq, tag(99));
199 cqv.Expect(tag(99), true);
200 cqv.Verify();
201 state = grpc_channel_check_connectivity_state(f.client, 0);
202 GPR_ASSERT(state == GRPC_CHANNEL_READY ||
203 state == GRPC_CHANNEL_CONNECTING ||
204 state == GRPC_CHANNEL_TRANSIENT_FAILURE);
205 }
206
207 /* Use a simple request to cancel and reset the max idle timer */
208 simple_request_body(config, &f);
209
210 /* wait for the channel to reach its maximum idle time */
211 grpc_channel_watch_connectivity_state(
212 f.client, GRPC_CHANNEL_READY,
213 gpr_time_add(grpc_timeout_milliseconds_to_deadline(3000),
214 gpr_time_from_millis(MAX_CONNECTION_IDLE_MS, GPR_TIMESPAN)),
215 f.cq, tag(99));
216 cqv.Expect(tag(99), true);
217 cqv.Verify();
218 state = grpc_channel_check_connectivity_state(f.client, 0);
219 GPR_ASSERT(state == GRPC_CHANNEL_TRANSIENT_FAILURE ||
220 state == GRPC_CHANNEL_CONNECTING || state == GRPC_CHANNEL_IDLE);
221
222 grpc_server_shutdown_and_notify(f.server, f.cq, tag(0xdead));
223 cqv.Expect(tag(0xdead), true);
224 cqv.Verify();
225
226 grpc_server_destroy(f.server);
227 grpc_channel_destroy(f.client);
228 grpc_completion_queue_shutdown(f.cq);
229 drain_cq(f.cq);
230 grpc_completion_queue_destroy(f.cq);
231 config.tear_down_data(&f);
232 }
233
234 void max_connection_idle(grpc_end2end_test_config config) {
235 test_max_connection_idle(config);
236 }
237
238 void max_connection_idle_pre_init(void) {}