Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | #include <unistd.h> |
| 39 | |
| 40 | #include <grpc/byte_buffer.h> |
| 41 | #include <grpc/support/alloc.h> |
| 42 | #include <grpc/support/log.h> |
| 43 | #include <grpc/support/time.h> |
| 44 | #include <grpc/support/useful.h> |
| 45 | #include "test/core/end2end/cq_verifier.h" |
| 46 | |
| 47 | enum { TIMEOUT = 200000 }; |
| 48 | |
| 49 | static void *tag(gpr_intptr t) { return (void *)t; } |
| 50 | |
| 51 | static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
| 52 | const char *test_name, |
| 53 | grpc_channel_args *client_args, |
| 54 | grpc_channel_args *server_args) { |
| 55 | grpc_end2end_test_fixture f; |
| 56 | gpr_log(GPR_INFO, "%s/%s", test_name, config.name); |
| 57 | f = config.create_fixture(client_args, server_args); |
| 58 | config.init_client(&f, client_args); |
| 59 | config.init_server(&f, server_args); |
| 60 | return f; |
| 61 | } |
| 62 | |
| 63 | static gpr_timespec n_seconds_time(int n) { |
Craig Tiller | 8ad8a41 | 2015-02-25 08:36:40 -0800 | [diff] [blame] | 64 | return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 67 | static void drain_cq(grpc_completion_queue *cq) { |
| 68 | grpc_event *ev; |
| 69 | grpc_completion_type type; |
| 70 | do { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 71 | ev = grpc_completion_queue_next(cq, n_seconds_time(5)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 72 | GPR_ASSERT(ev); |
| 73 | type = ev->type; |
| 74 | grpc_event_finish(ev); |
| 75 | } while (type != GRPC_QUEUE_SHUTDOWN); |
| 76 | } |
| 77 | |
| 78 | static void shutdown_server(grpc_end2end_test_fixture *f) { |
| 79 | if (!f->server) return; |
| 80 | grpc_server_shutdown(f->server); |
| 81 | grpc_server_destroy(f->server); |
| 82 | f->server = NULL; |
| 83 | } |
| 84 | |
| 85 | static void shutdown_client(grpc_end2end_test_fixture *f) { |
| 86 | if (!f->client) return; |
| 87 | grpc_channel_destroy(f->client); |
| 88 | f->client = NULL; |
| 89 | } |
| 90 | |
| 91 | static void end_test(grpc_end2end_test_fixture *f) { |
| 92 | shutdown_server(f); |
| 93 | shutdown_client(f); |
| 94 | |
| 95 | grpc_completion_queue_shutdown(f->server_cq); |
| 96 | drain_cq(f->server_cq); |
| 97 | grpc_completion_queue_destroy(f->server_cq); |
| 98 | grpc_completion_queue_shutdown(f->client_cq); |
| 99 | drain_cq(f->client_cq); |
| 100 | grpc_completion_queue_destroy(f->client_cq); |
| 101 | } |
| 102 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 103 | static gpr_slice large_slice(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 104 | gpr_slice slice = gpr_slice_malloc(1000000); |
| 105 | memset(GPR_SLICE_START_PTR(slice), 0xab, GPR_SLICE_LENGTH(slice)); |
| 106 | return slice; |
| 107 | } |
| 108 | |
| 109 | static void test_invoke_large_request(grpc_end2end_test_config config) { |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 110 | grpc_end2end_test_fixture f = begin_test(config, __FUNCTION__, NULL, NULL); |
| 111 | |
| 112 | gpr_slice request_payload_slice = large_slice(); |
| 113 | gpr_slice response_payload_slice = large_slice(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 114 | grpc_call *c; |
| 115 | grpc_call *s; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 116 | grpc_byte_buffer *request_payload = |
| 117 | grpc_byte_buffer_create(&request_payload_slice, 1); |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 118 | grpc_byte_buffer *response_payload = |
| 119 | grpc_byte_buffer_create(&response_payload_slice, 1); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 120 | gpr_timespec deadline = n_seconds_time(30); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 121 | cq_verifier *v_client = cq_verifier_create(f.client_cq); |
| 122 | cq_verifier *v_server = cq_verifier_create(f.server_cq); |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 123 | grpc_op ops[6]; |
| 124 | grpc_op *op; |
| 125 | grpc_metadata_array initial_metadata_recv; |
| 126 | grpc_metadata_array trailing_metadata_recv; |
| 127 | grpc_metadata_array request_metadata_recv; |
| 128 | grpc_byte_buffer *request_payload_recv = NULL; |
| 129 | grpc_byte_buffer *response_payload_recv = NULL; |
| 130 | grpc_call_details call_details; |
| 131 | grpc_status_code status; |
| 132 | char *details = NULL; |
| 133 | size_t details_capacity = 0; |
| 134 | int was_cancelled = 2; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 135 | |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 136 | c = grpc_channel_create_call(f.client, f.client_cq, "/foo", |
| 137 | "foo.test.google.fr", deadline); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 138 | GPR_ASSERT(c); |
| 139 | |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 140 | grpc_metadata_array_init(&initial_metadata_recv); |
| 141 | grpc_metadata_array_init(&trailing_metadata_recv); |
| 142 | grpc_metadata_array_init(&request_metadata_recv); |
| 143 | grpc_call_details_init(&call_details); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 144 | |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 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_MESSAGE; |
| 150 | op->data.send_message = request_payload; |
| 151 | op++; |
| 152 | op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 153 | op++; |
| 154 | op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 155 | op->data.recv_initial_metadata = &initial_metadata_recv; |
| 156 | op++; |
| 157 | op->op = GRPC_OP_RECV_MESSAGE; |
| 158 | op->data.recv_message = &response_payload_recv; |
| 159 | op++; |
| 160 | op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 161 | op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 162 | op->data.recv_status_on_client.status = &status; |
| 163 | op->data.recv_status_on_client.status_details = &details; |
| 164 | op->data.recv_status_on_client.status_details_capacity = &details_capacity; |
| 165 | op++; |
| 166 | GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(c, ops, op - ops, tag(1))); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 167 | |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 168 | GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(f.server, &s, |
| 169 | &call_details, |
| 170 | &request_metadata_recv, |
| 171 | f.server_cq, tag(101))); |
| 172 | cq_expect_completion(v_server, tag(101), GRPC_OP_OK); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 173 | cq_verify(v_server); |
| 174 | |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 175 | op = ops; |
| 176 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 177 | op->data.send_initial_metadata.count = 0; |
| 178 | op++; |
| 179 | op->op = GRPC_OP_SEND_MESSAGE; |
| 180 | op->data.send_message = response_payload; |
| 181 | op++; |
| 182 | op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
| 183 | op->data.send_status_from_server.trailing_metadata_count = 0; |
| 184 | op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED; |
| 185 | op->data.send_status_from_server.status_details = "xyz"; |
| 186 | op++; |
| 187 | op->op = GRPC_OP_RECV_MESSAGE; |
| 188 | op->data.recv_message = &request_payload_recv; |
| 189 | op++; |
| 190 | op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
| 191 | op->data.recv_close_on_server.cancelled = &was_cancelled; |
| 192 | op++; |
| 193 | GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(s, ops, op - ops, tag(102))); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 194 | |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 195 | cq_expect_completion(v_server, tag(102), GRPC_OP_OK); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 196 | cq_verify(v_server); |
| 197 | |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 198 | cq_expect_completion(v_client, tag(1), GRPC_OP_OK); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 199 | cq_verify(v_client); |
| 200 | |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 201 | GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED); |
| 202 | GPR_ASSERT(0 == strcmp(details, "xyz")); |
| 203 | GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); |
| 204 | GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr")); |
| 205 | GPR_ASSERT(was_cancelled == 0); |
| 206 | |
| 207 | gpr_free(details); |
| 208 | grpc_metadata_array_destroy(&initial_metadata_recv); |
| 209 | grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 210 | grpc_metadata_array_destroy(&request_metadata_recv); |
| 211 | grpc_call_details_destroy(&call_details); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 212 | |
| 213 | grpc_call_destroy(c); |
| 214 | grpc_call_destroy(s); |
| 215 | |
| 216 | cq_verifier_destroy(v_client); |
| 217 | cq_verifier_destroy(v_server); |
| 218 | |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 219 | grpc_byte_buffer_destroy(request_payload); |
| 220 | grpc_byte_buffer_destroy(response_payload); |
| 221 | grpc_byte_buffer_destroy(request_payload_recv); |
| 222 | grpc_byte_buffer_destroy(response_payload_recv); |
Craig Tiller | 5fe7e5d | 2015-05-01 10:52:35 -0700 | [diff] [blame] | 223 | gpr_slice_unref(request_payload_slice); |
| 224 | gpr_slice_unref(response_payload_slice); |
Craig Tiller | 8b1d989 | 2015-04-28 14:28:19 -0700 | [diff] [blame] | 225 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 226 | end_test(&f); |
| 227 | config.tear_down_data(&f); |
| 228 | } |
| 229 | |
| 230 | void grpc_end2end_tests(grpc_end2end_test_config config) { |
| 231 | test_invoke_large_request(config); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 232 | } |