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 "net/quic/quic_time.h" |
| 14 #include "webrtc/base/gunit.h" |
| 15 |
| 16 using cricket::QuicAlarm; |
| 17 using cricket::QuicConnectionHelper; |
| 18 |
| 19 using net::QuicClock; |
| 20 using net::QuicTime; |
| 21 using net::QuicWallTime; |
| 22 |
| 23 // Clock that can be set to arbitrary times. |
| 24 class MockClock : public QuicClock { |
| 25 public: |
| 26 MockClock() : now_(QuicTime::Zero()) {} |
| 27 |
| 28 void AdvanceTime(QuicTime::Delta delta) { now_ = now_.Add(delta); } |
| 29 |
| 30 QuicTime Now() const override { return now_; } |
| 31 |
| 32 QuicTime ApproximateNow() const override { return now_; } |
| 33 |
| 34 QuicWallTime WallNow() const override { |
| 35 return QuicWallTime::FromUNIXSeconds( |
| 36 now_.Subtract(QuicTime::Zero()).ToSeconds()); |
| 37 } |
| 38 |
| 39 base::TimeTicks NowInTicks() const { |
| 40 base::TimeTicks ticks; |
| 41 return ticks + base::TimeDelta::FromMicroseconds( |
| 42 now_.Subtract(QuicTime::Zero()).ToMicroseconds()); |
| 43 } |
| 44 |
| 45 private: |
| 46 QuicTime now_; |
| 47 }; |
| 48 |
| 49 // Implements OnAlarm() event which alarm triggers. |
| 50 class MockAlarmDelegate : public QuicAlarm::Delegate { |
| 51 public: |
| 52 MockAlarmDelegate() : fired_(false) {} |
| 53 |
| 54 QuicTime OnAlarm() override { |
| 55 fired_ = true; |
| 56 return QuicTime::Zero(); |
| 57 } |
| 58 |
| 59 bool fired() const { return fired_; } |
| 60 void Clear() { fired_ = false; } |
| 61 |
| 62 private: |
| 63 bool fired_; |
| 64 }; |
| 65 |
| 66 class QuicAlarmTest : public ::testing::Test { |
| 67 public: |
| 68 QuicAlarmTest() |
| 69 : delegate_(new MockAlarmDelegate()), |
| 70 alarm_(new QuicAlarm(&clock_, rtc::Thread::Current(), delegate_)) {} |
| 71 |
| 72 // Make the alarm fire after the given microseconds (us). Negative values |
| 73 // imply the alarm should fire immediately. |
| 74 void SetTime(int us) { |
| 75 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(us); |
| 76 alarm_->Set(clock_.Now().Add(delta)); |
| 77 } |
| 78 |
| 79 // Make rtc::Thread::Current() process the next message. |
| 80 void ProcessNextMessage() { rtc::Thread::Current()->ProcessMessages(0); } |
| 81 |
| 82 protected: |
| 83 // Handles event that alarm fires. |
| 84 MockAlarmDelegate* delegate_; |
| 85 // Used for setting clock time relative to alarm. |
| 86 MockClock clock_; |
| 87 |
| 88 scoped_ptr<QuicAlarm> alarm_; |
| 89 }; |
| 90 |
| 91 // Test that the alarm is fired. |
| 92 TEST_F(QuicAlarmTest, FireAlarm) { |
| 93 SetTime(-1); |
| 94 ProcessNextMessage(); |
| 95 ASSERT_TRUE(delegate_->fired()); |
| 96 ASSERT_EQ(QuicTime::Zero(), alarm_->deadline()); |
| 97 } |
| 98 |
| 99 // Test cancellation of alarm when it is set to fire. |
| 100 TEST_F(QuicAlarmTest, CancelAlarmAfterSet) { |
| 101 // TODO(mikescarlett): Test will fail in the future if |
| 102 // rtc::Thread::PostDelayed calls the delegate synchronously for times <= 0. |
| 103 // Rewrite this when rtc::Thread is able to use a mock clock. |
| 104 SetTime(-1); |
| 105 alarm_->Cancel(); |
| 106 ProcessNextMessage(); |
| 107 ASSERT_FALSE(delegate_->fired()); |
| 108 } |
| 109 |
| 110 // Test cancellation of alarm when it is not set to fire. |
| 111 TEST_F(QuicAlarmTest, CancelAlarmBeforeSet) { |
| 112 alarm_->Cancel(); |
| 113 ProcessNextMessage(); |
| 114 ASSERT_FALSE(delegate_->fired()); |
| 115 } |
| 116 |
| 117 // Test that timing for posting task is accurate. |
| 118 TEST_F(QuicAlarmTest, AlarmGetDelay) { |
| 119 SetTime(1000000); |
| 120 EXPECT_EQ(1000, alarm_->GetDelay()); |
| 121 clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(300000)); |
| 122 EXPECT_EQ(700, alarm_->GetDelay()); |
| 123 } |
OLD | NEW |