Fix "#if defined(DEBUG)" statements
[chromium-blink-merge.git] / net / tools / quic / quic_epoll_connection_helper_test.cc
blob7234ecfc0ac1afbf9764795294253c6994def31c
1 // Copyright (c) 2012 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.
5 #include "net/tools/quic/quic_epoll_connection_helper.h"
7 #include "net/quic/crypto/quic_random.h"
8 #include "net/tools/quic/test_tools/mock_epoll_server.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 using net::tools::test::MockEpollServer;
13 namespace net {
14 namespace tools {
15 namespace test {
16 namespace {
18 class TestDelegate : public QuicAlarm::Delegate {
19 public:
20 TestDelegate() : fired_(false) {}
22 virtual QuicTime OnAlarm() override {
23 fired_ = true;
24 return QuicTime::Zero();
27 bool fired() const { return fired_; }
29 private:
30 bool fired_;
33 class QuicEpollConnectionHelperTest : public ::testing::Test {
34 protected:
35 QuicEpollConnectionHelperTest() : helper_(&epoll_server_) {}
37 MockEpollServer epoll_server_;
38 QuicEpollConnectionHelper helper_;
41 TEST_F(QuicEpollConnectionHelperTest, GetClock) {
42 const QuicClock* clock = helper_.GetClock();
43 QuicTime start = clock->Now();
45 QuicTime::Delta delta = QuicTime::Delta::FromMilliseconds(5);
46 epoll_server_.AdvanceBy(delta.ToMicroseconds());
48 EXPECT_EQ(start.Add(delta), clock->Now());
51 TEST_F(QuicEpollConnectionHelperTest, GetRandomGenerator) {
52 QuicRandom* random = helper_.GetRandomGenerator();
53 EXPECT_EQ(QuicRandom::GetInstance(), random);
56 TEST_F(QuicEpollConnectionHelperTest, CreateAlarm) {
57 TestDelegate* delegate = new TestDelegate();
58 scoped_ptr<QuicAlarm> alarm(helper_.CreateAlarm(delegate));
60 const QuicClock* clock = helper_.GetClock();
61 QuicTime start = clock->Now();
62 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1);
63 alarm->Set(start.Add(delta));
65 epoll_server_.AdvanceByAndCallCallbacks(delta.ToMicroseconds());
66 EXPECT_EQ(start.Add(delta), clock->Now());
69 TEST_F(QuicEpollConnectionHelperTest, CreateAlarmAndCancel) {
70 TestDelegate* delegate = new TestDelegate();
71 scoped_ptr<QuicAlarm> alarm(helper_.CreateAlarm(delegate));
73 const QuicClock* clock = helper_.GetClock();
74 QuicTime start = clock->Now();
75 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1);
76 alarm->Set(start.Add(delta));
77 alarm->Cancel();
79 epoll_server_.AdvanceByExactlyAndCallCallbacks(delta.ToMicroseconds());
80 EXPECT_EQ(start.Add(delta), clock->Now());
81 EXPECT_FALSE(delegate->fired());
84 TEST_F(QuicEpollConnectionHelperTest, CreateAlarmAndReset) {
85 TestDelegate* delegate = new TestDelegate();
86 scoped_ptr<QuicAlarm> alarm(helper_.CreateAlarm(delegate));
88 const QuicClock* clock = helper_.GetClock();
89 QuicTime start = clock->Now();
90 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1);
91 alarm->Set(clock->Now().Add(delta));
92 alarm->Cancel();
93 QuicTime::Delta new_delta = QuicTime::Delta::FromMicroseconds(3);
94 alarm->Set(clock->Now().Add(new_delta));
96 epoll_server_.AdvanceByExactlyAndCallCallbacks(delta.ToMicroseconds());
97 EXPECT_EQ(start.Add(delta), clock->Now());
98 EXPECT_FALSE(delegate->fired());
100 epoll_server_.AdvanceByExactlyAndCallCallbacks(
101 new_delta.Subtract(delta).ToMicroseconds());
102 EXPECT_EQ(start.Add(new_delta), clock->Now());
103 EXPECT_TRUE(delegate->fired());
106 TEST_F(QuicEpollConnectionHelperTest, CreateAlarmAndUpdate) {
107 TestDelegate* delegate = new TestDelegate();
108 scoped_ptr<QuicAlarm> alarm(helper_.CreateAlarm(delegate));
110 const QuicClock* clock = helper_.GetClock();
111 QuicTime start = clock->Now();
112 QuicTime::Delta delta = QuicTime::Delta::FromMicroseconds(1);
113 alarm->Set(clock->Now().Add(delta));
114 QuicTime::Delta new_delta = QuicTime::Delta::FromMicroseconds(3);
115 alarm->Update(clock->Now().Add(new_delta),
116 QuicTime::Delta::FromMicroseconds(1));
118 epoll_server_.AdvanceByExactlyAndCallCallbacks(delta.ToMicroseconds());
119 EXPECT_EQ(start.Add(delta), clock->Now());
120 EXPECT_FALSE(delegate->fired());
122 // Move the alarm forward 1us and ensure it doesn't move forward.
123 alarm->Update(clock->Now().Add(new_delta),
124 QuicTime::Delta::FromMicroseconds(2));
126 epoll_server_.AdvanceByExactlyAndCallCallbacks(
127 new_delta.Subtract(delta).ToMicroseconds());
128 EXPECT_EQ(start.Add(new_delta), clock->Now());
129 EXPECT_TRUE(delegate->fired());
131 // Set the alarm via an update call.
132 new_delta = QuicTime::Delta::FromMicroseconds(5);
133 alarm->Update(clock->Now().Add(new_delta),
134 QuicTime::Delta::FromMicroseconds(1));
135 EXPECT_TRUE(alarm->IsSet());
137 // Update it with an uninitialized time and ensure it's cancelled.
138 alarm->Update(QuicTime::Zero(), QuicTime::Delta::FromMicroseconds(1));
139 EXPECT_FALSE(alarm->IsSet());
142 } // namespace
143 } // namespace test
144 } // namespace tools
145 } // namespace net