xref: /openbmc/sdbusplus/test/async/fdio_timed/suite.cpp (revision 2cd25e64299ecc20e2258727e836a31b7ce6fad3)
1 #include "suite.hpp"
2 
3 #include <unistd.h>
4 
5 #include <sdbusplus/async.hpp>
6 
7 #include <filesystem>
8 #include <format>
9 #include <fstream>
10 
11 #include <gtest/gtest.h>
12 
13 using namespace std::literals;
14 
15 namespace fs = std::filesystem;
16 
FdioTimedTest()17 FdioTimedTest::FdioTimedTest()
18 {
19     constexpr auto path_base = "/tmp/test_fdio_timed";
20 
21     path = std::format("{}{}", path_base, getpid());
22 
23     if (!fs::exists(path))
24     {
25         fs::create_directory(path);
26     }
27 
28     fd = inotify_init1(IN_NONBLOCK);
29     EXPECT_NE(fd, -1) << "Error occurred during the inotify_init1, error: "
30                       << errno;
31 
32     wd = inotify_add_watch(fd, path.c_str(), IN_CLOSE_WRITE);
33     EXPECT_NE(wd, -1) << "Error occurred during the inotify_add_watch, error: "
34                       << errno;
35     fdioInstance = std::make_unique<sdbusplus::async::fdio>(
36         *ctx, fd, std::chrono::microseconds(1000));
37 }
38 
~FdioTimedTest()39 FdioTimedTest::~FdioTimedTest() noexcept
40 {
41     if (fd != -1)
42     {
43         if (wd != -1)
44         {
45             inotify_rm_watch(fd, wd);
46         }
47         close(fd);
48     }
49     ctx.reset();
50 
51     if (fs::exists(path))
52     {
53         fs::remove_all(path);
54     }
55 }
56 
writeToFile()57 auto FdioTimedTest::writeToFile() -> sdbusplus::async::task<>
58 {
59     std::ofstream outfile((path / "test_fdio.txt").native());
60     EXPECT_TRUE(outfile.is_open())
61         << "Error occurred during file open, error: " << errno;
62     outfile << "Test fdio!" << std::endl;
63     outfile.close();
64     co_return;
65 }
66 
testFdTimedEvents(bool & ran,testWriteOperation writeOperation,int testIterations)67 auto FdioTimedTest::testFdTimedEvents(
68     bool& ran, testWriteOperation writeOperation, int testIterations)
69     -> sdbusplus::async::task<>
70 {
71     for (int i = 0; i < testIterations; i++)
72     {
73         switch (writeOperation)
74         {
75             case testWriteOperation::writeSync:
76                 co_await writeToFile();
77                 break;
78             case testWriteOperation::writeAsync:
79                 ctx->spawn(sdbusplus::async::sleep_for(*ctx, 1s) |
80                            stdexec::then([&]() { ctx->spawn(writeToFile()); }));
81                 break;
82             case testWriteOperation::writeSkip:
83             default:
84                 break;
85         }
86 
87         bool receivedTimeout = false;
88 
89         try
90         {
91             co_await fdioInstance->next();
92         }
93         catch (const sdbusplus::async::fdio_timeout_exception& e)
94         {
95             receivedTimeout = true;
96         }
97 
98         switch (writeOperation)
99         {
100             case testWriteOperation::writeSync:
101                 EXPECT_FALSE(receivedTimeout) << "Expected event";
102                 break;
103             case testWriteOperation::writeAsync:
104             case testWriteOperation::writeSkip:
105             default:
106                 EXPECT_TRUE(receivedTimeout) << "Expected timeout";
107                 break;
108         }
109     }
110     ran = true;
111 
112     co_return;
113 }
114