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