Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2014, 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 | #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) { |
| 64 | return gpr_time_add(gpr_now(), gpr_time_from_micros(GPR_US_PER_SEC * n)); |
| 65 | } |
| 66 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 67 | static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 68 | |
| 69 | static void drain_cq(grpc_completion_queue *cq) { |
| 70 | grpc_event *ev; |
| 71 | grpc_completion_type type; |
| 72 | do { |
| 73 | ev = grpc_completion_queue_next(cq, five_seconds_time()); |
| 74 | GPR_ASSERT(ev); |
| 75 | type = ev->type; |
| 76 | grpc_event_finish(ev); |
| 77 | } while (type != GRPC_QUEUE_SHUTDOWN); |
| 78 | } |
| 79 | |
| 80 | static void shutdown_server(grpc_end2end_test_fixture *f) { |
| 81 | if (!f->server) return; |
| 82 | grpc_server_shutdown(f->server); |
| 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->server_cq); |
| 98 | drain_cq(f->server_cq); |
| 99 | grpc_completion_queue_destroy(f->server_cq); |
| 100 | grpc_completion_queue_shutdown(f->client_cq); |
| 101 | drain_cq(f->client_cq); |
| 102 | grpc_completion_queue_destroy(f->client_cq); |
| 103 | } |
| 104 | |
| 105 | /* Client sends a request with payload, server reads then returns status. */ |
| 106 | static void test_invoke_request_with_payload(grpc_end2end_test_config config) { |
| 107 | grpc_call *c; |
| 108 | grpc_call *s; |
Craig Tiller | cff2d15 | 2015-02-05 13:22:49 -0800 | [diff] [blame] | 109 | gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world"); |
| 110 | grpc_byte_buffer *request_payload = |
| 111 | grpc_byte_buffer_create(&request_payload_slice, 1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 112 | gpr_timespec deadline = five_seconds_time(); |
| 113 | grpc_end2end_test_fixture f = begin_test(config, __FUNCTION__, NULL, NULL); |
| 114 | cq_verifier *v_client = cq_verifier_create(f.client_cq); |
| 115 | cq_verifier *v_server = cq_verifier_create(f.server_cq); |
Craig Tiller | cff2d15 | 2015-02-05 13:22:49 -0800 | [diff] [blame] | 116 | grpc_op ops[6]; |
| 117 | grpc_op *op; |
| 118 | grpc_metadata_array initial_metadata_recv; |
| 119 | grpc_metadata_array trailing_metadata_recv; |
| 120 | grpc_metadata_array request_metadata_recv; |
| 121 | grpc_byte_buffer *request_payload_recv = NULL; |
| 122 | grpc_call_details call_details; |
| 123 | grpc_status_code status; |
| 124 | char *details = NULL; |
| 125 | size_t details_capacity = 0; |
| 126 | int was_cancelled = 2; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 127 | |
Craig Tiller | cff2d15 | 2015-02-05 13:22:49 -0800 | [diff] [blame] | 128 | c = grpc_channel_create_call(f.client, f.client_cq, "/foo", "test.google.com", |
| 129 | deadline); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 130 | GPR_ASSERT(c); |
| 131 | |
Craig Tiller | cff2d15 | 2015-02-05 13:22:49 -0800 | [diff] [blame] | 132 | grpc_metadata_array_init(&initial_metadata_recv); |
| 133 | grpc_metadata_array_init(&trailing_metadata_recv); |
| 134 | grpc_metadata_array_init(&request_metadata_recv); |
| 135 | grpc_call_details_init(&call_details); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 136 | |
Craig Tiller | cff2d15 | 2015-02-05 13:22:49 -0800 | [diff] [blame] | 137 | op = ops; |
| 138 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 139 | op->data.send_initial_metadata.count = 0; |
| 140 | op++; |
| 141 | op->op = GRPC_OP_SEND_MESSAGE; |
| 142 | op->data.send_message = request_payload; |
| 143 | op++; |
| 144 | op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 145 | op++; |
| 146 | op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 147 | op->data.recv_initial_metadata = &initial_metadata_recv; |
| 148 | op++; |
| 149 | op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 150 | op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 151 | op->data.recv_status_on_client.status = &status; |
| 152 | op->data.recv_status_on_client.status_details = &details; |
| 153 | op->data.recv_status_on_client.status_details_capacity = &details_capacity; |
| 154 | op++; |
| 155 | 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] | 156 | |
Craig Tiller | cff2d15 | 2015-02-05 13:22:49 -0800 | [diff] [blame] | 157 | GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(f.server, &s, |
| 158 | &call_details, |
| 159 | &request_metadata_recv, |
| 160 | f.server_cq, tag(101))); |
| 161 | cq_expect_completion(v_server, tag(101), GRPC_OP_OK); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 162 | cq_verify(v_server); |
| 163 | |
Craig Tiller | cff2d15 | 2015-02-05 13:22:49 -0800 | [diff] [blame] | 164 | op = ops; |
| 165 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 166 | op->data.send_initial_metadata.count = 0; |
| 167 | op++; |
| 168 | op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
| 169 | op->data.send_status_from_server.trailing_metadata_count = 0; |
| 170 | op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED; |
| 171 | op->data.send_status_from_server.status_details = "xyz"; |
| 172 | op++; |
| 173 | op->op = GRPC_OP_RECV_MESSAGE; |
| 174 | op->data.recv_message = &request_payload_recv; |
| 175 | op++; |
| 176 | op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
| 177 | op->data.recv_close_on_server.cancelled = &was_cancelled; |
| 178 | op++; |
| 179 | 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] | 180 | |
Craig Tiller | cff2d15 | 2015-02-05 13:22:49 -0800 | [diff] [blame] | 181 | cq_expect_completion(v_server, tag(102), GRPC_OP_OK); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 182 | cq_verify(v_server); |
| 183 | |
Craig Tiller | cff2d15 | 2015-02-05 13:22:49 -0800 | [diff] [blame] | 184 | cq_expect_completion(v_client, tag(1), GRPC_OP_OK); |
| 185 | cq_verify(v_client); |
| 186 | |
| 187 | GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED); |
| 188 | GPR_ASSERT(0 == strcmp(details, "xyz")); |
| 189 | GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); |
| 190 | GPR_ASSERT(0 == strcmp(call_details.host, "test.google.com")); |
Craig Tiller | e801eb3 | 2015-02-10 14:01:14 -0800 | [diff] [blame^] | 191 | GPR_ASSERT(was_cancelled == 0); |
Craig Tiller | cff2d15 | 2015-02-05 13:22:49 -0800 | [diff] [blame] | 192 | GPR_ASSERT(byte_buffer_eq_string(request_payload_recv, "hello world")); |
| 193 | |
| 194 | gpr_free(details); |
| 195 | grpc_metadata_array_destroy(&initial_metadata_recv); |
| 196 | grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 197 | grpc_metadata_array_destroy(&request_metadata_recv); |
| 198 | grpc_call_details_destroy(&call_details); |
| 199 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 200 | grpc_call_destroy(c); |
| 201 | grpc_call_destroy(s); |
| 202 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 203 | cq_verifier_destroy(v_client); |
| 204 | cq_verifier_destroy(v_server); |
Craig Tiller | cff2d15 | 2015-02-05 13:22:49 -0800 | [diff] [blame] | 205 | |
| 206 | grpc_byte_buffer_destroy(request_payload); |
| 207 | grpc_byte_buffer_destroy(request_payload_recv); |
| 208 | |
| 209 | end_test(&f); |
| 210 | config.tear_down_data(&f); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | void grpc_end2end_tests(grpc_end2end_test_config config) { |
| 214 | test_invoke_request_with_payload(config); |
| 215 | } |