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

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

Issue 2325563003: [Offline Pages] Close race condition of multiple StartProcessing callers. (Closed)
Patch Set: 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
« 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
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 void PumpLoop(); 183 void PumpLoop();
184 184
185 RequestCoordinator* coordinator() { 185 RequestCoordinator* coordinator() {
186 return coordinator_.get(); 186 return coordinator_.get();
187 } 187 }
188 188
189 bool is_busy() { 189 bool is_busy() {
190 return coordinator_->is_busy(); 190 return coordinator_->is_busy();
191 } 191 }
192 192
193 bool is_starting() { return coordinator_->is_starting(); }
194
193 // Empty callback function. 195 // Empty callback function.
194 void EmptyCallbackFunction(bool result) { 196 void EmptyCallbackFunction(bool result) {
195 } 197 }
196 198
197 // Callback function which releases a wait for it. 199 // Callback function which releases a wait for it.
198 void WaitingCallbackFunction(bool result) { 200 void WaitingCallbackFunction(bool result) {
199 waiter_.Signal(); 201 waiter_.Signal();
200 } 202 }
201 203
202 // Callback for Add requests. 204 // Callback for Add requests.
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after
594 base::Unretained(this))); 596 base::Unretained(this)));
595 PumpLoop(); 597 PumpLoop();
596 598
597 DeviceConditions device_conditions(false, 75, 599 DeviceConditions device_conditions(false, 75,
598 net::NetworkChangeNotifier::CONNECTION_3G); 600 net::NetworkChangeNotifier::CONNECTION_3G);
599 base::Callback<void(bool)> callback = 601 base::Callback<void(bool)> callback =
600 base::Bind( 602 base::Bind(
601 &RequestCoordinatorTest::EmptyCallbackFunction, 603 &RequestCoordinatorTest::EmptyCallbackFunction,
602 base::Unretained(this)); 604 base::Unretained(this));
603 EXPECT_TRUE(coordinator()->StartProcessing(device_conditions, callback)); 605 EXPECT_TRUE(coordinator()->StartProcessing(device_conditions, callback));
606 EXPECT_TRUE(is_starting());
604 607
605 // Now, quick, before it can do much (we haven't called PumpLoop), cancel it. 608 // Now, quick, before it can do much (we haven't called PumpLoop), cancel it.
606 coordinator()->StopProcessing(); 609 coordinator()->StopProcessing();
607 610
608 // Let the async callbacks in the request coordinator run. 611 // Let the async callbacks in the request coordinator run.
609 PumpLoop(); 612 PumpLoop();
610 613
614 EXPECT_FALSE(is_starting());
615
611 // OfflinerDoneCallback will not end up getting called with status SAVED, 616 // OfflinerDoneCallback will not end up getting called with status SAVED,
612 // since we cancelled the event before it called offliner_->LoadAndSave(). 617 // since we cancelled the event before it called offliner_->LoadAndSave().
613 EXPECT_EQ(Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED, 618 EXPECT_EQ(Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED,
614 last_offlining_status()); 619 last_offlining_status());
615 620
616 // Since offliner was not started, it will not have seen cancel call. 621 // Since offliner was not started, it will not have seen cancel call.
617 EXPECT_FALSE(OfflinerWasCanceled()); 622 EXPECT_FALSE(OfflinerWasCanceled());
618 } 623 }
619 624
620 // This tests a StopProcessing call after the prerenderer has been started. 625 // This tests a StopProcessing call after the prerenderer has been started.
(...skipping 10 matching lines...) Expand all
631 // Ensure the start processing request stops before the completion callback. 636 // Ensure the start processing request stops before the completion callback.
632 EnableOfflinerCallback(false); 637 EnableOfflinerCallback(false);
633 638
634 DeviceConditions device_conditions(false, 75, 639 DeviceConditions device_conditions(false, 75,
635 net::NetworkChangeNotifier::CONNECTION_3G); 640 net::NetworkChangeNotifier::CONNECTION_3G);
636 base::Callback<void(bool)> callback = 641 base::Callback<void(bool)> callback =
637 base::Bind( 642 base::Bind(
638 &RequestCoordinatorTest::EmptyCallbackFunction, 643 &RequestCoordinatorTest::EmptyCallbackFunction,
639 base::Unretained(this)); 644 base::Unretained(this));
640 EXPECT_TRUE(coordinator()->StartProcessing(device_conditions, callback)); 645 EXPECT_TRUE(coordinator()->StartProcessing(device_conditions, callback));
646 EXPECT_TRUE(is_starting());
641 647
642 // Let all the async parts of the start processing pipeline run to completion. 648 // Let all the async parts of the start processing pipeline run to completion.
643 PumpLoop(); 649 PumpLoop();
644 650
645 // Coordinator should now be busy. 651 // Coordinator should now be busy.
646 EXPECT_TRUE(is_busy()); 652 EXPECT_TRUE(is_busy());
653 EXPECT_FALSE(is_starting());
647 654
648 // Now we cancel it while the prerenderer is busy. 655 // Now we cancel it while the prerenderer is busy.
649 coordinator()->StopProcessing(); 656 coordinator()->StopProcessing();
650 657
651 // Let the async callbacks in the cancel run. 658 // Let the async callbacks in the cancel run.
652 PumpLoop(); 659 PumpLoop();
653 660
654 EXPECT_FALSE(is_busy()); 661 EXPECT_FALSE(is_busy());
655 662
656 // OfflinerDoneCallback will not end up getting called with status SAVED, 663 // OfflinerDoneCallback will not end up getting called with status SAVED,
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
731 // Advance the mock clock far enough to cause a watchdog timeout 738 // Advance the mock clock far enough to cause a watchdog timeout
732 AdvanceClockBy(base::TimeDelta::FromSeconds(kTestTimeoutSeconds + 1)); 739 AdvanceClockBy(base::TimeDelta::FromSeconds(kTestTimeoutSeconds + 1));
733 PumpLoop(); 740 PumpLoop();
734 741
735 // Wait for timeout to expire. Use a TaskRunner with a DelayedTaskRunner 742 // Wait for timeout to expire. Use a TaskRunner with a DelayedTaskRunner
736 // which won't time out immediately, so the watchdog thread doesn't kill valid 743 // which won't time out immediately, so the watchdog thread doesn't kill valid
737 // tasks too soon. 744 // tasks too soon.
738 WaitForCallback(); 745 WaitForCallback();
739 PumpLoop(); 746 PumpLoop();
740 747
748 EXPECT_FALSE(is_starting());
741 EXPECT_TRUE(OfflinerWasCanceled()); 749 EXPECT_TRUE(OfflinerWasCanceled());
742 EXPECT_EQ(Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED, 750 EXPECT_EQ(Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED,
743 last_offlining_status()); 751 last_offlining_status());
744 } 752 }
745 753
746 TEST_F(RequestCoordinatorTest, TimeBudgetExceeded) { 754 TEST_F(RequestCoordinatorTest, TimeBudgetExceeded) {
747 // Build two requests to use with the pre-renderer, and put it on the queue. 755 // Build two requests to use with the pre-renderer, and put it on the queue.
748 offline_pages::SavePageRequest request1( 756 offline_pages::SavePageRequest request1(
749 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); 757 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested);
750 offline_pages::SavePageRequest request2( 758 offline_pages::SavePageRequest request2(
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
923 net::NetworkChangeNotifier::ConnectionType::CONNECTION_3G); 931 net::NetworkChangeNotifier::ConnectionType::CONNECTION_3G);
924 932
925 // Resume the request while connected. 933 // Resume the request while connected.
926 coordinator()->ResumeRequests(request_ids); 934 coordinator()->ResumeRequests(request_ids);
927 EXPECT_FALSE(is_busy()); 935 EXPECT_FALSE(is_busy());
928 PumpLoop(); 936 PumpLoop();
929 EXPECT_TRUE(is_busy()); 937 EXPECT_TRUE(is_busy());
930 } 938 }
931 939
932 } // namespace offline_pages 940 } // 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