timer_queue_test.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Portions Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. // borrowed from
  6. // http://www.crazygaze.com/blog/2016/03/24/portable-c-timer-queue/
  7. // Timer Queue
  8. //
  9. // License
  10. //
  11. // The source code in this article is licensed under the CC0 license, so feel
  12. // free
  13. // to copy, modify, share, do whatever you want with it.
  14. // No attribution is required, but Ill be happy if you do.
  15. // CC0 license
  16. // The person who associated a work with this deed has dedicated the work to the
  17. // public domain by waiving all of his or her rights to the work worldwide
  18. // under copyright law, including all related and neighboring rights, to the
  19. // extent allowed by law. You can copy, modify, distribute and perform the
  20. // work, even for
  21. // commercial purposes, all without asking permission. See Other Information
  22. // below.
  23. //
  24. #include "util/timer_queue.h"
  25. #include <future>
  26. #include "test_util/testharness.h"
  27. namespace ROCKSDB_NAMESPACE {
  28. namespace Timing {
  29. using Clock = std::chrono::high_resolution_clock;
  30. double now() {
  31. static auto start = Clock::now();
  32. return std::chrono::duration<double, std::milli>(Clock::now() - start)
  33. .count();
  34. }
  35. } // namespace Timing
  36. class TimerQueueTest : public testing::Test {};
  37. TEST_F(TimerQueueTest, BasicFunctionality) {
  38. TimerQueue q;
  39. double tnow = Timing::now();
  40. q.add(10000, [tnow](bool aborted) mutable {
  41. printf("T 1: %d, Elapsed %4.2fms\n", aborted, Timing::now() - tnow);
  42. return std::make_pair(false, 0);
  43. });
  44. q.add(10001, [tnow](bool aborted) mutable {
  45. printf("T 2: %d, Elapsed %4.2fms\n", aborted, Timing::now() - tnow);
  46. return std::make_pair(false, 0);
  47. });
  48. q.add(1000, [tnow](bool aborted) mutable {
  49. printf("T 3: %d, Elapsed %4.2fms\n", aborted, Timing::now() - tnow);
  50. return std::make_pair(!aborted, 1000);
  51. });
  52. auto id = q.add(2000, [tnow](bool aborted) mutable {
  53. printf("T 4: %d, Elapsed %4.2fms\n", aborted, Timing::now() - tnow);
  54. return std::make_pair(!aborted, 2000);
  55. });
  56. (void)id;
  57. // auto ret = q.cancel(id);
  58. // assert(ret == 1);
  59. // q.cancelAll();
  60. // Test passes if we can create and add timers without crashing
  61. ASSERT_TRUE(true);
  62. }
  63. } // namespace ROCKSDB_NAMESPACE
  64. int main(int argc, char** argv) {
  65. ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
  66. ::testing::InitGoogleTest(&argc, argv);
  67. return RUN_ALL_TESTS();
  68. }