Back to index

lightning-sunbird  0.9+nobinonly
TestJarCache.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla Communicator client code, released
00017  * March 31, 1998.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 1999
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *   John Bandhauer <jband@netscape.com>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 
00042 /*
00043  * This is a simple multithreaded test for the jar cache. Inorder to run it
00044  * you must create the zip files listed in "filenames" below. 
00045  */
00046 
00047 
00048 #include "nsISupports.h"
00049 #include "nsIServiceManager.h"
00050 #include "nsCRT.h"
00051 #include "nsMemory.h"
00052 #include "nsAutoLock.h"
00053 #include "nsIRunnable.h"
00054 #include "nsIThread.h"
00055 #include "nsIZipReader.h"
00056 #include "nsILocalFile.h"
00057 
00058 #include <stdio.h>
00059 #include <stdlib.h>
00060 
00061 static char** filenames; 
00062 
00063 #define ZIP_COUNT    8
00064 #define CACHE_SIZE   4
00065 #define THREAD_COUNT 6
00066 #define THREAD_LOOP_COUNT 1000
00067 
00068 static nsCOMPtr<nsILocalFile> files[ZIP_COUNT];
00069 
00070 static const char gCacheContractID[] = "@mozilla.org/libjar/zip-reader-cache;1";
00071 static const PRUint32 gCacheSize = 4;
00072 
00073 nsCOMPtr<nsIZipReaderCache> gCache = nsnull;
00074 
00075 static nsIZipReader* GetZipReader(nsILocalFile* file)
00076 {
00077     if(!gCache)
00078     {
00079         gCache = do_CreateInstance(gCacheContractID);
00080         if(!gCache || NS_FAILED(gCache->Init(CACHE_SIZE)))
00081             return nsnull;
00082     }
00083 
00084     nsIZipReader* reader = nsnull;
00085 
00086     if(!file || NS_FAILED(gCache->GetZip(file, &reader)))
00087         return nsnull;
00088 
00089     return reader;
00090 }
00091 
00092 /***************************************************************************/
00093 
00094 class TestThread : public nsIRunnable
00095 {
00096 public:
00097     NS_DECL_ISUPPORTS
00098     NS_DECL_NSIRUNNABLE
00099 
00100     TestThread();
00101     virtual ~TestThread();
00102 
00103 private:
00104     PRUint32 mID;
00105     static PRUint32 gCounter;
00106 };
00107 
00108 NS_IMPL_THREADSAFE_ISUPPORTS1(TestThread, nsIRunnable)
00109 
00110 PRUint32 TestThread::gCounter = 0;
00111 
00112 TestThread::TestThread()
00113     : mID(++gCounter)
00114 {
00115 }
00116 
00117 TestThread::~TestThread()
00118 {
00119 }
00120 
00121 NS_IMETHODIMP
00122 TestThread::Run()
00123 {
00124     printf("thread %d started\n", mID);
00125     
00126     nsCOMPtr<nsIZipReader> reader;
00127     int failure = 0;
00128     
00129     for(int i = 0; i < THREAD_LOOP_COUNT; i++)
00130     {
00131         int k = rand()%ZIP_COUNT;
00132         reader = dont_AddRef(GetZipReader(files[k]));
00133         if(!reader)
00134         {
00135             printf("thread %d failed to get reader for %s\n", mID, filenames[k]);
00136             failure = 1;
00137             break;         
00138         }
00139 
00140         //printf("thread %d got reader for %s\n", mID, filenames[k]);
00141 
00142         PR_Sleep(rand()%10);    
00143     }
00144     
00145     reader = nsnull;
00146 
00147     printf("thread %d finished\n", mID);
00148 
00149     if ( failure ) return NS_ERROR_FAILURE;
00150     return NS_OK;
00151 }
00152 
00153 /***************************************************************************/
00154 
00155 int main(int argc, char **argv)
00156 {
00157     nsresult rv;
00158     int i;
00159 
00160     if (ZIP_COUNT != (argc - 1)) 
00161     {
00162         printf("usage: TestJarCache ");
00163         for ( i = 0; i < ZIP_COUNT; i++)
00164             printf("file%1d ",i + 1);
00165         printf("\n");
00166         return 1;
00167     }
00168     filenames = argv + 1;
00169 
00170     rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
00171     if(NS_FAILED(rv)) 
00172     {
00173         printf("NS_InitXPCOM failed!\n");
00174         return 1;
00175     }
00176 
00177     // construct the cache
00178     nsIZipReader* bogus = GetZipReader(nsnull);
00179 
00180 
00181     for(i = 0; i < ZIP_COUNT; i++)
00182     {
00183         PRBool exists;
00184         rv = NS_NewLocalFile(filenames[i], PR_FALSE, getter_AddRefs(files[i]));
00185         if(NS_FAILED(rv) || NS_FAILED(files[i]->Exists(&exists)) || !exists) 
00186         {   
00187             printf("Couldn't find %s\n", filenames[i]);
00188             return 1;
00189         }
00190     }
00191 
00192     nsCOMPtr<nsIThread> threads[THREAD_COUNT];
00193 
00194     for(i = 0; i < THREAD_COUNT; i++)
00195     {
00196         rv = NS_NewThread(getter_AddRefs(threads[i]),
00197                           new TestThread(), 
00198                           0, PR_JOINABLE_THREAD);
00199         if(NS_FAILED(rv)) 
00200         {
00201             printf("NS_NewThread failed!\n");
00202             return 1;
00203         }
00204         PR_Sleep(10);
00205     }
00206 
00207     printf("all threads created\n");
00208 
00209     for(i = 0; i < THREAD_COUNT; i++)
00210     {
00211         if(threads[i])
00212         {
00213             threads[i]->Join();
00214             threads[i] = nsnull;
00215         }
00216     }
00217 
00218     for(i = 0; i < ZIP_COUNT; i++)
00219         files[i] = nsnull;
00220 
00221     // kill the cache
00222     gCache = nsnull;
00223 
00224     rv = NS_ShutdownXPCOM(nsnull);
00225     if(NS_FAILED(rv)) 
00226     {
00227         printf("NS_ShutdownXPCOM failed!\n");
00228         return 1;
00229     }
00230 
00231     printf("done\n");
00232 
00233     return 0;
00234 }    
00235 
00236