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

Side by Side Diff: components/offline_pages/background/request_coordinator_unittest.cc

Issue 2119613002: Adds a prerenderer watchdog timer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: CR feedback per DougArnett and DewittJ Created 4 years, 5 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
« no previous file with comments | « components/offline_pages/background/request_coordinator.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/background/request_coordinator.h" 5 #include "components/offline_pages/background/request_coordinator.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/location.h" 12 #include "base/location.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/test/test_simple_task_runner.h" 14 #include "base/synchronization/waitable_event.h"
15 #include "base/test/test_mock_time_task_runner.h"
15 #include "base/threading/thread_task_runner_handle.h" 16 #include "base/threading/thread_task_runner_handle.h"
17 #include "base/time/time.h"
16 #include "components/offline_pages/background/device_conditions.h" 18 #include "components/offline_pages/background/device_conditions.h"
17 #include "components/offline_pages/background/offliner.h" 19 #include "components/offline_pages/background/offliner.h"
18 #include "components/offline_pages/background/offliner_factory.h" 20 #include "components/offline_pages/background/offliner_factory.h"
19 #include "components/offline_pages/background/offliner_policy.h" 21 #include "components/offline_pages/background/offliner_policy.h"
20 #include "components/offline_pages/background/request_queue.h" 22 #include "components/offline_pages/background/request_queue.h"
21 #include "components/offline_pages/background/request_queue_in_memory_store.h" 23 #include "components/offline_pages/background/request_queue_in_memory_store.h"
22 #include "components/offline_pages/background/save_page_request.h" 24 #include "components/offline_pages/background/save_page_request.h"
23 #include "components/offline_pages/background/scheduler.h" 25 #include "components/offline_pages/background/scheduler.h"
24 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
25 27
26 namespace offline_pages { 28 namespace offline_pages {
27 29
28 namespace { 30 namespace {
29 // put test constants here 31 // put test constants here
30 const GURL kUrl("http://universe.com/everything"); 32 const GURL kUrl("http://universe.com/everything");
31 const ClientId kClientId("bookmark", "42"); 33 const ClientId kClientId("bookmark", "42");
32 const int kRequestId(1); 34 const int kRequestId(1);
35 const long TEST_TIMEOUT_SECONDS = 1;
36 const int BATTERY_PERCENTAGE_HIGH = 75;
37 const bool POWER_REQUIRED = true;
dewittj 2016/07/01 17:27:48 nit: looks like there is inconsistent naming: kFo
Pete Williamson 2016/07/01 17:56:44 Done. Right you are, old habits creeping back in.
33 } // namespace 38 } // namespace
34 39
35 class SchedulerStub : public Scheduler { 40 class SchedulerStub : public Scheduler {
36 public: 41 public:
37 SchedulerStub() 42 SchedulerStub()
38 : schedule_called_(false), 43 : schedule_called_(false),
39 unschedule_called_(false), 44 unschedule_called_(false),
40 conditions_(false, 0, false) {} 45 conditions_(false, 0, false) {}
41 46
42 void Schedule(const TriggerConditions& trigger_conditions) override { 47 void Schedule(const TriggerConditions& trigger_conditions) override {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 void PumpLoop(); 130 void PumpLoop();
126 131
127 RequestCoordinator* coordinator() { 132 RequestCoordinator* coordinator() {
128 return coordinator_.get(); 133 return coordinator_.get();
129 } 134 }
130 135
131 bool is_busy() { 136 bool is_busy() {
132 return coordinator_->is_busy(); 137 return coordinator_->is_busy();
133 } 138 }
134 139
135 void SendRequestToOffliner(SavePageRequest& request) {
136 coordinator_->SendRequestToOffliner(request);
137 }
138
139 // Empty callback function 140 // Empty callback function
140 void EmptyCallbackFunction(bool result) { 141 void EmptyCallbackFunction(bool result) {
141 } 142 }
142 143
144 // Callback function which releases a wait for it.
145 void WaitingCallbackFunction(bool result) {
146 waiter_.Signal();
147 }
148
143 // Callback for Add requests 149 // Callback for Add requests
144 void AddRequestDone(RequestQueue::AddRequestResult result, 150 void AddRequestDone(RequestQueue::AddRequestResult result,
145 const SavePageRequest& request); 151 const SavePageRequest& request);
146 152
147 // Callback for getting requests. 153 // Callback for getting requests.
148 void GetRequestsDone(RequestQueue::GetRequestsResult result, 154 void GetRequestsDone(RequestQueue::GetRequestsResult result,
149 const std::vector<SavePageRequest>& requests); 155 const std::vector<SavePageRequest>& requests);
150 156
151 void SendOfflinerDoneCallback(const SavePageRequest& request, 157 void SendOfflinerDoneCallback(const SavePageRequest& request,
152 Offliner::RequestStatus status); 158 Offliner::RequestStatus status);
153 159
154 RequestQueue::GetRequestsResult last_get_requests_result() const { 160 RequestQueue::GetRequestsResult last_get_requests_result() const {
155 return last_get_requests_result_; 161 return last_get_requests_result_;
156 } 162 }
157 163
158 const std::vector<SavePageRequest>& last_requests() const { 164 const std::vector<SavePageRequest>& last_requests() const {
159 return last_requests_; 165 return last_requests_;
160 } 166 }
161 167
162 void EnableOfflinerCallback(bool enable) { 168 void EnableOfflinerCallback(bool enable) {
163 offliner_->enable_callback(enable); 169 offliner_->enable_callback(enable);
164 } 170 }
165 171
172 void SetOfflinerTimeoutForTest(const base::TimeDelta& timeout) {
173 coordinator_->SetOfflinerTimeoutForTest(timeout);
174 }
175
176 void WaitForCallback() {
177 waiter_.Wait();
178 }
179
180 void AdvanceClockBy(base::TimeDelta delta) {
181 task_runner_->FastForwardBy(delta);
182 }
183
166 Offliner::RequestStatus last_offlining_status() const { 184 Offliner::RequestStatus last_offlining_status() const {
167 return coordinator_->last_offlining_status_; 185 return coordinator_->last_offlining_status_;
168 } 186 }
169 187
170 private: 188 private:
171 RequestQueue::GetRequestsResult last_get_requests_result_; 189 RequestQueue::GetRequestsResult last_get_requests_result_;
172 std::vector<SavePageRequest> last_requests_; 190 std::vector<SavePageRequest> last_requests_;
173 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 191 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
174 base::ThreadTaskRunnerHandle task_runner_handle_; 192 base::ThreadTaskRunnerHandle task_runner_handle_;
175 std::unique_ptr<RequestCoordinator> coordinator_; 193 std::unique_ptr<RequestCoordinator> coordinator_;
176 OfflinerStub* offliner_; 194 OfflinerStub* offliner_;
195 base::WaitableEvent waiter_;
177 }; 196 };
178 197
179 RequestCoordinatorTest::RequestCoordinatorTest() 198 RequestCoordinatorTest::RequestCoordinatorTest()
180 : last_get_requests_result_(RequestQueue::GetRequestsResult::STORE_FAILURE), 199 : last_get_requests_result_(RequestQueue::GetRequestsResult::STORE_FAILURE),
181 task_runner_(new base::TestSimpleTaskRunner), 200 task_runner_(new base::TestMockTimeTaskRunner),
182 task_runner_handle_(task_runner_), 201 task_runner_handle_(task_runner_),
183 offliner_(nullptr) {} 202 offliner_(nullptr),
203 waiter_(base::WaitableEvent::ResetPolicy::MANUAL,
204 base::WaitableEvent::InitialState::NOT_SIGNALED) {}
184 205
185 RequestCoordinatorTest::~RequestCoordinatorTest() {} 206 RequestCoordinatorTest::~RequestCoordinatorTest() {}
186 207
187 void RequestCoordinatorTest::SetUp() { 208 void RequestCoordinatorTest::SetUp() {
188 std::unique_ptr<OfflinerPolicy> policy(new OfflinerPolicy()); 209 std::unique_ptr<OfflinerPolicy> policy(new OfflinerPolicy());
189 std::unique_ptr<OfflinerFactory> factory(new OfflinerFactoryStub()); 210 std::unique_ptr<OfflinerFactory> factory(new OfflinerFactoryStub());
190 // Save the offliner for use by the tests. 211 // Save the offliner for use by the tests.
191 offliner_ = 212 offliner_ =
192 reinterpret_cast<OfflinerStub*>(factory->GetOffliner(policy.get())); 213 reinterpret_cast<OfflinerStub*>(factory->GetOffliner(policy.get()));
193 std::unique_ptr<RequestQueueInMemoryStore> 214 std::unique_ptr<RequestQueueInMemoryStore>
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 coordinator()->StopProcessing(); 430 coordinator()->StopProcessing();
410 431
411 // Let the async callbacks in the cancel run. 432 // Let the async callbacks in the cancel run.
412 PumpLoop(); 433 PumpLoop();
413 434
414 // OfflinerDoneCallback will not end up getting called with status SAVED, 435 // OfflinerDoneCallback will not end up getting called with status SAVED,
415 // Since we cancelled the event before it called offliner_->LoadAndSave(). 436 // Since we cancelled the event before it called offliner_->LoadAndSave().
416 EXPECT_EQ(Offliner::RequestStatus::CANCELED, last_offlining_status()); 437 EXPECT_EQ(Offliner::RequestStatus::CANCELED, last_offlining_status());
417 } 438 }
418 439
440 TEST_F(RequestCoordinatorTest, PrerendererTimeout) {
441 // Build a request to use with the pre-renderer, and put it on the queue.
442 offline_pages::SavePageRequest request(
443 kRequestId, kUrl, kClientId, base::Time::Now());
444 coordinator()->queue()->AddRequest(
445 request,
446 base::Bind(&RequestCoordinatorTest::AddRequestDone,
447 base::Unretained(this)));
448 PumpLoop();
449
450 // Set up for the call to StartProcessing.
451 DeviceConditions device_conditions(
452 !POWER_REQUIRED, BATTERY_PERCENTAGE_HIGH,
453 net::NetworkChangeNotifier::CONNECTION_3G);
454 base::Callback<void(bool)> callback =
455 base::Bind(&RequestCoordinatorTest::WaitingCallbackFunction,
456 base::Unretained(this));
457
458 // Ensure that the new request does not finish - we simulate it being
459 // in progress by asking it to skip making the completion callback.
460 EnableOfflinerCallback(false);
461
462 // Ask RequestCoordinator to stop waiting for the offliner after this many
463 // seconds.
464 SetOfflinerTimeoutForTest(base::TimeDelta::FromSeconds(TEST_TIMEOUT_SECONDS));
465
466 // Sending the request to the offliner.
467 EXPECT_TRUE(coordinator()->StartProcessing(device_conditions, callback));
468 PumpLoop();
469
470 // Advance the mock clock far enough to cause a watchdog timeout
471 AdvanceClockBy(base::TimeDelta::FromSeconds(TEST_TIMEOUT_SECONDS + 1));
472 PumpLoop();
473
474 // Wait for timeout to expire. Use a TaskRunner with a DelayedTaskRunner
475 // which won't time out immediately, so the watchdog thread doesn't kill valid
476 // tasks too soon.
477 WaitForCallback();
478 PumpLoop();
479
480 // Now trying to start processing on another request should return false.
481 EXPECT_EQ(Offliner::RequestStatus::CANCELED, last_offlining_status());
482 }
483
419 } // namespace offline_pages 484 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/background/request_coordinator.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698