Debian Codegraph @

test/core/xds/xds_transport_fake.cc

raw
1 //
2 // Copyright 2022 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include <grpc/support/port_platform.h>
18
19 #include "test/core/xds/xds_transport_fake.h"
20
21 #include <stdint.h>
22
23 #include <functional>
24 #include <memory>
25 #include <utility>
26
27 #include <grpc/event_engine/event_engine.h>
28 #include <grpc/support/log.h>
29
30 #include "src/core/ext/xds/xds_bootstrap.h"
31 #include "src/core/lib/event_engine/default_event_engine.h"
32 #include "src/core/lib/gprpp/orphanable.h"
33 #include "src/core/lib/gprpp/ref_counted_ptr.h"
34 #include "src/core/lib/iomgr/exec_ctx.h"
35 #include "test/core/util/test_config.h"
36
37 using grpc_event_engine::experimental::GetDefaultEventEngine;
38
39 namespace grpc_core {
40
41 //
42 // FakeXdsTransportFactory::FakeStreamingCall
43 //
44
45 FakeXdsTransportFactory::FakeStreamingCall::~FakeStreamingCall() {
46 // Can't call event_handler_->OnStatusReceived() or unref event_handler_
47 // synchronously, since those operations will trigger code in
48 // XdsClient that acquires its mutex, but it was already holding its
49 // mutex when it called us, so it would deadlock.
50 GetDefaultEventEngine()->Run([event_handler = std::move(event_handler_),
51 status_sent = status_sent_]() mutable {
52 ExecCtx exec_ctx;
53 if (!status_sent) event_handler->OnStatusReceived(absl::OkStatus());
54 event_handler.reset();
55 });
56 }
57
58 void FakeXdsTransportFactory::FakeStreamingCall::Orphan() {
59 transport_->RemoveStream(method_, this);
60 Unref();
61 }
62
63 void FakeXdsTransportFactory::FakeStreamingCall::SendMessage(
64 std::string payload) {
65 MutexLock lock(&mu_);
66 from_client_messages_.push_back(std::move(payload));
67 cv_.Signal();
68 if (transport_->auto_complete_messages_from_client()) {
69 CompleteSendMessageFromClientLocked(/*ok=*/true);
70 }
71 }
72
73 bool FakeXdsTransportFactory::FakeStreamingCall::HaveMessageFromClient() {
74 MutexLock lock(&mu_);
75 return !from_client_messages_.empty();
76 }
77
78 absl::optional<std::string>
79 FakeXdsTransportFactory::FakeStreamingCall::WaitForMessageFromClient(
80 absl::Duration timeout) {
81 MutexLock lock(&mu_);
82 while (from_client_messages_.empty()) {
83 if (cv_.WaitWithTimeout(&mu_, timeout * grpc_test_slowdown_factor())) {
84 return absl::nullopt;
85 }
86 }
87 std::string payload = from_client_messages_.front();
88 from_client_messages_.pop_front();
89 return payload;
90 }
91
92 void FakeXdsTransportFactory::FakeStreamingCall::
93 CompleteSendMessageFromClientLocked(bool ok) {
94 // Can't call event_handler_->OnRequestSent() synchronously, since that
95 // operation will trigger code in XdsClient that acquires its mutex, but it
96 // was already holding its mutex when it called us, so it would deadlock.
97 GetDefaultEventEngine()->Run(
98 [event_handler = event_handler_->Ref(), ok]() mutable {
99 ExecCtx exec_ctx;
100 event_handler->OnRequestSent(ok);
101 event_handler.reset();
102 });
103 }
104
105 void FakeXdsTransportFactory::FakeStreamingCall::CompleteSendMessageFromClient(
106 bool ok) {
107 GPR_ASSERT(!transport_->auto_complete_messages_from_client());
108 MutexLock lock(&mu_);
109 CompleteSendMessageFromClientLocked(ok);
110 }
111
112 void FakeXdsTransportFactory::FakeStreamingCall::SendMessageToClient(
113 absl::string_view payload) {
114 ExecCtx exec_ctx;
115 RefCountedPtr<RefCountedEventHandler> event_handler;
116 {
117 MutexLock lock(&mu_);
118 event_handler = event_handler_->Ref();
119 }
120 event_handler->OnRecvMessage(payload);
121 }
122
123 void FakeXdsTransportFactory::FakeStreamingCall::MaybeSendStatusToClient(
124 absl::Status status) {
125 ExecCtx exec_ctx;
126 RefCountedPtr<RefCountedEventHandler> event_handler;
127 {
128 MutexLock lock(&mu_);
129 if (status_sent_) return;
130 status_sent_ = true;
131 event_handler = event_handler_->Ref();
132 }
133 event_handler->OnStatusReceived(std::move(status));
134 }
135
136 //
137 // FakeXdsTransportFactory::FakeXdsTransport
138 //
139
140 void FakeXdsTransportFactory::FakeXdsTransport::TriggerConnectionFailure(
141 absl::Status status) {
142 RefCountedPtr<RefCountedOnConnectivityFailure> on_connectivity_failure;
143 {
144 MutexLock lock(&mu_);
145 on_connectivity_failure = on_connectivity_failure_->Ref();
146 }
147 ExecCtx exec_ctx;
148 if (on_connectivity_failure != nullptr) {
149 on_connectivity_failure->Run(std::move(status));
150 }
151 }
152
153 void FakeXdsTransportFactory::FakeXdsTransport::Orphan() {
154 {
155 MutexLock lock(&mu_);
156 // Can't destroy on_connectivity_failure_ synchronously, since that
157 // operation will trigger code in XdsClient that acquires its mutex, but
158 // it was already holding its mutex when it called us, so it would deadlock.
159 GetDefaultEventEngine()->Run([on_connectivity_failure = std::move(
160 on_connectivity_failure_)]() mutable {
161 ExecCtx exec_ctx;
162 on_connectivity_failure.reset();
163 });
164 }
165 Unref();
166 }
167
168 RefCountedPtr<FakeXdsTransportFactory::FakeStreamingCall>
169 FakeXdsTransportFactory::FakeXdsTransport::WaitForStream(
170 const char* method, absl::Duration timeout) {
171 MutexLock lock(&mu_);
172 auto it = active_calls_.find(method);
173 while (it == active_calls_.end() || it->second == nullptr) {
174 if (cv_.WaitWithTimeout(&mu_, timeout * grpc_test_slowdown_factor())) {
175 return nullptr;
176 }
177 it = active_calls_.find(method);
178 }
179 return it->second;
180 }
181
182 void FakeXdsTransportFactory::FakeXdsTransport::RemoveStream(
183 const char* method, FakeStreamingCall* call) {
184 MutexLock lock(&mu_);
185 auto it = active_calls_.find(method);
186 if (it != active_calls_.end() && it->second.get() == call) {
187 active_calls_.erase(it);
188 }
189 }
190
191 OrphanablePtr<XdsTransportFactory::XdsTransport::StreamingCall>
192 FakeXdsTransportFactory::FakeXdsTransport::CreateStreamingCall(
193 const char* method,
194 std::unique_ptr<StreamingCall::EventHandler> event_handler) {
195 auto call = MakeOrphanable<FakeStreamingCall>(Ref(), method,
196 std::move(event_handler));
197 MutexLock lock(&mu_);
198 active_calls_[method] = call->Ref();
199 cv_.Signal();
200 return call;
201 }
202
203 //
204 // FakeXdsTransportFactory
205 //
206
207 constexpr char FakeXdsTransportFactory::kAdsMethod[];
208 constexpr char FakeXdsTransportFactory::kAdsV2Method[];
209
210 OrphanablePtr<XdsTransportFactory::XdsTransport>
211 FakeXdsTransportFactory::Create(
212 const XdsBootstrap::XdsServer& server,
213 std::function<void(absl::Status)> on_connectivity_failure,
214 absl::Status* /*status*/) {
215 MutexLock lock(&mu_);
216 auto& entry = transport_map_[&server];
217 GPR_ASSERT(entry == nullptr);
218 auto transport = MakeOrphanable<FakeXdsTransport>(
219 std::move(on_connectivity_failure), auto_complete_messages_from_client_);
220 entry = transport->Ref();
221 return transport;
222 }
223
224 void FakeXdsTransportFactory::TriggerConnectionFailure(
225 const XdsBootstrap::XdsServer& server, absl::Status status) {
226 auto transport = GetTransport(server);
227 transport->TriggerConnectionFailure(std::move(status));
228 }
229
230 void FakeXdsTransportFactory::SetAutoCompleteMessagesFromClient(bool value) {
231 MutexLock lock(&mu_);
232 auto_complete_messages_from_client_ = value;
233 }
234
235 RefCountedPtr<FakeXdsTransportFactory::FakeStreamingCall>
236 FakeXdsTransportFactory::WaitForStream(const XdsBootstrap::XdsServer& server,
237 const char* method,
238 absl::Duration timeout) {
239 auto transport = GetTransport(server);
240 return transport->WaitForStream(method, timeout);
241 }
242
243 RefCountedPtr<FakeXdsTransportFactory::FakeXdsTransport>
244 FakeXdsTransportFactory::GetTransport(const XdsBootstrap::XdsServer& server) {
245 MutexLock lock(&mu_);
246 RefCountedPtr<FakeXdsTransport> transport = transport_map_[&server];
247 GPR_ASSERT(transport != nullptr);
248 return transport;
249 }
250
251 } // namespace grpc_core