| 1 |
// Copyright 2021 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 "test/core/transport/binder/end2end/fake_binder.h" |
| 16 |
|
| 17 |
#include <string> |
| 18 |
#include <utility> |
| 19 |
|
| 20 |
#include <grpc/support/log.h> |
| 21 |
|
| 22 |
namespace grpc_binder { |
| 23 |
namespace end2end_testing { |
| 24 |
|
| 25 |
TransactionProcessor* g_transaction_processor = nullptr; |
| 26 |
|
| 27 |
int32_t FakeWritableParcel::GetDataSize() const { return data_size_; } |
| 28 |
|
| 29 |
absl::Status FakeWritableParcel::WriteInt32(int32_t data) { |
| 30 |
data_.push_back(data); |
| 31 |
data_size_ += sizeof(int32_t); |
| 32 |
return absl::OkStatus(); |
| 33 |
} |
| 34 |
|
| 35 |
absl::Status FakeWritableParcel::WriteInt64(int64_t data) { |
| 36 |
data_.push_back(data); |
| 37 |
data_size_ += sizeof(int64_t); |
| 38 |
return absl::OkStatus(); |
| 39 |
} |
| 40 |
|
| 41 |
absl::Status FakeWritableParcel::WriteBinder(HasRawBinder* binder) { |
| 42 |
data_.push_back(binder->GetRawBinder()); |
| 43 |
data_size_ += sizeof(void*); |
| 44 |
return absl::OkStatus(); |
| 45 |
} |
| 46 |
|
| 47 |
absl::Status FakeWritableParcel::WriteString(absl::string_view s) { |
| 48 |
data_.push_back(std::string(s)); |
| 49 |
data_size_ += s.size(); |
| 50 |
return absl::OkStatus(); |
| 51 |
} |
| 52 |
|
| 53 |
absl::Status FakeWritableParcel::WriteByteArray(const int8_t* buffer, |
| 54 |
int32_t length) { |
| 55 |
data_.push_back(std::vector<int8_t>(buffer, buffer + length)); |
| 56 |
data_size_ += length; |
| 57 |
return absl::OkStatus(); |
| 58 |
} |
| 59 |
|
| 60 |
int32_t FakeReadableParcel::GetDataSize() const { return data_size_; } |
| 61 |
|
| 62 |
absl::Status FakeReadableParcel::ReadInt32(int32_t* data) { |
| 63 |
if (data_position_ >= data_.size() || |
| 64 |
!absl::holds_alternative<int32_t>(data_[data_position_])) { |
| 65 |
return absl::InternalError("ReadInt32 failed"); |
| 66 |
} |
| 67 |
*data = absl::get<int32_t>(data_[data_position_++]); |
| 68 |
return absl::OkStatus(); |
| 69 |
} |
| 70 |
|
| 71 |
absl::Status FakeReadableParcel::ReadInt64(int64_t* data) { |
| 72 |
if (data_position_ >= data_.size() || |
| 73 |
!absl::holds_alternative<int64_t>(data_[data_position_])) { |
| 74 |
return absl::InternalError("ReadInt64 failed"); |
| 75 |
} |
| 76 |
*data = absl::get<int64_t>(data_[data_position_++]); |
| 77 |
return absl::OkStatus(); |
| 78 |
} |
| 79 |
|
| 80 |
absl::Status FakeReadableParcel::ReadBinder(std::unique_ptr<Binder>* data) { |
| 81 |
if (data_position_ >= data_.size() || |
| 82 |
!absl::holds_alternative<void*>(data_[data_position_])) { |
| 83 |
return absl::InternalError("ReadBinder failed"); |
| 84 |
} |
| 85 |
void* endpoint = absl::get<void*>(data_[data_position_++]); |
| 86 |
if (!endpoint) return absl::InternalError("ReadBinder failed"); |
| 87 |
*data = std::make_unique<FakeBinder>(static_cast<FakeEndpoint*>(endpoint)); |
| 88 |
return absl::OkStatus(); |
| 89 |
} |
| 90 |
|
| 91 |
absl::Status FakeReadableParcel::ReadString(std::string* str) { |
| 92 |
if (data_position_ >= data_.size() || |
| 93 |
!absl::holds_alternative<std::string>(data_[data_position_])) { |
| 94 |
return absl::InternalError("ReadString failed"); |
| 95 |
} |
| 96 |
*str = absl::get<std::string>(data_[data_position_++]); |
| 97 |
return absl::OkStatus(); |
| 98 |
} |
| 99 |
|
| 100 |
absl::Status FakeReadableParcel::ReadByteArray(std::string* data) { |
| 101 |
if (data_position_ >= data_.size() || |
| 102 |
!absl::holds_alternative<std::vector<int8_t>>(data_[data_position_])) { |
| 103 |
return absl::InternalError("ReadByteArray failed"); |
| 104 |
} |
| 105 |
const std::vector<int8_t>& byte_array = |
| 106 |
absl::get<std::vector<int8_t>>(data_[data_position_++]); |
| 107 |
data->resize(byte_array.size()); |
| 108 |
for (size_t i = 0; i < byte_array.size(); ++i) { |
| 109 |
(*data)[i] = byte_array[i]; |
| 110 |
} |
| 111 |
return absl::OkStatus(); |
| 112 |
} |
| 113 |
|
| 114 |
absl::Status FakeBinder::Transact(BinderTransportTxCode tx_code) { |
| 115 |
endpoint_->tunnel->EnQueueTransaction(endpoint_->other_end, tx_code, |
| 116 |
input_->MoveData()); |
| 117 |
return absl::OkStatus(); |
| 118 |
} |
| 119 |
|
| 120 |
FakeTransactionReceiver::FakeTransactionReceiver( |
| 121 |
grpc_core::RefCountedPtr<WireReader> wire_reader_ref, |
| 122 |
TransactionReceiver::OnTransactCb transact_cb) { |
| 123 |
persistent_tx_receiver_ = &g_transaction_processor->NewPersistentTxReceiver( |
| 124 |
std::move(wire_reader_ref), std::move(transact_cb), |
| 125 |
std::make_unique<FakeBinderTunnel>()); |
| 126 |
} |
| 127 |
|
| 128 |
std::unique_ptr<TransactionReceiver> FakeBinder::ConstructTxReceiver( |
| 129 |
grpc_core::RefCountedPtr<WireReader> wire_reader_ref, |
| 130 |
TransactionReceiver::OnTransactCb cb) const { |
| 131 |
return std::make_unique<FakeTransactionReceiver>(wire_reader_ref, cb); |
| 132 |
} |
| 133 |
|
| 134 |
void* FakeTransactionReceiver::GetRawBinder() { |
| 135 |
return persistent_tx_receiver_->tunnel_->GetSendEndpoint(); |
| 136 |
} |
| 137 |
|
| 138 |
std::unique_ptr<Binder> FakeTransactionReceiver::GetSender() const { |
| 139 |
return std::make_unique<FakeBinder>( |
| 140 |
persistent_tx_receiver_->tunnel_->GetSendEndpoint()); |
| 141 |
} |
| 142 |
|
| 143 |
PersistentFakeTransactionReceiver::PersistentFakeTransactionReceiver( |
| 144 |
grpc_core::RefCountedPtr<WireReader> wire_reader_ref, |
| 145 |
TransactionReceiver::OnTransactCb cb, |
| 146 |
std::unique_ptr<FakeBinderTunnel> tunnel) |
| 147 |
: wire_reader_ref_(std::move(wire_reader_ref)), |
| 148 |
callback_(std::move(cb)), |
| 149 |
tunnel_(std::move(tunnel)) { |
| 150 |
FakeEndpoint* recv_endpoint = tunnel_->GetRecvEndpoint(); |
| 151 |
recv_endpoint->owner = this; |
| 152 |
} |
| 153 |
|
| 154 |
TransactionProcessor::TransactionProcessor(absl::Duration delay) |
| 155 |
: delay_nsec_(absl::ToInt64Nanoseconds(delay)), |
| 156 |
tx_thread_( |
| 157 |
"process-thread", |
| 158 |
[](void* arg) { |
| 159 |
grpc_core::ExecCtx exec_ctx; |
| 160 |
auto* self = static_cast<TransactionProcessor*>(arg); |
| 161 |
self->ProcessLoop(); |
| 162 |
}, |
| 163 |
this), |
| 164 |
terminated_(false) { |
| 165 |
tx_thread_.Start(); |
| 166 |
} |
| 167 |
|
| 168 |
void TransactionProcessor::SetDelay(absl::Duration delay) { |
| 169 |
delay_nsec_ = absl::ToInt64Nanoseconds(delay); |
| 170 |
} |
| 171 |
|
| 172 |
void TransactionProcessor::Terminate() { |
| 173 |
if (!terminated_.load(std::memory_order_seq_cst)) { |
| 174 |
gpr_log(GPR_INFO, "Terminating the processor"); |
| 175 |
terminated_.store(true, std::memory_order_seq_cst); |
| 176 |
tx_thread_.Join(); |
| 177 |
gpr_log(GPR_INFO, "Processor terminated"); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
void TransactionProcessor::WaitForNextTransaction() { |
| 182 |
absl::Time now = absl::Now(); |
| 183 |
if (now < deliver_time_) { |
| 184 |
absl::Duration diff = deliver_time_ - now; |
| 185 |
// Release the lock before going to sleep. |
| 186 |
mu_.Unlock(); |
| 187 |
absl::SleepFor(diff); |
| 188 |
mu_.Lock(); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
void TransactionProcessor::Flush() { |
| 193 |
while (true) { |
| 194 |
FakeEndpoint* target = nullptr; |
| 195 |
BinderTransportTxCode tx_code{}; |
| 196 |
FakeData data; |
| 197 |
mu_.Lock(); |
| 198 |
if (tx_queue_.empty()) { |
| 199 |
mu_.Unlock(); |
| 200 |
break; |
| 201 |
} |
| 202 |
WaitForNextTransaction(); |
| 203 |
std::tie(target, tx_code, data) = std::move(tx_queue_.front()); |
| 204 |
tx_queue_.pop(); |
| 205 |
if (!tx_queue_.empty()) { |
| 206 |
deliver_time_ = absl::Now() + GetRandomDelay(); |
| 207 |
} |
| 208 |
mu_.Unlock(); |
| 209 |
auto* tx_receiver = |
| 210 |
static_cast<PersistentFakeTransactionReceiver*>(target->owner); |
| 211 |
auto parcel = std::make_unique<FakeReadableParcel>(std::move(data)); |
| 212 |
tx_receiver->Receive(tx_code, parcel.get()).IgnoreError(); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
void TransactionProcessor::ProcessLoop() { |
| 217 |
while (!terminated_.load(std::memory_order_seq_cst)) { |
| 218 |
FakeEndpoint* target = nullptr; |
| 219 |
BinderTransportTxCode tx_code{}; |
| 220 |
FakeData data; |
| 221 |
mu_.Lock(); |
| 222 |
if (tx_queue_.empty()) { |
| 223 |
mu_.Unlock(); |
| 224 |
continue; |
| 225 |
} |
| 226 |
WaitForNextTransaction(); |
| 227 |
std::tie(target, tx_code, data) = std::move(tx_queue_.front()); |
| 228 |
tx_queue_.pop(); |
| 229 |
if (!tx_queue_.empty()) { |
| 230 |
deliver_time_ = absl::Now() + GetRandomDelay(); |
| 231 |
} |
| 232 |
mu_.Unlock(); |
| 233 |
auto* tx_receiver = |
| 234 |
static_cast<PersistentFakeTransactionReceiver*>(target->owner); |
| 235 |
auto parcel = std::make_unique<FakeReadableParcel>(std::move(data)); |
| 236 |
tx_receiver->Receive(tx_code, parcel.get()).IgnoreError(); |
| 237 |
grpc_core::ExecCtx::Get()->Flush(); |
| 238 |
} |
| 239 |
Flush(); |
| 240 |
} |
| 241 |
|
| 242 |
absl::Duration TransactionProcessor::GetRandomDelay() { |
| 243 |
int64_t delay = |
| 244 |
absl::Uniform<int64_t>(bit_gen_, delay_nsec_ / 2, delay_nsec_); |
| 245 |
return absl::Nanoseconds(delay); |
| 246 |
} |
| 247 |
|
| 248 |
void TransactionProcessor::EnQueueTransaction(FakeEndpoint* target, |
| 249 |
BinderTransportTxCode tx_code, |
| 250 |
FakeData data) { |
| 251 |
grpc_core::MutexLock lock(&mu_); |
| 252 |
if (tx_queue_.empty()) { |
| 253 |
// This is the first transaction in the queue. Compute its deliver time. |
| 254 |
deliver_time_ = absl::Now() + GetRandomDelay(); |
| 255 |
} |
| 256 |
tx_queue_.emplace(target, tx_code, std::move(data)); |
| 257 |
} |
| 258 |
|
| 259 |
FakeBinderTunnel::FakeBinderTunnel() |
| 260 |
: send_endpoint_(std::make_unique<FakeEndpoint>(this)), |
| 261 |
recv_endpoint_(std::make_unique<FakeEndpoint>(this)) { |
| 262 |
send_endpoint_->other_end = recv_endpoint_.get(); |
| 263 |
recv_endpoint_->other_end = send_endpoint_.get(); |
| 264 |
} |
| 265 |
|
| 266 |
std::pair<std::unique_ptr<Binder>, std::unique_ptr<TransactionReceiver>> |
| 267 |
NewBinderPair(TransactionReceiver::OnTransactCb transact_cb) { |
| 268 |
auto tx_receiver = std::make_unique<FakeTransactionReceiver>( |
| 269 |
nullptr, std::move(transact_cb)); |
| 270 |
std::unique_ptr<Binder> sender = tx_receiver->GetSender(); |
| 271 |
return std::make_pair(std::move(sender), std::move(tx_receiver)); |
| 272 |
} |
| 273 |
|
| 274 |
} // namespace end2end_testing |
| 275 |
} // namespace grpc_binder |