| 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 <stdint.h> |
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#include <grpc/grpc.h> |
| 23 |
#include <grpc/impl/codegen/propagation_bits.h> |
| 24 |
#include <grpc/slice.h> |
| 25 |
#include <grpc/status.h> |
| 26 |
#include <grpc/support/log.h> |
| 27 |
|
| 28 |
#include "test/core/end2end/cq_verifier.h" |
| 29 |
#include "test/core/end2end/end2end_tests.h" |
| 30 |
#include "test/core/util/test_config.h" |
| 31 |
|
| 32 |
static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); } |
| 33 |
|
| 34 |
static gpr_timespec n_seconds_from_now(int n) { |
| 35 |
return grpc_timeout_seconds_to_deadline(n); |
| 36 |
} |
| 37 |
|
| 38 |
static gpr_timespec five_seconds_from_now(void) { |
| 39 |
return n_seconds_from_now(5); |
| 40 |
} |
| 41 |
|
| 42 |
static void drain_cq(grpc_completion_queue* cq) { |
| 43 |
grpc_event ev; |
| 44 |
do { |
| 45 |
ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr); |
| 46 |
} while (ev.type != GRPC_QUEUE_SHUTDOWN); |
| 47 |
} |
| 48 |
|
| 49 |
static void shutdown_server(grpc_end2end_test_fixture* f) { |
| 50 |
if (!f->server) return; |
| 51 |
grpc_server_destroy(f->server); |
| 52 |
f->server = nullptr; |
| 53 |
} |
| 54 |
|
| 55 |
static void shutdown_client(grpc_end2end_test_fixture* f) { |
| 56 |
if (!f->client) return; |
| 57 |
grpc_channel_destroy(f->client); |
| 58 |
f->client = nullptr; |
| 59 |
} |
| 60 |
|
| 61 |
static void end_test(grpc_end2end_test_fixture* f) { |
| 62 |
shutdown_server(f); |
| 63 |
shutdown_client(f); |
| 64 |
|
| 65 |
grpc_completion_queue_shutdown(f->cq); |
| 66 |
drain_cq(f->cq); |
| 67 |
grpc_completion_queue_destroy(f->cq); |
| 68 |
} |
| 69 |
|
| 70 |
static void do_request_and_shutdown_server(grpc_end2end_test_config /*config*/, |
| 71 |
grpc_end2end_test_fixture* f, |
| 72 |
grpc_core::CqVerifier& cqv) { |
| 73 |
grpc_call* c; |
| 74 |
grpc_call* s; |
| 75 |
grpc_op ops[6]; |
| 76 |
grpc_op* op; |
| 77 |
grpc_metadata_array initial_metadata_recv; |
| 78 |
grpc_metadata_array trailing_metadata_recv; |
| 79 |
grpc_metadata_array request_metadata_recv; |
| 80 |
grpc_call_details call_details; |
| 81 |
grpc_status_code status; |
| 82 |
grpc_call_error error; |
| 83 |
grpc_slice details; |
| 84 |
int was_cancelled = 2; |
| 85 |
|
| 86 |
gpr_timespec deadline = five_seconds_from_now(); |
| 87 |
c = grpc_channel_create_call(f->client, nullptr, GRPC_PROPAGATE_DEFAULTS, |
| 88 |
f->cq, grpc_slice_from_static_string("/foo"), |
| 89 |
nullptr, deadline, nullptr); |
| 90 |
GPR_ASSERT(c); |
| 91 |
|
| 92 |
grpc_metadata_array_init(&initial_metadata_recv); |
| 93 |
grpc_metadata_array_init(&trailing_metadata_recv); |
| 94 |
grpc_metadata_array_init(&request_metadata_recv); |
| 95 |
grpc_call_details_init(&call_details); |
| 96 |
|
| 97 |
memset(ops, 0, sizeof(ops)); |
| 98 |
op = ops; |
| 99 |
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 100 |
op->data.send_initial_metadata.count = 0; |
| 101 |
op->flags = 0; |
| 102 |
op->reserved = nullptr; |
| 103 |
op++; |
| 104 |
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 105 |
op->flags = 0; |
| 106 |
op->reserved = nullptr; |
| 107 |
op++; |
| 108 |
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 109 |
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
| 110 |
op->flags = 0; |
| 111 |
op->reserved = nullptr; |
| 112 |
op++; |
| 113 |
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 114 |
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 115 |
op->data.recv_status_on_client.status = &status; |
| 116 |
op->data.recv_status_on_client.status_details = &details; |
| 117 |
op->flags = 0; |
| 118 |
op->reserved = nullptr; |
| 119 |
op++; |
| 120 |
error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1), |
| 121 |
nullptr); |
| 122 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 123 |
|
| 124 |
error = |
| 125 |
grpc_server_request_call(f->server, &s, &call_details, |
| 126 |
&request_metadata_recv, f->cq, f->cq, tag(101)); |
| 127 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 128 |
cqv.Expect(tag(101), true); |
| 129 |
cqv.Verify(); |
| 130 |
|
| 131 |
/* should be able to shut down the server early |
| 132 |
- and still complete the request */ |
| 133 |
grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000)); |
| 134 |
|
| 135 |
memset(ops, 0, sizeof(ops)); |
| 136 |
op = ops; |
| 137 |
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 138 |
op->data.send_initial_metadata.count = 0; |
| 139 |
op->flags = 0; |
| 140 |
op->reserved = nullptr; |
| 141 |
op++; |
| 142 |
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
| 143 |
op->data.send_status_from_server.trailing_metadata_count = 0; |
| 144 |
op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED; |
| 145 |
grpc_slice status_details = grpc_slice_from_static_string("xyz"); |
| 146 |
op->data.send_status_from_server.status_details = &status_details; |
| 147 |
op->flags = 0; |
| 148 |
op->reserved = nullptr; |
| 149 |
op++; |
| 150 |
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
| 151 |
op->data.recv_close_on_server.cancelled = &was_cancelled; |
| 152 |
op->flags = 0; |
| 153 |
op->reserved = nullptr; |
| 154 |
op++; |
| 155 |
error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102), |
| 156 |
nullptr); |
| 157 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 158 |
|
| 159 |
cqv.Expect(tag(102), true); |
| 160 |
cqv.Expect(tag(1), true); |
| 161 |
cqv.Expect(tag(1000), true); |
| 162 |
cqv.Verify(); |
| 163 |
/* Please refer https://github.com/grpc/grpc/issues/21221 for additional |
| 164 |
* details. |
| 165 |
* TODO(yashykt@) - The following line should be removeable after C-Core |
| 166 |
* correctly handles GOAWAY frames. Internal Reference b/135458602. If this |
| 167 |
* test remains flaky even after this, an alternative fix would be to send a |
| 168 |
* request when the server is in the shut down state. |
| 169 |
*/ |
| 170 |
cqv.VerifyEmpty(); |
| 171 |
|
| 172 |
GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED); |
| 173 |
GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz")); |
| 174 |
GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo")); |
| 175 |
GPR_ASSERT(was_cancelled == 0); |
| 176 |
|
| 177 |
grpc_slice_unref(details); |
| 178 |
grpc_metadata_array_destroy(&initial_metadata_recv); |
| 179 |
grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 180 |
grpc_metadata_array_destroy(&request_metadata_recv); |
| 181 |
grpc_call_details_destroy(&call_details); |
| 182 |
|
| 183 |
grpc_call_unref(c); |
| 184 |
grpc_call_unref(s); |
| 185 |
} |
| 186 |
|
| 187 |
static void disappearing_server_test(grpc_end2end_test_config config) { |
| 188 |
grpc_end2end_test_fixture f = config.create_fixture(nullptr, nullptr); |
| 189 |
grpc_core::CqVerifier cqv(f.cq); |
| 190 |
|
| 191 |
gpr_log(GPR_INFO, "Running test: %s/%s", "disappearing_server_test", |
| 192 |
config.name); |
| 193 |
|
| 194 |
config.init_client(&f, nullptr); |
| 195 |
config.init_server(&f, nullptr); |
| 196 |
|
| 197 |
do_request_and_shutdown_server(config, &f, cqv); |
| 198 |
|
| 199 |
/* now destroy and recreate the server */ |
| 200 |
config.init_server(&f, nullptr); |
| 201 |
|
| 202 |
do_request_and_shutdown_server(config, &f, cqv); |
| 203 |
|
| 204 |
end_test(&f); |
| 205 |
config.tear_down_data(&f); |
| 206 |
} |
| 207 |
|
| 208 |
void disappearing_server(grpc_end2end_test_config config) { |
| 209 |
GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION); |
| 210 |
#ifndef GPR_WINDOWS /* b/148110727 for more details */ |
| 211 |
disappearing_server_test(config); |
| 212 |
#endif /* GPR_WINDOWS */ |
| 213 |
} |
| 214 |
|
| 215 |
void disappearing_server_pre_init(void) {} |