blob: 7ec33e97a3b189ec9a1aef7ba74f11f897658928 [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);
Mark D. Rothfe858512016-11-01 11:14:35 -070056 config.init_client(&f, client_args);
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
Craig Tiller28b72422016-10-26 21:15:29 -070098/* Creates and returns a grpc_slice containing random alphanumeric characters.
99 */
Craig Tillerd41a4a72016-10-26 16:16:06 -0700100static grpc_slice generate_random_slice() {
Craig Tiller20afa3d2016-10-17 14:52:14 -0700101 size_t i;
102 static const char chars[] = "abcdefghijklmnopqrstuvwxyz1234567890";
Craig Tillerf4050352016-10-28 08:59:55 -0700103 char *output;
104 const size_t output_size = 1024 * 1024;
105 output = gpr_malloc(output_size);
106 for (i = 0; i < output_size - 1; ++i) {
Craig Tiller20afa3d2016-10-17 14:52:14 -0700107 output[i] = chars[rand() % (int)(sizeof(chars) - 1)];
108 }
Craig Tillerf4050352016-10-28 08:59:55 -0700109 output[output_size - 1] = '\0';
Craig Tiller41fd9f22016-10-31 14:42:53 -0700110 grpc_slice out = grpc_slice_from_copied_string(output);
Craig Tillerf4050352016-10-28 08:59:55 -0700111 gpr_free(output);
112 return out;
Craig Tiller20afa3d2016-10-17 14:52:14 -0700113}
114
115void resource_quota_server(grpc_end2end_test_config config) {
Craig Tillerafcc8752016-10-18 16:10:06 -0700116 grpc_resource_quota *resource_quota =
117 grpc_resource_quota_create("test_server");
Craig Tiller20afa3d2016-10-17 14:52:14 -0700118 grpc_resource_quota_resize(resource_quota, 5 * 1024 * 1024);
119
120#define NUM_CALLS 100
121#define CLIENT_BASE_TAG 1000
122#define SERVER_START_BASE_TAG 2000
123#define SERVER_RECV_BASE_TAG 3000
124#define SERVER_END_BASE_TAG 4000
125
126 grpc_arg arg;
Craig Tiller153eaa72016-10-21 13:52:36 -0700127 arg.key = GRPC_ARG_RESOURCE_QUOTA;
Craig Tiller20afa3d2016-10-17 14:52:14 -0700128 arg.type = GRPC_ARG_POINTER;
129 arg.value.pointer.p = resource_quota;
130 arg.value.pointer.vtable = grpc_resource_quota_arg_vtable();
131 grpc_channel_args args = {1, &arg};
132
133 grpc_end2end_test_fixture f =
134 begin_test(config, "resource_quota_server", NULL, &args);
135
136 /* Create large request and response bodies. These are big enough to require
137 * multiple round trips to deliver to the peer, and their exact contents of
138 * will be verified on completion. */
Craig Tillerd41a4a72016-10-26 16:16:06 -0700139 grpc_slice request_payload_slice = generate_random_slice();
Craig Tiller20afa3d2016-10-17 14:52:14 -0700140
Masood Malekghassemi5a400572016-10-31 17:20:15 -0700141 grpc_call **client_calls = malloc(sizeof(grpc_call *) * NUM_CALLS);
142 grpc_call **server_calls = malloc(sizeof(grpc_call *) * NUM_CALLS);
143 grpc_metadata_array *initial_metadata_recv =
144 malloc(sizeof(grpc_metadata_array) * NUM_CALLS);
145 grpc_metadata_array *trailing_metadata_recv =
146 malloc(sizeof(grpc_metadata_array) * NUM_CALLS);
147 grpc_metadata_array *request_metadata_recv =
148 malloc(sizeof(grpc_metadata_array) * NUM_CALLS);
149 grpc_call_details *call_details =
150 malloc(sizeof(grpc_call_details) * NUM_CALLS);
151 grpc_status_code *status = malloc(sizeof(grpc_status_code) * NUM_CALLS);
152 char **details = malloc(sizeof(char *) * NUM_CALLS);
153 size_t *details_capacity = malloc(sizeof(size_t) * NUM_CALLS);
154 grpc_byte_buffer **request_payload_recv =
155 malloc(sizeof(grpc_byte_buffer *) * NUM_CALLS);
156 int *was_cancelled = malloc(sizeof(int) * NUM_CALLS);
Craig Tiller20afa3d2016-10-17 14:52:14 -0700157 grpc_call_error error;
158 int pending_client_calls = 0;
159 int pending_server_start_calls = 0;
160 int pending_server_recv_calls = 0;
161 int pending_server_end_calls = 0;
162 int cancelled_calls_on_client = 0;
163 int cancelled_calls_on_server = 0;
164
165 grpc_byte_buffer *request_payload =
166 grpc_raw_byte_buffer_create(&request_payload_slice, 1);
167
168 grpc_op ops[6];
169 grpc_op *op;
170
171 for (int i = 0; i < NUM_CALLS; i++) {
172 grpc_metadata_array_init(&initial_metadata_recv[i]);
173 grpc_metadata_array_init(&trailing_metadata_recv[i]);
174 grpc_metadata_array_init(&request_metadata_recv[i]);
175 grpc_call_details_init(&call_details[i]);
176 details[i] = NULL;
177 details_capacity[i] = 0;
178 request_payload_recv[i] = NULL;
179 was_cancelled[i] = 0;
180 }
181
182 for (int i = 0; i < NUM_CALLS; i++) {
183 error = grpc_server_request_call(
184 f.server, &server_calls[i], &call_details[i], &request_metadata_recv[i],
185 f.cq, f.cq, tag(SERVER_START_BASE_TAG + i));
186 GPR_ASSERT(GRPC_CALL_OK == error);
187
188 pending_server_start_calls++;
189 }
190
191 for (int i = 0; i < NUM_CALLS; i++) {
192 client_calls[i] = grpc_channel_create_call(
193 f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, "/foo",
194 "foo.test.google.fr", n_seconds_time(60), NULL);
195
196 memset(ops, 0, sizeof(ops));
197 op = ops;
198 op->op = GRPC_OP_SEND_INITIAL_METADATA;
199 op->data.send_initial_metadata.count = 0;
200 op->flags = 0;
201 op->reserved = NULL;
202 op++;
203 op->op = GRPC_OP_SEND_MESSAGE;
204 op->data.send_message = request_payload;
205 op->flags = 0;
206 op->reserved = NULL;
207 op++;
208 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
209 op->flags = 0;
210 op->reserved = NULL;
211 op++;
212 op->op = GRPC_OP_RECV_INITIAL_METADATA;
213 op->data.recv_initial_metadata = &initial_metadata_recv[i];
214 op->flags = 0;
215 op->reserved = NULL;
216 op++;
217 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
218 op->data.recv_status_on_client.trailing_metadata =
219 &trailing_metadata_recv[i];
220 op->data.recv_status_on_client.status = &status[i];
221 op->data.recv_status_on_client.status_details = &details[i];
222 op->data.recv_status_on_client.status_details_capacity =
223 &details_capacity[i];
224 op->flags = 0;
225 op->reserved = NULL;
226 op++;
227 error = grpc_call_start_batch(client_calls[i], ops, (size_t)(op - ops),
228 tag(CLIENT_BASE_TAG + i), NULL);
229 GPR_ASSERT(GRPC_CALL_OK == error);
230
231 pending_client_calls++;
232 }
233
234 while (pending_client_calls + pending_server_recv_calls +
235 pending_server_end_calls >
236 0) {
237 grpc_event ev = grpc_completion_queue_next(f.cq, n_seconds_time(10), NULL);
238 GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
239
240 int ev_tag = (int)(intptr_t)ev.tag;
241 if (ev_tag < CLIENT_BASE_TAG) {
242 abort(); /* illegal tag */
243 } else if (ev_tag < SERVER_START_BASE_TAG) {
244 /* client call finished */
245 int call_id = ev_tag - CLIENT_BASE_TAG;
246 GPR_ASSERT(call_id >= 0);
247 GPR_ASSERT(call_id < NUM_CALLS);
248 switch (status[call_id]) {
249 case GRPC_STATUS_RESOURCE_EXHAUSTED:
250 cancelled_calls_on_client++;
251 break;
252 case GRPC_STATUS_OK:
253 break;
254 default:
255 gpr_log(GPR_ERROR, "Unexpected status code: %d", status[call_id]);
256 abort();
257 }
258 GPR_ASSERT(pending_client_calls > 0);
259
260 grpc_metadata_array_destroy(&initial_metadata_recv[call_id]);
261 grpc_metadata_array_destroy(&trailing_metadata_recv[call_id]);
262 grpc_call_destroy(client_calls[call_id]);
263 gpr_free(details[call_id]);
264
265 pending_client_calls--;
266 } else if (ev_tag < SERVER_RECV_BASE_TAG) {
267 /* new incoming call to the server */
268 int call_id = ev_tag - SERVER_START_BASE_TAG;
269 GPR_ASSERT(call_id >= 0);
270 GPR_ASSERT(call_id < NUM_CALLS);
271
272 memset(ops, 0, sizeof(ops));
273 op = ops;
274 op->op = GRPC_OP_SEND_INITIAL_METADATA;
275 op->data.send_initial_metadata.count = 0;
276 op->flags = 0;
277 op->reserved = NULL;
278 op++;
279 op->op = GRPC_OP_RECV_MESSAGE;
280 op->data.recv_message = &request_payload_recv[call_id];
281 op->flags = 0;
282 op->reserved = NULL;
283 op++;
284 error =
285 grpc_call_start_batch(server_calls[call_id], ops, (size_t)(op - ops),
286 tag(SERVER_RECV_BASE_TAG + call_id), NULL);
287 GPR_ASSERT(GRPC_CALL_OK == error);
288
289 GPR_ASSERT(pending_server_start_calls > 0);
290 pending_server_start_calls--;
291 pending_server_recv_calls++;
292
293 grpc_call_details_destroy(&call_details[call_id]);
294 grpc_metadata_array_destroy(&request_metadata_recv[call_id]);
295 } else if (ev_tag < SERVER_END_BASE_TAG) {
296 /* finished read on the server */
297 int call_id = ev_tag - SERVER_RECV_BASE_TAG;
298 GPR_ASSERT(call_id >= 0);
299 GPR_ASSERT(call_id < NUM_CALLS);
300
301 if (ev.success) {
302 if (request_payload_recv[call_id] != NULL) {
303 grpc_byte_buffer_destroy(request_payload_recv[call_id]);
304 request_payload_recv[call_id] = NULL;
305 }
306 } else {
307 GPR_ASSERT(request_payload_recv[call_id] == NULL);
308 }
309
310 memset(ops, 0, sizeof(ops));
311 op = ops;
312 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
313 op->data.recv_close_on_server.cancelled = &was_cancelled[call_id];
314 op->flags = 0;
315 op->reserved = NULL;
316 op++;
317 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
318 op->data.send_status_from_server.trailing_metadata_count = 0;
319 op->data.send_status_from_server.status = GRPC_STATUS_OK;
320 op->data.send_status_from_server.status_details = "xyz";
321 op->flags = 0;
322 op->reserved = NULL;
323 op++;
324 error =
325 grpc_call_start_batch(server_calls[call_id], ops, (size_t)(op - ops),
326 tag(SERVER_END_BASE_TAG + call_id), NULL);
327 GPR_ASSERT(GRPC_CALL_OK == error);
328
329 GPR_ASSERT(pending_server_recv_calls > 0);
330 pending_server_recv_calls--;
331 pending_server_end_calls++;
332 } else {
333 int call_id = ev_tag - SERVER_END_BASE_TAG;
334 GPR_ASSERT(call_id >= 0);
335 GPR_ASSERT(call_id < NUM_CALLS);
336
337 if (was_cancelled[call_id]) {
338 cancelled_calls_on_server++;
339 }
340 GPR_ASSERT(pending_server_end_calls > 0);
341 pending_server_end_calls--;
342
343 grpc_call_destroy(server_calls[call_id]);
344 }
345 }
346
347 gpr_log(
348 GPR_INFO,
349 "Done. %d total calls: %d cancelled at server, %d cancelled at client.",
350 NUM_CALLS, cancelled_calls_on_server, cancelled_calls_on_client);
351
Craig Tiller8abc7962016-10-26 21:31:29 -0700352 /* The call may be cancelled after the server has sent its status but before
Craig Tiller423efd52016-10-27 08:08:03 -0700353 * the client has received it. This means that we should see strictly more
354 * failures on the client than on the server. */
Craig Tiller20afa3d2016-10-17 14:52:14 -0700355 GPR_ASSERT(cancelled_calls_on_client >= cancelled_calls_on_server);
Craig Tiller6b5d6822016-10-25 16:13:26 -0700356 /* However, we shouldn't see radically more... 0.9 is a guessed bound on what
357 * we'd want that ratio to be... to at least trigger some investigation should
358 * that ratio become much higher. */
Craig Tiller20afa3d2016-10-17 14:52:14 -0700359 GPR_ASSERT(cancelled_calls_on_server >= 0.9 * cancelled_calls_on_client);
360
361 grpc_byte_buffer_destroy(request_payload);
Craig Tillerd41a4a72016-10-26 16:16:06 -0700362 grpc_slice_unref(request_payload_slice);
Craig Tiller20afa3d2016-10-17 14:52:14 -0700363 grpc_resource_quota_unref(resource_quota);
364
Masood Malekghassemi5a400572016-10-31 17:20:15 -0700365 free(client_calls);
366 free(server_calls);
367 free(initial_metadata_recv);
368 free(trailing_metadata_recv);
369 free(request_metadata_recv);
370 free(call_details);
371 free(status);
372 free(details);
373 free(details_capacity);
374 free(request_payload_recv);
375 free(was_cancelled);
376
Craig Tiller20afa3d2016-10-17 14:52:14 -0700377 end_test(&f);
378 config.tear_down_data(&f);
379}
380
381void resource_quota_server_pre_init(void) {}