CLOSED TREE: TraceMonkey merge head. (a=blockers)
[mozilla-central.git] / xpcom / tests / TestRacingServiceManager.cpp
bloba2772bcfbe54f3b2843533e77d4e8a8397334e3e
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is Racing Service Manager Test Code.
17 * The Initial Developer of the Original Code is
18 * Ben Turner <bent.mozilla@gmail.com>.
19 * Portions created by the Initial Developer are Copyright (C) 2008
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "TestHarness.h"
40 #include "nsIFactory.h"
41 #include "mozilla/Module.h"
42 #include "nsXULAppAPI.h"
43 #include "nsIThread.h"
44 #include "nsIComponentRegistrar.h"
46 #include "nsAutoPtr.h"
47 #include "nsThreadUtils.h"
48 #include "nsXPCOMCIDInternal.h"
49 #include "prmon.h"
51 #include "mozilla/Monitor.h"
52 using namespace mozilla;
54 #ifdef DEBUG
55 #define TEST_ASSERTION(_test, _msg) \
56 NS_ASSERTION(_test, _msg);
57 #else
58 #define TEST_ASSERTION(_test, _msg) \
59 PR_BEGIN_MACRO \
60 if (!(_test)) { \
61 NS_DebugBreak(NS_DEBUG_ABORT, _msg, #_test, __FILE__, __LINE__); \
62 } \
63 PR_END_MACRO
64 #endif
66 /* f93f6bdc-88af-42d7-9d64-1b43c649a3e5 */
67 #define FACTORY_CID1 \
68 { \
69 0xf93f6bdc, \
70 0x88af, \
71 0x42d7, \
72 { 0x9d, 0x64, 0x1b, 0x43, 0xc6, 0x49, 0xa3, 0xe5 } \
74 NS_DEFINE_CID(kFactoryCID1, FACTORY_CID1);
76 /* ef38ad65-6595-49f0-8048-e819f81d15e2 */
77 #define FACTORY_CID2 \
78 { \
79 0xef38ad65, \
80 0x6595, \
81 0x49f0, \
82 { 0x80, 0x48, 0xe8, 0x19, 0xf8, 0x1d, 0x15, 0xe2 } \
84 NS_DEFINE_CID(kFactoryCID2, FACTORY_CID2);
86 #define FACTORY_CONTRACTID \
87 "TestRacingThreadManager/factory;1"
89 PRInt32 gComponent1Count = 0;
90 PRInt32 gComponent2Count = 0;
92 Monitor* gMonitor = nsnull;
94 PRBool gCreateInstanceCalled = PR_FALSE;
95 PRBool gMainThreadWaiting = PR_FALSE;
97 class AutoCreateAndDestroyMonitor
99 public:
100 AutoCreateAndDestroyMonitor(Monitor** aMonitorPtr)
101 : mMonitorPtr(aMonitorPtr) {
102 *aMonitorPtr =
103 new Monitor("TestRacingServiceManager::AutoMon");
104 TEST_ASSERTION(*aMonitorPtr, "Out of memory!");
107 ~AutoCreateAndDestroyMonitor() {
108 if (*mMonitorPtr) {
109 delete *mMonitorPtr;
110 *mMonitorPtr = nsnull;
114 private:
115 Monitor** mMonitorPtr;
118 class Factory : public nsIFactory
120 public:
121 NS_DECL_ISUPPORTS
123 Factory() : mFirstComponentCreated(PR_FALSE) { }
125 NS_IMETHOD CreateInstance(nsISupports* aDelegate,
126 const nsIID& aIID,
127 void** aResult);
129 NS_IMETHOD LockFactory(PRBool aLock) {
130 return NS_OK;
133 PRBool mFirstComponentCreated;
136 NS_IMPL_THREADSAFE_ISUPPORTS1(Factory, nsIFactory)
138 class Component1 : public nsISupports
140 public:
141 NS_DECL_ISUPPORTS
143 Component1() {
144 // This is the real test - make sure that only one instance is ever created.
145 PRInt32 count = PR_AtomicIncrement(&gComponent1Count);
146 TEST_ASSERTION(count == 1, "Too many components created!");
150 NS_IMPL_THREADSAFE_ADDREF(Component1)
151 NS_IMPL_THREADSAFE_RELEASE(Component1)
153 NS_INTERFACE_MAP_BEGIN(Component1)
154 NS_INTERFACE_MAP_ENTRY(Component1)
155 NS_INTERFACE_MAP_ENTRY(nsISupports)
156 NS_INTERFACE_MAP_END
158 class Component2 : public nsISupports
160 public:
161 NS_DECL_ISUPPORTS
163 Component2() {
164 // This is the real test - make sure that only one instance is ever created.
165 PRInt32 count = PR_AtomicIncrement(&gComponent2Count);
166 TEST_ASSERTION(count == 1, "Too many components created!");
170 NS_IMPL_THREADSAFE_ADDREF(Component2)
171 NS_IMPL_THREADSAFE_RELEASE(Component2)
173 NS_INTERFACE_MAP_BEGIN(Component2)
174 NS_INTERFACE_MAP_ENTRY(Component2)
175 NS_INTERFACE_MAP_ENTRY(nsISupports)
176 NS_INTERFACE_MAP_END
178 NS_IMETHODIMP
179 Factory::CreateInstance(nsISupports* aDelegate,
180 const nsIID& aIID,
181 void** aResult)
183 // Make sure that the second thread beat the main thread to the getService
184 // call.
185 TEST_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
188 MonitorAutoEnter mon(*gMonitor);
190 gCreateInstanceCalled = PR_TRUE;
191 mon.Notify();
193 mon.Wait(PR_MillisecondsToInterval(3000));
196 NS_ENSURE_FALSE(aDelegate, NS_ERROR_NO_AGGREGATION);
197 NS_ENSURE_ARG_POINTER(aResult);
199 nsCOMPtr<nsISupports> instance;
201 if (!mFirstComponentCreated) {
202 instance = new Component1();
204 else {
205 instance = new Component2();
207 NS_ENSURE_TRUE(instance, NS_ERROR_OUT_OF_MEMORY);
209 nsresult rv = instance->QueryInterface(aIID, aResult);
210 NS_ENSURE_SUCCESS(rv, rv);
212 return NS_OK;
215 class Runnable : public nsRunnable
217 public:
218 NS_DECL_NSIRUNNABLE
220 Runnable() : mFirstRunnableDone(PR_FALSE) { }
222 PRBool mFirstRunnableDone;
225 NS_IMETHODIMP
226 Runnable::Run()
229 MonitorAutoEnter mon(*gMonitor);
231 while (!gMainThreadWaiting) {
232 mon.Wait();
236 nsresult rv;
237 nsCOMPtr<nsISupports> component;
239 if (!mFirstRunnableDone) {
240 component = do_GetService(kFactoryCID1, &rv);
242 else {
243 component = do_GetService(FACTORY_CONTRACTID, &rv);
245 TEST_ASSERTION(NS_SUCCEEDED(rv), "GetService failed!");
247 return NS_OK;
250 static Factory* gFactory;
252 static already_AddRefed<nsIFactory>
253 CreateFactory(const mozilla::Module& module, const mozilla::Module::CIDEntry& entry)
255 if (!gFactory) {
256 gFactory = new Factory();
257 NS_ADDREF(gFactory);
259 NS_ADDREF(gFactory);
260 return gFactory;
263 static const mozilla::Module::CIDEntry kLocalCIDs[] = {
264 { &kFactoryCID1, false, CreateFactory, NULL },
265 { &kFactoryCID2, false, CreateFactory, NULL },
266 { NULL }
269 static const mozilla::Module::ContractIDEntry kLocalContracts[] = {
270 { FACTORY_CONTRACTID, &kFactoryCID2 },
271 { NULL }
274 static const mozilla::Module kLocalModule = {
275 mozilla::Module::kVersion,
276 kLocalCIDs,
277 kLocalContracts
280 int main(int argc, char** argv)
282 nsresult rv;
283 XRE_AddStaticComponent(&kLocalModule);
285 ScopedXPCOM xpcom("RacingServiceManager");
286 NS_ENSURE_FALSE(xpcom.failed(), 1);
288 AutoCreateAndDestroyMonitor mon(&gMonitor);
290 nsRefPtr<Runnable> runnable = new Runnable();
291 NS_ENSURE_TRUE(runnable, 1);
293 // Run the classID test
294 nsCOMPtr<nsIThread> newThread;
295 rv = NS_NewThread(getter_AddRefs(newThread), runnable);
296 NS_ENSURE_SUCCESS(rv, 1);
299 MonitorAutoEnter mon(*gMonitor);
301 gMainThreadWaiting = PR_TRUE;
302 mon.Notify();
304 while (!gCreateInstanceCalled) {
305 mon.Wait();
309 nsCOMPtr<nsISupports> component(do_GetService(kFactoryCID1, &rv));
310 NS_ENSURE_SUCCESS(rv, 1);
312 // Reset for the contractID test
313 gMainThreadWaiting = gCreateInstanceCalled = PR_FALSE;
314 gFactory->mFirstComponentCreated = runnable->mFirstRunnableDone = PR_TRUE;
315 component = nsnull;
317 rv = newThread->Dispatch(runnable, NS_DISPATCH_NORMAL);
318 NS_ENSURE_SUCCESS(rv, 1);
321 MonitorAutoEnter mon(*gMonitor);
323 gMainThreadWaiting = PR_TRUE;
324 mon.Notify();
326 while (!gCreateInstanceCalled) {
327 mon.Wait();
331 component = do_GetService(FACTORY_CONTRACTID, &rv);
332 NS_ENSURE_SUCCESS(rv, 1);
334 NS_RELEASE(gFactory);
336 return 0;