Back to index

lightning-sunbird  0.9+nobinonly
TestServMgr.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "MyService.h"
00040 #include "nsXPCOM.h"
00041 #include "nsIServiceManager.h"
00042 #include "nsIComponentManager.h"
00043 #include <stdio.h>
00044 
00045 static NS_DEFINE_CID(kIMyServiceCID, NS_IMYSERVICE_CID);
00046 
00048 
00049 IMyService* myServ = NULL;
00050 
00051 nsresult
00052 BeginTest(int testNumber)
00053 {
00054     nsresult err;
00055     NS_ASSERTION(myServ == NULL, "myServ not reset");
00056     err = CallGetService(kIMyServiceCID, &myServ);
00057     return err;
00058 }
00059 
00060 nsresult
00061 EndTest(int testNumber)
00062 {
00063     nsresult err = NS_OK;
00064 
00065     if (myServ) {
00066         err = myServ->Doit();
00067         if (err != NS_OK) return err;
00068 
00069         NS_RELEASE(myServ);
00070     }
00071     
00072     printf("test %d succeeded\n", testNumber);
00073     return NS_OK;
00074 }
00075 
00076 nsresult
00077 SimpleTest(int testNumber)
00078 {
00079     // This test just gets a service, uses it and releases it.
00080 
00081     nsresult err;
00082     err = BeginTest(testNumber);
00083     if (err != NS_OK) return err;
00084     err = EndTest(testNumber);
00085     return err;
00086 }
00087 
00089 
00090 nsresult
00091 AsyncShutdown(int testNumber)
00092 {
00093     nsresult err = NS_OK;
00094 
00095     // If the AsyncShutdown was truly asynchronous and happened on another
00096     // thread, we'd have to protect all accesses to myServ throughout this
00097     // code with a monitor.
00098 
00099     // XXX-darin: say what?!?
00100 
00101     /*
00102     err = nsServiceManager::UnregisterService(kIMyServiceCID);
00103     if (err == NS_ERROR_SERVICE_NOT_AVAILABLE) {
00104         printf("async shutdown -- service not found\n");
00105         return NS_OK;
00106     }
00107     */
00108 
00109     return err;
00110 }
00111 
00112 nsresult
00113 AsyncNoShutdownTest(int testNumber)
00114 {
00115     // This test gets a service, and also gets an async request for shutdown,
00116     // but the service doesn't get shut down because some other client (who's
00117     // not participating in the async shutdown game as he should) is 
00118     // continuing to hang onto the service. This causes myServ variable to 
00119     // get set to NULL, but the service doesn't get unloaded right away as
00120     // it should.
00121 
00122     nsresult err;
00123 
00124     err = BeginTest(testNumber);
00125     if (err != NS_OK) return err;
00126 
00127     // Create some other user of kIMyServiceCID, preventing it from
00128     // really going away:
00129     IMyService* otherClient;
00130     err = CallGetService(kIMyServiceCID, &otherClient);
00131     if (err != NS_OK) return err;
00132 
00133     err = AsyncShutdown(testNumber);
00134     if (err != NS_OK) return err;
00135     err = EndTest(testNumber);
00136 
00137     // Finally, release the other client.
00138     NS_RELEASE(otherClient);
00139 
00140     return err;
00141 }
00142 
00144 int
00145 main(void)
00146 {
00147     nsresult err;
00148     int testNumber = 0;
00149 
00150     err = NS_InitXPCOM2(nsnull, nsnull, nsnull);
00151     if (NS_FAILED(err)) {
00152         printf("NS_InitXPCOM2 failed\n");
00153         return -1;
00154     }
00155 
00156     err = SimpleTest(++testNumber);
00157     if (err != NS_OK)
00158         goto error;
00159 
00160     err = AsyncNoShutdownTest(++testNumber);
00161     if (err != NS_OK)
00162         goto error;
00163 
00164     AsyncShutdown(++testNumber);
00165 
00166     printf("there was great success\n");
00167     return 0;
00168 
00169   error:
00170     printf("test %d failed\n", testNumber);
00171     return -1;
00172 }
00173