From 2ccfbcccb36868cc86a1e59e970afeff1879cbb9 Mon Sep 17 00:00:00 2001 From: Alex Xu Date: Tue, 30 Apr 2024 19:52:45 +0800 Subject: [PATCH] iouring: fix the IoUringImpl tests for latest kernel (#33833) Signed-off-by: He Jie Xu Signed-off-by: Ryan Northey --- test/common/io/BUILD | 13 ++------ test/common/io/io_uring_impl_test.cc | 46 ++++++++++++++++------------ 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/test/common/io/BUILD b/test/common/io/BUILD index b8c56ca96523..366fdaeba4ec 100644 --- a/test/common/io/BUILD +++ b/test/common/io/BUILD @@ -10,26 +10,19 @@ envoy_package() envoy_cc_test( name = "io_uring_impl_test", - srcs = select({ - "//bazel:linux_x86_64": ["io_uring_impl_test.cc"], - "//conditions:default": [], - }), + srcs = ["io_uring_impl_test.cc"], tags = [ "nocompdb", "skip_on_windows", ], deps = [ + "//source/common/io:io_uring_impl_lib", "//source/common/network:address_lib", "//test/mocks/io:io_mocks", "//test/mocks/server:server_mocks", "//test/test_common:environment_lib", "//test/test_common:utility_lib", - ] + select({ - "//bazel:linux": [ - "//source/common/io:io_uring_impl_lib", - ], - "//conditions:default": [], - }), + ], ) envoy_cc_test( diff --git a/test/common/io/io_uring_impl_test.cc b/test/common/io/io_uring_impl_test.cc index 3ac3fe6e4753..6d2f01dca22b 100644 --- a/test/common/io/io_uring_impl_test.cc +++ b/test/common/io/io_uring_impl_test.cc @@ -1,3 +1,5 @@ +#include + #include "source/common/io/io_uring_impl.h" #include "source/common/network/address_impl.h" @@ -22,6 +24,8 @@ class TestRequest : public Request { MockIoUringSocket mock_io_uring_socket_; }; +using WaitConditionFunc = std::function; + class IoUringImplTest : public ::testing::Test { public: IoUringImplTest() : api_(Api::createApiForTest()), should_skip_(!isIoUringSupported()) { @@ -48,6 +52,18 @@ class IoUringImplTest : public ::testing::Test { } } + void waitForCondition(Event::Dispatcher& dispatcher, WaitConditionFunc condition_func, + std::chrono::milliseconds wait_timeout = TestUtility::DefaultTimeout) { + Event::TestTimeSystem::RealTimeBound bound(wait_timeout); + while (!condition_func()) { + if (!bound.withinBound()) { + RELEASE_ASSERT(0, "Timed out waiting for the condition."); + break; + } + dispatcher.run(Event::Dispatcher::RunType::NonBlock); + } + } + Api::ApiPtr api_; testing::NiceMock context_; std::unique_ptr factory_{}; @@ -110,8 +126,7 @@ TEST_P(IoUringImplParamTest, InvalidParams) { res = io_uring_->submit(); EXPECT_EQ(res, IoUringResult::Ok); - dispatcher->run(Event::Dispatcher::RunType::NonBlock); - EXPECT_EQ(completions_nr, 2); + waitForCondition(*dispatcher, [&completions_nr]() { return completions_nr == 2; }); } TEST_F(IoUringImplTest, InjectCompletion) { @@ -141,8 +156,7 @@ TEST_F(IoUringImplTest, InjectCompletion) { file_event->activate(Event::FileReadyType::Read); - dispatcher->run(Event::Dispatcher::RunType::NonBlock); - EXPECT_EQ(completions_nr, 1); + waitForCondition(*dispatcher, [&completions_nr]() { return completions_nr == 1; }); } TEST_F(IoUringImplTest, NestInjectCompletion) { @@ -182,8 +196,7 @@ TEST_F(IoUringImplTest, NestInjectCompletion) { file_event->activate(Event::FileReadyType::Read); - dispatcher->run(Event::Dispatcher::RunType::NonBlock); - EXPECT_EQ(completions_nr, 2); + waitForCondition(*dispatcher, [&completions_nr]() { return completions_nr == 2; }); } TEST_F(IoUringImplTest, RemoveInjectCompletion) { @@ -218,8 +231,7 @@ TEST_F(IoUringImplTest, RemoveInjectCompletion) { EXPECT_EQ(-1, data2); file_event->activate(Event::FileReadyType::Read); - dispatcher->run(Event::Dispatcher::RunType::NonBlock); - EXPECT_EQ(completions_nr, 1); + waitForCondition(*dispatcher, [&completions_nr]() { return completions_nr == 1; }); } TEST_F(IoUringImplTest, NestRemoveInjectCompletion) { @@ -258,8 +270,7 @@ TEST_F(IoUringImplTest, NestRemoveInjectCompletion) { file_event->activate(Event::FileReadyType::Read); - dispatcher->run(Event::Dispatcher::RunType::NonBlock); - EXPECT_EQ(completions_nr, 2); + waitForCondition(*dispatcher, [&completions_nr]() { return completions_nr == 2; }); } TEST_F(IoUringImplTest, RegisterEventfd) { @@ -303,10 +314,8 @@ TEST_F(IoUringImplTest, PrepareReadvAllDataFitsOneChunk) { EXPECT_STREQ(static_cast(iov.iov_base), ""); io_uring_->submit(); - dispatcher->run(Event::Dispatcher::RunType::Block); - // Check that the completion callback has been actually called. - EXPECT_EQ(completions_nr, 1); + waitForCondition(*dispatcher, [&completions_nr]() { return completions_nr == 1; }); // The file's content is in the read buffer now. EXPECT_STREQ(static_cast(iov.iov_base), "test text"); } @@ -366,6 +375,7 @@ TEST_F(IoUringImplTest, PrepareReadvQueueOverflow) { res = io_uring_->submit(); EXPECT_EQ(res, IoUringResult::Ok); + waitForCondition(*dispatcher, [&completions_nr]() { return completions_nr == 2; }); // Even though we haven't been notified about ops completion the buffers // are filled already. EXPECT_EQ(static_cast(iov1.iov_base)[0], 'a'); @@ -373,11 +383,9 @@ TEST_F(IoUringImplTest, PrepareReadvQueueOverflow) { EXPECT_EQ(static_cast(iov2.iov_base)[0], 'c'); EXPECT_EQ(static_cast(iov2.iov_base)[1], 'd'); - dispatcher->run(Event::Dispatcher::RunType::NonBlock); - // Only 2 completions are expected because the completion queue can contain // no more than 2 entries. - EXPECT_EQ(completions_nr, 2); + waitForCondition(*dispatcher, [&completions_nr]() { return completions_nr == 2; }); // Check a new event gets handled in the next dispatcher run. res = io_uring_->prepareReadv(fd, &iov3, 1, 4, &request3); @@ -385,12 +393,10 @@ TEST_F(IoUringImplTest, PrepareReadvQueueOverflow) { res = io_uring_->submit(); EXPECT_EQ(res, IoUringResult::Ok); + waitForCondition(*dispatcher, [&completions_nr]() { return completions_nr == 3; }); + EXPECT_EQ(static_cast(iov3.iov_base)[0], 'e'); EXPECT_EQ(static_cast(iov3.iov_base)[1], 'f'); - - dispatcher->run(Event::Dispatcher::RunType::NonBlock); - // Check the completion callback was called actually. - EXPECT_EQ(completions_nr, 3); } } // namespace