2007-03-26 20:38:21 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-26 20:38:21 -07:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <prthread.h>
|
|
|
|
#include "nsExpirationTracker.h"
|
|
|
|
#include "nsMemory.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
#include "nsDirectoryServiceUtils.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsXPCOM.h"
|
2012-06-05 19:08:30 -07:00
|
|
|
#include "nsIFile.h"
|
2007-03-26 20:38:21 -07:00
|
|
|
#include "prinrval.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
2008-08-18 09:45:38 -07:00
|
|
|
namespace TestExpirationTracker {
|
|
|
|
|
2007-03-26 20:38:21 -07:00
|
|
|
struct Object {
|
2011-10-17 07:59:28 -07:00
|
|
|
Object() : mExpired(false) { Touch(); }
|
|
|
|
void Touch() { mLastUsed = PR_IntervalNow(); mExpired = false; }
|
2007-03-26 20:38:21 -07:00
|
|
|
|
|
|
|
nsExpirationState mExpiration;
|
|
|
|
nsExpirationState* GetExpirationState() { return &mExpiration; }
|
|
|
|
|
|
|
|
PRIntervalTime mLastUsed;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mExpired;
|
2007-03-26 20:38:21 -07:00
|
|
|
};
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool error;
|
2012-08-22 08:56:38 -07:00
|
|
|
static uint32_t periodMS = 100;
|
|
|
|
static uint32_t ops = 1000;
|
|
|
|
static uint32_t iterations = 2;
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool logging = 0;
|
2012-08-22 08:56:38 -07:00
|
|
|
static uint32_t sleepPeriodMS = 50;
|
|
|
|
static uint32_t slackMS = 20; // allow this much error
|
2007-03-26 20:38:21 -07:00
|
|
|
|
|
|
|
static void SignalError() {
|
|
|
|
printf("ERROR!\n");
|
2011-10-17 07:59:28 -07:00
|
|
|
error = true;
|
2007-03-26 20:38:21 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
template <uint32_t K> class Tracker : public nsExpirationTracker<Object,K> {
|
2007-03-26 20:38:21 -07:00
|
|
|
public:
|
|
|
|
Tracker() : nsExpirationTracker<Object,K>(periodMS) {
|
|
|
|
Object* obj = new Object();
|
|
|
|
mUniverse.AppendElement(obj);
|
|
|
|
LogAction(obj, "Created");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<nsAutoArrayPtr<Object> > mUniverse;
|
|
|
|
|
|
|
|
void LogAction(Object* aObj, const char* aAction) {
|
|
|
|
if (logging) {
|
2010-03-05 10:41:25 -08:00
|
|
|
printf("%d %p(%d): %s\n", PR_IntervalNow(),
|
|
|
|
static_cast<void*>(aObj), aObj->mLastUsed, aAction);
|
2007-03-26 20:38:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoRandomOperation() {
|
|
|
|
Object* obj;
|
|
|
|
switch (rand() & 0x7) {
|
|
|
|
case 0: {
|
|
|
|
if (mUniverse.Length() < 50) {
|
|
|
|
obj = new Object();
|
|
|
|
mUniverse.AppendElement(obj);
|
|
|
|
nsExpirationTracker<Object,K>::AddObject(obj);
|
|
|
|
LogAction(obj, "Created and added");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 4: {
|
|
|
|
if (mUniverse.Length() < 50) {
|
|
|
|
obj = new Object();
|
|
|
|
mUniverse.AppendElement(obj);
|
|
|
|
LogAction(obj, "Created");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1: {
|
2012-08-22 08:56:38 -07:00
|
|
|
obj = mUniverse[uint32_t(rand())%mUniverse.Length()];
|
2007-03-26 20:38:21 -07:00
|
|
|
if (obj->mExpiration.IsTracked()) {
|
|
|
|
nsExpirationTracker<Object,K>::RemoveObject(obj);
|
|
|
|
LogAction(obj, "Removed");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2: {
|
2012-08-22 08:56:38 -07:00
|
|
|
obj = mUniverse[uint32_t(rand())%mUniverse.Length()];
|
2007-03-26 20:38:21 -07:00
|
|
|
if (!obj->mExpiration.IsTracked()) {
|
|
|
|
obj->Touch();
|
|
|
|
nsExpirationTracker<Object,K>::AddObject(obj);
|
|
|
|
LogAction(obj, "Added");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 3: {
|
2012-08-22 08:56:38 -07:00
|
|
|
obj = mUniverse[uint32_t(rand())%mUniverse.Length()];
|
2007-03-26 20:38:21 -07:00
|
|
|
if (obj->mExpiration.IsTracked()) {
|
|
|
|
obj->Touch();
|
|
|
|
nsExpirationTracker<Object,K>::MarkUsed(obj);
|
|
|
|
LogAction(obj, "Marked used");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void NotifyExpired(Object* aObj) {
|
|
|
|
LogAction(aObj, "Expired");
|
|
|
|
PRIntervalTime now = PR_IntervalNow();
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t timeDiffMS = (now - aObj->mLastUsed)*1000/PR_TicksPerSecond();
|
2007-03-26 20:38:21 -07:00
|
|
|
// See the comment for NotifyExpired in nsExpirationTracker.h for these
|
|
|
|
// bounds
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t lowerBoundMS = (K-1)*periodMS - slackMS;
|
|
|
|
uint32_t upperBoundMS = K*(periodMS + sleepPeriodMS) + slackMS;
|
2007-03-26 20:38:21 -07:00
|
|
|
if (logging) {
|
|
|
|
printf("Checking: %d-%d = %d [%d,%d]\n",
|
|
|
|
now, aObj->mLastUsed, timeDiffMS, lowerBoundMS, upperBoundMS);
|
|
|
|
}
|
|
|
|
if (timeDiffMS < lowerBoundMS || timeDiffMS > upperBoundMS) {
|
|
|
|
if (timeDiffMS < periodMS && aObj->mExpired) {
|
|
|
|
// This is probably OK, it probably just expired twice
|
|
|
|
} else {
|
|
|
|
SignalError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
aObj->Touch();
|
2011-10-17 07:59:28 -07:00
|
|
|
aObj->mExpired = true;
|
2007-03-26 20:38:21 -07:00
|
|
|
DoRandomOperation();
|
|
|
|
DoRandomOperation();
|
|
|
|
DoRandomOperation();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
template <uint32_t K> static bool test_random() {
|
2007-03-26 20:38:21 -07:00
|
|
|
srand(K);
|
2011-10-17 07:59:28 -07:00
|
|
|
error = false;
|
2007-03-26 20:38:21 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t j = 0; j < iterations; ++j) {
|
2007-03-26 20:38:21 -07:00
|
|
|
Tracker<K> tracker;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i = 0;
|
2007-03-26 20:38:21 -07:00
|
|
|
for (i = 0; i < ops; ++i) {
|
|
|
|
if ((rand() & 0xF) == 0) {
|
|
|
|
// Simulate work that takes time
|
|
|
|
if (logging) {
|
|
|
|
printf("SLEEPING for %dms (%d)\n", sleepPeriodMS, PR_IntervalNow());
|
|
|
|
}
|
|
|
|
PR_Sleep(PR_MillisecondsToInterval(sleepPeriodMS));
|
|
|
|
// Process pending timer events
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ProcessPendingEvents(nullptr);
|
2007-03-26 20:38:21 -07:00
|
|
|
}
|
|
|
|
tracker.DoRandomOperation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return !error;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool test_random3() { return test_random<3>(); }
|
|
|
|
static bool test_random4() { return test_random<4>(); }
|
|
|
|
static bool test_random8() { return test_random<8>(); }
|
2007-03-26 20:38:21 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
typedef bool (*TestFunc)();
|
2007-03-26 20:38:21 -07:00
|
|
|
#define DECL_TEST(name) { #name, name }
|
|
|
|
|
|
|
|
static const struct Test {
|
|
|
|
const char* name;
|
|
|
|
TestFunc func;
|
|
|
|
} tests[] = {
|
|
|
|
DECL_TEST(test_random3),
|
|
|
|
DECL_TEST(test_random4),
|
|
|
|
DECL_TEST(test_random8),
|
2012-07-30 07:20:58 -07:00
|
|
|
{ nullptr, nullptr }
|
2007-03-26 20:38:21 -07:00
|
|
|
};
|
|
|
|
|
2008-08-18 09:45:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
using namespace TestExpirationTracker;
|
|
|
|
|
2007-03-26 20:38:21 -07:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
int count = 1;
|
|
|
|
if (argc > 1)
|
|
|
|
count = atoi(argv[1]);
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
if (NS_FAILED(NS_InitXPCOM2(nullptr, nullptr, nullptr)))
|
2007-03-26 20:38:21 -07:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
while (count--) {
|
2012-07-30 07:20:58 -07:00
|
|
|
for (const Test* t = tests; t->name != nullptr; ++t) {
|
2007-03-26 20:38:21 -07:00
|
|
|
printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ShutdownXPCOM(nullptr);
|
2007-03-26 20:38:21 -07:00
|
|
|
return 0;
|
|
|
|
}
|