| 1 |
// Copyright (c) 2019 Cloudflare, Inc. and contributors |
| 2 |
// Licensed under the MIT License: |
| 3 |
// |
| 4 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
// of this software and associated documentation files (the "Software"), to deal |
| 6 |
// in the Software without restriction, including without limitation the rights |
| 7 |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 |
// copies of the Software, and to permit persons to whom the Software is |
| 9 |
// furnished to do so, subject to the following conditions: |
| 10 |
// |
| 11 |
// The above copyright notice and this permission notice shall be included in |
| 12 |
// all copies or substantial portions of the Software. |
| 13 |
// |
| 14 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 |
// THE SOFTWARE. |
| 21 |
|
| 22 |
#if _WIN32 |
| 23 |
#include "win32-api-version.h" |
| 24 |
#endif |
| 25 |
|
| 26 |
#include "time.h" |
| 27 |
#include "debug.h" |
| 28 |
#include <kj/test.h> |
| 29 |
#include <time.h> |
| 30 |
|
| 31 |
#if _WIN32 |
| 32 |
#include <windows.h> |
| 33 |
#include "windows-sanity.h" |
| 34 |
#else |
| 35 |
#include <unistd.h> |
| 36 |
#endif |
| 37 |
|
| 38 |
namespace kj { |
| 39 |
namespace { |
| 40 |
|
| 41 |
KJ_TEST("stringify times") { |
| 42 |
KJ_EXPECT(kj::str(50 * kj::SECONDS) == "50s"); |
| 43 |
KJ_EXPECT(kj::str(5 * kj::SECONDS + 2 * kj::MILLISECONDS) == "5.002s"); |
| 44 |
KJ_EXPECT(kj::str(256 * kj::MILLISECONDS) == "256ms"); |
| 45 |
KJ_EXPECT(kj::str(5 * kj::MILLISECONDS + 2 * kj::NANOSECONDS) == "5.000002ms"); |
| 46 |
KJ_EXPECT(kj::str(50 * kj::MICROSECONDS) == "50μs"); |
| 47 |
KJ_EXPECT(kj::str(5 * kj::MICROSECONDS + 300 * kj::NANOSECONDS) == "5.3μs"); |
| 48 |
KJ_EXPECT(kj::str(50 * kj::NANOSECONDS) == "50ns"); |
| 49 |
KJ_EXPECT(kj::str(-256 * kj::MILLISECONDS) == "-256ms"); |
| 50 |
KJ_EXPECT(kj::str(-50 * kj::NANOSECONDS) == "-50ns"); |
| 51 |
KJ_EXPECT(kj::str((int64_t)kj::maxValue * kj::NANOSECONDS) == "9223372036.854775807s"); |
| 52 |
KJ_EXPECT(kj::str((int64_t)kj::minValue * kj::NANOSECONDS) == "-9223372036.854775808s"); |
| 53 |
} |
| 54 |
|
| 55 |
#if _WIN32 |
| 56 |
void delay(kj::Duration d) { |
| 57 |
Sleep(d / kj::MILLISECONDS); |
| 58 |
} |
| 59 |
#else |
| 60 |
void delay(kj::Duration d) { |
| 61 |
usleep(d / kj::MICROSECONDS); |
| 62 |
} |
| 63 |
#endif |
| 64 |
|
| 65 |
KJ_TEST("calendar clocks matches unix time") { |
| 66 |
// Check that the times returned by the calendar clock are within 1s of what time() returns. |
| 67 |
|
| 68 |
auto& coarse = systemCoarseCalendarClock(); |
| 69 |
auto& precise = systemPreciseCalendarClock(); |
| 70 |
|
| 71 |
Date p = precise.now(); |
| 72 |
Date c = coarse.now(); |
| 73 |
time_t t = time(nullptr); |
| 74 |
|
| 75 |
int64_t pi = (p - UNIX_EPOCH) / kj::SECONDS; |
| 76 |
int64_t ci = (c - UNIX_EPOCH) / kj::SECONDS; |
| 77 |
|
| 78 |
KJ_EXPECT(pi >= t - 1); |
| 79 |
KJ_EXPECT(pi <= t + 1); |
| 80 |
KJ_EXPECT(ci >= t - 1); |
| 81 |
KJ_EXPECT(ci <= t + 1); |
| 82 |
} |
| 83 |
|
| 84 |
KJ_TEST("monotonic clocks match each other") { |
| 85 |
// Check that the monotonic clocks return comparable times. |
| 86 |
|
| 87 |
auto& coarse = systemCoarseMonotonicClock(); |
| 88 |
auto& precise = systemPreciseMonotonicClock(); |
| 89 |
|
| 90 |
TimePoint p = precise.now(); |
| 91 |
TimePoint c = coarse.now(); |
| 92 |
|
| 93 |
// 40ms tolerance due to Windows timeslices being quite long, especially on GitHub Actions where |
| 94 |
// Windows is drunk and has completely lost track of time. |
| 95 |
KJ_EXPECT(p < c + 40 * kj::MILLISECONDS, p - c); |
| 96 |
KJ_EXPECT(p > c - 40 * kj::MILLISECONDS, c - p); |
| 97 |
} |
| 98 |
|
| 99 |
KJ_TEST("all clocks advance in real time") { |
| 100 |
Duration coarseCalDiff; |
| 101 |
Duration preciseCalDiff; |
| 102 |
Duration coarseMonoDiff; |
| 103 |
Duration preciseMonoDiff; |
| 104 |
|
| 105 |
for (uint retryCount KJ_UNUSED: kj::zeroTo(20)) { |
| 106 |
auto& coarseCal = systemCoarseCalendarClock(); |
| 107 |
auto& preciseCal = systemPreciseCalendarClock(); |
| 108 |
auto& coarseMono = systemCoarseMonotonicClock(); |
| 109 |
auto& preciseMono = systemPreciseMonotonicClock(); |
| 110 |
|
| 111 |
Date coarseCalBefore = coarseCal.now(); |
| 112 |
Date preciseCalBefore = preciseCal.now(); |
| 113 |
TimePoint coarseMonoBefore = coarseMono.now(); |
| 114 |
TimePoint preciseMonoBefore = preciseMono.now(); |
| 115 |
|
| 116 |
Duration delayTime = 150 * kj::MILLISECONDS; |
| 117 |
delay(delayTime); |
| 118 |
|
| 119 |
Date coarseCalAfter = coarseCal.now(); |
| 120 |
Date preciseCalAfter = preciseCal.now(); |
| 121 |
TimePoint coarseMonoAfter = coarseMono.now(); |
| 122 |
TimePoint preciseMonoAfter = preciseMono.now(); |
| 123 |
|
| 124 |
coarseCalDiff = coarseCalAfter - coarseCalBefore; |
| 125 |
preciseCalDiff = preciseCalAfter - preciseCalBefore; |
| 126 |
coarseMonoDiff = coarseMonoAfter - coarseMonoBefore; |
| 127 |
preciseMonoDiff = preciseMonoAfter - preciseMonoBefore; |
| 128 |
|
| 129 |
// 20ms tolerance due to Windows timeslices being quite long (and Windows sleeps being only |
| 130 |
// accurate to the timeslice). |
| 131 |
if (coarseCalDiff > delayTime - 20 * kj::MILLISECONDS && |
| 132 |
coarseCalDiff < delayTime + 20 * kj::MILLISECONDS && |
| 133 |
preciseCalDiff > delayTime - 20 * kj::MILLISECONDS && |
| 134 |
preciseCalDiff < delayTime + 20 * kj::MILLISECONDS && |
| 135 |
coarseMonoDiff > delayTime - 20 * kj::MILLISECONDS && |
| 136 |
coarseMonoDiff < delayTime + 20 * kj::MILLISECONDS && |
| 137 |
preciseMonoDiff > delayTime - 20 * kj::MILLISECONDS && |
| 138 |
preciseMonoDiff < delayTime + 20 * kj::MILLISECONDS) { |
| 139 |
// success |
| 140 |
return; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
KJ_FAIL_EXPECT("clocks seem inaccurate even after 20 tries", |
| 145 |
coarseCalDiff / kj::MICROSECONDS, preciseCalDiff / kj::MICROSECONDS, |
| 146 |
coarseMonoDiff / kj::MICROSECONDS, preciseMonoDiff / kj::MICROSECONDS); |
| 147 |
} |
| 148 |
|
| 149 |
} // namespace |
| 150 |
} // namespace kj |