Debian Codegraph @

test/core/surface/concurrent_connectivity_test.cc

raw
1 /*
2 *
3 * Copyright 2016 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 <string.h>
20
21 #include <algorithm>
22 #include <atomic>
23 #include <string>
24 #include <vector>
25
26 #include "absl/strings/str_cat.h"
27 #include "gtest/gtest.h"
28
29 #include <grpc/grpc.h>
30 #include <grpc/grpc_security.h>
31 #include <grpc/support/alloc.h>
32 #include <grpc/support/log.h>
33 #include <grpc/support/sync.h>
34 #include <grpc/support/time.h>
35
36 #include "src/core/lib/channel/channel_args_preconditioning.h"
37 #include "src/core/lib/config/core_configuration.h"
38 #include "src/core/lib/event_engine/channel_args_endpoint_config.h"
39 #include "src/core/lib/gprpp/thd.h"
40 #include "src/core/lib/gprpp/time.h"
41 #include "src/core/lib/iomgr/closure.h"
42 #include "src/core/lib/iomgr/endpoint.h"
43 #include "src/core/lib/iomgr/error.h"
44 #include "src/core/lib/iomgr/exec_ctx.h"
45 #include "src/core/lib/iomgr/iomgr_fwd.h"
46 #include "src/core/lib/iomgr/pollset.h"
47 #include "src/core/lib/iomgr/resolved_address.h"
48 #include "src/core/lib/iomgr/sockaddr.h"
49 #include "src/core/lib/iomgr/tcp_server.h"
50 #include "test/core/util/port.h"
51 #include "test/core/util/test_config.h"
52
53 /* TODO(yashykt): When our macos testing infrastructure becomes good enough, we
54 * wouldn't need to reduce the number of threads on MacOS */
55 #ifdef __APPLE__
56 #define NUM_THREADS 10
57 #else
58 #define NUM_THREADS 100
59 #endif /* __APPLE */
60
61 #define NUM_OUTER_LOOPS 10
62 #define NUM_INNER_LOOPS 10
63 #define DELAY_MILLIS 10
64 #define POLL_MILLIS 15000
65
66 #define NUM_OUTER_LOOPS_SHORT_TIMEOUTS 10
67 #define NUM_INNER_LOOPS_SHORT_TIMEOUTS 100
68 #define DELAY_MILLIS_SHORT_TIMEOUTS 1
69 // in a successful test run, POLL_MILLIS should never be reached because all
70 // runs should end after the shorter delay_millis
71 #define POLL_MILLIS_SHORT_TIMEOUTS 30000
72 // it should never take longer that this to shutdown the server
73 #define SERVER_SHUTDOWN_TIMEOUT 30000
74
75 static void* tag(int n) { return reinterpret_cast<void*>(n); }
76
77 void create_loop_destroy(void* addr) {
78 for (int i = 0; i < NUM_OUTER_LOOPS; ++i) {
79 grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
80 grpc_channel_credentials* creds = grpc_insecure_credentials_create();
81 grpc_channel* chan =
82 grpc_channel_create(static_cast<char*>(addr), creds, nullptr);
83 grpc_channel_credentials_release(creds);
84
85 for (int j = 0; j < NUM_INNER_LOOPS; ++j) {
86 gpr_timespec later_time =
87 grpc_timeout_milliseconds_to_deadline(DELAY_MILLIS);
88 grpc_connectivity_state state =
89 grpc_channel_check_connectivity_state(chan, 1);
90 grpc_channel_watch_connectivity_state(chan, state, later_time, cq,
91 nullptr);
92 gpr_timespec poll_time =
93 grpc_timeout_milliseconds_to_deadline(POLL_MILLIS);
94 ASSERT_EQ(grpc_completion_queue_next(cq, poll_time, nullptr).type,
95 GRPC_OP_COMPLETE);
96 /* check that the watcher from "watch state" was free'd */
97 ASSERT_EQ(grpc_channel_num_external_connectivity_watchers(chan), 0);
98 }
99 grpc_channel_destroy(chan);
100 grpc_completion_queue_destroy(cq);
101 }
102 }
103
104 // Always stack-allocate or new ServerThreadArgs; never use gpr_malloc since
105 // this contains C++ objects.
106 struct ServerThreadArgs {
107 std::string addr;
108 grpc_server* server = nullptr;
109 grpc_completion_queue* cq = nullptr;
110 std::vector<grpc_pollset*> pollset;
111 gpr_mu* mu = nullptr;
112 gpr_event ready;
113 std::atomic_bool stop{false};
114 };
115
116 void server_thread(void* vargs) {
117 struct ServerThreadArgs* args = static_cast<struct ServerThreadArgs*>(vargs);
118 grpc_event ev;
119 gpr_timespec deadline =
120 grpc_timeout_milliseconds_to_deadline(SERVER_SHUTDOWN_TIMEOUT);
121 ev = grpc_completion_queue_next(args->cq, deadline, nullptr);
122 ASSERT_EQ(ev.type, GRPC_OP_COMPLETE);
123 ASSERT_EQ(ev.tag, tag(0xd1e));
124 }
125
126 static void on_connect(void* vargs, grpc_endpoint* tcp,
127 grpc_pollset* /*accepting_pollset*/,
128 grpc_tcp_server_acceptor* acceptor) {
129 gpr_free(acceptor);
130 struct ServerThreadArgs* args = static_cast<struct ServerThreadArgs*>(vargs);
131 grpc_endpoint_shutdown(tcp, GRPC_ERROR_CREATE("Connected"));
132 grpc_endpoint_destroy(tcp);
133 gpr_mu_lock(args->mu);
134 GRPC_LOG_IF_ERROR("pollset_kick",
135 grpc_pollset_kick(args->pollset[0], nullptr));
136 gpr_mu_unlock(args->mu);
137 }
138
139 void bad_server_thread(void* vargs) {
140 struct ServerThreadArgs* args = static_cast<struct ServerThreadArgs*>(vargs);
141
142 grpc_core::ExecCtx exec_ctx;
143 grpc_resolved_address resolved_addr;
144 grpc_sockaddr* addr = reinterpret_cast<grpc_sockaddr*>(resolved_addr.addr);
145 int port;
146 grpc_tcp_server* s;
147 auto channel_args = grpc_core::CoreConfiguration::Get()
148 .channel_args_preconditioning()
149 .PreconditionChannelArgs(nullptr);
150 grpc_error_handle error = grpc_tcp_server_create(
151 nullptr,
152 grpc_event_engine::experimental::ChannelArgsEndpointConfig(channel_args),
153 &s);
154 ASSERT_TRUE(error.ok());
155 memset(&resolved_addr, 0, sizeof(resolved_addr));
156 addr->sa_family = GRPC_AF_INET;
157 error = grpc_tcp_server_add_port(s, &resolved_addr, &port);
158 ASSERT_TRUE(GRPC_LOG_IF_ERROR("grpc_tcp_server_add_port", error));
159 ASSERT_GT(port, 0);
160 args->addr = absl::StrCat("localhost:", port);
161
162 grpc_tcp_server_start(s, &args->pollset, on_connect, args);
163 gpr_event_set(&args->ready, reinterpret_cast<void*>(1));
164
165 gpr_mu_lock(args->mu);
166 while (!args->stop.load(std::memory_order_acquire)) {
167 grpc_core::Timestamp deadline =
168 grpc_core::Timestamp::Now() + grpc_core::Duration::Milliseconds(100);
169
170 grpc_pollset_worker* worker = nullptr;
171 if (!GRPC_LOG_IF_ERROR(
172 "pollset_work",
173 grpc_pollset_work(args->pollset[0], &worker, deadline))) {
174 args->stop.store(true, std::memory_order_release);
175 }
176 gpr_mu_unlock(args->mu);
177
178 gpr_mu_lock(args->mu);
179 }
180 gpr_mu_unlock(args->mu);
181
182 grpc_tcp_server_unref(s);
183 }
184
185 static void done_pollset_shutdown(void* pollset, grpc_error_handle /*error*/) {
186 grpc_pollset_destroy(static_cast<grpc_pollset*>(pollset));
187 gpr_free(pollset);
188 }
189
190 TEST(ConcurrentConnectivityTest, RunConcurrentConnectivityTest) {
191 struct ServerThreadArgs args;
192
193 /* First round, no server */
194 {
195 gpr_log(GPR_DEBUG, "Wave 1");
196 grpc_core::Thread threads[NUM_THREADS];
197 args.addr = "localhost:54321";
198 for (auto& th : threads) {
199 th = grpc_core::Thread("grpc_wave_1", create_loop_destroy,
200 const_cast<char*>(args.addr.c_str()));
201 th.Start();
202 }
203 for (auto& th : threads) {
204 th.Join();
205 }
206 }
207
208 /* Second round, actual grpc server */
209 {
210 gpr_log(GPR_DEBUG, "Wave 2");
211 int port = grpc_pick_unused_port_or_die();
212 args.addr = absl::StrCat("localhost:", port);
213 args.server = grpc_server_create(nullptr, nullptr);
214 grpc_server_credentials* server_creds =
215 grpc_insecure_server_credentials_create();
216 grpc_server_add_http2_port(args.server, args.addr.c_str(), server_creds);
217 grpc_server_credentials_release(server_creds);
218 args.cq = grpc_completion_queue_create_for_next(nullptr);
219 grpc_server_register_completion_queue(args.server, args.cq, nullptr);
220 grpc_server_start(args.server);
221 grpc_core::Thread server2("grpc_wave_2_server", server_thread, &args);
222 server2.Start();
223
224 grpc_core::Thread threads[NUM_THREADS];
225 for (auto& th : threads) {
226 th = grpc_core::Thread("grpc_wave_2", create_loop_destroy,
227 const_cast<char*>(args.addr.c_str()));
228 th.Start();
229 }
230 for (auto& th : threads) {
231 th.Join();
232 }
233 grpc_server_shutdown_and_notify(args.server, args.cq, tag(0xd1e));
234
235 server2.Join();
236 grpc_server_destroy(args.server);
237 grpc_completion_queue_destroy(args.cq);
238 }
239
240 /* Third round, bogus tcp server */
241 {
242 gpr_log(GPR_DEBUG, "Wave 3");
243 auto* pollset = static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
244 grpc_pollset_init(pollset, &args.mu);
245 args.pollset.push_back(pollset);
246 gpr_event_init(&args.ready);
247 grpc_core::Thread server3("grpc_wave_3_server", bad_server_thread, &args);
248 server3.Start();
249 gpr_event_wait(&args.ready, gpr_inf_future(GPR_CLOCK_MONOTONIC));
250
251 grpc_core::Thread threads[NUM_THREADS];
252 for (auto& th : threads) {
253 th = grpc_core::Thread("grpc_wave_3", create_loop_destroy,
254 const_cast<char*>(args.addr.c_str()));
255 th.Start();
256 }
257 for (auto& th : threads) {
258 th.Join();
259 }
260
261 args.stop.store(true, std::memory_order_release);
262 server3.Join();
263 {
264 grpc_core::ExecCtx exec_ctx;
265 grpc_pollset_shutdown(
266 args.pollset[0],
267 GRPC_CLOSURE_CREATE(done_pollset_shutdown, args.pollset[0],
268 grpc_schedule_on_exec_ctx));
269 }
270 }
271 }
272
273 void watches_with_short_timeouts(void* addr) {
274 for (int i = 0; i < NUM_OUTER_LOOPS_SHORT_TIMEOUTS; ++i) {
275 grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
276 grpc_channel_credentials* creds = grpc_insecure_credentials_create();
277 grpc_channel* chan =
278 grpc_channel_create(static_cast<char*>(addr), creds, nullptr);
279 grpc_channel_credentials_release(creds);
280
281 for (int j = 0; j < NUM_INNER_LOOPS_SHORT_TIMEOUTS; ++j) {
282 gpr_timespec later_time =
283 grpc_timeout_milliseconds_to_deadline(DELAY_MILLIS_SHORT_TIMEOUTS);
284 grpc_connectivity_state state =
285 grpc_channel_check_connectivity_state(chan, 0);
286 ASSERT_EQ(state, GRPC_CHANNEL_IDLE);
287 grpc_channel_watch_connectivity_state(chan, state, later_time, cq,
288 nullptr);
289 gpr_timespec poll_time =
290 grpc_timeout_milliseconds_to_deadline(POLL_MILLIS_SHORT_TIMEOUTS);
291 grpc_event ev = grpc_completion_queue_next(cq, poll_time, nullptr);
292 ASSERT_EQ(ev.type, GRPC_OP_COMPLETE);
293 ASSERT_EQ(ev.success, false);
294 /* check that the watcher from "watch state" was free'd */
295 ASSERT_EQ(grpc_channel_num_external_connectivity_watchers(chan), 0);
296 }
297 grpc_channel_destroy(chan);
298 grpc_completion_queue_destroy(cq);
299 }
300 }
301
302 // This test tries to catch deadlock situations.
303 // With short timeouts on "watches" and long timeouts on cq next calls,
304 // so that a QUEUE_TIMEOUT likely means that something is stuck.
305 TEST(ConcurrentConnectivityTest, RunConcurrentWatchesWithShortTimeoutsTest) {
306 grpc_core::Thread threads[NUM_THREADS];
307 for (auto& th : threads) {
308 th = grpc_core::Thread("grpc_short_watches", watches_with_short_timeouts,
309 const_cast<char*>("localhost:54321"));
310 th.Start();
311 }
312 for (auto& th : threads) {
313 th.Join();
314 }
315 }
316
317 int main(int argc, char** argv) {
318 grpc::testing::TestEnvironment env(&argc, argv);
319 ::testing::InitGoogleTest(&argc, argv);
320 grpc::testing::TestGrpcScope grpc_scope;
321 return RUN_ALL_TESTS();
322 }