summaryrefslogtreecommitdiff
path: root/tests/timed_events.cpp
diff options
context:
space:
mode:
authorVasudev Kamath <vasudev@copyninja.info>2016-10-23 21:09:41 +0530
committerVasudev Kamath <vasudev@copyninja.info>2016-10-23 21:09:41 +0530
commit4e4de7284e6e4d89d182ea459823bbec1e408842 (patch)
tree47e0ed5216b48649b138f168f61fddca2b0c076a /tests/timed_events.cpp
parentdfb3a6edfacf2f16a8a63690b3e8058b6295d1a3 (diff)
parenteda4b75b1cff83336e87da90efca9fd6b4ced2c7 (diff)
downloadbiboumi-4e4de7284e6e4d89d182ea459823bbec1e408842.tar.gz
biboumi-4e4de7284e6e4d89d182ea459823bbec1e408842.tar.bz2
biboumi-4e4de7284e6e4d89d182ea459823bbec1e408842.tar.xz
biboumi-4e4de7284e6e4d89d182ea459823bbec1e408842.zip
Updated version 3.0 from 'upstream/3.0'
with Debian dir 0f18938e98f5a466f36719f60cef0490163ab845
Diffstat (limited to 'tests/timed_events.cpp')
-rw-r--r--tests/timed_events.cpp62
1 files changed, 62 insertions, 0 deletions
diff --git a/tests/timed_events.cpp b/tests/timed_events.cpp
new file mode 100644
index 0000000..d63abef
--- /dev/null
+++ b/tests/timed_events.cpp
@@ -0,0 +1,62 @@
+#include "catch.hpp"
+
+#include <utils/timed_events.hpp>
+
+/**
+ * Let Catch know how to display std::chrono::duration values
+ */
+namespace Catch
+{
+ template<typename Rep, typename Period> struct StringMaker<std::chrono::duration<Rep, Period>>
+ {
+ static std::string convert(const std::chrono::duration<Rep, Period>& value)
+ {
+ return std::to_string(std::chrono::duration_cast<std::chrono::milliseconds>(value).count()) + "ms";
+ }
+ };
+}
+
+/**
+ * TODO, use a mock clock instead of relying on the real time with a sleep:
+ * it’s unreliable on heavy load.
+ */
+#include <thread>
+
+TEST_CASE("Test timed event expiration")
+{
+ SECTION("Check what happens when there is no events")
+ {
+ CHECK(TimedEventsManager::instance().get_timeout() == utils::no_timeout);
+ CHECK(TimedEventsManager::instance().execute_expired_events() == 0);
+ }
+
+ // Add a single event
+ TimedEventsManager::instance().add_event(TimedEvent(std::chrono::steady_clock::now() + 50ms, [](){}));
+
+ // The event should not yet be expired
+ CHECK(TimedEventsManager::instance().get_timeout() > 0ms);
+ CHECK(TimedEventsManager::instance().execute_expired_events() == 0);
+
+ std::chrono::milliseconds timoute = TimedEventsManager::instance().get_timeout();
+ INFO("Sleeping for " << timoute.count() << "ms");
+ std::this_thread::sleep_for(timoute + 1ms);
+
+ // Event is now expired
+ CHECK(TimedEventsManager::instance().execute_expired_events() == 1);
+ CHECK(TimedEventsManager::instance().get_timeout() == utils::no_timeout);
+}
+
+TEST_CASE("Test timed event cancellation")
+{
+ auto now = std::chrono::steady_clock::now();
+ TimedEventsManager::instance().add_event(TimedEvent(now + 100ms, [](){ }, "un"));
+ TimedEventsManager::instance().add_event(TimedEvent(now + 200ms, [](){ }, "deux"));
+ TimedEventsManager::instance().add_event(TimedEvent(now + 300ms, [](){ }, "deux"));
+
+ CHECK(TimedEventsManager::instance().get_timeout() > 0ms);
+ CHECK(TimedEventsManager::instance().size() == 3);
+ CHECK(TimedEventsManager::instance().cancel("un") == 1);
+ CHECK(TimedEventsManager::instance().size() == 2);
+ CHECK(TimedEventsManager::instance().cancel("deux") == 2);
+ CHECK(TimedEventsManager::instance().get_timeout() == utils::no_timeout);
+}