blob: 76374d16a5658e23dd3f8215f866f7418ef8ab34 [file] [log] [blame]
Craig Tiller20afa3d2016-10-17 14:52:14 -07001/*
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 <stdio.h>
37#include <string.h>
38
39#include <grpc/byte_buffer.h>
40#include <grpc/support/alloc.h>
41#include <grpc/support/log.h>
42#include <grpc/support/time.h>
43#include <grpc/support/useful.h>
44#include "test/core/end2end/cq_verifier.h"
45
46static void *tag(intptr_t t) { return (void *)t; }
47
48static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
49 const char *test_name,
50 grpc_channel_args *client_args,
51 grpc_channel_args *server_args) {
52 grpc_end2end_test_fixture f;
53 gpr_log(GPR_INFO, "%s/%s", test_name, config.name);
54 f = config.create_fixture(client_args, server_args);
55 config.init_server(&f, server_args);
Craig Tiller91d00932016-10-19 15:26:13 -070056 config.init_client(&f, client_args, NULL);
Craig Tiller20afa3d2016-10-17 14:52:14 -070057 return f;
58}
59
60static gpr_timespec n_seconds_time(int n) {
61 return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n);
62}
63
64static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); }
65
66static void drain_cq(grpc_completion_queue *cq) {
67 grpc_event ev;
68 do {
69 ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL);
70 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
71}
72
73static void shutdown_server(grpc_end2end_test_fixture *f) {
74 if (!f->server) return;
75 grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
76 GPR_ASSERT(grpc_completion_queue_pluck(
77 f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL)
78 .type == GRPC_OP_COMPLETE);
79 grpc_server_destroy(f->server);
80 f->server = NULL;
81}
82
83static void shutdown_client(grpc_end2end_test_fixture *f) {
84 if (!f->client) return;
85 grpc_channel_destroy(f->client);
86 f->client = NULL;
87}
88
89static void end_test(grpc_end2end_test_fixture *f) {
90 shutdown_server(f);
91 shutdown_client(f);
92
93 grpc_completion_queue_shutdown(f->cq);
94 drain_cq(f->cq);
95 grpc_completion_queue_destroy(f->cq);
96}
97
98/* Creates and returns a gpr_slice containing random alphanumeric characters. */
99static gpr_slice generate_random_slice() {
100 size_t i;
101 static const char chars[] = "abcdefghijklmnopqrstuvwxyz1234567890";
102 char output[1024 * 1024];
103 for (i = 0; i < GPR_ARRAY_SIZE(output) - 1; ++i) {
104 output[i] = chars[rand() % (int)(sizeof(chars) - 1)];
105 }
106 output[GPR_ARRAY_SIZE(output) - 1] = '\0';
107 return gpr_slice_from_copied_string(output);
108}
109
110void resource_quota_server(grpc_end2end_test_config config) {
Craig Tillerafcc8752016-10-18 16:10:06 -0700111 grpc_resource_quota *resource_quota =
112 grpc_resource_quota_create("test_server");
Craig Tiller20afa3d2016-10-17 14:52:14 -0700113 grpc_resource_quota_resize(resource_quota, 5 * 1024 * 1024);
114
115#define NUM_CALLS 100
116#define CLIENT_BASE_TAG 1000
117#define SERVER_START_BASE_TAG 2000
118#define SERVER_RECV_BASE_TAG 3000
119#define SERVER_END_BASE_TAG 4000
120
121 grpc_arg arg;
Craig Tiller153eaa72016-10-21 13:52:36 -0700122 arg.key = GRPC_ARG_RESOURCE_QUOTA;
Craig Tiller20afa3d2016-10-17 14:52:14 -0700123 arg.type = GRPC_ARG_POINTER;
124 arg.value.pointer.p = resource_quota;
125 arg.value.pointer.vtable = grpc_resource_quota_arg_vtable();
126 grpc_channel_args args = {1, &arg};
127
128 grpc_end2end_test_fixture f =
129 begin_test(config, "resource_quota_server", NULL, &args);
130
131 /* Create large request and response bodies. These are big enough to require
132 * multiple round trips to deliver to the peer, and their exact contents of
133 * will be verified on completion. */
134 gpr_slice request_payload_slice = generate_random_slice();
135
136 grpc_call *client_calls[NUM_CALLS];
137 grpc_call *server_calls[NUM_CALLS];
138 grpc_metadata_array initial_metadata_recv[NUM_CALLS];
139 grpc_metadata_array trailing_metadata_recv[NUM_CALLS];
140 grpc_metadata_array request_metadata_recv[NUM_CALLS];
141 grpc_call_details call_details[NUM_CALLS];
142 grpc_status_code status[NUM_CALLS];
143 char *details[NUM_CALLS];
144 size_t details_capacity[NUM_CALLS];
145 grpc_byte_buffer *request_payload_recv[NUM_CALLS];
146 int was_cancelled[NUM_CALLS];
147 grpc_call_error error;
148 int pending_client_calls = 0;
149 int pending_server_start_calls = 0;
150 int pending_server_recv_calls = 0;
151 int pending_server_end_calls = 0;
152 int cancelled_calls_on_client = 0;
153 int cancelled_calls_on_server = 0;
154
155 grpc_byte_buffer *request_payload =
156 grpc_raw_byte_buffer_create(&request_payload_slice, 1);
157
158 grpc_op ops[6];
159 grpc_op *op;
160
161 for (int i = 0; i < NUM_CALLS; i++) {
162 grpc_metadata_array_init(&initial_metadata_recv[i]);
163 grpc_metadata_array_init(&trailing_metadata_recv[i]);
164 grpc_metadata_array_init(&request_metadata_recv[i]);
165 grpc_call_details_init(&call_details[i]);
166 details[i] = NULL;
167 details_capacity[i] = 0;
168 request_payload_recv[i] = NULL;
169 was_cancelled[i] = 0;
170 }
171
172 for (int i = 0; i < NUM_CALLS; i++) {
173 error = grpc_server_request_call(
174 f.server, &server_calls[i], &call_details[i], &request_metadata_recv[i],
175 f.cq, f.cq, tag(SERVER_START_BASE_TAG + i));
176 GPR_ASSERT(GRPC_CALL_OK == error);
177
178 pending_server_start_calls++;
179 }
180
181 for (int i = 0; i < NUM_CALLS; i++) {
182 client_calls[i] = grpc_channel_create_call(
183 f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo",
184 "foo.test.google.fr", n_seconds_time(60), NULL);
185
186 memset(ops, 0, sizeof(ops));
187 op = ops;
188 op->op = GRPC_OP_SEND_INITIAL_METADATA;
189 op->data.send_initial_metadata.count = 0;
190 op->flags = 0;
191 op->reserved = NULL;
192 op++;
193 op->op = GRPC_OP_SEND_MESSAGE;
194 op->data.send_message = request_payload;
195 op->flags = 0;
196 op->reserved = NULL;
197 op++;
198 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
199 op->flags = 0;
200 op->reserved = NULL;
201 op++;
202 op->op = GRPC_OP_RECV_INITIAL_METADATA;
203 op->data.recv_initial_metadata = &initial_metadata_recv[i];
204 op->flags = 0;
205 op->reserved = NULL;
206 op++;
207 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
208 op->data.recv_status_on_client.trailing_metadata =
209 &trailing_metadata_recv[i];
210 op->data.recv_status_on_client.status = &status[i];
211 op->data.recv_status_on_client.status_details = &details[i];
212 op->data.recv_status_on_client.status_details_capacity =
213 &details_capacity[i];
214 op->flags = 0;
215 op->reserved = NULL;
216 op++;
217 error = grpc_call_start_batch(client_calls[i], ops, (size_t)(op - ops),
218 tag(CLIENT_BASE_TAG + i), NULL);
219 GPR_ASSERT(GRPC_CALL_OK == error);
220
221 pending_client_calls++;
222 }
223
224 while (pending_client_calls + pending_server_recv_calls +
225 pending_server_end_calls >
226 0) {
227 grpc_event ev = grpc_completion_queue_next(f.cq, n_seconds_time(10), NULL);
228 GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
229
230 int ev_tag = (int)(intptr_t)ev.tag;
231 if (ev_tag < CLIENT_BASE_TAG) {
232 abort(); /* illegal tag */
233 } else if (ev_tag < SERVER_START_BASE_TAG) {
234 /* client call finished */
235 int call_id = ev_tag - CLIENT_BASE_TAG;
236 GPR_ASSERT(call_id >= 0);
237 GPR_ASSERT(call_id < NUM_CALLS);
238 switch (status[call_id]) {
239 case GRPC_STATUS_RESOURCE_EXHAUSTED:
240 cancelled_calls_on_client++;
241 break;
242 case GRPC_STATUS_OK:
243 break;
244 default:
245 gpr_log(GPR_ERROR, "Unexpected status code: %d", status[call_id]);
246 abort();
247 }
248 GPR_ASSERT(pending_client_calls > 0);
249
250 grpc_metadata_array_destroy(&initial_metadata_recv[call_id]);
251 grpc_metadata_array_destroy(&trailing_metadata_recv[call_id]);
252 grpc_call_destroy(client_calls[call_id]);
253 gpr_free(details[call_id]);
254
255 pending_client_calls--;
256 } else if (ev_tag < SERVER_RECV_BASE_TAG) {
257 /* new incoming call to the server */
258 int call_id = ev_tag - SERVER_START_BASE_TAG;
259 GPR_ASSERT(call_id >= 0);
260 GPR_ASSERT(call_id < NUM_CALLS);
261
262 memset(ops, 0, sizeof(ops));
263 op = ops;
264 op->op = GRPC_OP_SEND_INITIAL_METADATA;
265 op->data.send_initial_metadata.count = 0;
266 op->flags = 0;
267 op->reserved = NULL;
268 op++;
269 op->op = GRPC_OP_RECV_MESSAGE;
270 op->data.recv_message = &request_payload_recv[call_id];
271 op->flags = 0;
272 op->reserved = NULL;
273 op++;
274 error =
275 grpc_call_start_batch(server_calls[call_id], ops, (size_t)(op - ops),
276 tag(SERVER_RECV_BASE_TAG + call_id), NULL);
277 GPR_ASSERT(GRPC_CALL_OK == error);
278
279 GPR_ASSERT(pending_server_start_calls > 0);
280 pending_server_start_calls--;
281 pending_server_recv_calls++;
282
283 grpc_call_details_destroy(&call_details[call_id]);
284 grpc_metadata_array_destroy(&request_metadata_recv[call_id]);
285 } else if (ev_tag < SERVER_END_BASE_TAG) {
286 /* finished read on the server */
287 int call_id = ev_tag - SERVER_RECV_BASE_TAG;
288 GPR_ASSERT(call_id >= 0);
289 GPR_ASSERT(call_id < NUM_CALLS);
290
291 if (ev.success) {
292 if (request_payload_recv[call_id] != NULL) {
293 grpc_byte_buffer_destroy(request_payload_recv[call_id]);
294 request_payload_recv[call_id] = NULL;
295 }
296 } else {
297 GPR_ASSERT(request_payload_recv[call_id] == NULL);
298 }
299
300 memset(ops, 0, sizeof(ops));
301 op = ops;
302 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
303 op->data.recv_close_on_server.cancelled = &was_cancelled[call_id];
304 op->flags = 0;
305 op->reserved = NULL;
306 op++;
307 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
308 op->data.send_status_from_server.trailing_metadata_count = 0;
309 op->data.send_status_from_server.status = GRPC_STATUS_OK;
310 op->data.send_status_from_server.status_details = "xyz";
311 op->flags = 0;
312 op->reserved = NULL;
313 op++;
314 error =
315 grpc_call_start_batch(server_calls[call_id], ops, (size_t)(op - ops),
316 tag(SERVER_END_BASE_TAG + call_id), NULL);
317 GPR_ASSERT(GRPC_CALL_OK == error);
318
319 GPR_ASSERT(pending_server_recv_calls > 0);
320 pending_server_recv_calls--;
321 pending_server_end_calls++;
322 } else {
323 int call_id = ev_tag - SERVER_END_BASE_TAG;
324 GPR_ASSERT(call_id >= 0);
325 GPR_ASSERT(call_id < NUM_CALLS);
326
327 if (was_cancelled[call_id]) {
328 cancelled_calls_on_server++;
329 }
330 GPR_ASSERT(pending_server_end_calls > 0);
331 pending_server_end_calls--;
332
333 grpc_call_destroy(server_calls[call_id]);
334 }
335 }
336
337 gpr_log(
338 GPR_INFO,
339 "Done. %d total calls: %d cancelled at server, %d cancelled at client.",
340 NUM_CALLS, cancelled_calls_on_server, cancelled_calls_on_client);
341
Craig Tiller8abc7962016-10-26 21:31:29 -0700342 /* The call may be cancelled after the server has sent its status but before
Craig Tiller423efd52016-10-27 08:08:03 -0700343 * the client has received it. This means that we should see strictly more
344 * failures on the client than on the server. */
Craig Tiller20afa3d2016-10-17 14:52:14 -0700345 GPR_ASSERT(cancelled_calls_on_client >= cancelled_calls_on_server);
Craig Tiller6b5d6822016-10-25 16:13:26 -0700346 /* However, we shouldn't see radically more... 0.9 is a guessed bound on what
347 * we'd want that ratio to be... to at least trigger some investigation should
348 * that ratio become much higher. */
Craig Tiller20afa3d2016-10-17 14:52:14 -0700349 GPR_ASSERT(cancelled_calls_on_server >= 0.9 * cancelled_calls_on_client);
350
351 grpc_byte_buffer_destroy(request_payload);
352 gpr_slice_unref(request_payload_slice);
353 grpc_resource_quota_unref(resource_quota);
354
355 end_test(&f);
356 config.tear_down_data(&f);
357}
358
359void resource_quota_server_pre_init(void) {}