1 /* -*- Mode: C++; tab-width: 4; 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
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Pierre Phaneuf <pp@ludusdesign.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "MyService.h"
41 #include "nsIServiceManager.h"
42 #include "nsIComponentManager.h"
43 #include "nsServiceManagerUtils.h"
46 static NS_DEFINE_CID(kIMyServiceCID
, NS_IMYSERVICE_CID
);
48 ////////////////////////////////////////////////////////////////////////////////
50 IMyService
* myServ
= NULL
;
53 BeginTest(int testNumber
)
56 NS_ASSERTION(myServ
== NULL
, "myServ not reset");
57 err
= CallGetService(kIMyServiceCID
, &myServ
);
62 EndTest(int testNumber
)
68 if (err
!= NS_OK
) return err
;
73 printf("test %d succeeded\n", testNumber
);
78 SimpleTest(int testNumber
)
80 // This test just gets a service, uses it and releases it.
83 err
= BeginTest(testNumber
);
84 if (err
!= NS_OK
) return err
;
85 err
= EndTest(testNumber
);
89 ////////////////////////////////////////////////////////////////////////////////
92 AsyncShutdown(int testNumber
)
96 // If the AsyncShutdown was truly asynchronous and happened on another
97 // thread, we'd have to protect all accesses to myServ throughout this
98 // code with a monitor.
100 // XXX-darin: say what?!?
103 err = nsServiceManager::UnregisterService(kIMyServiceCID);
104 if (err == NS_ERROR_SERVICE_NOT_AVAILABLE) {
105 printf("async shutdown -- service not found\n");
114 AsyncNoShutdownTest(int testNumber
)
116 // This test gets a service, and also gets an async request for shutdown,
117 // but the service doesn't get shut down because some other client (who's
118 // not participating in the async shutdown game as he should) is
119 // continuing to hang onto the service. This causes myServ variable to
120 // get set to NULL, but the service doesn't get unloaded right away as
125 err
= BeginTest(testNumber
);
126 if (err
!= NS_OK
) return err
;
128 // Create some other user of kIMyServiceCID, preventing it from
129 // really going away:
130 IMyService
* otherClient
;
131 err
= CallGetService(kIMyServiceCID
, &otherClient
);
132 if (err
!= NS_OK
) return err
;
134 err
= AsyncShutdown(testNumber
);
135 if (err
!= NS_OK
) return err
;
136 err
= EndTest(testNumber
);
138 // Finally, release the other client.
139 NS_RELEASE(otherClient
);
144 ////////////////////////////////////////////////////////////////////////////////
148 nsresult err
, rv
= 0;
151 err
= NS_InitXPCOM2(nsnull
, nsnull
, nsnull
);
152 if (NS_FAILED(err
)) {
153 printf("NS_InitXPCOM2 failed\n");
157 err
= SimpleTest(++testNumber
);
159 printf("test %d failed\n", testNumber
);
164 err
= AsyncNoShutdownTest(++testNumber
);
166 printf("test %d failed\n", testNumber
);
172 err
= AsyncShutdown(++testNumber
);
174 printf("test %d failed\n", testNumber
);
180 printf("there was great success\n");
182 err
= NS_ShutdownXPCOM(nsnull
);
183 NS_ASSERTION(NS_SUCCEEDED(err
), "NS_ShutdownXPCOM failed");
188 ////////////////////////////////////////////////////////////////////////////////