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 | |
| 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 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 46 | enum { TIMEOUT = 200000 }; |
| 47 | |
| 48 | static void *tag(gpr_intptr 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_client(&f, client_args); |
| 58 | config.init_server(&f, server_args); |
| 59 | return f; |
| 60 | } |
| 61 | |
| 62 | static gpr_timespec n_seconds_time(int n) { |
Craig Tiller | 8ad8a41 | 2015-02-25 08:36:40 -0800 | [diff] [blame] | 63 | return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Craig Tiller | 32946d3 | 2015-01-15 11:37:30 -0800 | [diff] [blame] | 66 | static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 67 | |
| 68 | static void drain_cq(grpc_completion_queue *cq) { |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 69 | grpc_event ev; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 70 | do { |
| 71 | ev = grpc_completion_queue_next(cq, five_seconds_time()); |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 72 | } while (ev.type != GRPC_QUEUE_SHUTDOWN); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static void shutdown_server(grpc_end2end_test_fixture *f) { |
| 76 | if (!f->server) return; |
| 77 | grpc_server_shutdown(f->server); |
| 78 | grpc_server_destroy(f->server); |
| 79 | f->server = NULL; |
| 80 | } |
| 81 | |
| 82 | static void shutdown_client(grpc_end2end_test_fixture *f) { |
| 83 | if (!f->client) return; |
| 84 | grpc_channel_destroy(f->client); |
| 85 | f->client = NULL; |
| 86 | } |
| 87 | |
| 88 | static void end_test(grpc_end2end_test_fixture *f) { |
| 89 | shutdown_server(f); |
| 90 | shutdown_client(f); |
| 91 | |
Craig Tiller | 5cef1b3 | 2015-05-11 11:00:42 -0700 | [diff] [blame] | 92 | grpc_completion_queue_shutdown(f->cq); |
| 93 | drain_cq(f->cq); |
| 94 | grpc_completion_queue_destroy(f->cq); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* Cancel before invoke */ |
Craig Tiller | 8ad8a41 | 2015-02-25 08:36:40 -0800 | [diff] [blame] | 98 | static void test_cancel_before_invoke(grpc_end2end_test_config config, |
| 99 | int test_ops) { |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 100 | grpc_op ops[6]; |
| 101 | grpc_op *op; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 102 | grpc_call *c; |
| 103 | grpc_end2end_test_fixture f = begin_test(config, __FUNCTION__, NULL, NULL); |
| 104 | gpr_timespec deadline = five_seconds_time(); |
Craig Tiller | 5cef1b3 | 2015-05-11 11:00:42 -0700 | [diff] [blame] | 105 | cq_verifier *cqv = cq_verifier_create(f.cq); |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 106 | grpc_metadata_array initial_metadata_recv; |
| 107 | grpc_metadata_array trailing_metadata_recv; |
| 108 | grpc_metadata_array request_metadata_recv; |
| 109 | grpc_call_details call_details; |
| 110 | grpc_status_code status; |
| 111 | char *details = NULL; |
| 112 | size_t details_capacity = 0; |
| 113 | grpc_byte_buffer *response_payload_recv = NULL; |
| 114 | gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world"); |
| 115 | grpc_byte_buffer *request_payload = |
| 116 | grpc_byte_buffer_create(&request_payload_slice, 1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 117 | |
Craig Tiller | 5cef1b3 | 2015-05-11 11:00:42 -0700 | [diff] [blame] | 118 | c = grpc_channel_create_call(f.client, f.cq, "/foo", |
Julien Boeuf | 597a4f2 | 2015-02-23 15:57:14 -0800 | [diff] [blame] | 119 | "foo.test.google.fr", deadline); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 120 | GPR_ASSERT(c); |
| 121 | |
| 122 | GPR_ASSERT(GRPC_CALL_OK == grpc_call_cancel(c)); |
| 123 | |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 124 | grpc_metadata_array_init(&initial_metadata_recv); |
| 125 | grpc_metadata_array_init(&trailing_metadata_recv); |
| 126 | grpc_metadata_array_init(&request_metadata_recv); |
| 127 | grpc_call_details_init(&call_details); |
| 128 | |
| 129 | op = ops; |
| 130 | op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 131 | op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 132 | op->data.recv_status_on_client.status = &status; |
| 133 | op->data.recv_status_on_client.status_details = &details; |
| 134 | op->data.recv_status_on_client.status_details_capacity = &details_capacity; |
| 135 | op++; |
| 136 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 137 | op->data.send_initial_metadata.count = 0; |
| 138 | op++; |
| 139 | op->op = GRPC_OP_SEND_MESSAGE; |
| 140 | op->data.send_message = request_payload; |
| 141 | op++; |
| 142 | op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 143 | op++; |
| 144 | op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 145 | op->data.recv_initial_metadata = &initial_metadata_recv; |
| 146 | op++; |
| 147 | op->op = GRPC_OP_RECV_MESSAGE; |
| 148 | op->data.recv_message = &response_payload_recv; |
| 149 | op++; |
| 150 | GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(c, ops, test_ops, tag(1))); |
| 151 | |
Craig Tiller | a0a6b12 | 2015-05-12 14:42:27 -0700 | [diff] [blame^] | 152 | cq_expect_completion(cqv, tag(1), 1); |
Craig Tiller | 5cef1b3 | 2015-05-11 11:00:42 -0700 | [diff] [blame] | 153 | cq_verify(cqv); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 154 | |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 155 | GPR_ASSERT(status == GRPC_STATUS_CANCELLED); |
| 156 | |
| 157 | grpc_metadata_array_destroy(&initial_metadata_recv); |
| 158 | grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 159 | grpc_metadata_array_destroy(&request_metadata_recv); |
| 160 | grpc_call_details_destroy(&call_details); |
| 161 | |
| 162 | grpc_byte_buffer_destroy(request_payload); |
| 163 | grpc_byte_buffer_destroy(response_payload_recv); |
| 164 | gpr_free(details); |
| 165 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 166 | grpc_call_destroy(c); |
| 167 | |
Craig Tiller | 5cef1b3 | 2015-05-11 11:00:42 -0700 | [diff] [blame] | 168 | cq_verifier_destroy(cqv); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 169 | end_test(&f); |
| 170 | config.tear_down_data(&f); |
| 171 | } |
| 172 | |
| 173 | void grpc_end2end_tests(grpc_end2end_test_config config) { |
Craig Tiller | fee91b4 | 2015-02-05 16:03:13 -0800 | [diff] [blame] | 174 | int i; |
| 175 | for (i = 1; i <= 6; i++) { |
| 176 | test_cancel_before_invoke(config, i); |
| 177 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 178 | } |