wub | be29b9e | 2019-11-24 06:56:04 -0800 | [diff] [blame] | 1 | // Copyright (c) 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/third_party/quiche/src/quic/core/quic_time_accumulator.h" |
| 6 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
| 7 | #include "net/third_party/quiche/src/quic/test_tools/mock_clock.h" |
| 8 | |
| 9 | namespace quic { |
| 10 | namespace test { |
| 11 | |
| 12 | TEST(QuicTimeAccumulator, DefaultConstruct) { |
| 13 | MockClock clock; |
| 14 | clock.AdvanceTime(QuicTime::Delta::FromMilliseconds(1)); |
| 15 | |
| 16 | QuicTimeAccumulator acc; |
| 17 | EXPECT_FALSE(acc.IsRunning()); |
| 18 | |
| 19 | clock.AdvanceTime(QuicTime::Delta::FromMilliseconds(1)); |
| 20 | EXPECT_EQ(QuicTime::Delta::Zero(), acc.GetTotalElapsedTime()); |
| 21 | EXPECT_EQ(QuicTime::Delta::Zero(), acc.GetTotalElapsedTime(clock.Now())); |
| 22 | } |
| 23 | |
| 24 | TEST(QuicTimeAccumulator, StartStop) { |
| 25 | MockClock clock; |
| 26 | clock.AdvanceTime(QuicTime::Delta::FromMilliseconds(1)); |
| 27 | |
| 28 | QuicTimeAccumulator acc; |
| 29 | acc.Start(clock.Now()); |
| 30 | EXPECT_TRUE(acc.IsRunning()); |
| 31 | |
| 32 | clock.AdvanceTime(QuicTime::Delta::FromMilliseconds(10)); |
| 33 | acc.Stop(clock.Now()); |
| 34 | EXPECT_FALSE(acc.IsRunning()); |
| 35 | |
| 36 | clock.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 37 | EXPECT_EQ(QuicTime::Delta::FromMilliseconds(10), acc.GetTotalElapsedTime()); |
| 38 | EXPECT_EQ(QuicTime::Delta::FromMilliseconds(10), |
| 39 | acc.GetTotalElapsedTime(clock.Now())); |
| 40 | |
| 41 | acc.Start(clock.Now()); |
| 42 | clock.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 43 | EXPECT_EQ(QuicTime::Delta::FromMilliseconds(10), acc.GetTotalElapsedTime()); |
| 44 | EXPECT_EQ(QuicTime::Delta::FromMilliseconds(15), |
| 45 | acc.GetTotalElapsedTime(clock.Now())); |
| 46 | |
| 47 | clock.AdvanceTime(QuicTime::Delta::FromMilliseconds(5)); |
| 48 | EXPECT_EQ(QuicTime::Delta::FromMilliseconds(10), acc.GetTotalElapsedTime()); |
| 49 | EXPECT_EQ(QuicTime::Delta::FromMilliseconds(20), |
| 50 | acc.GetTotalElapsedTime(clock.Now())); |
| 51 | |
| 52 | acc.Stop(clock.Now()); |
| 53 | EXPECT_EQ(QuicTime::Delta::FromMilliseconds(20), acc.GetTotalElapsedTime()); |
| 54 | EXPECT_EQ(QuicTime::Delta::FromMilliseconds(20), |
| 55 | acc.GetTotalElapsedTime(clock.Now())); |
| 56 | } |
| 57 | |
| 58 | TEST(QuicTimeAccumulator, ClockStepBackwards) { |
| 59 | MockClock clock; |
| 60 | clock.AdvanceTime(QuicTime::Delta::FromMilliseconds(100)); |
| 61 | |
| 62 | QuicTimeAccumulator acc; |
| 63 | acc.Start(clock.Now()); |
| 64 | |
| 65 | clock.AdvanceTime(QuicTime::Delta::FromMilliseconds(-10)); |
| 66 | acc.Stop(clock.Now()); |
| 67 | EXPECT_EQ(QuicTime::Delta::Zero(), acc.GetTotalElapsedTime()); |
| 68 | EXPECT_EQ(QuicTime::Delta::Zero(), acc.GetTotalElapsedTime(clock.Now())); |
| 69 | |
| 70 | acc.Start(clock.Now()); |
| 71 | clock.AdvanceTime(QuicTime::Delta::FromMilliseconds(50)); |
| 72 | acc.Stop(clock.Now()); |
| 73 | |
| 74 | acc.Start(clock.Now()); |
| 75 | clock.AdvanceTime(QuicTime::Delta::FromMilliseconds(-80)); |
| 76 | EXPECT_EQ(QuicTime::Delta::FromMilliseconds(50), acc.GetTotalElapsedTime()); |
| 77 | EXPECT_EQ(QuicTime::Delta::FromMilliseconds(50), |
| 78 | acc.GetTotalElapsedTime(clock.Now())); |
| 79 | } |
| 80 | |
| 81 | } // namespace test |
| 82 | } // namespace quic |