OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "util/misc/clock.h" |
| 16 |
| 17 #include <stdint.h> |
| 18 |
| 19 #include <algorithm> |
| 20 |
| 21 #include "base/basictypes.h" |
| 22 #include "base/logging.h" |
| 23 #include "base/strings/stringprintf.h" |
| 24 #include "gtest/gtest.h" |
| 25 |
| 26 namespace { |
| 27 |
| 28 using namespace crashpad; |
| 29 |
| 30 TEST(Clock, ClockMonotonicNanoseconds) { |
| 31 uint64_t start = ClockMonotonicNanoseconds(); |
| 32 EXPECT_GT(start, 0u); |
| 33 |
| 34 uint64_t now = start; |
| 35 for (size_t iteration = 0; iteration < 10; ++iteration) { |
| 36 uint64_t last = now; |
| 37 now = ClockMonotonicNanoseconds(); |
| 38 |
| 39 // Use EXPECT_GE instead of EXPECT_GT, because there are no guarantees about |
| 40 // the clock’s resolution. |
| 41 EXPECT_GE(now, last); |
| 42 } |
| 43 |
| 44 // SleepNanoseconds() should sleep for at least the value of the clock’s |
| 45 // resolution, so the clock’s value should definitely increase after a sleep. |
| 46 // EXPECT_GT can be used instead of EXPECT_GE after the sleep. |
| 47 SleepNanoseconds(1); |
| 48 now = ClockMonotonicNanoseconds(); |
| 49 EXPECT_GT(now, start); |
| 50 } |
| 51 |
| 52 void TestSleepNanoseconds(uint64_t nanoseconds) { |
| 53 uint64_t start = ClockMonotonicNanoseconds(); |
| 54 |
| 55 SleepNanoseconds(nanoseconds); |
| 56 |
| 57 uint64_t end = ClockMonotonicNanoseconds(); |
| 58 uint64_t diff = end - start; |
| 59 |
| 60 // |nanoseconds| is the lower bound for the actual amount of time spent |
| 61 // sleeping. |
| 62 EXPECT_GE(diff, nanoseconds); |
| 63 |
| 64 // It’s difficult to set an upper bound for the time spent sleeping. Allow |
| 65 // sleeps twice as long as requested, or sleeps a millisecond longer than |
| 66 // requested, whichever is larger. This is quite a lot of slop, but the |
| 67 // alternative would be test flakiness. |
| 68 uint64_t slop = std::max(static_cast<uint64_t>(1E6), nanoseconds); |
| 69 EXPECT_LE(diff, nanoseconds + slop); |
| 70 } |
| 71 |
| 72 TEST(Clock, SleepNanoseconds) { |
| 73 const uint64_t kTestData[] = { |
| 74 0, |
| 75 1, |
| 76 static_cast<uint64_t>(1E3), // 1 microsecond |
| 77 static_cast<uint64_t>(1E4), // 10 microseconds |
| 78 static_cast<uint64_t>(1E5), // 100 microseconds |
| 79 static_cast<uint64_t>(1E6), // 1 millisecond |
| 80 static_cast<uint64_t>(1E7), // 10 milliseconds |
| 81 static_cast<uint64_t>(2E7), // 20 milliseconds |
| 82 static_cast<uint64_t>(5E7), // 50 milliseconds |
| 83 }; |
| 84 |
| 85 for (size_t index = 0; index < arraysize(kTestData); ++index) { |
| 86 const uint64_t nanoseconds = kTestData[index]; |
| 87 SCOPED_TRACE( |
| 88 base::StringPrintf("index %zu, nanoseconds %llu", index, nanoseconds)); |
| 89 |
| 90 TestSleepNanoseconds(nanoseconds); |
| 91 } |
| 92 } |
| 93 |
| 94 } // namespace |
OLD | NEW |