Craig Tiller | 32bd81d | 2015-11-03 13:02:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Craig Tiller | 32bd81d | 2015-11-03 13:02:07 -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 | |
Craig Tiller | 32bd81d | 2015-11-03 13:02:07 -0800 | [diff] [blame] | 39 | #include <grpc/byte_buffer.h> |
| 40 | #include <grpc/grpc.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> |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 45 | #include "src/core/lib/support/string.h" |
Craig Tiller | 32bd81d | 2015-11-03 13:02:07 -0800 | [diff] [blame] | 46 | #include "test/core/end2end/cq_verifier.h" |
| 47 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 48 | static void *tag(intptr_t t) { return (void *)t; } |
Craig Tiller | 32bd81d | 2015-11-03 13:02:07 -0800 | [diff] [blame] | 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; |
Robbie Shade | 55a046a | 2017-01-25 15:14:28 -0500 | [diff] [blame] | 55 | gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name); |
Craig Tiller | 32bd81d | 2015-11-03 13:02:07 -0800 | [diff] [blame] | 56 | f = config.create_fixture(client_args, server_args); |
Craig Tiller | 32bd81d | 2015-11-03 13:02:07 -0800 | [diff] [blame] | 57 | config.init_server(&f, server_args); |
Mark D. Roth | e127a39 | 2016-10-27 08:27:15 -0700 | [diff] [blame] | 58 | config.init_client(&f, client_args); |
Craig Tiller | 32bd81d | 2015-11-03 13:02:07 -0800 | [diff] [blame] | 59 | return f; |
| 60 | } |
| 61 | |
Chris Evans | ed2a547 | 2017-03-27 17:34:51 -0500 | [diff] [blame] | 62 | static gpr_timespec n_seconds_from_now(int n) { |
Robbie Shade | ca7effc | 2017-01-17 09:14:29 -0500 | [diff] [blame] | 63 | return grpc_timeout_seconds_to_deadline(n); |
Craig Tiller | 32bd81d | 2015-11-03 13:02:07 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Chris Evans | ed2a547 | 2017-03-27 17:34:51 -0500 | [diff] [blame] | 66 | static gpr_timespec five_seconds_from_now(void) { |
| 67 | return n_seconds_from_now(5); |
| 68 | } |
Craig Tiller | 32bd81d | 2015-11-03 13:02:07 -0800 | [diff] [blame] | 69 | |
| 70 | static void drain_cq(grpc_completion_queue *cq) { |
| 71 | grpc_event ev; |
| 72 | do { |
Chris Evans | ed2a547 | 2017-03-27 17:34:51 -0500 | [diff] [blame] | 73 | ev = grpc_completion_queue_next(cq, five_seconds_from_now(), NULL); |
Craig Tiller | 32bd81d | 2015-11-03 13:02:07 -0800 | [diff] [blame] | 74 | } while (ev.type != GRPC_QUEUE_SHUTDOWN); |
| 75 | } |
| 76 | |
| 77 | static void shutdown_server(grpc_end2end_test_fixture *f) { |
| 78 | if (!f->server) return; |
| 79 | grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 80 | GPR_ASSERT(grpc_completion_queue_pluck( |
Robbie Shade | ca7effc | 2017-01-17 09:14:29 -0500 | [diff] [blame] | 81 | f->cq, tag(1000), grpc_timeout_seconds_to_deadline(5), NULL) |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 82 | .type == GRPC_OP_COMPLETE); |
Craig Tiller | 32bd81d | 2015-11-03 13:02:07 -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 | |
| 97 | grpc_completion_queue_shutdown(f->cq); |
| 98 | drain_cq(f->cq); |
| 99 | grpc_completion_queue_destroy(f->cq); |
| 100 | } |
| 101 | |
Muxi Yan | 5ebd327 | 2016-10-31 07:27:07 -0700 | [diff] [blame] | 102 | static void simple_request_body(grpc_end2end_test_config config, |
| 103 | grpc_end2end_test_fixture f, size_t num_ops) { |
Craig Tiller | 32bd81d | 2015-11-03 13:02:07 -0800 | [diff] [blame] | 104 | grpc_call *c; |
Craig Tiller | 32bd81d | 2015-11-03 13:02:07 -0800 | [diff] [blame] | 105 | cq_verifier *cqv = cq_verifier_create(f.cq); |
| 106 | grpc_op ops[6]; |
| 107 | grpc_op *op; |
| 108 | grpc_metadata_array initial_metadata_recv; |
| 109 | grpc_metadata_array trailing_metadata_recv; |
| 110 | grpc_status_code status; |
| 111 | grpc_call_error error; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 112 | grpc_slice details; |
Craig Tiller | 32bd81d | 2015-11-03 13:02:07 -0800 | [diff] [blame] | 113 | |
Yuchen Zeng | 64c0e8d | 2016-06-10 11:19:51 -0700 | [diff] [blame] | 114 | gpr_log(GPR_DEBUG, "test with %" PRIuPTR " ops", num_ops); |
Craig Tiller | 32bd81d | 2015-11-03 13:02:07 -0800 | [diff] [blame] | 115 | |
Chris Evans | ed2a547 | 2017-03-27 17:34:51 -0500 | [diff] [blame] | 116 | gpr_timespec deadline = five_seconds_from_now(); |
Muxi Yan | 5ebd327 | 2016-10-31 07:27:07 -0700 | [diff] [blame] | 117 | c = grpc_channel_create_call( |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 118 | f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq, |
| 119 | grpc_slice_from_static_string("/foo"), |
| 120 | get_host_override_slice("foo.test.google.fr:1234", config), deadline, |
Muxi Yan | 5ebd327 | 2016-10-31 07:27:07 -0700 | [diff] [blame] | 121 | NULL); |
Craig Tiller | 32bd81d | 2015-11-03 13:02:07 -0800 | [diff] [blame] | 122 | GPR_ASSERT(c); |
| 123 | |
| 124 | grpc_metadata_array_init(&initial_metadata_recv); |
| 125 | grpc_metadata_array_init(&trailing_metadata_recv); |
| 126 | |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 127 | memset(ops, 0, sizeof(ops)); |
Craig Tiller | 32bd81d | 2015-11-03 13:02:07 -0800 | [diff] [blame] | 128 | op = ops; |
| 129 | op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 130 | op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 131 | op->data.recv_status_on_client.status = &status; |
| 132 | op->data.recv_status_on_client.status_details = &details; |
Craig Tiller | 32bd81d | 2015-11-03 13:02:07 -0800 | [diff] [blame] | 133 | op->flags = 0; |
| 134 | op->reserved = NULL; |
| 135 | op++; |
| 136 | op->op = GRPC_OP_RECV_INITIAL_METADATA; |
Mark D. Roth | 435f9f2 | 2017-01-25 12:53:54 -0800 | [diff] [blame] | 137 | op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
Craig Tiller | 32bd81d | 2015-11-03 13:02:07 -0800 | [diff] [blame] | 138 | op->flags = 0; |
| 139 | op->reserved = NULL; |
| 140 | op++; |
| 141 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 142 | op->data.send_initial_metadata.count = 0; |
| 143 | op->flags = 0; |
| 144 | op->reserved = NULL; |
| 145 | op++; |
| 146 | op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 147 | op->flags = 0; |
| 148 | op->reserved = NULL; |
| 149 | op++; |
| 150 | GPR_ASSERT(num_ops <= (size_t)(op - ops)); |
| 151 | error = grpc_call_start_batch(c, ops, num_ops, tag(1), NULL); |
| 152 | GPR_ASSERT(GRPC_CALL_OK == error); |
| 153 | |
| 154 | grpc_call_cancel_with_status(c, GRPC_STATUS_UNIMPLEMENTED, "xyz", NULL); |
| 155 | |
Mark D. Roth | 7187ab9 | 2016-08-24 13:49:22 -0700 | [diff] [blame] | 156 | CQ_EXPECT_COMPLETION(cqv, tag(1), 1); |
Craig Tiller | 32bd81d | 2015-11-03 13:02:07 -0800 | [diff] [blame] | 157 | cq_verify(cqv); |
| 158 | |
| 159 | GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 160 | GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz")); |
Craig Tiller | 32bd81d | 2015-11-03 13:02:07 -0800 | [diff] [blame] | 161 | |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 162 | grpc_slice_unref(details); |
Craig Tiller | 32bd81d | 2015-11-03 13:02:07 -0800 | [diff] [blame] | 163 | grpc_metadata_array_destroy(&initial_metadata_recv); |
| 164 | grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 165 | |
| 166 | grpc_call_destroy(c); |
| 167 | |
| 168 | cq_verifier_destroy(cqv); |
| 169 | } |
| 170 | |
Craig Tiller | b774be4 | 2015-11-19 07:56:13 -0800 | [diff] [blame] | 171 | static void test_invoke_simple_request(grpc_end2end_test_config config, |
| 172 | size_t num_ops) { |
Craig Tiller | 32bd81d | 2015-11-03 13:02:07 -0800 | [diff] [blame] | 173 | grpc_end2end_test_fixture f; |
| 174 | |
| 175 | f = begin_test(config, "test_invoke_simple_request", NULL, NULL); |
Muxi Yan | 56456c3 | 2016-10-30 23:11:11 -0700 | [diff] [blame] | 176 | simple_request_body(config, f, num_ops); |
Craig Tiller | 32bd81d | 2015-11-03 13:02:07 -0800 | [diff] [blame] | 177 | end_test(&f); |
| 178 | config.tear_down_data(&f); |
| 179 | } |
| 180 | |
Craig Tiller | 0fe5ee7 | 2015-12-22 12:50:36 -0800 | [diff] [blame] | 181 | void cancel_with_status(grpc_end2end_test_config config) { |
Craig Tiller | 32bd81d | 2015-11-03 13:02:07 -0800 | [diff] [blame] | 182 | size_t i; |
| 183 | for (i = 1; i <= 4; i++) { |
| 184 | test_invoke_simple_request(config, i); |
| 185 | } |
| 186 | } |
Craig Tiller | 9e9edbc | 2016-04-04 10:38:49 -0700 | [diff] [blame] | 187 | |
| 188 | void cancel_with_status_pre_init(void) {} |