Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / xpcom / tests / TestExpirationTracker.cpp
blobf9a166c6ced1b59fe8f2013749eb982c5948814a
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
14 * License.
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.
22 * Contributor(s):
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 ***** */
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <prthread.h>
42 #include "nsExpirationTracker.h"
43 #include "nsMemory.h"
44 #include "nsAutoPtr.h"
45 #include "nsString.h"
46 #include "nsDirectoryServiceDefs.h"
47 #include "nsDirectoryServiceUtils.h"
48 #include "nsComponentManagerUtils.h"
49 #include "nsXPCOM.h"
50 #include "nsILocalFile.h"
51 #include "prinrval.h"
52 #include "nsThreadUtils.h"
54 namespace TestExpirationTracker {
56 struct Object {
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;
67 static PRBool error;
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() {
76 printf("ERROR!\n");
77 error = PR_TRUE;
80 template <PRUint32 K> class Tracker : public nsExpirationTracker<Object,K> {
81 public:
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) {
91 if (logging) {
92 printf("%d %p(%d): %s\n", PR_IntervalNow(), aObj, aObj->mLastUsed, aAction);
96 void DoRandomOperation() {
97 Object* obj;
98 switch (rand() & 0x7) {
99 case 0: {
100 if (mUniverse.Length() < 50) {
101 obj = new Object();
102 mUniverse.AppendElement(obj);
103 nsExpirationTracker<Object,K>::AddObject(obj);
104 LogAction(obj, "Created and added");
106 break;
108 case 4: {
109 if (mUniverse.Length() < 50) {
110 obj = new Object();
111 mUniverse.AppendElement(obj);
112 LogAction(obj, "Created");
114 break;
116 case 1: {
117 obj = mUniverse[PRUint32(rand())%mUniverse.Length()];
118 if (obj->mExpiration.IsTracked()) {
119 nsExpirationTracker<Object,K>::RemoveObject(obj);
120 LogAction(obj, "Removed");
122 break;
124 case 2: {
125 obj = mUniverse[PRUint32(rand())%mUniverse.Length()];
126 if (!obj->mExpiration.IsTracked()) {
127 obj->Touch();
128 nsExpirationTracker<Object,K>::AddObject(obj);
129 LogAction(obj, "Added");
131 break;
133 case 3: {
134 obj = mUniverse[PRUint32(rand())%mUniverse.Length()];
135 if (obj->mExpiration.IsTracked()) {
136 obj->Touch();
137 nsExpirationTracker<Object,K>::MarkUsed(obj);
138 LogAction(obj, "Marked used");
140 break;
145 protected:
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
151 // bounds
152 PRUint32 lowerBoundMS = (K-1)*periodMS - slackMS;
153 PRUint32 upperBoundMS = K*(periodMS + sleepPeriodMS) + slackMS;
154 if (logging) {
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
161 } else {
162 SignalError();
165 aObj->Touch();
166 aObj->mExpired = PR_TRUE;
167 DoRandomOperation();
168 DoRandomOperation();
169 DoRandomOperation();
173 template <PRUint32 K> static PRBool test_random() {
174 srand(K);
175 error = PR_FALSE;
177 for (PRUint32 j = 0; j < iterations; ++j) {
178 Tracker<K> tracker;
180 PRUint32 i = 0;
181 for (i = 0; i < ops; ++i) {
182 if ((rand() & 0xF) == 0) {
183 // Simulate work that takes time
184 if (logging) {
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();
195 return !error;
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 {
206 const char* name;
207 TestFunc func;
208 } tests[] = {
209 DECL_TEST(test_random3),
210 DECL_TEST(test_random4),
211 DECL_TEST(test_random8),
212 { nsnull, nsnull }
217 using namespace TestExpirationTracker;
219 int main(int argc, char **argv) {
220 int count = 1;
221 if (argc > 1)
222 count = atoi(argv[1]);
224 if (NS_FAILED(NS_InitXPCOM2(nsnull, nsnull, nsnull)))
225 return -1;
227 while (count--) {
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);
234 return 0;