21 #include <condition_variable>
28 #include <google/protobuf/arena.h>
29 #include <gtest/gtest.h>
42 #include "src/proto/grpc/testing/echo.grpc.pb.h"
51 class CallbackTestServiceImpl :
public EchoTestService::CallbackService {
55 void SetAllocatorMutator(
57 const EchoRequest*
req, EchoResponse*
resp)>
62 ServerUnaryReactor*
Echo(CallbackServerContext*
context,
69 auto* reactor =
context->DefaultReactor();
75 std::function<void(RpcAllocatorState* allocator_state,
const EchoRequest*
req,
92 return out <<
"TestScenario{protocol="
94 <<
"," <<
scenario.credentials_type <<
"}";
98 std::ostringstream
out;
103 class MessageAllocatorEnd2endTestBase
106 MessageAllocatorEnd2endTestBase() { GetParam().Log(); }
108 ~MessageAllocatorEnd2endTestBase()
override =
default;
110 void CreateServer(MessageAllocator<EchoRequest, EchoResponse>* allocator) {
126 void DestroyServer() {
134 ChannelArguments
args;
140 channel_creds,
args);
151 void TearDown()
override {
158 void SendRpcs(
int num_rpcs) {
160 for (
int i = 0;
i < num_rpcs;
i++) {
163 ClientContext cli_ctx;
166 request.set_message(test_string);
171 std::condition_variable
cv;
173 stub_->async()->Echo(
179 std::lock_guard<std::mutex>
l(
mu);
183 std::unique_lock<std::mutex>
l(
mu);
192 std::unique_ptr<EchoTestService::Stub>
stub_;
198 class NullAllocatorTest :
public MessageAllocatorEnd2endTestBase {};
200 TEST_P(NullAllocatorTest, SimpleRpc) {
206 class SimpleAllocatorTest :
public MessageAllocatorEnd2endTestBase {
208 class SimpleAllocator :
public MessageAllocator<EchoRequest, EchoResponse> {
210 class MessageHolderImpl :
public MessageHolder<EchoRequest, EchoResponse> {
216 set_request(
new EchoRequest);
217 set_response(
new EchoResponse);
219 void Release()
override {
220 (*messages_deallocation_count_)++;
225 void FreeRequest()
override {
226 (*request_deallocation_count_)++;
228 set_request(
nullptr);
231 EchoRequest* ReleaseRequest() {
233 set_request(
nullptr);
241 MessageHolder<EchoRequest, EchoResponse>* AllocateMessages()
override {
252 TEST_P(SimpleAllocatorTest, SimpleRpc) {
253 const int kRpcCount = 10;
254 std::unique_ptr<SimpleAllocator> allocator(
new SimpleAllocator);
261 EXPECT_EQ(kRpcCount, allocator->allocation_count);
262 EXPECT_EQ(kRpcCount, allocator->messages_deallocation_count);
263 EXPECT_EQ(0, allocator->request_deallocation_count);
266 TEST_P(SimpleAllocatorTest, RpcWithEarlyFreeRequest) {
267 const int kRpcCount = 10;
268 std::unique_ptr<SimpleAllocator> allocator(
new SimpleAllocator);
269 auto mutator = [](RpcAllocatorState* allocator_state,
const EchoRequest*
req,
270 EchoResponse*
resp) {
272 static_cast<SimpleAllocator::MessageHolderImpl*
>(allocator_state);
275 allocator_state->FreeRequest();
285 EXPECT_EQ(kRpcCount, allocator->allocation_count);
286 EXPECT_EQ(kRpcCount, allocator->messages_deallocation_count);
287 EXPECT_EQ(kRpcCount, allocator->request_deallocation_count);
290 TEST_P(SimpleAllocatorTest, RpcWithReleaseRequest) {
291 const int kRpcCount = 10;
292 std::unique_ptr<SimpleAllocator> allocator(
new SimpleAllocator);
293 std::vector<EchoRequest*> released_requests;
294 auto mutator = [&released_requests](RpcAllocatorState* allocator_state,
295 const EchoRequest*
req,
296 EchoResponse*
resp) {
298 static_cast<SimpleAllocator::MessageHolderImpl*
>(allocator_state);
301 released_requests.push_back(info->ReleaseRequest());
311 EXPECT_EQ(kRpcCount, allocator->allocation_count);
312 EXPECT_EQ(kRpcCount, allocator->messages_deallocation_count);
313 EXPECT_EQ(0, allocator->request_deallocation_count);
314 EXPECT_EQ(
static_cast<unsigned>(kRpcCount), released_requests.size());
315 for (
auto*
req : released_requests) {
320 class ArenaAllocatorTest :
public MessageAllocatorEnd2endTestBase {
322 class ArenaAllocator :
public MessageAllocator<EchoRequest, EchoResponse> {
324 class MessageHolderImpl :
public MessageHolder<EchoRequest, EchoResponse> {
326 MessageHolderImpl() {
328 google::protobuf::Arena::CreateMessage<EchoRequest>(&
arena_));
330 google::protobuf::Arena::CreateMessage<EchoResponse>(&
arena_));
332 void Release()
override {
delete this; }
333 void FreeRequest()
override {
GPR_ASSERT(0); }
338 MessageHolder<EchoRequest, EchoResponse>* AllocateMessages()
override {
340 return new MessageHolderImpl;
346 TEST_P(ArenaAllocatorTest, SimpleRpc) {
347 const int kRpcCount = 10;
348 std::unique_ptr<ArenaAllocator> allocator(
new ArenaAllocator);
352 EXPECT_EQ(kRpcCount, allocator->allocation_count);
357 std::vector<std::string> credentials_types{
365 if (test_insecure && insec_ok()) {
371 for (Protocol p : parr) {
372 for (
const auto& cred : credentials_types) {
395 int main(
int argc,
char** argv) {