|
|
@@ -0,0 +1,140 @@
|
|
|
+#include "Test.h"
|
|
|
+#include "Timer.h"
|
|
|
+#include <Logging.h>
|
|
|
+#include <unistd.h>
|
|
|
+#include <algorithm>
|
|
|
+#include <chrono>
|
|
|
+#include <deque>
|
|
|
+#include <memory>
|
|
|
+#include <sstream>
|
|
|
+#include <string>
|
|
|
+
|
|
|
+
|
|
|
+namespace Timer {
|
|
|
+namespace Test {
|
|
|
+
|
|
|
+void print()
|
|
|
+{
|
|
|
+ Logging::Log(Logging::Severity::Info, "Print");
|
|
|
+}
|
|
|
+
|
|
|
+void TestTimerAbort()
|
|
|
+{
|
|
|
+ Timer timer;
|
|
|
+ std::function<void()> f_print = print;
|
|
|
+
|
|
|
+ std::stringstream ss;
|
|
|
+ ss << "---" << std::endl;
|
|
|
+ auto start = std::chrono::steady_clock::now();
|
|
|
+ std::clock_t begin = std::clock();
|
|
|
+ timer.StartContinuous(1000, f_print);
|
|
|
+ timer.Stop();
|
|
|
+ timer.Wait();
|
|
|
+ auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start);
|
|
|
+ ss << "Elapsed: " << elapsed.count() << std::endl;
|
|
|
+ ss << "---" << std::endl << std::endl;
|
|
|
+ Logging::Log(Logging::Severity::Info, ss.str());
|
|
|
+
|
|
|
+ ss.str("");
|
|
|
+ ss << "---" << std::endl;
|
|
|
+ start = std::chrono::steady_clock::now();
|
|
|
+ timer.StartContinuous(3000, f_print);
|
|
|
+ timer.Abort();
|
|
|
+ timer.Wait();
|
|
|
+ elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start);
|
|
|
+ ss << "Elapsed: " << elapsed.count() << std::endl;
|
|
|
+ ss << "---" << std::endl << std::endl;
|
|
|
+ Logging::Log(Logging::Severity::Info, ss.str());
|
|
|
+
|
|
|
+ ss.str("");
|
|
|
+ ss << "---" << std::endl;
|
|
|
+ start = std::chrono::steady_clock::now();
|
|
|
+ timer.StartContinuous(3000, f_print);
|
|
|
+ sleep(1);
|
|
|
+ timer.Abort();
|
|
|
+ timer.Wait();
|
|
|
+ elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start);
|
|
|
+ ss << "Elapsed: " << elapsed.count() << std::endl;
|
|
|
+ ss << "---" << std::endl << std::endl;
|
|
|
+ Logging::Log(Logging::Severity::Info, ss.str());
|
|
|
+}
|
|
|
+
|
|
|
+template<typename T, typename... Args>
|
|
|
+std::unique_ptr<T> make_unique(Args&&... args) {
|
|
|
+ return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
|
|
|
+}
|
|
|
+
|
|
|
+void OnTimerEnd(const std::string& type, int id)
|
|
|
+{
|
|
|
+ std::stringstream ss;
|
|
|
+ ss << "OnTimerEnd() " << type << " " << id << std::endl;
|
|
|
+ Logging::Log(Logging::Severity::Info, ss.str());
|
|
|
+}
|
|
|
+
|
|
|
+void StartNewTimer(std::deque<TimerPointer>& timers)
|
|
|
+{
|
|
|
+ int delay(2000);
|
|
|
+ std::string identifier("Blabla");
|
|
|
+ int id(10);
|
|
|
+
|
|
|
+ // Protect!
|
|
|
+ auto timer = make_unique<Timer>();
|
|
|
+ timer->StartSingle(delay, static_cast<std::function<void(const std::string&, int)>>(std::bind(&OnTimerEnd, std::placeholders::_1, std::placeholders::_2)), identifier, id);
|
|
|
+ timers.push_back(std::move(timer));
|
|
|
+}
|
|
|
+
|
|
|
+void Cleanup(std::deque<TimerPointer>& timers)
|
|
|
+{
|
|
|
+ timers.erase(std::remove_if(timers.begin(), timers.end(), [](TimerPointer& t){return !t->IsRunning();}), timers.end());
|
|
|
+}
|
|
|
+
|
|
|
+void TestTimerDeque()
|
|
|
+{
|
|
|
+ std::stringstream ss;
|
|
|
+ std::deque<TimerPointer> timers;
|
|
|
+ StartNewTimer(timers);
|
|
|
+ ss << "Count: " << timers.size() << std::endl;
|
|
|
+ Logging::Log(Logging::Severity::Info, ss.str());
|
|
|
+
|
|
|
+ ss.str("");
|
|
|
+ StartNewTimer(timers);
|
|
|
+ ss << "Count: " << timers.size() << std::endl;
|
|
|
+ Logging::Log(Logging::Severity::Info, ss.str());
|
|
|
+
|
|
|
+ sleep(5);
|
|
|
+
|
|
|
+ ss.str("");
|
|
|
+ ss << "Count: " << timers.size() << std::endl;
|
|
|
+ Logging::Log(Logging::Severity::Info, ss.str());
|
|
|
+
|
|
|
+ ss.str("");
|
|
|
+ Cleanup(timers);
|
|
|
+ ss << "Count: " << timers.size() << std::endl;
|
|
|
+ Logging::Log(Logging::Severity::Info, ss.str());
|
|
|
+}
|
|
|
+
|
|
|
+bool TestTimer()
|
|
|
+{
|
|
|
+ std::stringstream ss;
|
|
|
+ ss << "----------------------------------" << std::endl;
|
|
|
+ ss << "| TestTimer |" << std::endl;
|
|
|
+ ss << "----------------------------------" << std::endl;
|
|
|
+ Logging::Log(Logging::Severity::Info, ss.str());
|
|
|
+
|
|
|
+ TestTimerAbort();
|
|
|
+ TestTimerDeque();
|
|
|
+
|
|
|
+ ss.str("");
|
|
|
+ ss << "==================================" << std::endl << std::endl;
|
|
|
+ Logging::Log(Logging::Severity::Info, ss.str());
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+} // namespace Test
|
|
|
+} // namespace Timer
|
|
|
+
|
|
|
+/*
|
|
|
+
|
|
|
+
|
|
|
+*/
|