1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#include "gtest/gtest.h"
#include "mozilla/TimeStamp.h"
#include "SystemTimeConverter.h"
using mozilla::SystemTimeConverter;
using mozilla::TimeDuration;
using mozilla::TimeStamp;
namespace {
// This class provides a mock implementation of the CurrentTimeGetter template
// type used in SystemTimeConverter. It can be constructed with a particular
// Time and always returns that Time.
template <typename Time>
class MockCurrentTimeGetter {
public:
MockCurrentTimeGetter() : mTime(0) {}
explicit MockCurrentTimeGetter(Time aTime) : mTime(aTime) {}
// Methods needed for CurrentTimeGetter compatibility
Time GetCurrentTime() const { return mTime; }
void GetTimeAsyncForPossibleBackwardsSkew(const TimeStamp& aNow) {}
private:
Time mTime;
};
// This is another mock implementation of the CurrentTimeGetter template
// type used in SystemTimeConverter, except this asserts that it will not be
// used. i.e. it should only be used in calls to SystemTimeConverter that we
// know will not invoke it.
template <typename Time>
class UnusedCurrentTimeGetter {
public:
Time GetCurrentTime() const {
EXPECT_TRUE(false);
return 0;
}
void GetTimeAsyncForPossibleBackwardsSkew(const TimeStamp& aNow) {
EXPECT_TRUE(false);
}
};
// This class provides a mock implementation of the TimeStampNowProvider
// template type used in SystemTimeConverter. It also has other things in it
// that allow the test to better control time for testing purposes.
class MockTimeStamp {
public:
// This should generally be called at the start of every test function, as
// it will initialize this class's static fields to sane values. In particular
// it will initialize the baseline TimeStamp against which all other
// TimeStamps are compared.
static void Init() {
sBaseline = TimeStamp::Now();
sTimeStamp = sBaseline;
}
// Advance the timestamp returned by `MockTimeStamp::Now()`
static void Advance(double ms) {
sTimeStamp += TimeDuration::FromMilliseconds(ms);
}
// Returns the baseline TimeStamp, that is used as a fixed reference point
// in time against which other TimeStamps can be compared. This is needed
// because mozilla::TimeStamp itself doesn't provide any conversion to
// human-readable strings, and we need to convert it to a TimeDuration in
// order to get that. This baseline TimeStamp can be used to turn an
// arbitrary TimeStamp into a TimeDuration.
static TimeStamp Baseline() { return sBaseline; }
// This is the method needed for TimeStampNowProvider compatibility, and
// simulates `TimeStamp::Now()`
static TimeStamp Now() { return sTimeStamp; }
private:
static TimeStamp sTimeStamp;
static TimeStamp sBaseline;
};
TimeStamp MockTimeStamp::sTimeStamp;
TimeStamp MockTimeStamp::sBaseline;
// Could have platform-specific implementations of this using DWORD, guint32,
// etc behind ifdefs. But this is sufficient for now.
using GTestTime = uint32_t;
using TimeConverter = SystemTimeConverter<GTestTime, MockTimeStamp>;
} // namespace
// Checks the expectation that the TimeStamp `ts` is exactly `ms` milliseconds
// after the baseline timestamp. This is a macro so gtest still gives us useful
// line numbers for failures.
#define EXPECT_TS(ts, ms) \
EXPECT_EQ((ts) - MockTimeStamp::Baseline(), \
TimeDuration::FromMilliseconds(ms))
#define EXPECT_TS_FUZZY(ts, ms) \
EXPECT_DOUBLE_EQ(((ts) - MockTimeStamp::Baseline()).ToMilliseconds(), ms)
TEST(TimeConverter, SanityCheck)
{
MockTimeStamp::Init();
MockCurrentTimeGetter timeGetter(10);
UnusedCurrentTimeGetter<GTestTime> unused;
TimeConverter converter;
// This call sets the reference time and timestamp
TimeStamp ts = converter.GetTimeStampFromSystemTime(10, timeGetter);
EXPECT_TS(ts, 0);
// Advance "TimeStamp::Now" by 10ms, use the same event time and OS time.
// Since the event time is the same as before, we expect to get back the
// same TimeStamp as before too, despite Now() changing.
MockTimeStamp::Advance(10);
ts = converter.GetTimeStampFromSystemTime(10, unused);
EXPECT_TS(ts, 0);
// Now let's use an event time 20ms after the old event. This will trigger
// forward skew detection and resync the TimeStamp for the new event to Now().
ts = converter.GetTimeStampFromSystemTime(30, unused);
EXPECT_TS(ts, 10);
}
TEST(TimeConverter, Overflow)
{
// This tests wrapping time around the max value supported in the GTestTime
// type and ensuring it is handled properly.
MockTimeStamp::Init();
const GTestTime max = std::numeric_limits<GTestTime>::max();
const GTestTime min = std::numeric_limits<GTestTime>::min();
double fullRange = (double)max - (double)min;
double wrapPeriod = fullRange + 1.0;
GTestTime almostOverflowed = max - 100;
GTestTime overflowed = max + 100;
MockCurrentTimeGetter timeGetter(almostOverflowed);
UnusedCurrentTimeGetter<GTestTime> unused;
TimeConverter converter;
// Set reference time to 100ms before the overflow point
TimeStamp ts =
converter.GetTimeStampFromSystemTime(almostOverflowed, timeGetter);
EXPECT_TS(ts, 0);
// Advance everything by 200ms and verify we get back a TimeStamp 200ms from
// the baseline despite wrapping an overflow.
MockTimeStamp::Advance(200);
ts = converter.GetTimeStampFromSystemTime(overflowed, unused);
EXPECT_TS(ts, 200);
// Advance by another full wraparound of the time. This loses some precision
// so we have to do the FUZZY match
MockTimeStamp::Advance(wrapPeriod);
ts = converter.GetTimeStampFromSystemTime(overflowed, unused);
EXPECT_TS_FUZZY(ts, 200.0 + wrapPeriod);
}
TEST(TimeConverter, InvertedOverflow)
{
// This tests time going from near the min value of GTestTime to the max
// value of GTestTime
MockTimeStamp::Init();
const GTestTime max = std::numeric_limits<GTestTime>::max();
const GTestTime min = std::numeric_limits<GTestTime>::min();
double fullRange = (double)max - (double)min;
double wrapPeriod = fullRange + 1.0;
GTestTime nearRangeMin = min + 100;
GTestTime nearRangeMax = max - 100;
double gap = (double)nearRangeMax - (double)nearRangeMin;
MockCurrentTimeGetter timeGetter(nearRangeMin);
UnusedCurrentTimeGetter<GTestTime> unused;
TimeConverter converter;
// Set reference time to value near min numeric limit
TimeStamp ts = converter.GetTimeStampFromSystemTime(nearRangeMin, timeGetter);
EXPECT_TS(ts, 0);
// Advance to value near max numeric limit
MockTimeStamp::Advance(gap);
ts = converter.GetTimeStampFromSystemTime(nearRangeMax, unused);
EXPECT_TS(ts, gap);
// Advance by another full wraparound of the time. This loses some precision
// so we have to do the FUZZY match
MockTimeStamp::Advance(wrapPeriod);
ts = converter.GetTimeStampFromSystemTime(nearRangeMax, unused);
EXPECT_TS_FUZZY(ts, gap + wrapPeriod);
}
TEST(TimeConverter, HalfRangeBoundary)
{
MockTimeStamp::Init();
GTestTime max = std::numeric_limits<GTestTime>::max();
GTestTime min = std::numeric_limits<GTestTime>::min();
double fullRange = (double)max - (double)min;
double wrapPeriod = fullRange + 1.0;
GTestTime halfRange = (GTestTime)(fullRange / 2.0);
GTestTime halfWrapPeriod = (GTestTime)(wrapPeriod / 2.0);
TimeConverter converter;
GTestTime firstEvent = 10;
MockCurrentTimeGetter timeGetter(firstEvent);
UnusedCurrentTimeGetter<GTestTime> unused;
// Set reference time
TimeStamp ts = converter.GetTimeStampFromSystemTime(firstEvent, timeGetter);
EXPECT_TS(ts, 0);
// Advance event time by just under the half-period, to trigger about as big
// a forwards skew as we possibly can.
GTestTime secondEvent = firstEvent + (halfWrapPeriod - 1);
ts = converter.GetTimeStampFromSystemTime(secondEvent, unused);
EXPECT_TS(ts, 0);
// The above forwards skew will have reset the reference timestamp. Now
// advance Now time by just under the half-range, to trigger about as big
// a backwards skew as we possibly can.
MockTimeStamp::Advance(halfRange - 1);
ts = converter.GetTimeStampFromSystemTime(secondEvent, unused);
EXPECT_TS(ts, 0);
}
TEST(TimeConverter, FractionalMillisBug1626734)
{
MockTimeStamp::Init();
TimeConverter converter;
GTestTime eventTime = 10;
MockCurrentTimeGetter timeGetter(eventTime);
UnusedCurrentTimeGetter<GTestTime> unused;
TimeStamp ts = converter.GetTimeStampFromSystemTime(eventTime, timeGetter);
EXPECT_TS(ts, 0);
MockTimeStamp::Advance(0.2);
ts = converter.GetTimeStampFromSystemTime(eventTime, unused);
EXPECT_TS(ts, 0);
MockTimeStamp::Advance(0.9);
TimeStamp ts2 = converter.GetTimeStampFromSystemTime(eventTime, unused);
EXPECT_TS(ts2, 0);
// Since ts2 came from a "future" call relative to ts, we expect ts2 to not
// be "before" ts. (i.e. time shouldn't go backwards, even by fractional
// milliseconds). This assertion is technically already implied by the
// EXPECT_TS checks above, but fixing this assertion is the end result that
// we wanted in bug 1626734 so it feels appropriate to recheck it explicitly.
EXPECT_TRUE(ts <= ts2);
}
TEST(TimeConverter, UnderflowWrapBaseline)
{
MockTimeStamp::Init();
// Our SystemTimeConverter for 32-bit event times.
TimeConverter converter;
// First call: set a reference time of 300, which also sets the reference
// TimeStamp to baseline (0ms offset).
MockCurrentTimeGetter<GTestTime> timeGetter(300);
TimeStamp ts = converter.GetTimeStampFromSystemTime(300, timeGetter);
// Confirm we anchored at baseline + 0ms:
EXPECT_TS(ts, 0);
// Advance "TimeStamp::Now()" by 200ms => Now() = baseline + 200ms.
MockTimeStamp::Advance(200);
// Now request a TimeStamp for an event time of 299 (1 less than our reference
// of 300). With the old buggy code, 299 - 300 underflows to 0xFFFFFFFF in
// 32-bit arithmetic, leading to an incorrect final timestamp that is
// negative. In the fixed code, we treat that difference as signed (-1),
// realize it's negative, and do the "isNewer" branch properly.
{
UnusedCurrentTimeGetter<GTestTime> unused;
TimeStamp ts2 = converter.GetTimeStampFromSystemTime(299, unused);
// Ensure it's not backward in time.
ASSERT_GE(ts2, MockTimeStamp::Baseline())
<< "Should not go behind the baseline!";
}
}
|