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 MockDelegate : public QuicAlarm::Delegate { | |
pthatcher1
2016/02/03 23:27:23
Can you call this MockAlarmDelegate?
mikescarlett
2016/02/05 21:10:29
Done.
| |
53 public: | |
54 MockDelegate() : 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 MockDelegate()), | |
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 MockDelegate* 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 that QuicClock::OnMessage() fires the alarm. | |
104 TEST_F(QuicAlarmTest, FireAlarmOnMessage) { | |
105 SetTime(-1); | |
106 alarm_->OnMessage(nullptr); | |
pthatcher1
2016/02/03 23:27:23
How is this different than ProcessNextMessage?
mikescarlett
2016/02/05 21:10:29
ProcessNextMessage causes rtc::Thread to process t
| |
107 ASSERT_TRUE(delegate_->fired()); | |
108 } | |
109 | |
110 // Test cancellation of alarm when it is set to fire. | |
111 TEST_F(QuicAlarmTest, CancelAlarmAfterSet) { | |
112 SetTime(-1); | |
113 alarm_->Cancel(); | |
114 ProcessNextMessage(); | |
115 ASSERT_FALSE(delegate_->fired()); | |
116 } | |
117 | |
118 // Test cancellation of alarm when it is not set to fire. | |
119 TEST_F(QuicAlarmTest, CancelAlarmBeforeSet) { | |
120 alarm_->Cancel(); | |
121 ProcessNextMessage(); | |
122 ASSERT_FALSE(delegate_->fired()); | |
123 } | |
124 | |
125 // Test that timing for posting task is accurate. | |
126 TEST_F(QuicAlarmTest, AlarmGetDelay) { | |
127 SetTime(1000000); | |
128 ASSERT_EQ(1000, alarm_->GetDelay()); | |
129 clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(300000)); | |
130 ASSERT_EQ(700, alarm_->GetDelay()); | |
pthatcher1
2016/02/03 23:27:23
These can all be EXPECT_EQ instead of ASSERT_EQ
mikescarlett
2016/02/05 21:10:29
Acknowledged.
| |
131 } | |
OLD | NEW |