nrappkit_unittest.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. // Original author: ekr@rtfm.com
  6. #include <iostream>
  7. #include "nsThreadUtils.h"
  8. #include "nsXPCOM.h"
  9. // nrappkit includes
  10. extern "C" {
  11. #include "nr_api.h"
  12. #include "async_timer.h"
  13. }
  14. #include "runnable_utils.h"
  15. #define GTEST_HAS_RTTI 0
  16. #include "gtest/gtest.h"
  17. #include "gtest_utils.h"
  18. using namespace mozilla;
  19. namespace {
  20. class TimerTest : public MtransportTest {
  21. public:
  22. TimerTest() : MtransportTest(), handle_(nullptr), fired_(false) {}
  23. virtual ~TimerTest() {}
  24. int ArmTimer(int timeout) {
  25. int ret;
  26. test_utils_->sts_target()->Dispatch(
  27. WrapRunnableRet(&ret, this, &TimerTest::ArmTimer_w, timeout),
  28. NS_DISPATCH_SYNC);
  29. return ret;
  30. }
  31. int ArmCancelTimer(int timeout) {
  32. int ret;
  33. test_utils_->sts_target()->Dispatch(
  34. WrapRunnableRet(&ret, this, &TimerTest::ArmCancelTimer_w, timeout),
  35. NS_DISPATCH_SYNC);
  36. return ret;
  37. }
  38. int ArmTimer_w(int timeout) {
  39. return NR_ASYNC_TIMER_SET(timeout, cb, this, &handle_);
  40. }
  41. int ArmCancelTimer_w(int timeout) {
  42. int r;
  43. r = ArmTimer_w(timeout);
  44. if (r)
  45. return r;
  46. return CancelTimer_w();
  47. }
  48. int CancelTimer() {
  49. int ret;
  50. test_utils_->sts_target()->Dispatch(
  51. WrapRunnableRet(&ret, this, &TimerTest::CancelTimer_w),
  52. NS_DISPATCH_SYNC);
  53. return ret;
  54. }
  55. int CancelTimer_w() {
  56. return NR_async_timer_cancel(handle_);
  57. }
  58. int Schedule() {
  59. int ret;
  60. test_utils_->sts_target()->Dispatch(
  61. WrapRunnableRet(&ret, this, &TimerTest::Schedule_w),
  62. NS_DISPATCH_SYNC);
  63. return ret;
  64. }
  65. int Schedule_w() {
  66. NR_ASYNC_SCHEDULE(cb, this);
  67. return 0;
  68. }
  69. static void cb(NR_SOCKET r, int how, void *arg) {
  70. std::cerr << "Timer fired " << std::endl;
  71. TimerTest *t = static_cast<TimerTest *>(arg);
  72. t->fired_ = true;
  73. }
  74. protected:
  75. void *handle_;
  76. bool fired_;
  77. };
  78. }
  79. TEST_F(TimerTest, SimpleTimer) {
  80. ArmTimer(100);
  81. ASSERT_TRUE_WAIT(fired_, 1000);
  82. }
  83. TEST_F(TimerTest, CancelTimer) {
  84. ArmTimer(1000);
  85. CancelTimer();
  86. PR_Sleep(2000);
  87. ASSERT_FALSE(fired_);
  88. }
  89. TEST_F(TimerTest, CancelTimer0) {
  90. ArmCancelTimer(0);
  91. PR_Sleep(100);
  92. ASSERT_FALSE(fired_);
  93. }
  94. TEST_F(TimerTest, ScheduleTest) {
  95. Schedule();
  96. ASSERT_TRUE_WAIT(fired_, 1000);
  97. }