wire_writer_test.cc
Go to the documentation of this file.
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 
16 
17 #include <string>
18 #include <utility>
19 
20 #include <gtest/gtest.h>
21 
22 #include "absl/memory/memory.h"
23 
25 
28 
29 namespace grpc_binder {
30 
32 
33 MATCHER_P(StrEqInt8Ptr, target, "") {
34  return std::string(reinterpret_cast<const char*>(arg), target.size()) ==
35  target;
36 }
37 
38 TEST(WireWriterTest, RpcCall) {
40  init_lib.init();
41  // Required because wire writer uses combiner internally.
43  auto mock_binder = absl::make_unique<MockBinder>();
44  MockBinder& mock_binder_ref = *mock_binder;
45  MockWritableParcel mock_writable_parcel;
46  ON_CALL(mock_binder_ref, GetWritableParcel)
47  .WillByDefault(Return(&mock_writable_parcel));
48  WireWriterImpl wire_writer(std::move(mock_binder));
49 
50  auto ExpectWriteByteArray = [&](const std::string& target) {
51  // length
52  EXPECT_CALL(mock_writable_parcel, WriteInt32(target.size()));
53  if (!target.empty()) {
54  // content
55  EXPECT_CALL(mock_writable_parcel,
56  WriteByteArray(StrEqInt8Ptr(target), target.size()));
57  }
58  };
59 
60  ::testing::InSequence sequence;
61  int sequence_number = 0;
62 
63  {
64  // flag
65  EXPECT_CALL(mock_writable_parcel, WriteInt32(0));
66  // sequence number
67  EXPECT_CALL(mock_writable_parcel, WriteInt32(sequence_number));
68 
69  EXPECT_CALL(mock_binder_ref, Transact(BinderTransportTxCode(kFirstCallId)));
70 
71  auto tx = std::make_unique<Transaction>(kFirstCallId, /*is_client=*/true);
72  EXPECT_TRUE(wire_writer.RpcCall(std::move(tx)).ok());
73  sequence_number++;
75  }
76  {
77  // flag
78  EXPECT_CALL(mock_writable_parcel, WriteInt32(kFlagPrefix));
79  // sequence number. This is another stream so the sequence number starts
80  // with 0.
81  EXPECT_CALL(mock_writable_parcel, WriteInt32(0));
82 
83  EXPECT_CALL(mock_writable_parcel,
84  WriteString(absl::string_view("/example/method/ref")));
85 
86  const std::vector<std::pair<std::string, std::string>> kMetadata = {
87  {"", ""},
88  {"", "value"},
89  {"key", ""},
90  {"key", "value"},
91  {"another-key", "another-value"},
92  };
93 
94  // Number of metadata
95  EXPECT_CALL(mock_writable_parcel, WriteInt32(kMetadata.size()));
96 
97  for (const auto& md : kMetadata) {
98  ExpectWriteByteArray(md.first);
99  ExpectWriteByteArray(md.second);
100  }
101 
102  EXPECT_CALL(mock_binder_ref,
103  Transact(BinderTransportTxCode(kFirstCallId + 1)));
104 
105  auto tx =
106  std::make_unique<Transaction>(kFirstCallId + 1, /*is_client=*/true);
107  tx->SetPrefix(kMetadata);
108  tx->SetMethodRef("/example/method/ref");
109  EXPECT_TRUE(wire_writer.RpcCall(std::move(tx)).ok());
111  }
112  {
113  // flag
114  EXPECT_CALL(mock_writable_parcel, WriteInt32(kFlagMessageData));
115  // sequence number
116  EXPECT_CALL(mock_writable_parcel, WriteInt32(sequence_number));
117 
118  ExpectWriteByteArray("data");
119  EXPECT_CALL(mock_binder_ref, Transact(BinderTransportTxCode(kFirstCallId)));
120 
121  auto tx = std::make_unique<Transaction>(kFirstCallId, /*is_client=*/true);
122  tx->SetData("data");
123  EXPECT_TRUE(wire_writer.RpcCall(std::move(tx)).ok());
124  sequence_number++;
126  }
127  {
128  // flag
129  EXPECT_CALL(mock_writable_parcel, WriteInt32(kFlagSuffix));
130  // sequence number
131  EXPECT_CALL(mock_writable_parcel, WriteInt32(sequence_number));
132 
133  EXPECT_CALL(mock_binder_ref, Transact(BinderTransportTxCode(kFirstCallId)));
134 
135  auto tx = std::make_unique<Transaction>(kFirstCallId, /*is_client=*/true);
136  tx->SetSuffix({});
137  EXPECT_TRUE(wire_writer.RpcCall(std::move(tx)).ok());
138  sequence_number++;
140  }
141  {
142  // flag
143  EXPECT_CALL(mock_writable_parcel,
144  WriteInt32(kFlagPrefix | kFlagMessageData | kFlagSuffix));
145  // sequence number
146  EXPECT_CALL(mock_writable_parcel, WriteInt32(sequence_number));
147 
148  EXPECT_CALL(mock_writable_parcel,
149  WriteString(absl::string_view("/example/method/ref")));
150 
151  const std::vector<std::pair<std::string, std::string>> kMetadata = {
152  {"", ""},
153  {"", "value"},
154  {"key", ""},
155  {"key", "value"},
156  {"another-key", "another-value"},
157  };
158 
159  // Number of metadata
160  EXPECT_CALL(mock_writable_parcel, WriteInt32(kMetadata.size()));
161 
162  for (const auto& md : kMetadata) {
163  ExpectWriteByteArray(md.first);
164  ExpectWriteByteArray(md.second);
165  }
166 
167  // Empty message data
168  ExpectWriteByteArray("");
169 
170  EXPECT_CALL(mock_binder_ref, Transact(BinderTransportTxCode(kFirstCallId)));
171 
172  auto tx = std::make_unique<Transaction>(kFirstCallId, /*is_client=*/true);
173  // TODO(waynetu): Implement a helper function that automatically creates
174  // EXPECT_CALL based on the tx object.
175  tx->SetPrefix(kMetadata);
176  tx->SetMethodRef("/example/method/ref");
177  tx->SetData("");
178  tx->SetSuffix({});
179  EXPECT_TRUE(wire_writer.RpcCall(std::move(tx)).ok());
180  sequence_number++;
182  }
183 
184  // Really large message
185  {
186  EXPECT_CALL(mock_writable_parcel,
188  EXPECT_CALL(mock_writable_parcel, WriteInt32(0));
189  ExpectWriteByteArray(std::string(WireWriterImpl::kBlockSize, 'a'));
190  EXPECT_CALL(mock_writable_parcel, GetDataSize)
192  EXPECT_CALL(mock_binder_ref,
193  Transact(BinderTransportTxCode(kFirstCallId + 2)));
194 
195  EXPECT_CALL(mock_writable_parcel,
197  EXPECT_CALL(mock_writable_parcel, WriteInt32(1));
198  ExpectWriteByteArray(std::string(WireWriterImpl::kBlockSize, 'a'));
199  EXPECT_CALL(mock_writable_parcel, GetDataSize)
201  EXPECT_CALL(mock_binder_ref,
202  Transact(BinderTransportTxCode(kFirstCallId + 2)));
203 
204  EXPECT_CALL(mock_writable_parcel, WriteInt32(kFlagMessageData));
205  EXPECT_CALL(mock_writable_parcel, WriteInt32(2));
206  ExpectWriteByteArray("a");
207  EXPECT_CALL(mock_writable_parcel, GetDataSize).WillOnce(Return(1));
208  EXPECT_CALL(mock_binder_ref,
209  Transact(BinderTransportTxCode(kFirstCallId + 2)));
210 
211  // Use a new stream.
212  auto tx =
213  std::make_unique<Transaction>(kFirstCallId + 2, /*is_client=*/true);
214  tx->SetData(std::string(2 * WireWriterImpl::kBlockSize + 1, 'a'));
215  EXPECT_TRUE(wire_writer.RpcCall(std::move(tx)).ok());
217  }
218  // Really large message with metadata
219  {
220  EXPECT_CALL(
221  mock_writable_parcel,
223  EXPECT_CALL(mock_writable_parcel, WriteInt32(0));
224  EXPECT_CALL(mock_writable_parcel, WriteString(absl::string_view("123")));
225  EXPECT_CALL(mock_writable_parcel, WriteInt32(0));
226  ExpectWriteByteArray(std::string(WireWriterImpl::kBlockSize, 'a'));
227  EXPECT_CALL(mock_writable_parcel, GetDataSize)
229  EXPECT_CALL(mock_binder_ref,
230  Transact(BinderTransportTxCode(kFirstCallId + 3)));
231 
232  EXPECT_CALL(mock_writable_parcel,
234  EXPECT_CALL(mock_writable_parcel, WriteInt32(1));
235  ExpectWriteByteArray(std::string(WireWriterImpl::kBlockSize, 'a'));
236  EXPECT_CALL(mock_writable_parcel, GetDataSize)
238  EXPECT_CALL(mock_binder_ref,
239  Transact(BinderTransportTxCode(kFirstCallId + 3)));
240 
241  EXPECT_CALL(mock_writable_parcel,
242  WriteInt32(kFlagMessageData | kFlagSuffix));
243  EXPECT_CALL(mock_writable_parcel, WriteInt32(2));
244  ExpectWriteByteArray("a");
245  EXPECT_CALL(mock_writable_parcel, GetDataSize).WillOnce(Return(1));
246  EXPECT_CALL(mock_binder_ref,
247  Transact(BinderTransportTxCode(kFirstCallId + 3)));
248 
249  // Use a new stream.
250  auto tx =
251  std::make_unique<Transaction>(kFirstCallId + 3, /*is_client=*/true);
252  tx->SetPrefix({});
253  tx->SetMethodRef("123");
254  tx->SetData(std::string(2 * WireWriterImpl::kBlockSize + 1, 'a'));
255  tx->SetSuffix({});
256  EXPECT_TRUE(wire_writer.RpcCall(std::move(tx)).ok());
258  }
260  init_lib.shutdown();
261 }
262 
263 } // namespace grpc_binder
264 
265 int main(int argc, char** argv) {
266  ::testing::InitGoogleTest(&argc, argv);
267  grpc::testing::TestEnvironment env(&argc, argv);
268  return RUN_ALL_TESTS();
269 }
generate.env
env
Definition: generate.py:37
testing::Return
internal::ReturnAction< R > Return(R value)
Definition: bloaty/third_party/googletest/googlemock/include/gmock/gmock-actions.h:1004
grpc_binder::WireWriterImpl::kBlockSize
static const int64_t kBlockSize
Definition: wire_writer.h:71
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
grpc_binder
Definition: connection_id_generator.cc:45
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
mock_objects.h
grpc_binder::kFlagMessageDataIsPartial
const int kFlagMessageDataIsPartial
Definition: transaction.cc:30
grpc_binder::BinderTransportTxCode
BinderTransportTxCode
Definition: binder_constants.h:31
grpc_binder::TEST
TEST(EndpointBinderPoolTest, AddBeforeGet)
Definition: endpoint_binder_pool_test.cc:38
grpc_binder::MATCHER_P
MATCHER_P(StrEqInt8Ptr, target, "")
Definition: wire_writer_test.cc:33
grpc_binder::MockWritableParcel
Definition: mock_objects.h:28
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
grpc_core::ExecCtx::Flush
bool Flush()
Definition: exec_ctx.cc:69
testing::InSequence
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:9838
main
int main(int argc, char **argv)
Definition: wire_writer_test.cc:265
arg
Definition: cmdline.cc:40
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc::internal::GrpcLibrary
Definition: grpcpp/impl/grpc_library.h:32
grpc_core::ExecCtx
Definition: exec_ctx.h:97
grpc_binder::MockBinder
Definition: mock_objects.h:53
EXPECT_CALL
#define EXPECT_CALL(obj, call)
ON_CALL
#define ON_CALL(obj, call)
test_config.h
grpc_binder::kFlagPrefix
const int kFlagPrefix
Definition: transaction.cc:23
benchmark.md
md
Definition: benchmark.py:86
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc_library.h
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
grpc_binder::kFlagSuffix
const int kFlagSuffix
Definition: transaction.cc:25
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
wire_writer.h
grpc_binder::kFlagMessageData
const int kFlagMessageData
Definition: transaction.cc:24
absl::Status::ok
ABSL_MUST_USE_RESULT bool ok() const
Definition: third_party/abseil-cpp/absl/status/status.h:802
grpc_binder::WireWriterImpl
Definition: wire_writer.h:42
grpc::internal::GrpcLibrary::init
void init() override
Definition: grpcpp/impl/grpc_library.h:34
grpc::internal::GrpcLibrary::shutdown
void shutdown() override
Definition: grpcpp/impl/grpc_library.h:35
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
grpc_binder::kFirstCallId
const int kFirstCallId
Definition: binder_constants.cc:26
grpc_binder::WireWriterImpl::RpcCall
absl::Status RpcCall(std::unique_ptr< Transaction > tx) override
Definition: wire_writer.cc:270
setup.target
target
Definition: third_party/bloaty/third_party/protobuf/python/setup.py:179
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:54