Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/offline_pages/core/background/request_coordinator.h" | 5 #include "components/offline_pages/core/background/request_coordinator.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 ~RequestCoordinatorTest() override; | 122 ~RequestCoordinatorTest() override; |
| 123 | 123 |
| 124 void SetUp() override; | 124 void SetUp() override; |
| 125 | 125 |
| 126 void PumpLoop(); | 126 void PumpLoop(); |
| 127 | 127 |
| 128 RequestCoordinator* coordinator() const { | 128 RequestCoordinator* coordinator() const { |
| 129 return coordinator_taco_->request_coordinator(); | 129 return coordinator_taco_->request_coordinator(); |
| 130 } | 130 } |
| 131 | 131 |
| 132 bool is_busy() { return coordinator()->is_busy(); } | 132 bool is_busy() { |
|
Pete Williamson
2017/03/28 00:35:13
Better to get these to return the state, and check
romax
2017/03/28 01:09:56
Done.
| |
| 133 return coordinator()->state() == | |
| 134 RequestCoordinator::RequestCoordinatorState::OFFLINING; | |
| 135 } | |
| 133 | 136 |
| 134 bool is_starting() { return coordinator()->is_starting(); } | 137 bool is_starting() { |
| 138 return coordinator()->state() == | |
| 139 RequestCoordinator::RequestCoordinatorState::STARTING; | |
| 140 } | |
| 135 | 141 |
| 136 // Test processing callback function. | 142 // Test processing callback function. |
| 137 void ProcessingCallbackFunction(bool result) { | 143 void ProcessingCallbackFunction(bool result) { |
| 138 processing_callback_called_ = true; | 144 processing_callback_called_ = true; |
| 139 processing_callback_result_ = result; | 145 processing_callback_result_ = result; |
| 140 } | 146 } |
| 141 | 147 |
| 142 // Callback function which releases a wait for it. | 148 // Callback function which releases a wait for it. |
| 143 void WaitingCallbackFunction(bool result) { waiter_.Signal(); } | 149 void WaitingCallbackFunction(bool result) { waiter_.Signal(); } |
| 144 | 150 |
| (...skipping 1019 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1164 1)); | 1170 1)); |
| 1165 PumpLoop(); | 1171 PumpLoop(); |
| 1166 | 1172 |
| 1167 // Wait for timeout to expire. Use a TaskRunner with a DelayedTaskRunner | 1173 // Wait for timeout to expire. Use a TaskRunner with a DelayedTaskRunner |
| 1168 // which won't time out immediately, so the watchdog thread doesn't kill valid | 1174 // which won't time out immediately, so the watchdog thread doesn't kill valid |
| 1169 // tasks too soon. | 1175 // tasks too soon. |
| 1170 WaitForCallback(); | 1176 WaitForCallback(); |
| 1171 PumpLoop(); | 1177 PumpLoop(); |
| 1172 | 1178 |
| 1173 EXPECT_FALSE(is_starting()); | 1179 EXPECT_FALSE(is_starting()); |
| 1174 EXPECT_FALSE(coordinator()->is_busy()); | 1180 EXPECT_FALSE(is_busy()); |
| 1175 EXPECT_TRUE(OfflinerWasCanceled()); | 1181 EXPECT_TRUE(OfflinerWasCanceled()); |
| 1176 } | 1182 } |
| 1177 | 1183 |
| 1178 TEST_F(RequestCoordinatorTest, | 1184 TEST_F(RequestCoordinatorTest, |
| 1179 WatchdogTimeoutForImmediateProcessingNoLastSnapshot) { | 1185 WatchdogTimeoutForImmediateProcessingNoLastSnapshot) { |
| 1180 // Ensure that the new request does not finish - we simulate it being | 1186 // Ensure that the new request does not finish - we simulate it being |
| 1181 // in progress by asking it to skip making the completion callback. | 1187 // in progress by asking it to skip making the completion callback. |
| 1182 EnableOfflinerCallback(false); | 1188 EnableOfflinerCallback(false); |
| 1183 | 1189 |
| 1184 EXPECT_NE(0, SavePageLater()); | 1190 EXPECT_NE(0, SavePageLater()); |
| 1185 PumpLoop(); | 1191 PumpLoop(); |
| 1186 | 1192 |
| 1187 // Verify that immediate start from adding the request did happen. | 1193 // Verify that immediate start from adding the request did happen. |
| 1188 EXPECT_TRUE(coordinator()->is_busy()); | 1194 EXPECT_TRUE(is_busy()); |
| 1189 | 1195 |
| 1190 // Advance the mock clock 1 second before the watchdog timeout. | 1196 // Advance the mock clock 1 second before the watchdog timeout. |
| 1191 AdvanceClockBy(base::TimeDelta::FromSeconds( | 1197 AdvanceClockBy(base::TimeDelta::FromSeconds( |
| 1192 coordinator() | 1198 coordinator() |
| 1193 ->policy() | 1199 ->policy() |
| 1194 ->GetSinglePageTimeLimitForImmediateLoadInSeconds() - | 1200 ->GetSinglePageTimeLimitForImmediateLoadInSeconds() - |
| 1195 1)); | 1201 1)); |
| 1196 PumpLoop(); | 1202 PumpLoop(); |
| 1197 | 1203 |
| 1198 // Verify still busy. | 1204 // Verify still busy. |
| 1199 EXPECT_TRUE(coordinator()->is_busy()); | 1205 EXPECT_TRUE(is_busy()); |
| 1200 EXPECT_FALSE(OfflinerWasCanceled()); | 1206 EXPECT_FALSE(OfflinerWasCanceled()); |
| 1201 | 1207 |
| 1202 // Advance the mock clock past the watchdog timeout now. | 1208 // Advance the mock clock past the watchdog timeout now. |
| 1203 AdvanceClockBy(base::TimeDelta::FromSeconds(2)); | 1209 AdvanceClockBy(base::TimeDelta::FromSeconds(2)); |
| 1204 PumpLoop(); | 1210 PumpLoop(); |
| 1205 | 1211 |
| 1206 // Verify the request timed out. | 1212 // Verify the request timed out. |
| 1207 EXPECT_TRUE(OfflinerWasCanceled()); | 1213 EXPECT_TRUE(OfflinerWasCanceled()); |
| 1208 } | 1214 } |
| 1209 | 1215 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1253 AddRequest2(); | 1259 AddRequest2(); |
| 1254 PumpLoop(); | 1260 PumpLoop(); |
| 1255 | 1261 |
| 1256 // Set up for the call to StartScheduledProcessing. | 1262 // Set up for the call to StartScheduledProcessing. |
| 1257 EnableOfflinerCallback(false); | 1263 EnableOfflinerCallback(false); |
| 1258 | 1264 |
| 1259 // Sending the request to the offliner. | 1265 // Sending the request to the offliner. |
| 1260 EXPECT_TRUE(coordinator()->StartScheduledProcessing(device_conditions(), | 1266 EXPECT_TRUE(coordinator()->StartScheduledProcessing(device_conditions(), |
| 1261 waiting_callback())); | 1267 waiting_callback())); |
| 1262 PumpLoop(); | 1268 PumpLoop(); |
| 1263 EXPECT_TRUE(coordinator()->is_busy()); | 1269 EXPECT_TRUE(is_busy()); |
| 1264 | 1270 |
| 1265 // Now lose the network connection. | 1271 // Now lose the network connection. |
| 1266 SetNetworkConnected(false); | 1272 SetNetworkConnected(false); |
| 1267 | 1273 |
| 1268 // Complete first request and then TryNextRequest should decide not | 1274 // Complete first request and then TryNextRequest should decide not |
| 1269 // to pick another request (because of no network connection). | 1275 // to pick another request (because of no network connection). |
| 1270 SendOfflinerDoneCallback(request1, Offliner::RequestStatus::SAVED); | 1276 SendOfflinerDoneCallback(request1, Offliner::RequestStatus::SAVED); |
| 1271 PumpLoop(); | 1277 PumpLoop(); |
| 1272 | 1278 |
| 1273 // Not starting nor busy with next request. | 1279 // Not starting nor busy with next request. |
| 1274 EXPECT_FALSE(coordinator()->is_starting()); | 1280 EXPECT_FALSE(is_starting()); |
| 1275 EXPECT_FALSE(coordinator()->is_busy()); | 1281 EXPECT_FALSE(is_busy()); |
| 1276 | 1282 |
| 1277 // Get queued requests. | 1283 // Get queued requests. |
| 1278 coordinator()->queue()->GetRequests(base::Bind( | 1284 coordinator()->queue()->GetRequests(base::Bind( |
| 1279 &RequestCoordinatorTest::GetRequestsDone, base::Unretained(this))); | 1285 &RequestCoordinatorTest::GetRequestsDone, base::Unretained(this))); |
| 1280 PumpLoop(); | 1286 PumpLoop(); |
| 1281 | 1287 |
| 1282 // We should find one request in the queue. | 1288 // We should find one request in the queue. |
| 1283 EXPECT_EQ(1UL, last_requests().size()); | 1289 EXPECT_EQ(1UL, last_requests().size()); |
| 1284 } | 1290 } |
| 1285 | 1291 |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1545 // Repeat the timeout for MaxCompleteTries - 1 times in order to increase the | 1551 // Repeat the timeout for MaxCompleteTries - 1 times in order to increase the |
| 1546 // completed tries on this request. | 1552 // completed tries on this request. |
| 1547 int max_tries = coordinator()->policy()->GetMaxCompletedTries(); | 1553 int max_tries = coordinator()->policy()->GetMaxCompletedTries(); |
| 1548 for (int i = 0; i < max_tries - 1; i++) { | 1554 for (int i = 0; i < max_tries - 1; i++) { |
| 1549 PumpLoop(); | 1555 PumpLoop(); |
| 1550 // Reset states. | 1556 // Reset states. |
| 1551 ResetOfflinerWasCanceled(); | 1557 ResetOfflinerWasCanceled(); |
| 1552 observer().Clear(); | 1558 observer().Clear(); |
| 1553 | 1559 |
| 1554 // Verify that the request is being processed. | 1560 // Verify that the request is being processed. |
| 1555 EXPECT_TRUE(coordinator()->is_busy()); | 1561 EXPECT_TRUE(is_busy()); |
| 1556 | 1562 |
| 1557 // Advance the mock clock 1 second more than the watchdog timeout. | 1563 // Advance the mock clock 1 second more than the watchdog timeout. |
| 1558 AdvanceClockBy(base::TimeDelta::FromSeconds( | 1564 AdvanceClockBy(base::TimeDelta::FromSeconds( |
| 1559 coordinator() | 1565 coordinator() |
| 1560 ->policy() | 1566 ->policy() |
| 1561 ->GetSinglePageTimeLimitForImmediateLoadInSeconds() + | 1567 ->GetSinglePageTimeLimitForImmediateLoadInSeconds() + |
| 1562 1)); | 1568 1)); |
| 1563 PumpLoop(); | 1569 PumpLoop(); |
| 1564 | 1570 |
| 1565 // Verify the request timed out. | 1571 // Verify the request timed out. |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 1582 PumpLoop(); | 1588 PumpLoop(); |
| 1583 | 1589 |
| 1584 // The last time would trigger the snapshot on last retry and succeed. | 1590 // The last time would trigger the snapshot on last retry and succeed. |
| 1585 EXPECT_FALSE(OfflinerWasCanceled()); | 1591 EXPECT_FALSE(OfflinerWasCanceled()); |
| 1586 EXPECT_EQ(RequestCoordinator::BackgroundSavePageResult::SUCCESS, | 1592 EXPECT_EQ(RequestCoordinator::BackgroundSavePageResult::SUCCESS, |
| 1587 observer().last_status()); | 1593 observer().last_status()); |
| 1588 EXPECT_TRUE(observer().completed_called()); | 1594 EXPECT_TRUE(observer().completed_called()); |
| 1589 } | 1595 } |
| 1590 | 1596 |
| 1591 } // namespace offline_pages | 1597 } // namespace offline_pages |
| OLD | NEW |