1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
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 ExpirationTracker tests.
18 * The Initial Developer of the Original Code is Mozilla Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2007
20 * the Initial Developer. All Rights Reserved.
23 * Robert O'Callahan <robert@ocallahan.org>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * 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.
37 * ***** END LICENSE BLOCK ***** */
42 #include "nsExpirationTracker.h"
44 #include "nsAutoPtr.h"
46 #include "nsDirectoryServiceDefs.h"
47 #include "nsDirectoryServiceUtils.h"
48 #include "nsComponentManagerUtils.h"
50 #include "nsILocalFile.h"
52 #include "nsThreadUtils.h"
54 namespace TestExpirationTracker
{
57 Object() : mExpired(PR_FALSE
) { Touch(); }
58 void Touch() { mLastUsed
= PR_IntervalNow(); mExpired
= PR_FALSE
; }
60 nsExpirationState mExpiration
;
61 nsExpirationState
* GetExpirationState() { return &mExpiration
; }
63 PRIntervalTime mLastUsed
;
64 PRPackedBool mExpired
;
68 static PRUint32 periodMS
= 100;
69 static PRUint32 ops
= 1000;
70 static PRUint32 iterations
= 2;
71 static PRBool logging
= 0;
72 static PRUint32 sleepPeriodMS
= 50;
73 static PRUint32 slackMS
= 20; // allow this much error
75 static void SignalError() {
80 template <PRUint32 K
> class Tracker
: public nsExpirationTracker
<Object
,K
> {
82 Tracker() : nsExpirationTracker
<Object
,K
>(periodMS
) {
83 Object
* obj
= new Object();
84 mUniverse
.AppendElement(obj
);
85 LogAction(obj
, "Created");
88 nsTArray
<nsAutoArrayPtr
<Object
> > mUniverse
;
90 void LogAction(Object
* aObj
, const char* aAction
) {
92 printf("%d %p(%d): %s\n", PR_IntervalNow(), aObj
, aObj
->mLastUsed
, aAction
);
96 void DoRandomOperation() {
98 switch (rand() & 0x7) {
100 if (mUniverse
.Length() < 50) {
102 mUniverse
.AppendElement(obj
);
103 nsExpirationTracker
<Object
,K
>::AddObject(obj
);
104 LogAction(obj
, "Created and added");
109 if (mUniverse
.Length() < 50) {
111 mUniverse
.AppendElement(obj
);
112 LogAction(obj
, "Created");
117 obj
= mUniverse
[PRUint32(rand())%mUniverse
.Length()];
118 if (obj
->mExpiration
.IsTracked()) {
119 nsExpirationTracker
<Object
,K
>::RemoveObject(obj
);
120 LogAction(obj
, "Removed");
125 obj
= mUniverse
[PRUint32(rand())%mUniverse
.Length()];
126 if (!obj
->mExpiration
.IsTracked()) {
128 nsExpirationTracker
<Object
,K
>::AddObject(obj
);
129 LogAction(obj
, "Added");
134 obj
= mUniverse
[PRUint32(rand())%mUniverse
.Length()];
135 if (obj
->mExpiration
.IsTracked()) {
137 nsExpirationTracker
<Object
,K
>::MarkUsed(obj
);
138 LogAction(obj
, "Marked used");
146 void NotifyExpired(Object
* aObj
) {
147 LogAction(aObj
, "Expired");
148 PRIntervalTime now
= PR_IntervalNow();
149 PRUint32 timeDiffMS
= (now
- aObj
->mLastUsed
)*1000/PR_TicksPerSecond();
150 // See the comment for NotifyExpired in nsExpirationTracker.h for these
152 PRUint32 lowerBoundMS
= (K
-1)*periodMS
- slackMS
;
153 PRUint32 upperBoundMS
= K
*(periodMS
+ sleepPeriodMS
) + slackMS
;
155 printf("Checking: %d-%d = %d [%d,%d]\n",
156 now
, aObj
->mLastUsed
, timeDiffMS
, lowerBoundMS
, upperBoundMS
);
158 if (timeDiffMS
< lowerBoundMS
|| timeDiffMS
> upperBoundMS
) {
159 if (timeDiffMS
< periodMS
&& aObj
->mExpired
) {
160 // This is probably OK, it probably just expired twice
166 aObj
->mExpired
= PR_TRUE
;
173 template <PRUint32 K
> static PRBool
test_random() {
177 for (PRUint32 j
= 0; j
< iterations
; ++j
) {
181 for (i
= 0; i
< ops
; ++i
) {
182 if ((rand() & 0xF) == 0) {
183 // Simulate work that takes time
185 printf("SLEEPING for %dms (%d)\n", sleepPeriodMS
, PR_IntervalNow());
187 PR_Sleep(PR_MillisecondsToInterval(sleepPeriodMS
));
188 // Process pending timer events
189 NS_ProcessPendingEvents(nsnull
);
191 tracker
.DoRandomOperation();
198 static PRBool
test_random3() { return test_random
<3>(); }
199 static PRBool
test_random4() { return test_random
<4>(); }
200 static PRBool
test_random8() { return test_random
<8>(); }
202 typedef PRBool (*TestFunc
)();
203 #define DECL_TEST(name) { #name, name }
205 static const struct Test
{
209 DECL_TEST(test_random3
),
210 DECL_TEST(test_random4
),
211 DECL_TEST(test_random8
),
217 using namespace TestExpirationTracker
;
219 int main(int argc
, char **argv
) {
222 count
= atoi(argv
[1]);
224 if (NS_FAILED(NS_InitXPCOM2(nsnull
, nsnull
, nsnull
)))
228 for (const Test
* t
= tests
; t
->name
!= nsnull
; ++t
) {
229 printf("%25s : %s\n", t
->name
, t
->func() ? "SUCCESS" : "FAILURE");
233 NS_ShutdownXPCOM(nsnull
);