Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / modules / libjar / test / TestJarCache.cpp
blob43abde0ebe19395a32d65ed0473e3b19465889a8
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is Mozilla Communicator client code, released
17 * March 31, 1998.
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1999
22 * the Initial Developer. All Rights Reserved.
24 * Contributor(s):
25 * John Bandhauer <jband@netscape.com>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
43 * This is a simple multithreaded test for the jar cache. Inorder to run it
44 * you must create the zip files listed in "filenames" below.
48 #include "nsISupports.h"
49 #include "nsIServiceManager.h"
50 #include "nsCRT.h"
51 #include "nsMemory.h"
52 #include "nsAutoLock.h"
53 #include "nsIRunnable.h"
54 #include "nsIThread.h"
55 #include "nsIZipReader.h"
56 #include "nsILocalFile.h"
58 #include <stdio.h>
59 #include <stdlib.h>
61 static char** filenames;
63 #define ZIP_COUNT 8
64 #define CACHE_SIZE 4
65 #define THREAD_COUNT 6
66 #define THREAD_LOOP_COUNT 1000
68 static nsCOMPtr<nsILocalFile> files[ZIP_COUNT];
70 static const char gCacheContractID[] = "@mozilla.org/libjar/zip-reader-cache;1";
71 static const PRUint32 gCacheSize = 4;
73 nsCOMPtr<nsIZipReaderCache> gCache = nsnull;
75 static nsIZipReader* GetZipReader(nsILocalFile* file)
77 if(!gCache)
79 gCache = do_CreateInstance(gCacheContractID);
80 if(!gCache || NS_FAILED(gCache->Init(CACHE_SIZE)))
81 return nsnull;
84 nsIZipReader* reader = nsnull;
86 if(!file || NS_FAILED(gCache->GetZip(file, &reader)))
87 return nsnull;
89 return reader;
92 /***************************************************************************/
94 class TestThread : public nsIRunnable
96 public:
97 NS_DECL_ISUPPORTS
98 NS_DECL_NSIRUNNABLE
100 TestThread();
101 virtual ~TestThread();
103 private:
104 PRUint32 mID;
105 static PRUint32 gCounter;
108 NS_IMPL_THREADSAFE_ISUPPORTS1(TestThread, nsIRunnable)
110 PRUint32 TestThread::gCounter = 0;
112 TestThread::TestThread()
113 : mID(++gCounter)
117 TestThread::~TestThread()
121 NS_IMETHODIMP
122 TestThread::Run()
124 printf("thread %d started\n", mID);
126 nsCOMPtr<nsIZipReader> reader;
127 int failure = 0;
129 for(int i = 0; i < THREAD_LOOP_COUNT; i++)
131 int k = rand()%ZIP_COUNT;
132 reader = dont_AddRef(GetZipReader(files[k]));
133 if(!reader)
135 printf("thread %d failed to get reader for %s\n", mID, filenames[k]);
136 failure = 1;
137 break;
140 //printf("thread %d got reader for %s\n", mID, filenames[k]);
142 PR_Sleep(rand()%10);
145 reader = nsnull;
147 printf("thread %d finished\n", mID);
149 if ( failure ) return NS_ERROR_FAILURE;
150 return NS_OK;
153 /***************************************************************************/
155 int main(int argc, char **argv)
157 nsresult rv;
158 int i;
160 if (ZIP_COUNT != (argc - 1))
162 printf("usage: TestJarCache ");
163 for ( i = 0; i < ZIP_COUNT; i++)
164 printf("file%1d ",i + 1);
165 printf("\n");
166 return 1;
168 filenames = argv + 1;
170 rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
171 if(NS_FAILED(rv))
173 printf("NS_InitXPCOM failed!\n");
174 return 1;
177 // construct the cache
178 nsIZipReader* bogus = GetZipReader(nsnull);
181 for(i = 0; i < ZIP_COUNT; i++)
183 PRBool exists;
184 rv = NS_NewLocalFile(filenames[i], PR_FALSE, getter_AddRefs(files[i]));
185 if(NS_FAILED(rv) || NS_FAILED(files[i]->Exists(&exists)) || !exists)
187 printf("Couldn't find %s\n", filenames[i]);
188 return 1;
192 nsCOMPtr<nsIThread> threads[THREAD_COUNT];
194 for(i = 0; i < THREAD_COUNT; i++)
196 rv = NS_NewThread(getter_AddRefs(threads[i]),
197 new TestThread(),
198 0, PR_JOINABLE_THREAD);
199 if(NS_FAILED(rv))
201 printf("NS_NewThread failed!\n");
202 return 1;
204 PR_Sleep(10);
207 printf("all threads created\n");
209 for(i = 0; i < THREAD_COUNT; i++)
211 if(threads[i])
213 threads[i]->Join();
214 threads[i] = nsnull;
218 for(i = 0; i < ZIP_COUNT; i++)
219 files[i] = nsnull;
221 // kill the cache
222 gCache = nsnull;
224 rv = NS_ShutdownXPCOM(nsnull);
225 if(NS_FAILED(rv))
227 printf("NS_ShutdownXPCOM failed!\n");
228 return 1;
231 printf("done\n");
233 return 0;