OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015, 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 |
| 46 enum { TIMEOUT = 200000 }; |
| 47 |
| 48 static void *tag(intptr_t t) { return (void *)t; } |
| 49 |
| 50 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
| 51 const char *test_name, |
| 52 grpc_channel_args *client_args, |
| 53 grpc_channel_args *server_args) { |
| 54 grpc_end2end_test_fixture f; |
| 55 gpr_log(GPR_INFO, "%s/%s", test_name, config.name); |
| 56 f = config.create_fixture(client_args, server_args); |
| 57 config.init_server(&f, server_args); |
| 58 config.init_client(&f, client_args); |
| 59 return f; |
| 60 } |
| 61 |
| 62 static gpr_timespec n_seconds_time(int n) { |
| 63 return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); |
| 64 } |
| 65 |
| 66 static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } |
| 67 |
| 68 static void drain_cq(grpc_completion_queue *cq) { |
| 69 grpc_event ev; |
| 70 do { |
| 71 ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL); |
| 72 } while (ev.type != GRPC_QUEUE_SHUTDOWN); |
| 73 } |
| 74 |
| 75 static void shutdown_server(grpc_end2end_test_fixture *f) { |
| 76 if (!f->server) return; |
| 77 grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); |
| 78 GPR_ASSERT(grpc_completion_queue_pluck(f->cq, tag(1000), |
| 79 GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), |
| 80 NULL).type == GRPC_OP_COMPLETE); |
| 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->cq); |
| 96 drain_cq(f->cq); |
| 97 grpc_completion_queue_destroy(f->cq); |
| 98 } |
| 99 |
| 100 static void request_response_with_payload(grpc_end2end_test_fixture f) { |
| 101 gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world"); |
| 102 gpr_slice response_payload_slice = gpr_slice_from_copied_string("hello you"); |
| 103 grpc_call *c; |
| 104 grpc_call *s; |
| 105 grpc_byte_buffer *request_payload = |
| 106 grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
| 107 grpc_byte_buffer *response_payload = |
| 108 grpc_raw_byte_buffer_create(&response_payload_slice, 1); |
| 109 gpr_timespec deadline = five_seconds_time(); |
| 110 cq_verifier *cqv = cq_verifier_create(f.cq); |
| 111 grpc_op ops[6]; |
| 112 grpc_op *op; |
| 113 grpc_metadata_array initial_metadata_recv; |
| 114 grpc_metadata_array trailing_metadata_recv; |
| 115 grpc_metadata_array request_metadata_recv; |
| 116 grpc_byte_buffer *request_payload_recv = NULL; |
| 117 grpc_byte_buffer *response_payload_recv = NULL; |
| 118 grpc_call_details call_details; |
| 119 grpc_status_code status; |
| 120 grpc_call_error error; |
| 121 char *details = NULL; |
| 122 size_t details_capacity = 0; |
| 123 int was_cancelled = 2; |
| 124 |
| 125 c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, |
| 126 "/foo", "foo.test.google.fr", deadline, NULL); |
| 127 GPR_ASSERT(c); |
| 128 |
| 129 grpc_metadata_array_init(&initial_metadata_recv); |
| 130 grpc_metadata_array_init(&trailing_metadata_recv); |
| 131 grpc_metadata_array_init(&request_metadata_recv); |
| 132 grpc_call_details_init(&call_details); |
| 133 |
| 134 op = ops; |
| 135 op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 136 op->data.send_initial_metadata.count = 0; |
| 137 op->flags = 0; |
| 138 op->reserved = NULL; |
| 139 op++; |
| 140 op->op = GRPC_OP_SEND_MESSAGE; |
| 141 op->data.send_message = request_payload; |
| 142 op->flags = 0; |
| 143 op->reserved = NULL; |
| 144 op++; |
| 145 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 146 op->flags = 0; |
| 147 op->reserved = NULL; |
| 148 op++; |
| 149 op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 150 op->data.recv_initial_metadata = &initial_metadata_recv; |
| 151 op->flags = 0; |
| 152 op->reserved = NULL; |
| 153 op++; |
| 154 op->op = GRPC_OP_RECV_MESSAGE; |
| 155 op->data.recv_message = &response_payload_recv; |
| 156 op->flags = 0; |
| 157 op->reserved = NULL; |
| 158 op++; |
| 159 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 160 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 161 op->data.recv_status_on_client.status = &status; |
| 162 op->data.recv_status_on_client.status_details = &details; |
| 163 op->data.recv_status_on_client.status_details_capacity = &details_capacity; |
| 164 op->flags = 0; |
| 165 op->reserved = NULL; |
| 166 op++; |
| 167 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL); |
| 168 GPR_ASSERT(GRPC_CALL_OK == error); |
| 169 |
| 170 error = |
| 171 grpc_server_request_call(f.server, &s, &call_details, |
| 172 &request_metadata_recv, f.cq, f.cq, tag(101)); |
| 173 GPR_ASSERT(GRPC_CALL_OK == error); |
| 174 cq_expect_completion(cqv, tag(101), 1); |
| 175 cq_verify(cqv); |
| 176 |
| 177 op = ops; |
| 178 op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 179 op->data.send_initial_metadata.count = 0; |
| 180 op->flags = 0; |
| 181 op->reserved = NULL; |
| 182 op++; |
| 183 op->op = GRPC_OP_RECV_MESSAGE; |
| 184 op->data.recv_message = &request_payload_recv; |
| 185 op->flags = 0; |
| 186 op->reserved = NULL; |
| 187 op++; |
| 188 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL); |
| 189 GPR_ASSERT(GRPC_CALL_OK == error); |
| 190 |
| 191 cq_expect_completion(cqv, tag(102), 1); |
| 192 cq_verify(cqv); |
| 193 |
| 194 op = ops; |
| 195 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
| 196 op->data.recv_close_on_server.cancelled = &was_cancelled; |
| 197 op->flags = 0; |
| 198 op->reserved = NULL; |
| 199 op++; |
| 200 op->op = GRPC_OP_SEND_MESSAGE; |
| 201 op->data.send_message = response_payload; |
| 202 op->flags = 0; |
| 203 op->reserved = NULL; |
| 204 op++; |
| 205 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
| 206 op->data.send_status_from_server.trailing_metadata_count = 0; |
| 207 op->data.send_status_from_server.status = GRPC_STATUS_OK; |
| 208 op->data.send_status_from_server.status_details = "xyz"; |
| 209 op->flags = 0; |
| 210 op->reserved = NULL; |
| 211 op++; |
| 212 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(103), NULL); |
| 213 GPR_ASSERT(GRPC_CALL_OK == error); |
| 214 |
| 215 cq_expect_completion(cqv, tag(103), 1); |
| 216 cq_expect_completion(cqv, tag(1), 1); |
| 217 cq_verify(cqv); |
| 218 |
| 219 GPR_ASSERT(status == GRPC_STATUS_OK); |
| 220 GPR_ASSERT(0 == strcmp(details, "xyz")); |
| 221 GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); |
| 222 GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr")); |
| 223 GPR_ASSERT(was_cancelled == 0); |
| 224 GPR_ASSERT(byte_buffer_eq_string(request_payload_recv, "hello world")); |
| 225 GPR_ASSERT(byte_buffer_eq_string(response_payload_recv, "hello you")); |
| 226 |
| 227 gpr_free(details); |
| 228 grpc_metadata_array_destroy(&initial_metadata_recv); |
| 229 grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 230 grpc_metadata_array_destroy(&request_metadata_recv); |
| 231 grpc_call_details_destroy(&call_details); |
| 232 |
| 233 grpc_call_destroy(c); |
| 234 grpc_call_destroy(s); |
| 235 |
| 236 cq_verifier_destroy(cqv); |
| 237 |
| 238 grpc_byte_buffer_destroy(request_payload); |
| 239 grpc_byte_buffer_destroy(response_payload); |
| 240 grpc_byte_buffer_destroy(request_payload_recv); |
| 241 grpc_byte_buffer_destroy(response_payload_recv); |
| 242 } |
| 243 |
| 244 /* Client sends a request with payload, server reads then returns a response |
| 245 payload and status. */ |
| 246 static void test_invoke_request_response_with_payload( |
| 247 grpc_end2end_test_config config) { |
| 248 grpc_end2end_test_fixture f = begin_test( |
| 249 config, "test_invoke_request_response_with_payload", NULL, NULL); |
| 250 request_response_with_payload(f); |
| 251 end_test(&f); |
| 252 config.tear_down_data(&f); |
| 253 } |
| 254 |
| 255 static void test_invoke_10_request_response_with_payload( |
| 256 grpc_end2end_test_config config) { |
| 257 int i; |
| 258 grpc_end2end_test_fixture f = begin_test( |
| 259 config, "test_invoke_10_request_response_with_payload", NULL, NULL); |
| 260 for (i = 0; i < 10; i++) { |
| 261 request_response_with_payload(f); |
| 262 } |
| 263 end_test(&f); |
| 264 config.tear_down_data(&f); |
| 265 } |
| 266 |
| 267 void payload(grpc_end2end_test_config config) { |
| 268 test_invoke_request_response_with_payload(config); |
| 269 test_invoke_10_request_response_with_payload(config); |
| 270 } |
OLD | NEW |