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

Side by Side Diff: chrome/browser/chromeos/policy/status_uploader_unittest.cc

Issue 2296923003: Make TestSimpleTaskRunner thread-safe. (Closed)
Patch Set: fix asan error Created 4 years, 3 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
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. 1 // Copyright (c) 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 "chrome/browser/chromeos/policy/status_uploader.h" 5 #include "chrome/browser/chromeos/policy/status_uploader.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/test/test_simple_task_runner.h" 10 #include "base/test/test_simple_task_runner.h"
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 134
135 // Make sure no status upload is queued up yet (since an upload is in 135 // Make sure no status upload is queued up yet (since an upload is in
136 // progress). 136 // progress).
137 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); 137 EXPECT_TRUE(task_runner_->GetPendingTasks().empty());
138 138
139 // Now invoke the response. 139 // Now invoke the response.
140 callback.Run(true); 140 callback.Run(true);
141 141
142 // Now that the previous request was satisfied, a task to do the next 142 // Now that the previous request was satisfied, a task to do the next
143 // upload should be queued. 143 // upload should be queued.
144 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); 144 EXPECT_EQ(1U, task_runner_->NumPendingTasks());
145 145
146 CheckPendingTaskDelay(uploader, expected_delay); 146 CheckPendingTaskDelay(uploader, expected_delay);
147 } 147 }
148 148
149 void CheckPendingTaskDelay(const StatusUploader& uploader, 149 void CheckPendingTaskDelay(const StatusUploader& uploader,
150 base::TimeDelta expected_delay) { 150 base::TimeDelta expected_delay) {
151 // The next task should be scheduled sometime between |last_upload| + 151 // The next task should be scheduled sometime between |last_upload| +
152 // |expected_delay| and |now| + |expected_delay|. 152 // |expected_delay| and |now| + |expected_delay|.
153 base::Time now = base::Time::NowFromSystemTime(); 153 base::Time now = base::Time::NowFromSystemTime();
154 base::Time next_task = now + task_runner_->NextPendingTaskDelay(); 154 base::Time next_task = now + task_runner_->NextPendingTaskDelay();
155 155
156 EXPECT_LE(next_task, now + expected_delay); 156 EXPECT_LE(next_task, now + expected_delay);
157 EXPECT_GE(next_task, uploader.last_upload() + expected_delay); 157 EXPECT_GE(next_task, uploader.last_upload() + expected_delay);
158 } 158 }
159 159
160 content::TestBrowserThreadBundle thread_bundle_; 160 content::TestBrowserThreadBundle thread_bundle_;
161 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 161 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
162 chromeos::ScopedCrosSettingsTestHelper settings_helper_; 162 chromeos::ScopedCrosSettingsTestHelper settings_helper_;
163 std::unique_ptr<MockDeviceStatusCollector> collector_; 163 std::unique_ptr<MockDeviceStatusCollector> collector_;
164 MockDeviceStatusCollector* collector_ptr_; 164 MockDeviceStatusCollector* collector_ptr_;
165 ui::UserActivityDetector detector_; 165 ui::UserActivityDetector detector_;
166 MockCloudPolicyClient client_; 166 MockCloudPolicyClient client_;
167 MockDeviceManagementService device_management_service_; 167 MockDeviceManagementService device_management_service_;
168 TestingPrefServiceSimple prefs_; 168 TestingPrefServiceSimple prefs_;
169 }; 169 };
170 170
171 TEST_F(StatusUploaderTest, BasicTest) { 171 TEST_F(StatusUploaderTest, BasicTest) {
172 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); 172 EXPECT_TRUE(task_runner_->GetPendingTasks().empty());
173 StatusUploader uploader(&client_, std::move(collector_), task_runner_); 173 StatusUploader uploader(&client_, std::move(collector_), task_runner_);
174 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); 174 EXPECT_EQ(1U, task_runner_->NumPendingTasks());
175 // On startup, first update should happen in 1 minute. 175 // On startup, first update should happen in 1 minute.
176 EXPECT_EQ(base::TimeDelta::FromMinutes(1), 176 EXPECT_EQ(base::TimeDelta::FromMinutes(1),
177 task_runner_->NextPendingTaskDelay()); 177 task_runner_->NextPendingTaskDelay());
178 } 178 }
179 179
180 TEST_F(StatusUploaderTest, DifferentFrequencyAtStart) { 180 TEST_F(StatusUploaderTest, DifferentFrequencyAtStart) {
181 const int new_delay = StatusUploader::kDefaultUploadDelayMs * 2; 181 const int new_delay = StatusUploader::kDefaultUploadDelayMs * 2;
182 settings_helper_.SetInteger(chromeos::kReportUploadFrequency, new_delay); 182 settings_helper_.SetInteger(chromeos::kReportUploadFrequency, new_delay);
183 const base::TimeDelta expected_delay = base::TimeDelta::FromMilliseconds( 183 const base::TimeDelta expected_delay = base::TimeDelta::FromMilliseconds(
184 new_delay); 184 new_delay);
185 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); 185 EXPECT_TRUE(task_runner_->GetPendingTasks().empty());
186 StatusUploader uploader(&client_, std::move(collector_), task_runner_); 186 StatusUploader uploader(&client_, std::move(collector_), task_runner_);
187 ASSERT_EQ(1U, task_runner_->GetPendingTasks().size()); 187 ASSERT_EQ(1U, task_runner_->NumPendingTasks());
188 // On startup, first update should happen in 1 minute. 188 // On startup, first update should happen in 1 minute.
189 EXPECT_EQ(base::TimeDelta::FromMinutes(1), 189 EXPECT_EQ(base::TimeDelta::FromMinutes(1),
190 task_runner_->NextPendingTaskDelay()); 190 task_runner_->NextPendingTaskDelay());
191 191
192 // Second update should use the delay specified in settings. 192 // Second update should use the delay specified in settings.
193 RunPendingUploadTaskAndCheckNext(uploader, expected_delay); 193 RunPendingUploadTaskAndCheckNext(uploader, expected_delay);
194 } 194 }
195 195
196 TEST_F(StatusUploaderTest, ResetTimerAfterStatusCollection) { 196 TEST_F(StatusUploaderTest, ResetTimerAfterStatusCollection) {
197 StatusUploader uploader(&client_, std::move(collector_), task_runner_); 197 StatusUploader uploader(&client_, std::move(collector_), task_runner_);
198 const base::TimeDelta expected_delay = base::TimeDelta::FromMilliseconds( 198 const base::TimeDelta expected_delay = base::TimeDelta::FromMilliseconds(
199 StatusUploader::kDefaultUploadDelayMs); 199 StatusUploader::kDefaultUploadDelayMs);
200 RunPendingUploadTaskAndCheckNext(uploader, expected_delay); 200 RunPendingUploadTaskAndCheckNext(uploader, expected_delay);
201 201
202 // Handle this response also, and ensure new task is queued. 202 // Handle this response also, and ensure new task is queued.
203 RunPendingUploadTaskAndCheckNext(uploader, expected_delay); 203 RunPendingUploadTaskAndCheckNext(uploader, expected_delay);
204 204
205 // Now that the previous request was satisfied, a task to do the next 205 // Now that the previous request was satisfied, a task to do the next
206 // upload should be queued again. 206 // upload should be queued again.
207 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); 207 EXPECT_EQ(1U, task_runner_->NumPendingTasks());
208 } 208 }
209 209
210 TEST_F(StatusUploaderTest, ResetTimerAfterFailedStatusCollection) { 210 TEST_F(StatusUploaderTest, ResetTimerAfterFailedStatusCollection) {
211 StatusUploader uploader(&client_, std::move(collector_), task_runner_); 211 StatusUploader uploader(&client_, std::move(collector_), task_runner_);
212 212
213 // Running the queued task should pass two callbacks into GetDeviceStatusAsync 213 // Running the queued task should pass two callbacks into GetDeviceStatusAsync
214 // and GetDeviceSessionStatusAsync. We'll grab these two callbacks and send 214 // and GetDeviceSessionStatusAsync. We'll grab these two callbacks and send
215 // nullptrs to them in order to simulate failure to get status. 215 // nullptrs to them in order to simulate failure to get status.
216 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); 216 EXPECT_EQ(1U, task_runner_->NumPendingTasks());
217 DeviceStatusCollector::DeviceStatusCallback ds_callback; 217 DeviceStatusCollector::DeviceStatusCallback ds_callback;
218 DeviceStatusCollector::DeviceSessionStatusCallback ss_callback; 218 DeviceStatusCollector::DeviceSessionStatusCallback ss_callback;
219 EXPECT_CALL(*collector_ptr_, GetDeviceStatusAsync(_)) 219 EXPECT_CALL(*collector_ptr_, GetDeviceStatusAsync(_))
220 .WillOnce(SaveArg<0>(&ds_callback)); 220 .WillOnce(SaveArg<0>(&ds_callback));
221 EXPECT_CALL(*collector_ptr_, GetDeviceSessionStatusAsync(_)) 221 EXPECT_CALL(*collector_ptr_, GetDeviceSessionStatusAsync(_))
222 .WillOnce(SaveArg<0>(&ss_callback)); 222 .WillOnce(SaveArg<0>(&ss_callback));
223 task_runner_->RunPendingTasks(); 223 task_runner_->RunPendingTasks();
224 testing::Mock::VerifyAndClearExpectations(&device_management_service_); 224 testing::Mock::VerifyAndClearExpectations(&device_management_service_);
225 225
226 // Running the session and device callbacks should trigger 226 // Running the session and device callbacks should trigger
227 // StatusUploader::OnStatusReceived, which in turn should recognize the 227 // StatusUploader::OnStatusReceived, which in turn should recognize the
228 // failure to get status and queue another status upload. 228 // failure to get status and queue another status upload.
229 std::unique_ptr<em::DeviceStatusReportRequest> invalid_device_status; 229 std::unique_ptr<em::DeviceStatusReportRequest> invalid_device_status;
230 ds_callback.Run(std::move(invalid_device_status)); 230 ds_callback.Run(std::move(invalid_device_status));
231 EXPECT_EQ(0U, task_runner_->GetPendingTasks().size()); // Not yet... 231 EXPECT_EQ(0U, task_runner_->NumPendingTasks()); // Not yet...
232 232
233 std::unique_ptr<em::SessionStatusReportRequest> invalid_session_status; 233 std::unique_ptr<em::SessionStatusReportRequest> invalid_session_status;
234 ss_callback.Run(std::move(invalid_session_status)); 234 ss_callback.Run(std::move(invalid_session_status));
235 EXPECT_EQ(1U, task_runner_->GetPendingTasks().size()); // but now! 235 EXPECT_EQ(1U, task_runner_->NumPendingTasks()); // but now!
236 236
237 // Check the delay of the queued upload 237 // Check the delay of the queued upload
238 const base::TimeDelta expected_delay = base::TimeDelta::FromMilliseconds( 238 const base::TimeDelta expected_delay = base::TimeDelta::FromMilliseconds(
239 StatusUploader::kDefaultUploadDelayMs); 239 StatusUploader::kDefaultUploadDelayMs);
240 CheckPendingTaskDelay(uploader, expected_delay); 240 CheckPendingTaskDelay(uploader, expected_delay);
241 } 241 }
242 242
243 TEST_F(StatusUploaderTest, ChangeFrequency) { 243 TEST_F(StatusUploaderTest, ChangeFrequency) {
244 StatusUploader uploader(&client_, std::move(collector_), task_runner_); 244 StatusUploader uploader(&client_, std::move(collector_), task_runner_);
245 // Change the frequency. The new frequency should be reflected in the timing 245 // Change the frequency. The new frequency should be reflected in the timing
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 // Now mock video capture, and no session data should be allowed. 285 // Now mock video capture, and no session data should be allowed.
286 MediaCaptureDevicesDispatcher::GetInstance()->OnMediaRequestStateChanged( 286 MediaCaptureDevicesDispatcher::GetInstance()->OnMediaRequestStateChanged(
287 0, 0, 0, GURL("http://www.google.com"), 287 0, 0, 0, GURL("http://www.google.com"),
288 content::MEDIA_DEVICE_VIDEO_CAPTURE, 288 content::MEDIA_DEVICE_VIDEO_CAPTURE,
289 content::MEDIA_REQUEST_STATE_OPENING); 289 content::MEDIA_REQUEST_STATE_OPENING);
290 base::RunLoop().RunUntilIdle(); 290 base::RunLoop().RunUntilIdle();
291 EXPECT_FALSE(uploader.IsSessionDataUploadAllowed()); 291 EXPECT_FALSE(uploader.IsSessionDataUploadAllowed());
292 } 292 }
293 293
294 } // namespace policy 294 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698