OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #include "webrtc/p2p/quic/quicconnectionhelper.h" |
| 12 |
| 13 #include "webrtc/base/gunit.h" |
| 14 |
| 15 #include "net/quic/quic_time.h" |
| 16 |
| 17 using cricket::QuicAlarm; |
| 18 using cricket::QuicConnectionHelper; |
| 19 |
| 20 using net::QuicClock; |
| 21 using net::QuicTime; |
| 22 using net::QuicWallTime; |
| 23 |
| 24 // Clock that can be set to arbitrary times. |
| 25 class MockClock : public QuicClock { |
| 26 public: |
| 27 MockClock() : now_(QuicTime::Zero()) {} |
| 28 ~MockClock() override {} |
| 29 |
| 30 void AdvanceTime(QuicTime::Delta delta) { now_ = now_.Add(delta); } |
| 31 |
| 32 QuicTime Now() const override { return now_; } |
| 33 |
| 34 QuicTime ApproximateNow() const override { return now_; } |
| 35 |
| 36 QuicWallTime WallNow() const override { |
| 37 return QuicWallTime::FromUNIXSeconds( |
| 38 now_.Subtract(QuicTime::Zero()).ToSeconds()); |
| 39 } |
| 40 |
| 41 base::TimeTicks NowInTicks() const { |
| 42 base::TimeTicks ticks; |
| 43 return ticks + base::TimeDelta::FromMicroseconds( |
| 44 now_.Subtract(QuicTime::Zero()).ToMicroseconds()); |
| 45 } |
| 46 |
| 47 private: |
| 48 QuicTime now_; |
| 49 }; |
| 50 |
| 51 // Implements OnAlarm() event which alarm triggers. |
| 52 class MockAlarmDelegate : public QuicAlarm::Delegate { |
| 53 public: |
| 54 MockAlarmDelegate() : fired_(false) {} |
| 55 |
| 56 QuicTime OnAlarm() override { |
| 57 fired_ = true; |
| 58 return QuicTime::Zero(); |
| 59 } |
| 60 |
| 61 bool fired() const { return fired_; } |
| 62 void Clear() { fired_ = false; } |
| 63 |
| 64 private: |
| 65 bool fired_; |
| 66 }; |
| 67 |
| 68 class QuicAlarmTest : public ::testing::Test { |
| 69 public: |
| 70 QuicAlarmTest() |
| 71 : delegate_(new MockAlarmDelegate()), |
| 72 alarm_(new QuicAlarm(&clock_, rtc::Thread::Current(), delegate_)) {} |
| 73 |
| 74 virtual ~QuicAlarmTest() {} |
| 75 |
| 76 // Make the alarm fire after the given microseconds (us). Negative values |
| 77 // imply the alarm should fire immediately. |
| 78 void SetTime(int us) { |
| 79 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(us); |
| 80 alarm_->Set(clock_.Now().Add(delta)); |
| 81 } |
| 82 |
| 83 // Make rtc::Thread::Current() process the next message. |
| 84 void ProcessNextMessage() { rtc::Thread::Current()->ProcessMessages(0); } |
| 85 |
| 86 protected: |
| 87 // Handles event that alarm fires. |
| 88 MockAlarmDelegate* delegate_; |
| 89 // Used for setting clock time relative to alarm. |
| 90 MockClock clock_; |
| 91 |
| 92 scoped_ptr<QuicAlarm> alarm_; |
| 93 }; |
| 94 |
| 95 // Test that the alarm is fired. |
| 96 TEST_F(QuicAlarmTest, FireAlarm) { |
| 97 SetTime(-1); |
| 98 ProcessNextMessage(); |
| 99 ASSERT_TRUE(delegate_->fired()); |
| 100 ASSERT_EQ(QuicTime::Zero(), alarm_->deadline()); |
| 101 } |
| 102 |
| 103 // Test cancellation of alarm when it is set to fire. |
| 104 TEST_F(QuicAlarmTest, CancelAlarmAfterSet) { |
| 105 SetTime(-1); |
| 106 alarm_->Cancel(); |
| 107 ProcessNextMessage(); |
| 108 ASSERT_FALSE(delegate_->fired()); |
| 109 } |
| 110 |
| 111 // Test cancellation of alarm when it is not set to fire. |
| 112 TEST_F(QuicAlarmTest, CancelAlarmBeforeSet) { |
| 113 alarm_->Cancel(); |
| 114 ProcessNextMessage(); |
| 115 ASSERT_FALSE(delegate_->fired()); |
| 116 } |
| 117 |
| 118 // Test that timing for posting task is accurate. |
| 119 TEST_F(QuicAlarmTest, AlarmGetDelay) { |
| 120 SetTime(1000000); |
| 121 EXPECT_EQ(1000, alarm_->GetDelay()); |
| 122 clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(300000)); |
| 123 EXPECT_EQ(700, alarm_->GetDelay()); |
| 124 } |
OLD | NEW |