Debian Codegraph @

test/core/event_engine/test_suite/client_test.cc

raw
1 // Copyright 2022 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <stdint.h>
16
17 #include <algorithm>
18 #include <chrono>
19 #include <memory>
20 #include <ratio>
21 #include <string>
22 #include <thread>
23 #include <tuple>
24 #include <utility>
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 "absl/time/clock.h"
31 #include "absl/time/time.h"
32 #include "gtest/gtest.h"
33
34 #include <grpc/event_engine/event_engine.h>
35 #include <grpc/event_engine/memory_allocator.h>
36 #include <grpc/grpc.h>
37 #include <grpc/support/log.h>
38
39 #include "src/core/lib/channel/channel_args.h"
40 #include "src/core/lib/event_engine/channel_args_endpoint_config.h"
41 #include "src/core/lib/gprpp/notification.h"
42 #include "src/core/lib/iomgr/exec_ctx.h"
43 #include "src/core/lib/resource_quota/memory_quota.h"
44 #include "src/core/lib/resource_quota/resource_quota.h"
45 #include "test/core/event_engine/test_suite/event_engine_test.h"
46 #include "test/core/event_engine/test_suite/event_engine_test_utils.h"
47 #include "test/core/util/port.h"
48
49 class EventEngineClientTest : public EventEngineTest {};
50
51 using namespace std::chrono_literals;
52
53 namespace {
54
55 using ::grpc_event_engine::experimental::ChannelArgsEndpointConfig;
56 using ::grpc_event_engine::experimental::EventEngine;
57 using ::grpc_event_engine::experimental::URIToResolvedAddress;
58 using Endpoint = ::grpc_event_engine::experimental::EventEngine::Endpoint;
59 using Listener = ::grpc_event_engine::experimental::EventEngine::Listener;
60 using ::grpc_event_engine::experimental::GetNextSendMessage;
61 using ::grpc_event_engine::experimental::WaitForSingleOwner;
62
63 constexpr int kNumExchangedMessages = 100;
64
65 } // namespace
66
67 // Create a connection using the test EventEngine to a non-existent listener
68 // and verify that the connection fails.
69 TEST_F(EventEngineClientTest, ConnectToNonExistentListenerTest) {
70 grpc_core::ExecCtx ctx;
71 std::shared_ptr<EventEngine> test_ee(this->NewEventEngine());
72 grpc_core::Notification signal;
73 auto memory_quota = std::make_unique<grpc_core::MemoryQuota>("bar");
74 std::string target_addr = absl::StrCat(
75 "ipv6:[::1]:", std::to_string(grpc_pick_unused_port_or_die()));
76 // Create a test EventEngine client endpoint and connect to a non existent
77 // listener.
78 ChannelArgsEndpointConfig config;
79 test_ee->Connect(
80 [&signal](absl::StatusOr<std::unique_ptr<Endpoint>> status) {
81 // Connect should fail.
82 ASSERT_FALSE(status.ok());
83 signal.Notify();
84 },
85 URIToResolvedAddress(target_addr), config,
86 memory_quota->CreateMemoryAllocator("conn-1"), 24h);
87 signal.WaitForNotification();
88 WaitForSingleOwner(std::move(test_ee));
89 }
90
91 // Create a connection using the test EventEngine to a listener created
92 // by the oracle EventEngine and exchange bi-di data over the connection.
93 // For each data transfer, verify that data written at one end of the stream
94 // equals data read at the other end of the stream.
95
96 TEST_F(EventEngineClientTest, ConnectExchangeBidiDataTransferTest) {
97 grpc_core::ExecCtx ctx;
98 auto oracle_ee = this->NewOracleEventEngine();
99 std::shared_ptr<EventEngine> test_ee(this->NewEventEngine());
100 auto memory_quota = std::make_unique<grpc_core::MemoryQuota>("bar");
101 std::string target_addr = absl::StrCat(
102 "ipv6:[::1]:", std::to_string(grpc_pick_unused_port_or_die()));
103 std::unique_ptr<EventEngine::Endpoint> client_endpoint;
104 std::unique_ptr<EventEngine::Endpoint> server_endpoint;
105 grpc_core::Notification client_signal;
106 grpc_core::Notification server_signal;
107
108 Listener::AcceptCallback accept_cb =
109 [&server_endpoint, &server_signal](
110 std::unique_ptr<Endpoint> ep,
111 grpc_core::MemoryAllocator /*memory_allocator*/) {
112 server_endpoint = std::move(ep);
113 server_signal.Notify();
114 };
115
116 grpc_core::ChannelArgs args;
117 auto quota = grpc_core::ResourceQuota::Default();
118 args = args.Set(GRPC_ARG_RESOURCE_QUOTA, quota);
119 ChannelArgsEndpointConfig config(args);
120 auto listener = *oracle_ee->CreateListener(
121 std::move(accept_cb),
122 [](absl::Status status) { GPR_ASSERT(status.ok()); }, config,
123 std::make_unique<grpc_core::MemoryQuota>("foo"));
124
125 ASSERT_TRUE(listener->Bind(URIToResolvedAddress(target_addr)).ok());
126 ASSERT_TRUE(listener->Start().ok());
127
128 test_ee->Connect(
129 [&client_endpoint,
130 &client_signal](absl::StatusOr<std::unique_ptr<Endpoint>> endpoint) {
131 ASSERT_TRUE(endpoint.ok());
132 client_endpoint = std::move(*endpoint);
133 client_signal.Notify();
134 },
135 URIToResolvedAddress(target_addr), config,
136 memory_quota->CreateMemoryAllocator("conn-1"), 24h);
137
138 client_signal.WaitForNotification();
139 server_signal.WaitForNotification();
140 ASSERT_NE(client_endpoint.get(), nullptr);
141 ASSERT_NE(server_endpoint.get(), nullptr);
142
143 // Alternate message exchanges between client -- server and server --
144 // client.
145 for (int i = 0; i < kNumExchangedMessages; i++) {
146 // Send from client to server and verify data read at the server.
147 ASSERT_TRUE(SendValidatePayload(GetNextSendMessage(), client_endpoint.get(),
148 server_endpoint.get())
149 .ok());
150
151 // Send from server to client and verify data read at the client.
152 ASSERT_TRUE(SendValidatePayload(GetNextSendMessage(), server_endpoint.get(),
153 client_endpoint.get())
154 .ok());
155 }
156 client_endpoint.reset();
157 server_endpoint.reset();
158 WaitForSingleOwner(std::move(test_ee));
159 }
160
161 // Create 1 listener bound to N IPv6 addresses and M connections where M > N and
162 // exchange and verify random number of messages over each connection.
163 TEST_F(EventEngineClientTest, MultipleIPv6ConnectionsToOneOracleListenerTest) {
164 grpc_core::ExecCtx ctx;
165 static constexpr int kNumListenerAddresses = 10; // N
166 static constexpr int kNumConnections = 10; // M
167 auto oracle_ee = this->NewOracleEventEngine();
168 std::shared_ptr<EventEngine> test_ee(this->NewEventEngine());
169 auto memory_quota = std::make_unique<grpc_core::MemoryQuota>("bar");
170 std::unique_ptr<EventEngine::Endpoint> server_endpoint;
171 // Notifications can only be fired once, so they are newed every loop
172 grpc_core::Notification* server_signal = new grpc_core::Notification();
173 std::vector<std::string> target_addrs;
174 std::vector<std::tuple<std::unique_ptr<Endpoint>, std::unique_ptr<Endpoint>>>
175 connections;
176
177 Listener::AcceptCallback accept_cb =
178 [&server_endpoint, &server_signal](
179 std::unique_ptr<Endpoint> ep,
180 grpc_core::MemoryAllocator /*memory_allocator*/) {
181 server_endpoint = std::move(ep);
182 server_signal->Notify();
183 };
184 grpc_core::ChannelArgs args;
185 auto quota = grpc_core::ResourceQuota::Default();
186 args = args.Set(GRPC_ARG_RESOURCE_QUOTA, quota);
187 ChannelArgsEndpointConfig config(args);
188 auto listener = *oracle_ee->CreateListener(
189 std::move(accept_cb),
190 [](absl::Status status) { GPR_ASSERT(status.ok()); }, config,
191 std::make_unique<grpc_core::MemoryQuota>("foo"));
192
193 target_addrs.reserve(kNumListenerAddresses);
194 for (int i = 0; i < kNumListenerAddresses; i++) {
195 std::string target_addr = absl::StrCat(
196 "ipv6:[::1]:", std::to_string(grpc_pick_unused_port_or_die()));
197 ASSERT_TRUE(listener->Bind(URIToResolvedAddress(target_addr)).ok());
198 target_addrs.push_back(target_addr);
199 }
200 ASSERT_TRUE(listener->Start().ok());
201 absl::SleepFor(absl::Milliseconds(500));
202 for (int i = 0; i < kNumConnections; i++) {
203 std::unique_ptr<EventEngine::Endpoint> client_endpoint;
204 grpc_core::Notification client_signal;
205 // Create a test EventEngine client endpoint and connect to a one of the
206 // addresses bound to the oracle listener. Verify that the connection
207 // succeeds.
208 grpc_core::ChannelArgs client_args;
209 auto client_quota = grpc_core::ResourceQuota::Default();
210 client_args = client_args.Set(GRPC_ARG_RESOURCE_QUOTA, client_quota);
211 ChannelArgsEndpointConfig client_config(client_args);
212 test_ee->Connect(
213 [&client_endpoint,
214 &client_signal](absl::StatusOr<std::unique_ptr<Endpoint>> endpoint) {
215 ASSERT_TRUE(endpoint.ok());
216 client_endpoint = std::move(*endpoint);
217 client_signal.Notify();
218 },
219 URIToResolvedAddress(target_addrs[i % kNumListenerAddresses]),
220 client_config,
221 memory_quota->CreateMemoryAllocator(
222 absl::StrCat("conn-", std::to_string(i))),
223 24h);
224
225 client_signal.WaitForNotification();
226 server_signal->WaitForNotification();
227 ASSERT_NE(client_endpoint.get(), nullptr);
228 ASSERT_NE(server_endpoint.get(), nullptr);
229 connections.push_back(std::make_tuple(std::move(client_endpoint),
230 std::move(server_endpoint)));
231 delete server_signal;
232 server_signal = new grpc_core::Notification();
233 }
234 delete server_signal;
235
236 std::vector<std::thread> threads;
237 // Create one thread for each connection. For each connection, create
238 // 2 more worker threads: to exchange and verify bi-directional data
239 // transfer.
240 threads.reserve(kNumConnections);
241 for (int i = 0; i < kNumConnections; i++) {
242 // For each connection, simulate a parallel bi-directional data transfer.
243 // All bi-directional transfers are run in parallel across all
244 // connections. Each bi-directional data transfer uses a random number of
245 // messages.
246 threads.emplace_back([client_endpoint =
247 std::move(std::get<0>(connections[i])),
248 server_endpoint =
249 std::move(std::get<1>(connections[i]))]() {
250 std::vector<std::thread> workers;
251 workers.reserve(2);
252 auto worker = [client_endpoint = client_endpoint.get(),
253 server_endpoint =
254 server_endpoint.get()](bool client_to_server) {
255 grpc_core::ExecCtx ctx;
256 for (int i = 0; i < kNumExchangedMessages; i++) {
257 // If client_to_server is true, send from client to server and
258 // verify data read at the server. Otherwise send data from server
259 // to client and verify data read at client.
260 if (client_to_server) {
261 ASSERT_TRUE(SendValidatePayload(GetNextSendMessage(),
262 client_endpoint, server_endpoint)
263 .ok());
264 } else {
265 ASSERT_TRUE(SendValidatePayload(GetNextSendMessage(),
266 server_endpoint, client_endpoint)
267 .ok());
268 }
269 }
270 };
271 // worker[0] simulates a flow from client to server endpoint
272 workers.emplace_back([&worker]() { worker(true); });
273 // worker[1] simulates a flow from server to client endpoint
274 workers.emplace_back([&worker]() { worker(false); });
275 workers[0].join();
276 workers[1].join();
277 });
278 }
279 for (auto& t : threads) {
280 t.join();
281 }
282 server_endpoint.reset();
283 WaitForSingleOwner(std::move(test_ee));
284 }
285
286 // TODO(vigneshbabu): Add more tests which create listeners bound to a mix
287 // Ipv6 and other type of addresses (UDS) in the same test.