| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2017 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 |
|
| 21 |
#include <functional> |
| 22 |
#include <memory> |
| 23 |
#include <string> |
| 24 |
#include <thread> |
| 25 |
#include <vector> |
| 26 |
|
| 27 |
#include "absl/status/status.h" |
| 28 |
#include "absl/status/statusor.h" |
| 29 |
#include "absl/strings/str_cat.h" |
| 30 |
#include "gtest/gtest.h" |
| 31 |
|
| 32 |
#include <grpc/grpc.h> |
| 33 |
#include <grpc/grpc_security.h> |
| 34 |
#include <grpc/slice.h> |
| 35 |
#include <grpc/slice_buffer.h> |
| 36 |
#include <grpc/support/alloc.h> |
| 37 |
#include <grpc/support/atm.h> |
| 38 |
#include <grpc/support/log.h> |
| 39 |
#include <grpc/support/sync.h> |
| 40 |
#include <grpc/support/time.h> |
| 41 |
|
| 42 |
#include "src/core/lib/channel/channel_args_preconditioning.h" |
| 43 |
#include "src/core/lib/config/core_configuration.h" |
| 44 |
#include "src/core/lib/event_engine/channel_args_endpoint_config.h" |
| 45 |
#include "src/core/lib/gprpp/status_helper.h" |
| 46 |
#include "src/core/lib/gprpp/time.h" |
| 47 |
#include "src/core/lib/iomgr/closure.h" |
| 48 |
#include "src/core/lib/iomgr/endpoint.h" |
| 49 |
#include "src/core/lib/iomgr/error.h" |
| 50 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 51 |
#include "src/core/lib/iomgr/iomgr_fwd.h" |
| 52 |
#include "src/core/lib/iomgr/pollset.h" |
| 53 |
#include "src/core/lib/iomgr/pollset_set.h" |
| 54 |
#include "src/core/lib/iomgr/resolve_address.h" |
| 55 |
#include "src/core/lib/iomgr/resolved_address.h" |
| 56 |
#include "src/core/lib/iomgr/tcp_client.h" |
| 57 |
#include "src/core/lib/resource_quota/api.h" |
| 58 |
#include "test/core/util/port.h" |
| 59 |
#include "test/core/util/test_config.h" |
| 60 |
|
| 61 |
namespace grpc_core { |
| 62 |
namespace test { |
| 63 |
namespace { |
| 64 |
|
| 65 |
// A gRPC server, running in its own thread. |
| 66 |
class ServerThread { |
| 67 |
public: |
| 68 |
explicit ServerThread(const char* address) : address_(address) {} |
| 69 |
|
| 70 |
void Start() { |
| 71 |
// Start server with 1-second handshake timeout. |
| 72 |
grpc_arg a[2]; |
| 73 |
a[0].type = GRPC_ARG_INTEGER; |
| 74 |
a[0].key = const_cast<char*>(GRPC_ARG_SERVER_HANDSHAKE_TIMEOUT_MS); |
| 75 |
a[0].value.integer = 1000; |
| 76 |
a[1].key = const_cast<char*>(GRPC_ARG_RESOURCE_QUOTA); |
| 77 |
a[1].type = GRPC_ARG_POINTER; |
| 78 |
a[1].value.pointer.p = grpc_resource_quota_create("test"); |
| 79 |
a[1].value.pointer.vtable = grpc_resource_quota_arg_vtable(); |
| 80 |
grpc_channel_args args = {2, a}; |
| 81 |
server_ = grpc_server_create(&args, nullptr); |
| 82 |
grpc_server_credentials* server_creds = |
| 83 |
grpc_insecure_server_credentials_create(); |
| 84 |
ASSERT_TRUE(grpc_server_add_http2_port(server_, address_, server_creds)); |
| 85 |
grpc_server_credentials_release(server_creds); |
| 86 |
cq_ = grpc_completion_queue_create_for_next(nullptr); |
| 87 |
grpc_server_register_completion_queue(server_, cq_, nullptr); |
| 88 |
grpc_server_start(server_); |
| 89 |
thread_ = |
| 90 |
std::make_unique<std::thread>(std::bind(&ServerThread::Serve, this)); |
| 91 |
grpc_resource_quota_unref( |
| 92 |
static_cast<grpc_resource_quota*>(a[1].value.pointer.p)); |
| 93 |
} |
| 94 |
|
| 95 |
void Shutdown() { |
| 96 |
grpc_completion_queue* shutdown_cq = |
| 97 |
grpc_completion_queue_create_for_pluck(nullptr); |
| 98 |
grpc_server_shutdown_and_notify(server_, shutdown_cq, nullptr); |
| 99 |
GPR_ASSERT(grpc_completion_queue_pluck(shutdown_cq, nullptr, |
| 100 |
grpc_timeout_seconds_to_deadline(1), |
| 101 |
nullptr) |
| 102 |
.type == GRPC_OP_COMPLETE); |
| 103 |
grpc_completion_queue_destroy(shutdown_cq); |
| 104 |
grpc_server_destroy(server_); |
| 105 |
grpc_completion_queue_destroy(cq_); |
| 106 |
thread_->join(); |
| 107 |
} |
| 108 |
|
| 109 |
private: |
| 110 |
void Serve() { |
| 111 |
// The completion queue should not return anything other than shutdown. |
| 112 |
grpc_event ev = grpc_completion_queue_next( |
| 113 |
cq_, gpr_inf_future(GPR_CLOCK_MONOTONIC), nullptr); |
| 114 |
ASSERT_EQ(GRPC_QUEUE_SHUTDOWN, ev.type); |
| 115 |
} |
| 116 |
|
| 117 |
const char* address_; // Do not own. |
| 118 |
grpc_server* server_ = nullptr; |
| 119 |
grpc_completion_queue* cq_ = nullptr; |
| 120 |
std::unique_ptr<std::thread> thread_; |
| 121 |
}; |
| 122 |
|
| 123 |
// A TCP client that connects to the server, reads data until the server |
| 124 |
// closes, and then terminates. |
| 125 |
class Client { |
| 126 |
public: |
| 127 |
explicit Client(const char* server_address) |
| 128 |
: server_address_(server_address) {} |
| 129 |
|
| 130 |
void Connect() { |
| 131 |
ExecCtx exec_ctx; |
| 132 |
absl::StatusOr<std::vector<grpc_resolved_address>> addresses_or = |
| 133 |
GetDNSResolver()->LookupHostnameBlocking(server_address_, "80"); |
| 134 |
ASSERT_EQ(absl::OkStatus(), addresses_or.status()) |
| 135 |
<< addresses_or.status().ToString(); |
| 136 |
ASSERT_GE(addresses_or->size(), 1UL); |
| 137 |
pollset_ = static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size())); |
| 138 |
grpc_pollset_init(pollset_, &mu_); |
| 139 |
grpc_pollset_set* pollset_set = grpc_pollset_set_create(); |
| 140 |
grpc_pollset_set_add_pollset(pollset_set, pollset_); |
| 141 |
EventState state; |
| 142 |
auto args = CoreConfiguration::Get() |
| 143 |
.channel_args_preconditioning() |
| 144 |
.PreconditionChannelArgs(nullptr); |
| 145 |
grpc_tcp_client_connect( |
| 146 |
state.closure(), &endpoint_, pollset_set, |
| 147 |
grpc_event_engine::experimental::ChannelArgsEndpointConfig(args), |
| 148 |
addresses_or->data(), Timestamp::Now() + Duration::Seconds(1)); |
| 149 |
ASSERT_TRUE(PollUntilDone(&state, Timestamp::InfFuture())); |
| 150 |
ASSERT_EQ(absl::OkStatus(), state.error()); |
| 151 |
grpc_pollset_set_destroy(pollset_set); |
| 152 |
grpc_endpoint_add_to_pollset(endpoint_, pollset_); |
| 153 |
} |
| 154 |
|
| 155 |
// Reads until an error is returned. |
| 156 |
// Returns true if an error was encountered before the deadline. |
| 157 |
bool ReadUntilError() { |
| 158 |
ExecCtx exec_ctx; |
| 159 |
grpc_slice_buffer read_buffer; |
| 160 |
grpc_slice_buffer_init(&read_buffer); |
| 161 |
bool retval = true; |
| 162 |
// Use a deadline of 3 seconds, which is a lot more than we should |
| 163 |
// need for a 1-second timeout, but this helps avoid flakes. |
| 164 |
Timestamp deadline = Timestamp::Now() + Duration::Seconds(3); |
| 165 |
while (true) { |
| 166 |
EventState state; |
| 167 |
grpc_endpoint_read(endpoint_, &read_buffer, state.closure(), |
| 168 |
/*urgent=*/true, /*min_progress_size=*/1); |
| 169 |
if (!PollUntilDone(&state, deadline)) { |
| 170 |
retval = false; |
| 171 |
break; |
| 172 |
} |
| 173 |
if (state.error() != absl::OkStatus()) break; |
| 174 |
gpr_log(GPR_INFO, "client read %" PRIuPTR " bytes", read_buffer.length); |
| 175 |
grpc_slice_buffer_reset_and_unref(&read_buffer); |
| 176 |
} |
| 177 |
grpc_endpoint_shutdown(endpoint_, GRPC_ERROR_CREATE("shutdown")); |
| 178 |
grpc_slice_buffer_destroy(&read_buffer); |
| 179 |
return retval; |
| 180 |
} |
| 181 |
|
| 182 |
void Shutdown() { |
| 183 |
ExecCtx exec_ctx; |
| 184 |
grpc_endpoint_destroy(endpoint_); |
| 185 |
grpc_pollset_shutdown(pollset_, |
| 186 |
GRPC_CLOSURE_CREATE(&Client::PollsetDestroy, pollset_, |
| 187 |
grpc_schedule_on_exec_ctx)); |
| 188 |
} |
| 189 |
|
| 190 |
private: |
| 191 |
// State used to wait for an I/O event. |
| 192 |
class EventState { |
| 193 |
public: |
| 194 |
EventState() { |
| 195 |
GRPC_CLOSURE_INIT(&closure_, &EventState::OnEventDone, this, |
| 196 |
grpc_schedule_on_exec_ctx); |
| 197 |
} |
| 198 |
|
| 199 |
~EventState() {} |
| 200 |
|
| 201 |
grpc_closure* closure() { return &closure_; } |
| 202 |
|
| 203 |
bool done() const { return gpr_atm_acq_load(&done_atm_) != 0; } |
| 204 |
|
| 205 |
// Caller does NOT take ownership of the error. |
| 206 |
grpc_error_handle error() const { return error_; } |
| 207 |
|
| 208 |
private: |
| 209 |
static void OnEventDone(void* arg, grpc_error_handle error) { |
| 210 |
gpr_log(GPR_INFO, "OnEventDone(): %s", StatusToString(error).c_str()); |
| 211 |
EventState* state = static_cast<EventState*>(arg); |
| 212 |
state->error_ = error; |
| 213 |
gpr_atm_rel_store(&state->done_atm_, 1); |
| 214 |
} |
| 215 |
|
| 216 |
grpc_closure closure_; |
| 217 |
gpr_atm done_atm_ = 0; |
| 218 |
grpc_error_handle error_; |
| 219 |
}; |
| 220 |
|
| 221 |
// Returns true if done, or false if deadline exceeded. |
| 222 |
bool PollUntilDone(EventState* state, Timestamp deadline) { |
| 223 |
while (true) { |
| 224 |
grpc_pollset_worker* worker = nullptr; |
| 225 |
gpr_mu_lock(mu_); |
| 226 |
GRPC_LOG_IF_ERROR( |
| 227 |
"grpc_pollset_work", |
| 228 |
grpc_pollset_work(pollset_, &worker, |
| 229 |
Timestamp::Now() + Duration::Milliseconds(100))); |
| 230 |
// Flushes any work scheduled before or during polling. |
| 231 |
ExecCtx::Get()->Flush(); |
| 232 |
gpr_mu_unlock(mu_); |
| 233 |
if (state != nullptr && state->done()) return true; |
| 234 |
if (Timestamp::Now() >= deadline) return false; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
static void PollsetDestroy(void* arg, grpc_error_handle /*error*/) { |
| 239 |
grpc_pollset* pollset = static_cast<grpc_pollset*>(arg); |
| 240 |
grpc_pollset_destroy(pollset); |
| 241 |
gpr_free(pollset); |
| 242 |
} |
| 243 |
|
| 244 |
const char* server_address_; // Do not own. |
| 245 |
grpc_endpoint* endpoint_; |
| 246 |
gpr_mu* mu_; |
| 247 |
grpc_pollset* pollset_; |
| 248 |
}; |
| 249 |
|
| 250 |
TEST(SettingsTimeout, Basic) { |
| 251 |
// Construct server address string. |
| 252 |
const int server_port = grpc_pick_unused_port_or_die(); |
| 253 |
std::string server_address_string = absl::StrCat("localhost:", server_port); |
| 254 |
// Start server. |
| 255 |
gpr_log(GPR_INFO, "starting server on %s", server_address_string.c_str()); |
| 256 |
ServerThread server_thread(server_address_string.c_str()); |
| 257 |
server_thread.Start(); |
| 258 |
// Create client and connect to server. |
| 259 |
gpr_log(GPR_INFO, "starting client connect"); |
| 260 |
Client client(server_address_string.c_str()); |
| 261 |
client.Connect(); |
| 262 |
// Client read. Should fail due to server dropping connection. |
| 263 |
gpr_log(GPR_INFO, "starting client read"); |
| 264 |
EXPECT_TRUE(client.ReadUntilError()); |
| 265 |
// Shut down client. |
| 266 |
gpr_log(GPR_INFO, "shutting down client"); |
| 267 |
client.Shutdown(); |
| 268 |
// Shut down server. |
| 269 |
gpr_log(GPR_INFO, "shutting down server"); |
| 270 |
server_thread.Shutdown(); |
| 271 |
// Clean up. |
| 272 |
} |
| 273 |
|
| 274 |
} // namespace |
| 275 |
} // namespace test |
| 276 |
} // namespace grpc_core |
| 277 |
|
| 278 |
int main(int argc, char** argv) { |
| 279 |
::testing::InitGoogleTest(&argc, argv); |
| 280 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 281 |
grpc_init(); |
| 282 |
int result = RUN_ALL_TESTS(); |
| 283 |
grpc_shutdown(); |
| 284 |
return result; |
| 285 |
} |