OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/test/chromoting_test_driver_environment.h" | 5 #include "remoting/test/chromoting_test_driver_environment.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 if (!RetrieveAccessToken(auth_code)) { | 79 if (!RetrieveAccessToken(auth_code)) { |
80 // If we cannot retrieve an access token, then nothing is going to work. | 80 // If we cannot retrieve an access token, then nothing is going to work. |
81 // Let the caller know that our object is not ready to be used. | 81 // Let the caller know that our object is not ready to be used. |
82 return false; | 82 return false; |
83 } | 83 } |
84 | 84 |
85 return true; | 85 return true; |
86 } | 86 } |
87 | 87 |
88 void ChromotingTestDriverEnvironment::DisplayHostList() { | 88 void ChromotingTestDriverEnvironment::DisplayHostList() { |
89 const char kHostAvailabilityFormatString[] = "%-45s%-15s%-35s"; | 89 const char kHostAvailabilityFormatString[] = "%-25s%-15s%-35s\n"; |
90 | 90 |
91 LOG(INFO) << base::StringPrintf(kHostAvailabilityFormatString, | 91 printf(kHostAvailabilityFormatString, "Host Name", "Host Status", "Host JID"); |
92 "Host Name", "Host Status", "Host JID"); | 92 printf(kHostAvailabilityFormatString, "---------", "-----------", "--------"); |
93 LOG(INFO) << base::StringPrintf(kHostAvailabilityFormatString, | |
94 "---------", "-----------", "--------"); | |
95 | 93 |
96 std::string status; | 94 std::string status; |
97 for (const HostInfo& host_info : host_list_) { | 95 for (const HostInfo& host_info : host_list_) { |
98 HostStatus host_status = host_info.status; | 96 HostStatus host_status = host_info.status; |
99 if (host_status == kHostStatusOnline) { | 97 if (host_status == kHostStatusOnline) { |
100 status = "ONLINE"; | 98 status = "ONLINE"; |
101 } else if (host_status == kHostStatusOffline) { | 99 } else if (host_status == kHostStatusOffline) { |
102 status = "OFFLINE"; | 100 status = "OFFLINE"; |
103 } else { | 101 } else { |
104 status = "UNKNOWN"; | 102 status = "UNKNOWN"; |
105 } | 103 } |
106 | 104 |
107 LOG(INFO) << base::StringPrintf( | 105 printf(kHostAvailabilityFormatString, host_info.host_name.c_str(), |
108 kHostAvailabilityFormatString, host_info.host_name.c_str(), | 106 status.c_str(), host_info.host_jid.c_str()); |
109 status.c_str(), host_info.host_jid.c_str()); | |
110 } | 107 } |
111 } | 108 } |
112 | 109 |
113 bool ChromotingTestDriverEnvironment::WaitForHostOnline( | 110 bool ChromotingTestDriverEnvironment::WaitForHostOnline() { |
114 const std::string& host_jid, | |
115 const std::string& host_name) { | |
116 if (host_list_.empty()) { | 111 if (host_list_.empty()) { |
117 RetrieveHostList(); | 112 RetrieveHostList(); |
118 } | 113 } |
119 | 114 |
| 115 DisplayHostList(); |
| 116 |
120 // Refresh the |host_list_| periodically to check if expected JID is online. | 117 // Refresh the |host_list_| periodically to check if expected JID is online. |
121 const base::TimeDelta kTotalTimeInSeconds = base::TimeDelta::FromSeconds(60); | 118 const base::TimeDelta kTotalTimeInSeconds = base::TimeDelta::FromSeconds(60); |
122 const base::TimeDelta kSleepTimeInSeconds = base::TimeDelta::FromSeconds(5); | 119 const base::TimeDelta kSleepTimeInSeconds = base::TimeDelta::FromSeconds(5); |
123 const int kMaxIterations = kTotalTimeInSeconds / kSleepTimeInSeconds; | 120 const int kMaxIterations = kTotalTimeInSeconds / kSleepTimeInSeconds; |
124 | 121 |
125 int num_iterations = 0; | 122 for (int iterations = 0; iterations < kMaxIterations; iterations++) { |
126 while (num_iterations < kMaxIterations) { | 123 if (!FindHostInHostList()) { |
| 124 LOG(WARNING) << "Host '" << host_name_ << "' with JID '" << host_jid_ |
| 125 << "' not found in host list."; |
| 126 return false; |
| 127 } |
| 128 |
127 if (host_info_.IsReadyForConnection()) { | 129 if (host_info_.IsReadyForConnection()) { |
128 if (num_iterations > 0) { | 130 if (iterations > 0) { |
129 VLOG(0) << "Host online after: " | 131 VLOG(0) << "Host online after: " |
130 << num_iterations * kSleepTimeInSeconds.InSeconds() | 132 << iterations * kSleepTimeInSeconds.InSeconds() << " seconds."; |
131 << " seconds."; | |
132 } | 133 } |
133 return true; | 134 return true; |
134 } | 135 } |
135 | 136 |
136 // Wait a while before refreshing host list. | 137 // Wait a while before refreshing host list. |
137 base::PlatformThread::Sleep(kSleepTimeInSeconds); | 138 base::PlatformThread::Sleep(kSleepTimeInSeconds); |
138 RefreshHostList(); | 139 RefreshHostList(); |
139 ++num_iterations; | |
140 } | 140 } |
141 | 141 |
142 LOG(ERROR) << "Host with JID '" << host_jid << "' still not online after " | 142 LOG(ERROR) << "Host '" << host_name_ << "' with JID '" << host_jid_ |
143 << num_iterations * kSleepTimeInSeconds.InSeconds() << " seconds."; | 143 << "' still not online after " |
| 144 << kMaxIterations * kSleepTimeInSeconds.InSeconds() << " seconds."; |
144 return false; | 145 return false; |
145 } | 146 } |
146 | 147 |
| 148 bool ChromotingTestDriverEnvironment::FindHostInHostList() { |
| 149 bool host_found = false; |
| 150 for (HostInfo& host_info : host_list_) { |
| 151 // The JID is optional so we consider an empty string to be a '*' match. |
| 152 bool host_jid_match = |
| 153 host_jid_.empty() || (host_jid_ == host_info.host_jid); |
| 154 bool host_name_match = host_name_ == host_info.host_name; |
| 155 |
| 156 if (host_name_match && host_jid_match) { |
| 157 host_info_ = host_info; |
| 158 host_found = true; |
| 159 break; |
| 160 } |
| 161 } |
| 162 return host_found; |
| 163 } |
| 164 |
147 void ChromotingTestDriverEnvironment::SetAccessTokenFetcherForTest( | 165 void ChromotingTestDriverEnvironment::SetAccessTokenFetcherForTest( |
148 AccessTokenFetcher* access_token_fetcher) { | 166 AccessTokenFetcher* access_token_fetcher) { |
149 DCHECK(access_token_fetcher); | 167 DCHECK(access_token_fetcher); |
150 | 168 |
151 test_access_token_fetcher_ = access_token_fetcher; | 169 test_access_token_fetcher_ = access_token_fetcher; |
152 } | 170 } |
153 | 171 |
154 void ChromotingTestDriverEnvironment::SetRefreshTokenStoreForTest( | 172 void ChromotingTestDriverEnvironment::SetRefreshTokenStoreForTest( |
155 RefreshTokenStore* refresh_token_store) { | 173 RefreshTokenStore* refresh_token_store) { |
156 DCHECK(refresh_token_store); | 174 DCHECK(refresh_token_store); |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 | 317 |
300 run_loop.Run(); | 318 run_loop.Run(); |
301 | 319 |
302 if (host_list_.empty()) { | 320 if (host_list_.empty()) { |
303 // Note: Access token may have expired, but it is unlikely. | 321 // Note: Access token may have expired, but it is unlikely. |
304 LOG(ERROR) << "Retrieved host list is empty.\n" | 322 LOG(ERROR) << "Retrieved host list is empty.\n" |
305 << "Does the account have hosts set up?"; | 323 << "Does the account have hosts set up?"; |
306 return false; | 324 return false; |
307 } | 325 } |
308 | 326 |
309 DisplayHostList(); | 327 return true; |
310 for (HostInfo& host_info : host_list_) { | |
311 // The JID is optional so we consider an empty string to be a '*' match. | |
312 bool host_jid_match = | |
313 host_jid_.empty() || (host_jid_ == host_info.host_jid); | |
314 bool host_name_match = host_name_ == host_info.host_name; | |
315 | |
316 if (host_name_match && host_jid_match) { | |
317 host_info_ = host_info; | |
318 | |
319 if (host_info.IsReadyForConnection()) { | |
320 return true; | |
321 } else { | |
322 LOG(WARNING) << "Host '" << host_name_ << "' with JID '" << host_jid_ | |
323 << "' not online."; | |
324 return false; | |
325 } | |
326 } | |
327 } | |
328 LOG(WARNING) << "Host '" << host_name_ << "' with JID '" << host_jid_ | |
329 << "' not found in host list."; | |
330 return false; | |
331 } | 328 } |
332 | 329 |
333 void ChromotingTestDriverEnvironment::OnHostListRetrieved( | 330 void ChromotingTestDriverEnvironment::OnHostListRetrieved( |
334 base::Closure done_closure, | 331 base::Closure done_closure, |
335 const std::vector<HostInfo>& retrieved_host_list) { | 332 const std::vector<HostInfo>& retrieved_host_list) { |
336 VLOG(1) << "OnHostListRetrieved() Called"; | 333 VLOG(1) << "OnHostListRetrieved() Called"; |
337 | 334 |
338 host_list_ = retrieved_host_list; | 335 host_list_ = retrieved_host_list; |
339 | 336 |
340 done_closure.Run(); | 337 done_closure.Run(); |
341 } | 338 } |
342 | 339 |
343 } // namespace test | 340 } // namespace test |
344 } // namespace remoting | 341 } // namespace remoting |
OLD | NEW |