test: split up long-running tests
gtests runs testcases sequentially as part of a test suite which is
causing long test runs. Split up the tests into separate files to
optimize test run duration.
Test suite definition is done via `suite.hpp`/`suite.cpp`.
There is a tradeoff here since the test folder structure now does not
directly mirror the source folder structure anymore. The folder name
is chosen to clarify which implementation file is being tested.
Timeouts inside testcases are not touched, it is assumed these were
chosen correctly.
In case of fdio_timed there is dependency on a folder, which is made
unique via `getpid()` call to avoid races.
Command: `time ninja -C build test`
Before: (as of current master branch, commit
`c6fee5a94bbb4b4fbb6212942f0f2cfa3049c255`)
```
real 0m18.581s
user 0m9.977s
sys 0m5.494s
```
After:
```
real 0m6.430s
user 0m10.021s
sys 0m5.501s
```
Change-Id: I2ba096cb8f9d8ffcc146448d22b7e75a1c25d103
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/test/async/fdio_timed.cpp b/test/async/fdio_timed.cpp
deleted file mode 100644
index 85c93b6..0000000
--- a/test/async/fdio_timed.cpp
+++ /dev/null
@@ -1,179 +0,0 @@
-#include <sdbusplus/async.hpp>
-
-#include <filesystem>
-#include <fstream>
-
-#include <gtest/gtest.h>
-
-using namespace std::literals;
-
-namespace fs = std::filesystem;
-
-class FdioTimedTest : public ::testing::Test
-{
- protected:
- enum class testWriteOperation
- {
- writeSync,
- writeAsync,
- writeSkip
- };
-
- const fs::path path = "/tmp/test_fdio_timed";
-
- FdioTimedTest()
- {
- if (!fs::exists(path))
- {
- fs::create_directory(path);
- }
-
- fd = inotify_init1(IN_NONBLOCK);
- EXPECT_NE(fd, -1) << "Error occurred during the inotify_init1, error: "
- << errno;
-
- wd = inotify_add_watch(fd, path.c_str(), IN_CLOSE_WRITE);
- EXPECT_NE(wd, -1)
- << "Error occurred during the inotify_add_watch, error: " << errno;
- fdioInstance = std::make_unique<sdbusplus::async::fdio>(
- *ctx, fd, std::chrono::microseconds(1000));
- }
-
- ~FdioTimedTest() noexcept override
- {
- if (fd != -1)
- {
- if (wd != -1)
- {
- inotify_rm_watch(fd, wd);
- }
- close(fd);
- }
- ctx.reset();
-
- if (fs::exists(path))
- {
- fs::remove_all(path);
- }
- }
-
- auto writeToFile() -> sdbusplus::async::task<>
- {
- std::ofstream outfile((path / "test_fdio.txt").native());
- EXPECT_TRUE(outfile.is_open())
- << "Error occurred during file open, error: " << errno;
- outfile << "Test fdio!" << std::endl;
- outfile.close();
- co_return;
- }
-
- auto testFdTimedEvents(bool& ran, testWriteOperation writeOperation,
- int testIterations) -> sdbusplus::async::task<>
- {
- for (int i = 0; i < testIterations; i++)
- {
- switch (writeOperation)
- {
- case testWriteOperation::writeSync:
- co_await writeToFile();
- break;
- case testWriteOperation::writeAsync:
- ctx->spawn(
- sdbusplus::async::sleep_for(*ctx, 1s) |
- stdexec::then([&]() { ctx->spawn(writeToFile()); }));
- break;
- case testWriteOperation::writeSkip:
- default:
- break;
- }
-
- bool receivedTimeout = false;
-
- try
- {
- co_await fdioInstance->next();
- }
- catch (const sdbusplus::async::fdio_timeout_exception& e)
- {
- receivedTimeout = true;
- }
-
- switch (writeOperation)
- {
- case testWriteOperation::writeSync:
- EXPECT_FALSE(receivedTimeout) << "Expected event";
- break;
- case testWriteOperation::writeAsync:
- case testWriteOperation::writeSkip:
- default:
- EXPECT_TRUE(receivedTimeout) << "Expected timeout";
- break;
- }
- }
- ran = true;
-
- co_return;
- }
-
- std::unique_ptr<sdbusplus::async::fdio> fdioInstance;
- std::optional<sdbusplus::async::context> ctx{std::in_place};
-
- private:
- int fd = -1;
- int wd = -1;
-};
-
-TEST_F(FdioTimedTest, TestWriteSkipWithTimeout)
-{
- bool ran = false;
- ctx->spawn(testFdTimedEvents(ran, testWriteOperation::writeSkip, 1));
- ctx->spawn(
- sdbusplus::async::sleep_for(*ctx, 2s) |
- sdbusplus::async::execution::then([&]() { ctx->request_stop(); }));
- ctx->run();
- EXPECT_TRUE(ran);
-}
-
-TEST_F(FdioTimedTest, TestWriteAsyncWithTimeout)
-{
- bool ran = false;
- ctx->spawn(testFdTimedEvents(ran, testWriteOperation::writeAsync, 1));
- ctx->spawn(
- sdbusplus::async::sleep_for(*ctx, 2s) |
- sdbusplus::async::execution::then([&]() { ctx->request_stop(); }));
- ctx->run();
- EXPECT_TRUE(ran);
-}
-
-TEST_F(FdioTimedTest, TestWriteAsyncWithTimeoutIterative)
-{
- bool ran = false;
- ctx->spawn(testFdTimedEvents(ran, testWriteOperation::writeAsync, 100));
- ctx->spawn(
- sdbusplus::async::sleep_for(*ctx, 2s) |
- sdbusplus::async::execution::then([&]() { ctx->request_stop(); }));
- ctx->run();
- EXPECT_TRUE(ran);
-}
-
-TEST_F(FdioTimedTest, TestWriteSync)
-{
- bool ran = false;
- ctx->spawn(testFdTimedEvents(ran, testWriteOperation::writeSync, 1));
- ctx->spawn(
- sdbusplus::async::sleep_for(*ctx, 2s) |
- sdbusplus::async::execution::then([&]() { ctx->request_stop(); }));
- ctx->run();
- EXPECT_TRUE(ran);
-}
-
-TEST_F(FdioTimedTest, TestWriteSyncIterative)
-{
- bool ran = false;
- ctx->spawn(testFdTimedEvents(ran, testWriteOperation::writeSync, 100));
- ctx->spawn(
- sdbusplus::async::sleep_for(*ctx, 2s) |
- sdbusplus::async::execution::then([&]() { ctx->request_stop(); }));
- ctx->run();
- EXPECT_TRUE(ran);
-}
diff --git a/test/async/fdio_timed/fdio_timed_write_async_with_timeout.cpp b/test/async/fdio_timed/fdio_timed_write_async_with_timeout.cpp
new file mode 100644
index 0000000..0121fde
--- /dev/null
+++ b/test/async/fdio_timed/fdio_timed_write_async_with_timeout.cpp
@@ -0,0 +1,18 @@
+#include "suite.hpp"
+
+#include <sdbusplus/async.hpp>
+
+#include <gtest/gtest.h>
+
+using namespace std::literals;
+
+TEST_F(FdioTimedTest, TestWriteAsyncWithTimeout)
+{
+ bool ran = false;
+ ctx->spawn(testFdTimedEvents(ran, testWriteOperation::writeAsync, 1));
+ ctx->spawn(
+ sdbusplus::async::sleep_for(*ctx, 2s) |
+ sdbusplus::async::execution::then([&]() { ctx->request_stop(); }));
+ ctx->run();
+ EXPECT_TRUE(ran);
+}
diff --git a/test/async/fdio_timed/fdio_timed_write_async_with_timeout_iterative.cpp b/test/async/fdio_timed/fdio_timed_write_async_with_timeout_iterative.cpp
new file mode 100644
index 0000000..a26c963
--- /dev/null
+++ b/test/async/fdio_timed/fdio_timed_write_async_with_timeout_iterative.cpp
@@ -0,0 +1,18 @@
+#include "suite.hpp"
+
+#include <sdbusplus/async.hpp>
+
+#include <gtest/gtest.h>
+
+using namespace std::literals;
+
+TEST_F(FdioTimedTest, TestWriteAsyncWithTimeoutIterative)
+{
+ bool ran = false;
+ ctx->spawn(testFdTimedEvents(ran, testWriteOperation::writeAsync, 100));
+ ctx->spawn(
+ sdbusplus::async::sleep_for(*ctx, 2s) |
+ sdbusplus::async::execution::then([&]() { ctx->request_stop(); }));
+ ctx->run();
+ EXPECT_TRUE(ran);
+}
diff --git a/test/async/fdio_timed/fdio_timed_write_skip_with_timeout.cpp b/test/async/fdio_timed/fdio_timed_write_skip_with_timeout.cpp
new file mode 100644
index 0000000..8fd83d1
--- /dev/null
+++ b/test/async/fdio_timed/fdio_timed_write_skip_with_timeout.cpp
@@ -0,0 +1,18 @@
+#include "suite.hpp"
+
+#include <sdbusplus/async.hpp>
+
+#include <gtest/gtest.h>
+
+using namespace std::literals;
+
+TEST_F(FdioTimedTest, TestWriteSkipWithTimeout)
+{
+ bool ran = false;
+ ctx->spawn(testFdTimedEvents(ran, testWriteOperation::writeSkip, 1));
+ ctx->spawn(
+ sdbusplus::async::sleep_for(*ctx, 2s) |
+ sdbusplus::async::execution::then([&]() { ctx->request_stop(); }));
+ ctx->run();
+ EXPECT_TRUE(ran);
+}
diff --git a/test/async/fdio_timed/fdio_timed_write_sync.cpp b/test/async/fdio_timed/fdio_timed_write_sync.cpp
new file mode 100644
index 0000000..49e3d22
--- /dev/null
+++ b/test/async/fdio_timed/fdio_timed_write_sync.cpp
@@ -0,0 +1,18 @@
+#include "suite.hpp"
+
+#include <sdbusplus/async.hpp>
+
+#include <gtest/gtest.h>
+
+using namespace std::literals;
+
+TEST_F(FdioTimedTest, TestWriteSync)
+{
+ bool ran = false;
+ ctx->spawn(testFdTimedEvents(ran, testWriteOperation::writeSync, 1));
+ ctx->spawn(
+ sdbusplus::async::sleep_for(*ctx, 2s) |
+ sdbusplus::async::execution::then([&]() { ctx->request_stop(); }));
+ ctx->run();
+ EXPECT_TRUE(ran);
+}
diff --git a/test/async/fdio_timed/fdio_timed_write_sync_iterative.cpp b/test/async/fdio_timed/fdio_timed_write_sync_iterative.cpp
new file mode 100644
index 0000000..4cf548c
--- /dev/null
+++ b/test/async/fdio_timed/fdio_timed_write_sync_iterative.cpp
@@ -0,0 +1,18 @@
+#include "suite.hpp"
+
+#include <sdbusplus/async.hpp>
+
+#include <gtest/gtest.h>
+
+using namespace std::literals;
+
+TEST_F(FdioTimedTest, TestWriteSyncIterative)
+{
+ bool ran = false;
+ ctx->spawn(testFdTimedEvents(ran, testWriteOperation::writeSync, 100));
+ ctx->spawn(
+ sdbusplus::async::sleep_for(*ctx, 2s) |
+ sdbusplus::async::execution::then([&]() { ctx->request_stop(); }));
+ ctx->run();
+ EXPECT_TRUE(ran);
+}
diff --git a/test/async/fdio_timed/meson.build b/test/async/fdio_timed/meson.build
new file mode 100644
index 0000000..57588be
--- /dev/null
+++ b/test/async/fdio_timed/meson.build
@@ -0,0 +1,20 @@
+
+async_tests = [
+ 'fdio_timed_write_async_with_timeout',
+ 'fdio_timed_write_async_with_timeout_iterative',
+ 'fdio_timed_write_skip_with_timeout',
+ 'fdio_timed_write_sync',
+ 'fdio_timed_write_sync_iterative',
+]
+
+foreach t : async_tests
+ test(
+ 'test_' + t.underscorify(),
+ executable(
+ 'test-' + t.underscorify(),
+ t + '.cpp',
+ 'suite.cpp',
+ dependencies: [gtest_dep, gmock_dep, sdbusplus_dep],
+ ),
+ )
+endforeach
diff --git a/test/async/fdio_timed/suite.cpp b/test/async/fdio_timed/suite.cpp
new file mode 100644
index 0000000..16397f6
--- /dev/null
+++ b/test/async/fdio_timed/suite.cpp
@@ -0,0 +1,113 @@
+#include "suite.hpp"
+
+#include <unistd.h>
+
+#include <sdbusplus/async.hpp>
+
+#include <filesystem>
+#include <format>
+#include <fstream>
+
+#include <gtest/gtest.h>
+
+using namespace std::literals;
+
+namespace fs = std::filesystem;
+
+FdioTimedTest::FdioTimedTest()
+{
+ constexpr auto path_base = "/tmp/test_fdio_timed";
+
+ path = std::format("{}{}", path_base, getpid());
+
+ if (!fs::exists(path))
+ {
+ fs::create_directory(path);
+ }
+
+ fd = inotify_init1(IN_NONBLOCK);
+ EXPECT_NE(fd, -1) << "Error occurred during the inotify_init1, error: "
+ << errno;
+
+ wd = inotify_add_watch(fd, path.c_str(), IN_CLOSE_WRITE);
+ EXPECT_NE(wd, -1) << "Error occurred during the inotify_add_watch, error: "
+ << errno;
+ fdioInstance = std::make_unique<sdbusplus::async::fdio>(
+ *ctx, fd, std::chrono::microseconds(1000));
+}
+
+FdioTimedTest::~FdioTimedTest() noexcept
+{
+ if (fd != -1)
+ {
+ if (wd != -1)
+ {
+ inotify_rm_watch(fd, wd);
+ }
+ close(fd);
+ }
+ ctx.reset();
+
+ if (fs::exists(path))
+ {
+ fs::remove_all(path);
+ }
+}
+
+auto FdioTimedTest::writeToFile() -> sdbusplus::async::task<>
+{
+ std::ofstream outfile((path / "test_fdio.txt").native());
+ EXPECT_TRUE(outfile.is_open())
+ << "Error occurred during file open, error: " << errno;
+ outfile << "Test fdio!" << std::endl;
+ outfile.close();
+ co_return;
+}
+
+auto FdioTimedTest::testFdTimedEvents(
+ bool& ran, testWriteOperation writeOperation, int testIterations)
+ -> sdbusplus::async::task<>
+{
+ for (int i = 0; i < testIterations; i++)
+ {
+ switch (writeOperation)
+ {
+ case testWriteOperation::writeSync:
+ co_await writeToFile();
+ break;
+ case testWriteOperation::writeAsync:
+ ctx->spawn(sdbusplus::async::sleep_for(*ctx, 1s) |
+ stdexec::then([&]() { ctx->spawn(writeToFile()); }));
+ break;
+ case testWriteOperation::writeSkip:
+ default:
+ break;
+ }
+
+ bool receivedTimeout = false;
+
+ try
+ {
+ co_await fdioInstance->next();
+ }
+ catch (const sdbusplus::async::fdio_timeout_exception& e)
+ {
+ receivedTimeout = true;
+ }
+
+ switch (writeOperation)
+ {
+ case testWriteOperation::writeSync:
+ EXPECT_FALSE(receivedTimeout) << "Expected event";
+ break;
+ case testWriteOperation::writeAsync:
+ case testWriteOperation::writeSkip:
+ default:
+ EXPECT_TRUE(receivedTimeout) << "Expected timeout";
+ break;
+ }
+ }
+ ran = true;
+
+ co_return;
+}
diff --git a/test/async/fdio_timed/suite.hpp b/test/async/fdio_timed/suite.hpp
new file mode 100644
index 0000000..a6282e2
--- /dev/null
+++ b/test/async/fdio_timed/suite.hpp
@@ -0,0 +1,42 @@
+#pragma once
+
+#include <unistd.h>
+
+#include <sdbusplus/async.hpp>
+
+#include <filesystem>
+
+#include <gtest/gtest.h>
+
+using namespace std::literals;
+
+namespace fs = std::filesystem;
+
+class FdioTimedTest : public ::testing::Test
+{
+ protected:
+ enum class testWriteOperation
+ {
+ writeSync,
+ writeAsync,
+ writeSkip
+ };
+
+ fs::path path;
+
+ FdioTimedTest();
+
+ ~FdioTimedTest() noexcept override;
+
+ auto writeToFile() -> sdbusplus::async::task<>;
+
+ auto testFdTimedEvents(bool& ran, testWriteOperation writeOperation,
+ int testIterations) -> sdbusplus::async::task<>;
+
+ std::unique_ptr<sdbusplus::async::fdio> fdioInstance;
+ std::optional<sdbusplus::async::context> ctx{std::in_place};
+
+ private:
+ int fd = -1;
+ int wd = -1;
+};
diff --git a/test/async/meson.build b/test/async/meson.build
new file mode 100644
index 0000000..d3edd65
--- /dev/null
+++ b/test/async/meson.build
@@ -0,0 +1,15 @@
+
+async_tests = ['context', 'fdio', 'mutex', 'task', 'timer', 'watchdog']
+
+foreach t : async_tests
+ test(
+ 'test_' + t.underscorify(),
+ executable(
+ 'test-' + t.underscorify(),
+ t + '.cpp',
+ dependencies: [gtest_dep, gmock_dep, sdbusplus_dep],
+ ),
+ )
+endforeach
+
+subdir('fdio_timed')