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> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 38 | |
Craig Tiller | 7da8ef1 | 2015-01-23 15:07:22 -0800 | [diff] [blame] | 39 | #include "src/core/support/string.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | #include <grpc/byte_buffer.h> |
Craig Tiller | ea61b07 | 2015-02-03 19:19:27 -0800 | [diff] [blame] | 41 | #include <grpc/grpc.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 42 | #include <grpc/support/alloc.h> |
| 43 | #include <grpc/support/log.h> |
| 44 | #include <grpc/support/time.h> |
| 45 | #include <grpc/support/useful.h> |
| 46 | #include "test/core/end2end/cq_verifier.h" |
| 47 | |
| 48 | enum { TIMEOUT = 200000 }; |
| 49 | |
| 50 | static void *tag(gpr_intptr t) { return (void *)t; } |
| 51 | |
| 52 | static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
| 53 | const char *test_name, |
| 54 | grpc_channel_args *client_args, |
| 55 | grpc_channel_args *server_args) { |
| 56 | grpc_end2end_test_fixture f; |
| 57 | gpr_log(GPR_INFO, "%s/%s", test_name, config.name); |
| 58 | f = config.create_fixture(client_args, server_args); |
| 59 | config.init_client(&f, client_args); |
| 60 | config.init_server(&f, server_args); |
| 61 | return f; |
| 62 | } |
| 63 | |
| 64 | static gpr_timespec n_seconds_time(int n) { |
Craig Tiller | 8ad8a41 | 2015-02-25 08:36:40 -0800 | [diff] [blame] | 65 | return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 68 | static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 69 | |
| 70 | static void drain_cq(grpc_completion_queue *cq) { |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 71 | grpc_event ev; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 72 | do { |
| 73 | ev = grpc_completion_queue_next(cq, five_seconds_time()); |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 74 | } while (ev.type != GRPC_QUEUE_SHUTDOWN); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static void shutdown_server(grpc_end2end_test_fixture *f) { |
| 78 | if (!f->server) return; |
Craig Tiller | ba2e755 | 2015-05-28 12:53:54 -0700 | [diff] [blame] | 79 | grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); |
Craig Tiller | 9a57633 | 2015-06-17 10:21:49 -0700 | [diff] [blame] | 80 | GPR_ASSERT(grpc_completion_queue_pluck(f->cq, tag(1000), |
| 81 | GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5)) |
| 82 | .type == GRPC_OP_COMPLETE); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 83 | grpc_server_destroy(f->server); |
| 84 | f->server = NULL; |
| 85 | } |
| 86 | |
| 87 | static void shutdown_client(grpc_end2end_test_fixture *f) { |
| 88 | if (!f->client) return; |
| 89 | grpc_channel_destroy(f->client); |
| 90 | f->client = NULL; |
| 91 | } |
| 92 | |
| 93 | static void end_test(grpc_end2end_test_fixture *f) { |
| 94 | shutdown_server(f); |
| 95 | shutdown_client(f); |
| 96 | |
Craig Tiller | bc0ec33 | 2015-05-11 12:11:32 -0700 | [diff] [blame] | 97 | grpc_completion_queue_shutdown(f->cq); |
| 98 | drain_cq(f->cq); |
| 99 | grpc_completion_queue_destroy(f->cq); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static void simple_request_body(grpc_end2end_test_fixture f) { |
| 103 | grpc_call *c; |
| 104 | grpc_call *s; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 105 | gpr_timespec deadline = five_seconds_time(); |
Craig Tiller | bc0ec33 | 2015-05-11 12:11:32 -0700 | [diff] [blame] | 106 | cq_verifier *cqv = cq_verifier_create(f.cq); |
Craig Tiller | ea61b07 | 2015-02-03 19:19:27 -0800 | [diff] [blame] | 107 | grpc_op ops[6]; |
Craig Tiller | 5a34d45 | 2015-02-03 14:29:22 -0800 | [diff] [blame] | 108 | grpc_op *op; |
Craig Tiller | ea61b07 | 2015-02-03 19:19:27 -0800 | [diff] [blame] | 109 | grpc_metadata_array initial_metadata_recv; |
Craig Tiller | 5a34d45 | 2015-02-03 14:29:22 -0800 | [diff] [blame] | 110 | grpc_metadata_array trailing_metadata_recv; |
| 111 | grpc_metadata_array request_metadata_recv; |
| 112 | grpc_call_details call_details; |
| 113 | grpc_status_code status; |
| 114 | char *details = NULL; |
Craig Tiller | 4f97273 | 2015-02-05 12:40:20 -0800 | [diff] [blame] | 115 | size_t details_capacity = 0; |
Craig Tiller | ea61b07 | 2015-02-03 19:19:27 -0800 | [diff] [blame] | 116 | int was_cancelled = 2; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 117 | |
Craig Tiller | bc0ec33 | 2015-05-11 12:11:32 -0700 | [diff] [blame] | 118 | c = grpc_channel_create_call(f.client, f.cq, "/foo", |
Craig Tiller | deb49dd | 2015-02-25 11:58:49 -0800 | [diff] [blame] | 119 | "foo.test.google.fr:1234", deadline); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 120 | GPR_ASSERT(c); |
| 121 | |
Craig Tiller | ea61b07 | 2015-02-03 19:19:27 -0800 | [diff] [blame] | 122 | grpc_metadata_array_init(&initial_metadata_recv); |
Craig Tiller | 5a34d45 | 2015-02-03 14:29:22 -0800 | [diff] [blame] | 123 | grpc_metadata_array_init(&trailing_metadata_recv); |
| 124 | grpc_metadata_array_init(&request_metadata_recv); |
| 125 | grpc_call_details_init(&call_details); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 126 | |
Craig Tiller | 5a34d45 | 2015-02-03 14:29:22 -0800 | [diff] [blame] | 127 | op = ops; |
| 128 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 129 | op->data.send_initial_metadata.count = 0; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 130 | op->flags = 0; |
Craig Tiller | 5a34d45 | 2015-02-03 14:29:22 -0800 | [diff] [blame] | 131 | op++; |
Craig Tiller | ea61b07 | 2015-02-03 19:19:27 -0800 | [diff] [blame] | 132 | op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 133 | op->flags = 0; |
Craig Tiller | ea61b07 | 2015-02-03 19:19:27 -0800 | [diff] [blame] | 134 | op++; |
| 135 | op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 136 | op->data.recv_initial_metadata = &initial_metadata_recv; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 137 | op->flags = 0; |
Craig Tiller | ea61b07 | 2015-02-03 19:19:27 -0800 | [diff] [blame] | 138 | op++; |
Craig Tiller | 5a34d45 | 2015-02-03 14:29:22 -0800 | [diff] [blame] | 139 | op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
Craig Tiller | ea61b07 | 2015-02-03 19:19:27 -0800 | [diff] [blame] | 140 | op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
Craig Tiller | 5a34d45 | 2015-02-03 14:29:22 -0800 | [diff] [blame] | 141 | op->data.recv_status_on_client.status = &status; |
| 142 | op->data.recv_status_on_client.status_details = &details; |
| 143 | op->data.recv_status_on_client.status_details_capacity = &details_capacity; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 144 | op->flags = 0; |
Craig Tiller | 5a34d45 | 2015-02-03 14:29:22 -0800 | [diff] [blame] | 145 | op++; |
Craig Tiller | ea61b07 | 2015-02-03 19:19:27 -0800 | [diff] [blame] | 146 | GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(c, ops, op - ops, tag(1))); |
| 147 | |
Craig Tiller | 9a57633 | 2015-06-17 10:21:49 -0700 | [diff] [blame] | 148 | GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call( |
| 149 | f.server, &s, &call_details, |
| 150 | &request_metadata_recv, f.cq, f.cq, tag(101))); |
Craig Tiller | a0a6b12 | 2015-05-12 14:42:27 -0700 | [diff] [blame] | 151 | cq_expect_completion(cqv, tag(101), 1); |
Craig Tiller | bc0ec33 | 2015-05-11 12:11:32 -0700 | [diff] [blame] | 152 | cq_verify(cqv); |
Craig Tiller | ea61b07 | 2015-02-03 19:19:27 -0800 | [diff] [blame] | 153 | |
| 154 | op = ops; |
| 155 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 156 | op->data.send_initial_metadata.count = 0; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 157 | op->flags = 0; |
Craig Tiller | 5a34d45 | 2015-02-03 14:29:22 -0800 | [diff] [blame] | 158 | op++; |
Craig Tiller | ea61b07 | 2015-02-03 19:19:27 -0800 | [diff] [blame] | 159 | op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
| 160 | op->data.send_status_from_server.trailing_metadata_count = 0; |
| 161 | op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED; |
| 162 | op->data.send_status_from_server.status_details = "xyz"; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 163 | op->flags = 0; |
Craig Tiller | ea61b07 | 2015-02-03 19:19:27 -0800 | [diff] [blame] | 164 | op++; |
| 165 | op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
| 166 | op->data.recv_close_on_server.cancelled = &was_cancelled; |
David Garcia Quintas | 1d5aca5 | 2015-06-14 14:42:04 -0700 | [diff] [blame] | 167 | op->flags = 0; |
Craig Tiller | ea61b07 | 2015-02-03 19:19:27 -0800 | [diff] [blame] | 168 | op++; |
| 169 | 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] | 170 | |
Craig Tiller | a0a6b12 | 2015-05-12 14:42:27 -0700 | [diff] [blame] | 171 | cq_expect_completion(cqv, tag(102), 1); |
| 172 | cq_expect_completion(cqv, tag(1), 1); |
Craig Tiller | bc0ec33 | 2015-05-11 12:11:32 -0700 | [diff] [blame] | 173 | cq_verify(cqv); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 174 | |
Craig Tiller | ea61b07 | 2015-02-03 19:19:27 -0800 | [diff] [blame] | 175 | GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED); |
| 176 | GPR_ASSERT(0 == strcmp(details, "xyz")); |
| 177 | GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); |
Craig Tiller | deb49dd | 2015-02-25 11:58:49 -0800 | [diff] [blame] | 178 | GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr:1234")); |
Craig Tiller | aea081f | 2015-06-11 14:19:33 -0700 | [diff] [blame] | 179 | GPR_ASSERT(was_cancelled == 1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 180 | |
Craig Tiller | 4f97273 | 2015-02-05 12:40:20 -0800 | [diff] [blame] | 181 | gpr_free(details); |
| 182 | grpc_metadata_array_destroy(&initial_metadata_recv); |
| 183 | grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 184 | grpc_metadata_array_destroy(&request_metadata_recv); |
| 185 | grpc_call_details_destroy(&call_details); |
| 186 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 187 | grpc_call_destroy(c); |
| 188 | grpc_call_destroy(s); |
| 189 | |
Craig Tiller | bc0ec33 | 2015-05-11 12:11:32 -0700 | [diff] [blame] | 190 | cq_verifier_destroy(cqv); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Craig Tiller | ea61b07 | 2015-02-03 19:19:27 -0800 | [diff] [blame] | 193 | static void test_invoke_simple_request(grpc_end2end_test_config config) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 194 | grpc_end2end_test_fixture f; |
| 195 | |
Craig Tiller | 3569619 | 2015-05-24 15:00:37 -0700 | [diff] [blame] | 196 | f = begin_test(config, "test_invoke_simple_request", NULL, NULL); |
Craig Tiller | ea61b07 | 2015-02-03 19:19:27 -0800 | [diff] [blame] | 197 | simple_request_body(f); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 198 | end_test(&f); |
| 199 | config.tear_down_data(&f); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static void test_invoke_10_simple_requests(grpc_end2end_test_config config) { |
| 203 | int i; |
Craig Tiller | 9a57633 | 2015-06-17 10:21:49 -0700 | [diff] [blame] | 204 | grpc_end2end_test_fixture f = |
| 205 | begin_test(config, "test_invoke_10_simple_requests", NULL, NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 206 | for (i = 0; i < 10; i++) { |
| 207 | simple_request_body(f); |
| 208 | gpr_log(GPR_INFO, "Passed simple request %d", i); |
| 209 | } |
| 210 | end_test(&f); |
| 211 | config.tear_down_data(&f); |
| 212 | } |
| 213 | |
| 214 | void grpc_end2end_tests(grpc_end2end_test_config config) { |
Craig Tiller | e398fa2 | 2015-06-02 12:51:53 -0700 | [diff] [blame] | 215 | int i; |
| 216 | for (i = 0; i < 10; i++) { |
| 217 | test_invoke_simple_request(config); |
| 218 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 219 | test_invoke_10_simple_requests(config); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 220 | } |