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
16 * The Original Code is Mozilla Communicator client code, released
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.
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"
52 #include "nsAutoLock.h"
53 #include "nsIRunnable.h"
54 #include "nsIThread.h"
55 #include "nsIZipReader.h"
56 #include "nsILocalFile.h"
61 static char** filenames
;
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
)
79 gCache
= do_CreateInstance(gCacheContractID
);
80 if(!gCache
|| NS_FAILED(gCache
->Init(CACHE_SIZE
)))
84 nsIZipReader
* reader
= nsnull
;
86 if(!file
|| NS_FAILED(gCache
->GetZip(file
, &reader
)))
92 /***************************************************************************/
94 class TestThread
: public nsIRunnable
101 virtual ~TestThread();
105 static PRUint32 gCounter
;
108 NS_IMPL_THREADSAFE_ISUPPORTS1(TestThread
, nsIRunnable
)
110 PRUint32
TestThread::gCounter
= 0;
112 TestThread::TestThread()
117 TestThread::~TestThread()
124 printf("thread %d started\n", mID
);
126 nsCOMPtr
<nsIZipReader
> reader
;
129 for(int i
= 0; i
< THREAD_LOOP_COUNT
; i
++)
131 int k
= rand()%ZIP_COUNT
;
132 reader
= dont_AddRef(GetZipReader(files
[k
]));
135 printf("thread %d failed to get reader for %s\n", mID
, filenames
[k
]);
140 //printf("thread %d got reader for %s\n", mID, filenames[k]);
147 printf("thread %d finished\n", mID
);
149 if ( failure
) return NS_ERROR_FAILURE
;
153 /***************************************************************************/
155 int main(int argc
, char **argv
)
160 if (ZIP_COUNT
!= (argc
- 1))
162 printf("usage: TestJarCache ");
163 for ( i
= 0; i
< ZIP_COUNT
; i
++)
164 printf("file%1d ",i
+ 1);
168 filenames
= argv
+ 1;
170 rv
= NS_InitXPCOM2(nsnull
, nsnull
, nsnull
);
173 printf("NS_InitXPCOM failed!\n");
177 // construct the cache
178 nsIZipReader
* bogus
= GetZipReader(nsnull
);
181 for(i
= 0; i
< ZIP_COUNT
; i
++)
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
]);
192 nsCOMPtr
<nsIThread
> threads
[THREAD_COUNT
];
194 for(i
= 0; i
< THREAD_COUNT
; i
++)
196 rv
= NS_NewThread(getter_AddRefs(threads
[i
]),
198 0, PR_JOINABLE_THREAD
);
201 printf("NS_NewThread failed!\n");
207 printf("all threads created\n");
209 for(i
= 0; i
< THREAD_COUNT
; i
++)
218 for(i
= 0; i
< ZIP_COUNT
; i
++)
224 rv
= NS_ShutdownXPCOM(nsnull
);
227 printf("NS_ShutdownXPCOM failed!\n");