Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(224)

Side by Side Diff: remoting/test/connection_time_observer_unittest.cc

Issue 1238343002: Added ConnectionTimeObserver to calculate the times to authenticate and connect. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Moved ConnectionStateToFriendlyString to ConnectionToHostImpl and Added environment class to main. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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 "remoting/test/connection_time_observer.h"
6
7 #include <utility>
8
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "base/time/time.h"
12 #include "base/timer/timer.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace remoting {
16 namespace test {
17
18 class ConnectionTimeObserverTest : public ::testing::Test {
19 public:
20 ConnectionTimeObserverTest();
21 ~ConnectionTimeObserverTest() override;
22
23 protected:
24 // Test interface.
25 void SetUp() override;
26
27 // A map of fake times to calculate TimeDelta between two states.
28 std::map<protocol::ConnectionToHost::State, base::TimeTicks> test_map_;
joedow 2015/07/28 23:09:05 please add newline here
tonychun 2015/07/29 16:10:14 Done.
29 private:
30 DISALLOW_COPY_AND_ASSIGN(ConnectionTimeObserverTest);
31 };
32
33 ConnectionTimeObserverTest::ConnectionTimeObserverTest() {
34 }
35
36 ConnectionTimeObserverTest::~ConnectionTimeObserverTest() {
37 }
38
39 void ConnectionTimeObserverTest::SetUp() {
40 base::TimeTicks now = base::TimeTicks::Now();
41 test_map_.insert(std::make_pair(
42 protocol::ConnectionToHost::State::INITIALIZING,
43 now + base::TimeDelta::FromMilliseconds(10)));
44 test_map_.insert(std::make_pair(
45 protocol::ConnectionToHost::State::CONNECTING,
46 now + base::TimeDelta::FromMilliseconds(20)));
47 test_map_.insert(std::make_pair(
48 protocol::ConnectionToHost::State::AUTHENTICATED,
49 now + base::TimeDelta::FromMilliseconds(30)));
50 test_map_.insert(std::make_pair(
51 protocol::ConnectionToHost::State::CONNECTED,
52 now + base::TimeDelta::FromMilliseconds(40)));
53 test_map_.insert(std::make_pair(
54 protocol::ConnectionToHost::State::CLOSED,
55 now + base::TimeDelta::FromMilliseconds(50)));
56 }
57
58 TEST_F(ConnectionTimeObserverTest, ChromotingConnectionSuccess) {
59 ConnectionTimeObserver connection_time_observer;
60
61 connection_time_observer.SetTransitionTimesMapForTest(test_map_);
62
63 EXPECT_EQ(connection_time_observer.GetStateTransitionTime(
64 protocol::ConnectionToHost::State::INITIALIZING,
65 protocol::ConnectionToHost::State::CONNECTING).InMilliseconds(), 10);
66 EXPECT_EQ(connection_time_observer.GetStateTransitionTime(
67 protocol::ConnectionToHost::State::CONNECTING,
68 protocol::ConnectionToHost::State::AUTHENTICATED).InMilliseconds(), 10);
69 EXPECT_EQ(connection_time_observer.GetStateTransitionTime(
70 protocol::ConnectionToHost::State::AUTHENTICATED,
71 protocol::ConnectionToHost::State::CONNECTED).InMilliseconds(), 10);
72 EXPECT_EQ(connection_time_observer.GetStateTransitionTime(
73 protocol::ConnectionToHost::State::CONNECTED,
74 protocol::ConnectionToHost::State::CLOSED).InMilliseconds(), 10);
75 }
76
77 TEST_F(ConnectionTimeObserverTest, StartStateNotFound) {
78 ConnectionTimeObserver connection_time_observer;
79
80 connection_time_observer.SetTransitionTimesMapForTest(test_map_);
81
82 EXPECT_TRUE(connection_time_observer.GetStateTransitionTime(
83 protocol::ConnectionToHost::State::FAILED,
84 protocol::ConnectionToHost::State::CLOSED).is_max());
85 }
86
87 TEST_F(ConnectionTimeObserverTest, EndStateNotFound) {
88 ConnectionTimeObserver connection_time_observer;
89
90 connection_time_observer.SetTransitionTimesMapForTest(test_map_);
91
92 EXPECT_TRUE(connection_time_observer.GetStateTransitionTime(
93 protocol::ConnectionToHost::State::INITIALIZING,
94 protocol::ConnectionToHost::State::FAILED).is_max());
95 }
96
97 TEST_F(ConnectionTimeObserverTest, NegativeTransitionDelay) {
98 ConnectionTimeObserver connection_time_observer;
99
100 connection_time_observer.SetTransitionTimesMapForTest(test_map_);
101
102 EXPECT_TRUE(connection_time_observer.GetStateTransitionTime(
103 protocol::ConnectionToHost::State::CLOSED,
104 protocol::ConnectionToHost::State::INITIALIZING).is_max());
105 }
106
107 TEST_F(ConnectionTimeObserverTest, TestOnConnectionStateChangedWithTestMap) {
108 ConnectionTimeObserver connection_time_observer;
joedow 2015/07/28 23:09:05 Can you move the connection_time_observer into the
tonychun 2015/07/29 16:10:13 Done.
109
110 connection_time_observer.SetTransitionTimesMapForTest(test_map_);
111
112 // Should fail to find FAILED.
113 EXPECT_TRUE(connection_time_observer.GetStateTransitionTime(
114 protocol::ConnectionToHost::State::INITIALIZING,
115 protocol::ConnectionToHost::State::FAILED).is_max());
116
117 // Registers the time at which FAILED state occurred into the map.
118 connection_time_observer.ConnectionStateChanged(
119 protocol::ConnectionToHost::State::FAILED,
120 protocol::ErrorCode::PEER_IS_OFFLINE);
121
122 EXPECT_FALSE(connection_time_observer.GetStateTransitionTime(
123 protocol::ConnectionToHost::State::INITIALIZING,
124 protocol::ConnectionToHost::State::FAILED).is_zero());
125 }
126
127 TEST_F(ConnectionTimeObserverTest, TestOnConnectionStateChangedWithoutTestMap) {
128 ConnectionTimeObserver connection_time_observer;
129
130 base::MessageLoopForIO message_loop;
131 base::RunLoop run_loop;
132
133 // Should fail to find INITIALIZING in an empty map.
134 EXPECT_TRUE(connection_time_observer.GetStateTransitionTime(
135 protocol::ConnectionToHost::State::INITIALIZING,
136 protocol::ConnectionToHost::State::FAILED).is_max());
137
138 connection_time_observer.ConnectionStateChanged(
139 protocol::ConnectionToHost::State::INITIALIZING,
140 protocol::ErrorCode::OK);
141 connection_time_observer.ConnectionStateChanged(
142 protocol::ConnectionToHost::State::CONNECTING,
143 protocol::ErrorCode::OK);
144 connection_time_observer.ConnectionStateChanged(
145 protocol::ConnectionToHost::State::AUTHENTICATED,
146 protocol::ErrorCode::OK);
147
148 // Wait for 10 milliseconds for CONNECTED state.
149 // Note: This test can only guarantee a positive TimeDelta between a previous
150 // state and the CONNECTED state. Prior states have non-deterministic times
151 // between each other.
152 base::Timer timer(true, false);
153 timer.Start(FROM_HERE,
154 base::TimeDelta::FromMilliseconds(10),
155 run_loop.QuitClosure());
156 run_loop.Run();
157
158 connection_time_observer.ConnectionStateChanged(
159 protocol::ConnectionToHost::State::CONNECTED,
160 protocol::ErrorCode::OK);
161
162 // Verify that the time waited is positive and at least 10 milliseconds.
163 EXPECT_GE(connection_time_observer.GetStateTransitionTime(
164 protocol::ConnectionToHost::State::AUTHENTICATED,
165 protocol::ConnectionToHost::State::CONNECTED).InMilliseconds(), 10);
166 }
167
168 TEST_F(ConnectionTimeObserverTest, TestOnConnectionStateDataDoesNotChange) {
169 ConnectionTimeObserver connection_time_observer;
170
171 connection_time_observer.SetTransitionTimesMapForTest(test_map_);
172
173 base::TimeDelta first_time_delta =
174 connection_time_observer.GetStateTransitionTime(
175 protocol::ConnectionToHost::State::INITIALIZING,
176 protocol::ConnectionToHost::State::CONNECTED);
177
178 // This check makes sure that the two states are actually found.
179 EXPECT_FALSE(first_time_delta.is_zero());
180
181 // The initial data should not change and should log an error.
182 connection_time_observer.ConnectionStateChanged(
183 protocol::ConnectionToHost::State::CONNECTED, protocol::ErrorCode::OK);
184
185 EXPECT_EQ(connection_time_observer.GetStateTransitionTime(
186 protocol::ConnectionToHost::State::INITIALIZING,
187 protocol::ConnectionToHost::State::CONNECTED), first_time_delta);
188 }
189
190 } // namespace test
191 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698