test/io_uring: Fix flaky test
We shouldn't use file descriptor numbers directly or use the possibly
closed stdin descriptor. Just use stdout and stderr to improve test
reliability.
Change-Id: Ibc197433beb53dbe4d792149d0733c603cc14003
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/test/io_uring.cpp b/test/io_uring.cpp
index c98462d..755ecf8 100644
--- a/test/io_uring.cpp
+++ b/test/io_uring.cpp
@@ -178,25 +178,29 @@
std::optional<IoUring::FileHandle> fh;
// Slots are always allocated linearly and re-used if invalidated
- fh = ring.registerFile(0);
+ fh = ring.registerFile(STDERR_FILENO);
EXPECT_GT(ring.getFiles().size(), 1);
EXPECT_EQ(*fh, 0);
- fh = ring.registerFile(1);
+ fh = ring.registerFile(STDOUT_FILENO);
EXPECT_EQ(*fh, 1);
- EXPECT_EQ(ring.getFiles()[1], 1);
+ EXPECT_GT(ring.getFiles().size(), 2);
+ EXPECT_EQ(ring.getFiles()[0], -1);
+ EXPECT_EQ(ring.getFiles()[1], STDOUT_FILENO);
// The first handle should have dropped and can be replaced
- fh = ring.registerFile(2);
+ fh = ring.registerFile(STDERR_FILENO);
EXPECT_EQ(*fh, 0);
- EXPECT_EQ(ring.getFiles()[0], 2);
+ EXPECT_GT(ring.getFiles().size(), 1);
+ EXPECT_EQ(ring.getFiles()[0], STDERR_FILENO);
+ EXPECT_EQ(ring.getFiles()[1], -1);
// We should be able to write to stderr via the fixed file and regular fd
- testFdWrite(2, 0);
+ testFdWrite(STDERR_FILENO, 0);
testFdWrite(*fh, IOSQE_FIXED_FILE);
// Without registration we should only be able to write to the regular fd
fh.reset();
- testFdWrite(2, 0);
+ testFdWrite(STDERR_FILENO, 0);
testFdWrite(*fh, IOSQE_FIXED_FILE, -EBADF);
std::vector<IoUring::FileHandle> fhs;
@@ -205,11 +209,11 @@
EXPECT_EQ(ring.getFiles().size(), 9);
for (size_t i = 0; i < 9; ++i)
{
- fhs.emplace_back(ring.registerFile(2));
+ fhs.emplace_back(ring.registerFile(STDERR_FILENO));
testFdWrite(fhs.back(), IOSQE_FIXED_FILE);
}
EXPECT_EQ(ring.getFiles().size(), 9);
- fhs.emplace_back(ring.registerFile(2));
+ fhs.emplace_back(ring.registerFile(STDERR_FILENO));
testFdWrite(fhs.back(), IOSQE_FIXED_FILE);
EXPECT_GE(ring.getFiles().size(), 10);
}