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
|
---|
4 | *
|
---|
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/
|
---|
9 | *
|
---|
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.
|
---|
14 | *
|
---|
15 | * The Original Code is mozilla.org code.
|
---|
16 | *
|
---|
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.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | * Pierre Phaneuf <[email protected]>
|
---|
24 | *
|
---|
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.
|
---|
36 | *
|
---|
37 | * ***** END LICENSE BLOCK ***** */
|
---|
38 |
|
---|
39 | #include "MyService.h"
|
---|
40 | #include "nsXPCOM.h"
|
---|
41 | #include "nsIServiceManager.h"
|
---|
42 | #include "nsIComponentManager.h"
|
---|
43 | #include <stdio.h>
|
---|
44 |
|
---|
45 | static NS_DEFINE_CID(kIMyServiceCID, NS_IMYSERVICE_CID);
|
---|
46 |
|
---|
47 | ////////////////////////////////////////////////////////////////////////////////
|
---|
48 |
|
---|
49 | IMyService* myServ = NULL;
|
---|
50 |
|
---|
51 | nsresult
|
---|
52 | BeginTest(int testNumber)
|
---|
53 | {
|
---|
54 | nsresult err;
|
---|
55 | NS_ASSERTION(myServ == NULL, "myServ not reset");
|
---|
56 | err = nsServiceManager::GetService(kIMyServiceCID, NS_GET_IID(IMyService),
|
---|
57 | (nsISupports**)&myServ);
|
---|
58 | return err;
|
---|
59 | }
|
---|
60 |
|
---|
61 | nsresult
|
---|
62 | EndTest(int testNumber)
|
---|
63 | {
|
---|
64 | nsresult err = NS_OK;
|
---|
65 |
|
---|
66 | if (myServ) {
|
---|
67 | err = myServ->Doit();
|
---|
68 | if (err != NS_OK) return err;
|
---|
69 |
|
---|
70 | err = nsServiceManager::ReleaseService(kIMyServiceCID, myServ);
|
---|
71 | if (err != NS_OK) return err;
|
---|
72 | myServ = NULL;
|
---|
73 | }
|
---|
74 |
|
---|
75 | printf("test %d succeeded\n", testNumber);
|
---|
76 | return NS_OK;
|
---|
77 | }
|
---|
78 |
|
---|
79 | nsresult
|
---|
80 | SimpleTest(int testNumber)
|
---|
81 | {
|
---|
82 | // This test just gets a service, uses it and releases it.
|
---|
83 |
|
---|
84 | nsresult err;
|
---|
85 | err = BeginTest(testNumber);
|
---|
86 | if (err != NS_OK) return err;
|
---|
87 | err = EndTest(testNumber);
|
---|
88 | return err;
|
---|
89 | }
|
---|
90 |
|
---|
91 | ////////////////////////////////////////////////////////////////////////////////
|
---|
92 |
|
---|
93 | nsresult
|
---|
94 | AsyncShutdown(int testNumber)
|
---|
95 | {
|
---|
96 | nsresult err;
|
---|
97 |
|
---|
98 | // If the AsyncShutdown was truly asynchronous and happened on another
|
---|
99 | // thread, we'd have to protect all accesses to myServ throughout this
|
---|
100 | // code with a monitor.
|
---|
101 |
|
---|
102 | err = nsServiceManager::UnregisterService(kIMyServiceCID);
|
---|
103 | if (err == NS_ERROR_SERVICE_NOT_AVAILABLE) {
|
---|
104 | printf("async shutdown -- service not found\n");
|
---|
105 | return NS_OK;
|
---|
106 | }
|
---|
107 | return err;
|
---|
108 | }
|
---|
109 |
|
---|
110 | nsresult
|
---|
111 | AsyncNoShutdownTest(int testNumber)
|
---|
112 | {
|
---|
113 | // This test gets a service, and also gets an async request for shutdown,
|
---|
114 | // but the service doesn't get shut down because some other client (who's
|
---|
115 | // not participating in the async shutdown game as he should) is
|
---|
116 | // continuing to hang onto the service. This causes myServ variable to
|
---|
117 | // get set to NULL, but the service doesn't get unloaded right away as
|
---|
118 | // it should.
|
---|
119 |
|
---|
120 | nsresult err;
|
---|
121 |
|
---|
122 | err = BeginTest(testNumber);
|
---|
123 | if (err != NS_OK) return err;
|
---|
124 |
|
---|
125 | // Create some other user of kIMyServiceCID, preventing it from
|
---|
126 | // really going away:
|
---|
127 | IMyService* otherClient;
|
---|
128 | err = nsServiceManager::GetService(kIMyServiceCID, NS_GET_IID(IMyService),
|
---|
129 | (nsISupports**)&otherClient);
|
---|
130 | if (err != NS_OK) return err;
|
---|
131 |
|
---|
132 | err = AsyncShutdown(testNumber);
|
---|
133 | if (err != NS_OK) return err;
|
---|
134 | err = EndTest(testNumber);
|
---|
135 |
|
---|
136 | // Finally, release the other client.
|
---|
137 | err = nsServiceManager::ReleaseService(kIMyServiceCID, otherClient);
|
---|
138 |
|
---|
139 | return err;
|
---|
140 | }
|
---|
141 |
|
---|
142 | ////////////////////////////////////////////////////////////////////////////////
|
---|
143 | int
|
---|
144 | main(void)
|
---|
145 | {
|
---|
146 | nsresult err;
|
---|
147 | int testNumber = 0;
|
---|
148 |
|
---|
149 | err = NS_InitXPCOM2(nsnull, nsnull, nsnull);
|
---|
150 | if (NS_FAILED(err)) {
|
---|
151 | printf("NS_InitXPCOM2 failed\n");
|
---|
152 | return -1;
|
---|
153 | }
|
---|
154 |
|
---|
155 | err = SimpleTest(++testNumber);
|
---|
156 | if (err != NS_OK)
|
---|
157 | goto error;
|
---|
158 |
|
---|
159 | err = AsyncNoShutdownTest(++testNumber);
|
---|
160 | if (err != NS_OK)
|
---|
161 | goto error;
|
---|
162 |
|
---|
163 | AsyncShutdown(++testNumber);
|
---|
164 |
|
---|
165 | printf("there was great success\n");
|
---|
166 | return 0;
|
---|
167 |
|
---|
168 | error:
|
---|
169 | printf("test %d failed\n", testNumber);
|
---|
170 | return -1;
|
---|
171 | }
|
---|
172 |
|
---|
173 | ////////////////////////////////////////////////////////////////////////////////
|
---|