Debian Codegraph @

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