Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/xpcom/tests/gtest/TestExpirationTracker.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include <stdlib.h>
8
#include <stdio.h>
9
#include <prthread.h>
10
#include "nsExpirationTracker.h"
11
#include "nsMemory.h"
12
#include "nsAutoPtr.h"
13
#include "nsString.h"
14
#include "nsDirectoryServiceDefs.h"
15
#include "nsDirectoryServiceUtils.h"
16
#include "nsComponentManagerUtils.h"
17
#include "nsXPCOM.h"
18
#include "nsIFile.h"
19
#include "prinrval.h"
20
#include "nsThreadUtils.h"
21
#include "mozilla/UniquePtr.h"
22
#include "gtest/gtest.h"
23
24
namespace TestExpirationTracker {
25
26
struct Object {
27
0
  Object() : mExpired(false) { Touch(); }
28
0
  void Touch() { mLastUsed = PR_IntervalNow(); mExpired = false; }
29
30
  nsExpirationState mExpiration;
31
0
  nsExpirationState* GetExpirationState() { return &mExpiration; }
32
33
  PRIntervalTime mLastUsed;
34
  bool           mExpired;
35
};
36
37
static bool error;
38
static uint32_t periodMS = 100;
39
static uint32_t ops = 1000;
40
static uint32_t iterations = 2;
41
static bool logging = 0;
42
static uint32_t sleepPeriodMS = 50;
43
static uint32_t slackMS = 30; // allow this much error
44
45
template <uint32_t K> class Tracker : public nsExpirationTracker<Object,K> {
46
public:
47
0
  Tracker() : nsExpirationTracker<Object,K>(periodMS, "Tracker") {
48
0
    Object* obj = new Object();
49
0
    mUniverse.AppendElement(obj);
50
0
    LogAction(obj, "Created");
51
0
  }
Unexecuted instantiation: TestExpirationTracker::Tracker<3u>::Tracker()
Unexecuted instantiation: TestExpirationTracker::Tracker<4u>::Tracker()
Unexecuted instantiation: TestExpirationTracker::Tracker<8u>::Tracker()
52
53
  nsTArray<mozilla::UniquePtr<Object>> mUniverse;
54
55
0
  void LogAction(Object* aObj, const char* aAction) {
56
0
    if (logging) {
57
0
      printf("%d %p(%d): %s\n", PR_IntervalNow(),
58
0
             static_cast<void*>(aObj), aObj->mLastUsed, aAction);
59
0
    }
60
0
  }
Unexecuted instantiation: TestExpirationTracker::Tracker<3u>::LogAction(TestExpirationTracker::Object*, char const*)
Unexecuted instantiation: TestExpirationTracker::Tracker<4u>::LogAction(TestExpirationTracker::Object*, char const*)
Unexecuted instantiation: TestExpirationTracker::Tracker<8u>::LogAction(TestExpirationTracker::Object*, char const*)
61
62
0
  void DoRandomOperation() {
63
0
    using mozilla::UniquePtr;
64
0
65
0
    Object* obj;
66
0
    switch (rand() & 0x7) {
67
0
    case 0: {
68
0
      if (mUniverse.Length() < 50) {
69
0
        obj = new Object();
70
0
        mUniverse.AppendElement(obj);
71
0
        nsExpirationTracker<Object,K>::AddObject(obj);
72
0
        LogAction(obj, "Created and added");
73
0
      }
74
0
      break;
75
0
    }
76
0
    case 4: {
77
0
      if (mUniverse.Length() < 50) {
78
0
        obj = new Object();
79
0
        mUniverse.AppendElement(obj);
80
0
        LogAction(obj, "Created");
81
0
      }
82
0
      break;
83
0
    }
84
0
    case 1: {
85
0
      UniquePtr<Object>& objref = mUniverse[uint32_t(rand())%mUniverse.Length()];
86
0
      if (objref->mExpiration.IsTracked()) {
87
0
        nsExpirationTracker<Object,K>::RemoveObject(objref.get());
88
0
        LogAction(objref.get(), "Removed");
89
0
      }
90
0
      break;
91
0
    }
92
0
    case 2: {
93
0
      UniquePtr<Object>& objref = mUniverse[uint32_t(rand())%mUniverse.Length()];
94
0
      if (!objref->mExpiration.IsTracked()) {
95
0
        objref->Touch();
96
0
        nsExpirationTracker<Object,K>::AddObject(objref.get());
97
0
        LogAction(objref.get(), "Added");
98
0
      }
99
0
      break;
100
0
    }
101
0
    case 3: {
102
0
      UniquePtr<Object>& objref = mUniverse[uint32_t(rand())%mUniverse.Length()];
103
0
      if (objref->mExpiration.IsTracked()) {
104
0
        objref->Touch();
105
0
        nsExpirationTracker<Object,K>::MarkUsed(objref.get());
106
0
        LogAction(objref.get(), "Marked used");
107
0
      }
108
0
      break;
109
0
    }
110
0
    }
111
0
  }
Unexecuted instantiation: TestExpirationTracker::Tracker<3u>::DoRandomOperation()
Unexecuted instantiation: TestExpirationTracker::Tracker<4u>::DoRandomOperation()
Unexecuted instantiation: TestExpirationTracker::Tracker<8u>::DoRandomOperation()
112
113
protected:
114
0
  void NotifyExpired(Object* aObj) override {
115
0
    LogAction(aObj, "Expired");
116
0
    PRIntervalTime now = PR_IntervalNow();
117
0
    uint32_t timeDiffMS = (now - aObj->mLastUsed)*1000/PR_TicksPerSecond();
118
0
    // See the comment for NotifyExpired in nsExpirationTracker.h for these
119
0
    // bounds
120
0
    uint32_t lowerBoundMS = (K-1)*periodMS - slackMS;
121
0
    uint32_t upperBoundMS = K*(periodMS + sleepPeriodMS) + slackMS;
122
0
    if (logging) {
123
0
      printf("Checking: %d-%d = %d [%d,%d]\n",
124
0
             now, aObj->mLastUsed, timeDiffMS, lowerBoundMS, upperBoundMS);
125
0
    }
126
0
    if (timeDiffMS < lowerBoundMS || timeDiffMS > upperBoundMS) {
127
0
      EXPECT_TRUE(timeDiffMS < periodMS && aObj->mExpired);
128
0
    }
129
0
    aObj->Touch();
130
0
    aObj->mExpired = true;
131
0
    DoRandomOperation();
132
0
    DoRandomOperation();
133
0
    DoRandomOperation();
134
0
  }
Unexecuted instantiation: TestExpirationTracker::Tracker<3u>::NotifyExpired(TestExpirationTracker::Object*)
Unexecuted instantiation: TestExpirationTracker::Tracker<4u>::NotifyExpired(TestExpirationTracker::Object*)
Unexecuted instantiation: TestExpirationTracker::Tracker<8u>::NotifyExpired(TestExpirationTracker::Object*)
135
};
136
137
0
template <uint32_t K> static bool test_random() {
138
0
  srand(K);
139
0
  error = false;
140
0
141
0
  for (uint32_t j = 0; j < iterations; ++j) {
142
0
    Tracker<K> tracker;
143
0
144
0
    uint32_t i = 0;
145
0
    for (i = 0; i < ops; ++i) {
146
0
      if ((rand() & 0xF) == 0) {
147
0
        // Simulate work that takes time
148
0
        if (logging) {
149
0
          printf("SLEEPING for %dms (%d)\n", sleepPeriodMS, PR_IntervalNow());
150
0
        }
151
0
        PR_Sleep(PR_MillisecondsToInterval(sleepPeriodMS));
152
0
        // Process pending timer events
153
0
        NS_ProcessPendingEvents(nullptr);
154
0
      }
155
0
      tracker.DoRandomOperation();
156
0
    }
157
0
  }
158
0
159
0
  return !error;
160
0
}
Unexecuted instantiation: Unified_cpp_xpcom_tests_gtest1.cpp:bool TestExpirationTracker::test_random<3u>()
Unexecuted instantiation: Unified_cpp_xpcom_tests_gtest1.cpp:bool TestExpirationTracker::test_random<4u>()
Unexecuted instantiation: Unified_cpp_xpcom_tests_gtest1.cpp:bool TestExpirationTracker::test_random<8u>()
161
162
0
static bool test_random3() { return test_random<3>(); }
163
0
static bool test_random4() { return test_random<4>(); }
164
0
static bool test_random8() { return test_random<8>(); }
165
166
typedef bool (*TestFunc)();
167
#define DECL_TEST(name) { #name, name }
168
169
static const struct Test {
170
  const char* name;
171
  TestFunc    func;
172
} tests[] = {
173
  DECL_TEST(test_random3),
174
  DECL_TEST(test_random4),
175
  DECL_TEST(test_random8),
176
  { nullptr, nullptr }
177
};
178
179
TEST(ExpirationTracker, main)
180
0
{
181
0
  for (const TestExpirationTracker::Test* t = tests;
182
0
       t->name != nullptr; ++t) {
183
0
    EXPECT_TRUE(t->func());
184
0
  }
185
0
}
186
187
} // namespace TestExpirationTracker