Metronome.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "Test.h"
  2. #include "Metronome.h"
  3. #include <Logging.h>
  4. #include <chrono>
  5. #include <ctime>
  6. #include <sstream>
  7. #include <string>
  8. #include <unistd.h>
  9. namespace Timer {
  10. namespace Test {
  11. int g_callbacks = 0;
  12. void Callback()
  13. {
  14. auto now = std::chrono::system_clock::now();
  15. auto in_time_t = std::chrono::system_clock::to_time_t(now);
  16. std::stringstream ss;
  17. char time[10];
  18. if (0 < strftime(time, sizeof(time), "%X", std::localtime(&in_time_t)))
  19. ss << time << " ";
  20. ss << "Callback Triggered" << std::endl;
  21. ++g_callbacks;
  22. Logging::Log(Logging::Severity::Info, ss.str());
  23. }
  24. bool TestMetronome()
  25. {
  26. std::stringstream ss;
  27. ss << "----------------------------------" << std::endl;
  28. ss << "| TestMetronome |" << std::endl;
  29. ss << "----------------------------------" << std::endl;
  30. Logging::Log(Logging::Severity::Info, ss.str());
  31. auto callback = std::bind(&Callback);
  32. int iterations = 3;
  33. int waitTime = 2;
  34. Metronome metronome(Metronome::Quantity::Seconds, waitTime);
  35. metronome.Connect(callback);
  36. auto now = std::chrono::system_clock::now();
  37. auto in_time_t = std::chrono::system_clock::to_time_t(now);
  38. ss.str("");
  39. char time[10];
  40. if (0 < strftime(time, sizeof(time), "%X", std::localtime(&in_time_t)))
  41. ss << time << " ";
  42. ss << "Start Metronome" << std::endl;
  43. Logging::Log(Logging::Severity::Info, ss.str());
  44. sleep((iterations * waitTime) + 1);
  45. ss.str("");
  46. ss << "==================================" << std::endl << std::endl;
  47. Logging::Log(Logging::Severity::Info, ss.str());
  48. return (g_callbacks == iterations);
  49. }
  50. } // namespace Test
  51. } // namespace Timer