| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <inttypes.h> |
| 20 |
#include <stdio.h> |
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#include <grpc/byte_buffer.h> |
| 24 |
#include <grpc/grpc.h> |
| 25 |
#include <grpc/impl/codegen/propagation_bits.h> |
| 26 |
#include <grpc/slice.h> |
| 27 |
#include <grpc/status.h> |
| 28 |
#include <grpc/support/log.h> |
| 29 |
|
| 30 |
#include "src/core/lib/gpr/useful.h" |
| 31 |
#include "test/core/end2end/cq_verifier.h" |
| 32 |
#include "test/core/end2end/end2end_tests.h" |
| 33 |
#include "test/core/end2end/tests/cancel_test_helpers.h" |
| 34 |
#include "test/core/util/test_config.h" |
| 35 |
|
| 36 |
static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); } |
| 37 |
|
| 38 |
static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
| 39 |
const char* test_name, |
| 40 |
cancellation_mode mode, |
| 41 |
size_t test_ops, |
| 42 |
grpc_channel_args* client_args, |
| 43 |
grpc_channel_args* server_args) { |
| 44 |
grpc_end2end_test_fixture f; |
| 45 |
gpr_log(GPR_INFO, "Running test: %s/%s/%s [%" PRIdPTR " ops]", test_name, |
| 46 |
config.name, mode.name, test_ops); |
| 47 |
f = config.create_fixture(client_args, server_args); |
| 48 |
config.init_server(&f, server_args); |
| 49 |
config.init_client(&f, client_args); |
| 50 |
return f; |
| 51 |
} |
| 52 |
|
| 53 |
static gpr_timespec n_seconds_from_now(int n) { |
| 54 |
return grpc_timeout_seconds_to_deadline(n); |
| 55 |
} |
| 56 |
|
| 57 |
static gpr_timespec five_seconds_from_now(void) { |
| 58 |
return n_seconds_from_now(5); |
| 59 |
} |
| 60 |
|
| 61 |
static void drain_cq(grpc_completion_queue* cq) { |
| 62 |
grpc_event ev; |
| 63 |
do { |
| 64 |
ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr); |
| 65 |
} while (ev.type != GRPC_QUEUE_SHUTDOWN); |
| 66 |
} |
| 67 |
|
| 68 |
static void shutdown_server(grpc_end2end_test_fixture* f) { |
| 69 |
if (!f->server) return; |
| 70 |
grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); |
| 71 |
grpc_event ev = grpc_completion_queue_next( |
| 72 |
f->cq, grpc_timeout_seconds_to_deadline(5), nullptr); |
| 73 |
GPR_ASSERT(ev.type == GRPC_OP_COMPLETE); |
| 74 |
GPR_ASSERT(ev.tag == tag(1000)); |
| 75 |
grpc_server_destroy(f->server); |
| 76 |
f->server = nullptr; |
| 77 |
} |
| 78 |
|
| 79 |
static void shutdown_client(grpc_end2end_test_fixture* f) { |
| 80 |
if (!f->client) return; |
| 81 |
grpc_channel_destroy(f->client); |
| 82 |
f->client = nullptr; |
| 83 |
} |
| 84 |
|
| 85 |
static void end_test(grpc_end2end_test_fixture* f) { |
| 86 |
shutdown_server(f); |
| 87 |
shutdown_client(f); |
| 88 |
|
| 89 |
grpc_completion_queue_shutdown(f->cq); |
| 90 |
drain_cq(f->cq); |
| 91 |
grpc_completion_queue_destroy(f->cq); |
| 92 |
} |
| 93 |
|
| 94 |
/* Cancel after invoke, no payload */ |
| 95 |
static void test_cancel_after_invoke(grpc_end2end_test_config config, |
| 96 |
cancellation_mode mode, size_t test_ops) { |
| 97 |
grpc_op ops[6]; |
| 98 |
grpc_op* op; |
| 99 |
grpc_call* c; |
| 100 |
grpc_end2end_test_fixture f = begin_test(config, "test_cancel_after_invoke", |
| 101 |
mode, test_ops, nullptr, nullptr); |
| 102 |
grpc_core::CqVerifier cqv(f.cq); |
| 103 |
grpc_metadata_array initial_metadata_recv; |
| 104 |
grpc_metadata_array trailing_metadata_recv; |
| 105 |
grpc_metadata_array request_metadata_recv; |
| 106 |
grpc_call_details call_details; |
| 107 |
grpc_status_code status; |
| 108 |
grpc_call_error error; |
| 109 |
grpc_slice details; |
| 110 |
grpc_byte_buffer* response_payload_recv = nullptr; |
| 111 |
grpc_slice request_payload_slice = |
| 112 |
grpc_slice_from_copied_string("hello world"); |
| 113 |
grpc_byte_buffer* request_payload = |
| 114 |
grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
| 115 |
|
| 116 |
gpr_timespec deadline = five_seconds_from_now(); |
| 117 |
c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
| 118 |
grpc_slice_from_static_string("/foo"), nullptr, |
| 119 |
deadline, nullptr); |
| 120 |
GPR_ASSERT(c); |
| 121 |
|
| 122 |
grpc_metadata_array_init(&initial_metadata_recv); |
| 123 |
grpc_metadata_array_init(&trailing_metadata_recv); |
| 124 |
grpc_metadata_array_init(&request_metadata_recv); |
| 125 |
grpc_call_details_init(&call_details); |
| 126 |
|
| 127 |
memset(ops, 0, sizeof(ops)); |
| 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; |
| 133 |
op->flags = 0; |
| 134 |
op->reserved = nullptr; |
| 135 |
op++; |
| 136 |
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 137 |
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
| 138 |
op->flags = 0; |
| 139 |
op->reserved = nullptr; |
| 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 = nullptr; |
| 145 |
op++; |
| 146 |
op->op = GRPC_OP_SEND_MESSAGE; |
| 147 |
op->data.send_message.send_message = request_payload; |
| 148 |
op->flags = 0; |
| 149 |
op->reserved = nullptr; |
| 150 |
op++; |
| 151 |
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 152 |
op->flags = 0; |
| 153 |
op->reserved = nullptr; |
| 154 |
op++; |
| 155 |
op->op = GRPC_OP_RECV_MESSAGE; |
| 156 |
op->data.recv_message.recv_message = &response_payload_recv; |
| 157 |
op->flags = 0; |
| 158 |
op->reserved = nullptr; |
| 159 |
op++; |
| 160 |
error = grpc_call_start_batch(c, ops, test_ops, tag(1), nullptr); |
| 161 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 162 |
|
| 163 |
GPR_ASSERT(GRPC_CALL_OK == mode.initiate_cancel(c, nullptr)); |
| 164 |
|
| 165 |
cqv.Expect(tag(1), true); |
| 166 |
cqv.Verify(); |
| 167 |
|
| 168 |
GPR_ASSERT(status == mode.expect_status || status == GRPC_STATUS_INTERNAL); |
| 169 |
|
| 170 |
grpc_metadata_array_destroy(&initial_metadata_recv); |
| 171 |
grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 172 |
grpc_metadata_array_destroy(&request_metadata_recv); |
| 173 |
grpc_call_details_destroy(&call_details); |
| 174 |
|
| 175 |
grpc_byte_buffer_destroy(request_payload); |
| 176 |
grpc_byte_buffer_destroy(response_payload_recv); |
| 177 |
grpc_slice_unref(details); |
| 178 |
|
| 179 |
grpc_call_unref(c); |
| 180 |
|
| 181 |
end_test(&f); |
| 182 |
config.tear_down_data(&f); |
| 183 |
} |
| 184 |
|
| 185 |
void cancel_after_invoke(grpc_end2end_test_config config) { |
| 186 |
unsigned i, j; |
| 187 |
|
| 188 |
for (j = 3; j < 6; j++) { |
| 189 |
for (i = 0; i < GPR_ARRAY_SIZE(cancellation_modes); i++) { |
| 190 |
if (config.feature_mask & FEATURE_MASK_DOES_NOT_SUPPORT_DEADLINES && |
| 191 |
cancellation_modes[i].expect_status == |
| 192 |
GRPC_STATUS_DEADLINE_EXCEEDED) { |
| 193 |
continue; |
| 194 |
} |
| 195 |
test_cancel_after_invoke(config, cancellation_modes[i], j); |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
void cancel_after_invoke_pre_init(void) {} |