1 /*
2 * Copyright (C) 2025 The Android Open Source Project
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 <IOUringSocketHandler/IOUringSocketHandler.h>
18
19 #include <gtest/gtest.h>
20
21 class IOUringSocketHandlerTest : public testing::Test {
22 public:
IsIouringEnabled()23 bool IsIouringEnabled() {
24 return IOUringSocketHandler::isIouringEnabled();
25 }
26
27 protected:
28 std::unique_ptr<IOUringSocketHandler> handler_;
29 void InitializeHandler(int socket_fd = 1);
30 int queue_depth_ = 10;
31 };
32
InitializeHandler(int socket_fd)33 void IOUringSocketHandlerTest::InitializeHandler(int socket_fd) {
34 handler_ = std::make_unique<IOUringSocketHandler>(socket_fd);
35 }
36
TEST_F(IOUringSocketHandlerTest,SetupIoUring)37 TEST_F(IOUringSocketHandlerTest, SetupIoUring) {
38 if (!IsIouringEnabled()) {
39 return;
40 }
41 InitializeHandler();
42 EXPECT_TRUE(handler_->SetupIoUring(queue_depth_));
43 }
44
TEST_F(IOUringSocketHandlerTest,AllocateAndRegisterBuffers)45 TEST_F(IOUringSocketHandlerTest, AllocateAndRegisterBuffers) {
46 if (!IsIouringEnabled()) {
47 return;
48 }
49 InitializeHandler();
50 EXPECT_TRUE(handler_->SetupIoUring(queue_depth_));
51 EXPECT_TRUE(handler_->AllocateAndRegisterBuffers(8, 4096));
52 }
53
TEST_F(IOUringSocketHandlerTest,MultipleAllocateAndRegisterBuffers)54 TEST_F(IOUringSocketHandlerTest, MultipleAllocateAndRegisterBuffers) {
55 if (!IsIouringEnabled()) {
56 return;
57 }
58 InitializeHandler();
59
60 EXPECT_TRUE(handler_->SetupIoUring(queue_depth_));
61
62 EXPECT_TRUE(handler_->AllocateAndRegisterBuffers(4, 4096));
63 handler_->DeRegisterBuffers();
64
65 EXPECT_TRUE(handler_->AllocateAndRegisterBuffers(2, 1024*1024L));
66 handler_->DeRegisterBuffers();
67
68 EXPECT_TRUE(handler_->AllocateAndRegisterBuffers(32, 1024));
69 handler_->DeRegisterBuffers();
70
71 // num_buffers should be power of 2
72 EXPECT_FALSE(handler_->AllocateAndRegisterBuffers(5, 4096));
73 }
74
main(int argc,char ** argv)75 int main(int argc, char** argv) {
76 ::testing::InitGoogleTest(&argc, argv);
77 return RUN_ALL_TESTS();
78 }
79