Skip to content

Commit

Permalink
iouring: fix the IoUringImpl tests for latest kernel (#33833)
Browse files Browse the repository at this point in the history
Signed-off-by: He Jie Xu <hejie.xu@intel.com>

Signed-off-by: Alex Xu <hejie.xu@intel.com>
  • Loading branch information
soulxu authored and phlax committed May 2, 2024
1 parent cdd258e commit dbe5677
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
15 changes: 4 additions & 11 deletions test/common/io/BUILD
Expand Up @@ -10,22 +10,15 @@ 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",
"//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",
"//test/mocks/server:server_mocks",
],
"//conditions:default": [],
}),
],
)
34 changes: 22 additions & 12 deletions test/common/io/io_uring_impl_test.cc
@@ -1,3 +1,5 @@
#include <functional>

#include "source/common/io/io_uring_impl.h"

#include "test/mocks/server/mocks.h"
Expand All @@ -10,6 +12,8 @@ namespace Envoy {
namespace Io {
namespace {

using WaitConditionFunc = std::function<bool()>;

class IoUringImplTest : public ::testing::Test {
public:
IoUringImplTest() : api_(Api::createApiForTest()) {
Expand Down Expand Up @@ -38,6 +42,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<Server::Configuration::MockServerFactoryContext> context_;
std::unique_ptr<IoUringFactoryImpl> factory_{};
Expand Down Expand Up @@ -101,8 +117,7 @@ TEST_P(IoUringImplParamTest, InvalidParams) {
res = 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, Instantiate) {
Expand Down Expand Up @@ -155,10 +170,8 @@ TEST_F(IoUringImplTest, PrepareReadvAllDataFitsOneChunk) {
EXPECT_STREQ(static_cast<char*>(iov.iov_base), "");
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<char*>(iov.iov_base), "test text");
}
Expand Down Expand Up @@ -214,31 +227,28 @@ TEST_F(IoUringImplTest, PrepareReadvQueueOverflow) {
res = 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<char*>(iov1.iov_base)[0], 'a');
EXPECT_EQ(static_cast<char*>(iov1.iov_base)[1], 'b');
EXPECT_EQ(static_cast<char*>(iov2.iov_base)[0], 'c');
EXPECT_EQ(static_cast<char*>(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 = uring.prepareReadv(fd, &iov3, 1, 4, reinterpret_cast<void*>(3));
EXPECT_EQ(res, IoUringResult::Ok);
res = uring.submit();
EXPECT_EQ(res, IoUringResult::Ok);

waitForCondition(*dispatcher, [&completions_nr]() { return completions_nr == 3; });

EXPECT_EQ(static_cast<char*>(iov3.iov_base)[0], 'e');
EXPECT_EQ(static_cast<char*>(iov3.iov_base)[1], 'f');

dispatcher->run(Event::Dispatcher::RunType::NonBlock);
// Check the completion callback was called actually.
EXPECT_EQ(completions_nr, 3);
}

} // namespace
Expand Down

0 comments on commit dbe5677

Please sign in to comment.