|
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
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 |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "TestHarness.h" |
|
7 |
|
8 #include "nsIThread.h" |
|
9 #include "nsITimer.h" |
|
10 |
|
11 #include "nsCOMPtr.h" |
|
12 #include "nsComponentManagerUtils.h" |
|
13 #include "nsServiceManagerUtils.h" |
|
14 #include "nsThreadUtils.h" |
|
15 #include "prinrval.h" |
|
16 #include "prmon.h" |
|
17 #include "prthread.h" |
|
18 #include "mozilla/Attributes.h" |
|
19 |
|
20 #include "mozilla/ReentrantMonitor.h" |
|
21 using namespace mozilla; |
|
22 |
|
23 typedef nsresult(*TestFuncPtr)(); |
|
24 |
|
25 class AutoTestThread |
|
26 { |
|
27 public: |
|
28 AutoTestThread() { |
|
29 nsCOMPtr<nsIThread> newThread; |
|
30 nsresult rv = NS_NewThread(getter_AddRefs(newThread)); |
|
31 if (NS_FAILED(rv)) |
|
32 return; |
|
33 |
|
34 newThread.swap(mThread); |
|
35 } |
|
36 |
|
37 ~AutoTestThread() { |
|
38 mThread->Shutdown(); |
|
39 } |
|
40 |
|
41 operator nsIThread*() const { |
|
42 return mThread; |
|
43 } |
|
44 |
|
45 nsIThread* operator->() const { |
|
46 return mThread; |
|
47 } |
|
48 |
|
49 private: |
|
50 nsCOMPtr<nsIThread> mThread; |
|
51 }; |
|
52 |
|
53 class AutoCreateAndDestroyReentrantMonitor |
|
54 { |
|
55 public: |
|
56 AutoCreateAndDestroyReentrantMonitor() { |
|
57 mReentrantMonitor = new ReentrantMonitor("TestTimers::AutoMon"); |
|
58 NS_ASSERTION(mReentrantMonitor, "Out of memory!"); |
|
59 } |
|
60 |
|
61 ~AutoCreateAndDestroyReentrantMonitor() { |
|
62 delete mReentrantMonitor; |
|
63 } |
|
64 |
|
65 operator ReentrantMonitor* () { |
|
66 return mReentrantMonitor; |
|
67 } |
|
68 |
|
69 private: |
|
70 ReentrantMonitor* mReentrantMonitor; |
|
71 }; |
|
72 |
|
73 class TimerCallback MOZ_FINAL : public nsITimerCallback |
|
74 { |
|
75 public: |
|
76 NS_DECL_THREADSAFE_ISUPPORTS |
|
77 |
|
78 TimerCallback(nsIThread** aThreadPtr, ReentrantMonitor* aReentrantMonitor) |
|
79 : mThreadPtr(aThreadPtr), mReentrantMonitor(aReentrantMonitor) { } |
|
80 |
|
81 NS_IMETHOD Notify(nsITimer* aTimer) { |
|
82 NS_ASSERTION(mThreadPtr, "Callback was not supposed to be called!"); |
|
83 nsCOMPtr<nsIThread> current(do_GetCurrentThread()); |
|
84 |
|
85 ReentrantMonitorAutoEnter mon(*mReentrantMonitor); |
|
86 |
|
87 NS_ASSERTION(!*mThreadPtr, "Timer called back more than once!"); |
|
88 *mThreadPtr = current; |
|
89 |
|
90 mon.Notify(); |
|
91 |
|
92 return NS_OK; |
|
93 } |
|
94 private: |
|
95 nsIThread** mThreadPtr; |
|
96 ReentrantMonitor* mReentrantMonitor; |
|
97 }; |
|
98 |
|
99 NS_IMPL_ISUPPORTS(TimerCallback, nsITimerCallback) |
|
100 |
|
101 nsresult |
|
102 TestTargetedTimers() |
|
103 { |
|
104 AutoCreateAndDestroyReentrantMonitor newMon; |
|
105 NS_ENSURE_TRUE(newMon, NS_ERROR_OUT_OF_MEMORY); |
|
106 |
|
107 AutoTestThread testThread; |
|
108 NS_ENSURE_TRUE(testThread, NS_ERROR_OUT_OF_MEMORY); |
|
109 |
|
110 nsresult rv; |
|
111 nsCOMPtr<nsITimer> timer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv); |
|
112 NS_ENSURE_SUCCESS(rv, rv); |
|
113 |
|
114 nsIEventTarget* target = static_cast<nsIEventTarget*>(testThread); |
|
115 |
|
116 rv = timer->SetTarget(target); |
|
117 NS_ENSURE_SUCCESS(rv, rv); |
|
118 |
|
119 nsIThread* notifiedThread = nullptr; |
|
120 |
|
121 nsCOMPtr<nsITimerCallback> callback = |
|
122 new TimerCallback(¬ifiedThread, newMon); |
|
123 NS_ENSURE_TRUE(callback, NS_ERROR_OUT_OF_MEMORY); |
|
124 |
|
125 rv = timer->InitWithCallback(callback, PR_MillisecondsToInterval(2000), |
|
126 nsITimer::TYPE_ONE_SHOT); |
|
127 NS_ENSURE_SUCCESS(rv, rv); |
|
128 |
|
129 ReentrantMonitorAutoEnter mon(*newMon); |
|
130 while (!notifiedThread) { |
|
131 mon.Wait(); |
|
132 } |
|
133 NS_ENSURE_TRUE(notifiedThread == testThread, NS_ERROR_FAILURE); |
|
134 |
|
135 return NS_OK; |
|
136 } |
|
137 |
|
138 nsresult |
|
139 TestTimerWithStoppedTarget() |
|
140 { |
|
141 AutoTestThread testThread; |
|
142 NS_ENSURE_TRUE(testThread, NS_ERROR_OUT_OF_MEMORY); |
|
143 |
|
144 nsresult rv; |
|
145 nsCOMPtr<nsITimer> timer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv); |
|
146 NS_ENSURE_SUCCESS(rv, rv); |
|
147 |
|
148 nsIEventTarget* target = static_cast<nsIEventTarget*>(testThread); |
|
149 |
|
150 rv = timer->SetTarget(target); |
|
151 NS_ENSURE_SUCCESS(rv, rv); |
|
152 |
|
153 // If this is called, we'll assert |
|
154 nsCOMPtr<nsITimerCallback> callback = |
|
155 new TimerCallback(nullptr, nullptr); |
|
156 NS_ENSURE_TRUE(callback, NS_ERROR_OUT_OF_MEMORY); |
|
157 |
|
158 rv = timer->InitWithCallback(callback, PR_MillisecondsToInterval(100), |
|
159 nsITimer::TYPE_ONE_SHOT); |
|
160 NS_ENSURE_SUCCESS(rv, rv); |
|
161 |
|
162 testThread->Shutdown(); |
|
163 |
|
164 PR_Sleep(400); |
|
165 |
|
166 return NS_OK; |
|
167 } |
|
168 |
|
169 int main(int argc, char** argv) |
|
170 { |
|
171 ScopedXPCOM xpcom("TestTimers"); |
|
172 NS_ENSURE_FALSE(xpcom.failed(), 1); |
|
173 |
|
174 static TestFuncPtr testsToRun[] = { |
|
175 TestTargetedTimers, |
|
176 TestTimerWithStoppedTarget |
|
177 }; |
|
178 static uint32_t testCount = sizeof(testsToRun) / sizeof(testsToRun[0]); |
|
179 |
|
180 for (uint32_t i = 0; i < testCount; i++) { |
|
181 nsresult rv = testsToRun[i](); |
|
182 NS_ENSURE_SUCCESS(rv, 1); |
|
183 } |
|
184 |
|
185 return 0; |
|
186 } |