| 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 |
#ifndef GRPC_CORE_EXT_XDS_XDS_TRANSPORT_FAKE_H |
| 18 |
#define GRPC_CORE_EXT_XDS_XDS_TRANSPORT_FAKE_H |
| 19 |
|
| 20 |
#include <grpc/support/port_platform.h> |
| 21 |
|
| 22 |
#include <deque> |
| 23 |
#include <functional> |
| 24 |
#include <map> |
| 25 |
#include <memory> |
| 26 |
#include <string> |
| 27 |
#include <utility> |
| 28 |
|
| 29 |
#include "absl/base/thread_annotations.h" |
| 30 |
#include "absl/status/status.h" |
| 31 |
#include "absl/strings/string_view.h" |
| 32 |
#include "absl/time/time.h" |
| 33 |
#include "absl/types/optional.h" |
| 34 |
|
| 35 |
#include "src/core/ext/xds/xds_bootstrap.h" |
| 36 |
#include "src/core/ext/xds/xds_transport.h" |
| 37 |
#include "src/core/lib/gprpp/orphanable.h" |
| 38 |
#include "src/core/lib/gprpp/ref_counted.h" |
| 39 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 40 |
#include "src/core/lib/gprpp/sync.h" |
| 41 |
|
| 42 |
namespace grpc_core { |
| 43 |
|
| 44 |
class FakeXdsTransportFactory : public XdsTransportFactory { |
| 45 |
private: |
| 46 |
class FakeXdsTransport; |
| 47 |
|
| 48 |
public: |
| 49 |
static constexpr char kAdsMethod[] = |
| 50 |
"/envoy.service.discovery.v3.AggregatedDiscoveryService/" |
| 51 |
"StreamAggregatedResources"; |
| 52 |
static constexpr char kAdsV2Method[] = |
| 53 |
"/envoy.service.discovery.v2.AggregatedDiscoveryService/" |
| 54 |
"StreamAggregatedResources"; |
| 55 |
|
| 56 |
class FakeStreamingCall : public XdsTransport::StreamingCall { |
| 57 |
public: |
| 58 |
FakeStreamingCall( |
| 59 |
RefCountedPtr<FakeXdsTransport> transport, const char* method, |
| 60 |
std::unique_ptr<StreamingCall::EventHandler> event_handler) |
| 61 |
: transport_(std::move(transport)), |
| 62 |
method_(method), |
| 63 |
event_handler_(MakeRefCounted<RefCountedEventHandler>( |
| 64 |
std::move(event_handler))) {} |
| 65 |
|
| 66 |
~FakeStreamingCall() override; |
| 67 |
|
| 68 |
void Orphan() override; |
| 69 |
|
| 70 |
using StreamingCall::Ref; // Make it public. |
| 71 |
|
| 72 |
bool HaveMessageFromClient(); |
| 73 |
absl::optional<std::string> WaitForMessageFromClient( |
| 74 |
absl::Duration timeout); |
| 75 |
|
| 76 |
// If FakeXdsTransportFactory::SetAutoCompleteMessagesFromClient() |
| 77 |
// was called to set the value to false before the creation of the |
| 78 |
// transport that underlies this stream, then this must be called |
| 79 |
// to invoke EventHandler::OnRequestSent() for every message read |
| 80 |
// via WaitForMessageFromClient(). |
| 81 |
void CompleteSendMessageFromClient(bool ok = true); |
| 82 |
|
| 83 |
void SendMessageToClient(absl::string_view payload); |
| 84 |
void MaybeSendStatusToClient(absl::Status status); |
| 85 |
|
| 86 |
private: |
| 87 |
class RefCountedEventHandler : public RefCounted<RefCountedEventHandler> { |
| 88 |
public: |
| 89 |
explicit RefCountedEventHandler( |
| 90 |
std::unique_ptr<StreamingCall::EventHandler> event_handler) |
| 91 |
: event_handler_(std::move(event_handler)) {} |
| 92 |
|
| 93 |
void OnRequestSent(bool ok) { event_handler_->OnRequestSent(ok); } |
| 94 |
void OnRecvMessage(absl::string_view payload) { |
| 95 |
event_handler_->OnRecvMessage(payload); |
| 96 |
} |
| 97 |
void OnStatusReceived(absl::Status status) { |
| 98 |
event_handler_->OnStatusReceived(std::move(status)); |
| 99 |
} |
| 100 |
|
| 101 |
private: |
| 102 |
std::unique_ptr<StreamingCall::EventHandler> event_handler_; |
| 103 |
}; |
| 104 |
|
| 105 |
void SendMessage(std::string payload) override; |
| 106 |
|
| 107 |
void CompleteSendMessageFromClientLocked(bool ok) |
| 108 |
ABSL_EXCLUSIVE_LOCKS_REQUIRED(&mu_); |
| 109 |
|
| 110 |
RefCountedPtr<FakeXdsTransport> transport_; |
| 111 |
const char* method_; |
| 112 |
|
| 113 |
Mutex mu_; |
| 114 |
CondVar cv_; |
| 115 |
RefCountedPtr<RefCountedEventHandler> event_handler_ ABSL_GUARDED_BY(&mu_); |
| 116 |
std::deque<std::string> from_client_messages_ ABSL_GUARDED_BY(&mu_); |
| 117 |
bool status_sent_ ABSL_GUARDED_BY(&mu_) = false; |
| 118 |
}; |
| 119 |
|
| 120 |
FakeXdsTransportFactory() = default; |
| 121 |
|
| 122 |
using XdsTransportFactory::Ref; // Make it public. |
| 123 |
|
| 124 |
void TriggerConnectionFailure(const XdsBootstrap::XdsServer& server, |
| 125 |
absl::Status status); |
| 126 |
|
| 127 |
// By default, FakeStreamingCall will automatically invoke |
| 128 |
// EventHandler::OnRequestSent() upon reading a request from the client. |
| 129 |
// If this is set to false, that behavior will be inhibited, and |
| 130 |
// EventHandler::OnRequestSent() will not be called until the test |
| 131 |
// expicitly calls FakeStreamingCall::CompleteSendMessageFromClient(). |
| 132 |
// |
| 133 |
// This value affects all transports created after this call is |
| 134 |
// complete. Any transport that already exists prior to this call |
| 135 |
// will not be affected. |
| 136 |
void SetAutoCompleteMessagesFromClient(bool value); |
| 137 |
|
| 138 |
RefCountedPtr<FakeStreamingCall> WaitForStream( |
| 139 |
const XdsBootstrap::XdsServer& server, const char* method, |
| 140 |
absl::Duration timeout); |
| 141 |
|
| 142 |
void Orphan() override { Unref(); } |
| 143 |
|
| 144 |
private: |
| 145 |
class FakeXdsTransport : public XdsTransport { |
| 146 |
public: |
| 147 |
FakeXdsTransport(std::function<void(absl::Status)> on_connectivity_failure, |
| 148 |
bool auto_complete_messages_from_client) |
| 149 |
: auto_complete_messages_from_client_( |
| 150 |
auto_complete_messages_from_client), |
| 151 |
on_connectivity_failure_( |
| 152 |
MakeRefCounted<RefCountedOnConnectivityFailure>( |
| 153 |
std::move(on_connectivity_failure))) {} |
| 154 |
|
| 155 |
void Orphan() override; |
| 156 |
|
| 157 |
bool auto_complete_messages_from_client() const { |
| 158 |
return auto_complete_messages_from_client_; |
| 159 |
} |
| 160 |
|
| 161 |
using XdsTransport::Ref; // Make it public. |
| 162 |
|
| 163 |
void TriggerConnectionFailure(absl::Status status); |
| 164 |
|
| 165 |
RefCountedPtr<FakeStreamingCall> WaitForStream(const char* method, |
| 166 |
absl::Duration timeout); |
| 167 |
|
| 168 |
void RemoveStream(const char* method, FakeStreamingCall* call); |
| 169 |
|
| 170 |
private: |
| 171 |
class RefCountedOnConnectivityFailure |
| 172 |
: public RefCounted<RefCountedOnConnectivityFailure> { |
| 173 |
public: |
| 174 |
explicit RefCountedOnConnectivityFailure( |
| 175 |
std::function<void(absl::Status)> on_connectivity_failure) |
| 176 |
: on_connectivity_failure_(std::move(on_connectivity_failure)) {} |
| 177 |
|
| 178 |
void Run(absl::Status status) { |
| 179 |
on_connectivity_failure_(std::move(status)); |
| 180 |
} |
| 181 |
|
| 182 |
private: |
| 183 |
std::function<void(absl::Status)> on_connectivity_failure_; |
| 184 |
}; |
| 185 |
|
| 186 |
OrphanablePtr<StreamingCall> CreateStreamingCall( |
| 187 |
const char* method, |
| 188 |
std::unique_ptr<StreamingCall::EventHandler> event_handler) override; |
| 189 |
|
| 190 |
void ResetBackoff() override {} |
| 191 |
|
| 192 |
const bool auto_complete_messages_from_client_; |
| 193 |
|
| 194 |
Mutex mu_; |
| 195 |
CondVar cv_; |
| 196 |
RefCountedPtr<RefCountedOnConnectivityFailure> on_connectivity_failure_ |
| 197 |
ABSL_GUARDED_BY(&mu_); |
| 198 |
std::map<std::string /*method*/, RefCountedPtr<FakeStreamingCall>> |
| 199 |
active_calls_ ABSL_GUARDED_BY(&mu_); |
| 200 |
}; |
| 201 |
|
| 202 |
OrphanablePtr<XdsTransport> Create( |
| 203 |
const XdsBootstrap::XdsServer& server, |
| 204 |
std::function<void(absl::Status)> on_connectivity_failure, |
| 205 |
absl::Status* status) override; |
| 206 |
|
| 207 |
RefCountedPtr<FakeXdsTransport> GetTransport( |
| 208 |
const XdsBootstrap::XdsServer& server); |
| 209 |
|
| 210 |
Mutex mu_; |
| 211 |
std::map<const XdsBootstrap::XdsServer*, RefCountedPtr<FakeXdsTransport>> |
| 212 |
transport_map_ ABSL_GUARDED_BY(&mu_); |
| 213 |
bool auto_complete_messages_from_client_ ABSL_GUARDED_BY(&mu_) = true; |
| 214 |
}; |
| 215 |
|
| 216 |
} // namespace grpc_core |
| 217 |
|
| 218 |
#endif // GRPC_CORE_EXT_XDS_XDS_TRANSPORT_FAKE_H |