diff options
Diffstat (limited to 'test/base/platform/unix/EventLoopTest.cpp')
-rw-r--r-- | test/base/platform/unix/EventLoopTest.cpp | 56 |
1 files changed, 55 insertions, 1 deletions
diff --git a/test/base/platform/unix/EventLoopTest.cpp b/test/base/platform/unix/EventLoopTest.cpp index f5936b2a..4660efb3 100644 --- a/test/base/platform/unix/EventLoopTest.cpp +++ b/test/base/platform/unix/EventLoopTest.cpp @@ -20,5 +20,59 @@ TEST_CASE("UnixTimerFile Work", "[unix][time]") { fds[0].fd = timer.GetReadFd(); fds[0].events = POLLIN; REQUIRE(::poll(fds, 1, test_miliseconds * 2) == 1); - REQUIRE((std::chrono::steady_clock::now() - start) > test_duration); + auto delay = std::chrono::steady_clock::now() - start; + REQUIRE(delay > test_duration); + REQUIRE(delay < std::chrono::milliseconds(500)); +} + +TEST_CASE("UnixEventLoop Timer", "[unix][time]") { + using namespace cru; + using namespace cru::platform::unix; + + UnixEventLoop loop; + + auto test_miliseconds = 300; + auto test_duration = std::chrono::milliseconds(test_miliseconds); + auto start = std::chrono::steady_clock::now(); + + int counter = 0; + + loop.SetTimeout( + [test_duration, start] { + auto delay = std::chrono::steady_clock::now() - start; + REQUIRE(delay > test_duration); + REQUIRE(delay < std::chrono::milliseconds(500)); + }, + test_duration); + + int timer_id; + timer_id = loop.SetInterval( + [&loop, timer_id, test_duration, start, &counter] { + switch (counter) { + case 0: { + auto delay = std::chrono::steady_clock::now() - start; + REQUIRE(delay > test_duration); + REQUIRE(delay < std::chrono::milliseconds(500)); + counter++; + break; + } + case 1: { + auto delay = std::chrono::steady_clock::now() - start; + REQUIRE(delay > test_duration * 2); + REQUIRE(delay < std::chrono::milliseconds(1000)); + counter++; + break; + } + default: { + loop.CancelTimer(timer_id); + loop.RequestQuit(); + break; + } + } + }, + test_duration); + + auto exit_code = loop.Run(); + REQUIRE(exit_code == 0); + REQUIRE(counter == 2); } |