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

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

Issue 2317893002: [Offline Pages] Close race condition of multiple StartProcessing callers. (Closed)
Patch Set: Pete feedback - clear starting flag in deadbeat timeout case 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 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 387 matching lines...) Expand 10 before | Expand all | Expand 10 after
590 592
591 // If one item completes, and there are no more user requeted items left, 593 // If one item completes, and there are no more user requeted items left,
592 // we should make a scheduler entry for a non-user requested item. 594 // we should make a scheduler entry for a non-user requested item.
593 TEST_F(RequestCoordinatorTest, RequestNotPickedNonUserRequestedItemsRemain) { 595 TEST_F(RequestCoordinatorTest, RequestNotPickedNonUserRequestedItemsRemain) {
594 // Call start processing just to set up a scheduler callback. 596 // Call start processing just to set up a scheduler callback.
595 DeviceConditions device_conditions(false, 75, 597 DeviceConditions device_conditions(false, 75,
596 net::NetworkChangeNotifier::CONNECTION_3G); 598 net::NetworkChangeNotifier::CONNECTION_3G);
597 base::Callback<void(bool)> callback = base::Bind( 599 base::Callback<void(bool)> callback = base::Bind(
598 &RequestCoordinatorTest::EmptyCallbackFunction, base::Unretained(this)); 600 &RequestCoordinatorTest::EmptyCallbackFunction, base::Unretained(this));
599 coordinator()->StartProcessing(device_conditions, callback); 601 coordinator()->StartProcessing(device_conditions, callback);
602 EXPECT_TRUE(is_starting());
600 603
601 // Call RequestNotPicked, and make sure we pick schedule a task for non user 604 // Call RequestNotPicked, and make sure we pick schedule a task for non user
602 // requested conditions. 605 // requested conditions.
603 CallRequestNotPicked(true); 606 CallRequestNotPicked(true);
604 PumpLoop(); 607 PumpLoop();
605 608
609 EXPECT_FALSE(is_starting());
610
606 // The scheduler should have been called to schedule the non-user requested 611 // The scheduler should have been called to schedule the non-user requested
607 // task. 612 // task.
608 SchedulerStub* scheduler_stub = 613 SchedulerStub* scheduler_stub =
609 reinterpret_cast<SchedulerStub*>(coordinator()->scheduler()); 614 reinterpret_cast<SchedulerStub*>(coordinator()->scheduler());
610 EXPECT_TRUE(scheduler_stub->schedule_called()); 615 EXPECT_TRUE(scheduler_stub->schedule_called());
611 EXPECT_TRUE(scheduler_stub->unschedule_called()); 616 EXPECT_TRUE(scheduler_stub->unschedule_called());
612 const Scheduler::TriggerConditions* conditions = scheduler_stub->conditions(); 617 const Scheduler::TriggerConditions* conditions = scheduler_stub->conditions();
613 EXPECT_EQ(conditions->require_power_connected, 618 EXPECT_EQ(conditions->require_power_connected,
614 coordinator()->policy()->PowerRequired(!kUserRequested)); 619 coordinator()->policy()->PowerRequired(!kUserRequested));
615 EXPECT_EQ( 620 EXPECT_EQ(
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
664 base::Unretained(this))); 669 base::Unretained(this)));
665 PumpLoop(); 670 PumpLoop();
666 671
667 DeviceConditions device_conditions(false, 75, 672 DeviceConditions device_conditions(false, 75,
668 net::NetworkChangeNotifier::CONNECTION_3G); 673 net::NetworkChangeNotifier::CONNECTION_3G);
669 base::Callback<void(bool)> callback = 674 base::Callback<void(bool)> callback =
670 base::Bind( 675 base::Bind(
671 &RequestCoordinatorTest::EmptyCallbackFunction, 676 &RequestCoordinatorTest::EmptyCallbackFunction,
672 base::Unretained(this)); 677 base::Unretained(this));
673 EXPECT_TRUE(coordinator()->StartProcessing(device_conditions, callback)); 678 EXPECT_TRUE(coordinator()->StartProcessing(device_conditions, callback));
679 EXPECT_TRUE(is_starting());
674 680
675 // Now, quick, before it can do much (we haven't called PumpLoop), cancel it. 681 // Now, quick, before it can do much (we haven't called PumpLoop), cancel it.
676 coordinator()->StopProcessing(); 682 coordinator()->StopProcessing();
677 683
678 // Let the async callbacks in the request coordinator run. 684 // Let the async callbacks in the request coordinator run.
679 PumpLoop(); 685 PumpLoop();
680 686
687 EXPECT_FALSE(is_starting());
688
681 // OfflinerDoneCallback will not end up getting called with status SAVED, 689 // OfflinerDoneCallback will not end up getting called with status SAVED,
682 // since we cancelled the event before it called offliner_->LoadAndSave(). 690 // since we cancelled the event before it called offliner_->LoadAndSave().
683 EXPECT_EQ(Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED, 691 EXPECT_EQ(Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED,
684 last_offlining_status()); 692 last_offlining_status());
685 693
686 // Since offliner was not started, it will not have seen cancel call. 694 // Since offliner was not started, it will not have seen cancel call.
687 EXPECT_FALSE(OfflinerWasCanceled()); 695 EXPECT_FALSE(OfflinerWasCanceled());
688 } 696 }
689 697
690 // This tests a StopProcessing call after the prerenderer has been started. 698 // This tests a StopProcessing call after the prerenderer has been started.
(...skipping 10 matching lines...) Expand all
701 // Ensure the start processing request stops before the completion callback. 709 // Ensure the start processing request stops before the completion callback.
702 EnableOfflinerCallback(false); 710 EnableOfflinerCallback(false);
703 711
704 DeviceConditions device_conditions(false, 75, 712 DeviceConditions device_conditions(false, 75,
705 net::NetworkChangeNotifier::CONNECTION_3G); 713 net::NetworkChangeNotifier::CONNECTION_3G);
706 base::Callback<void(bool)> callback = 714 base::Callback<void(bool)> callback =
707 base::Bind( 715 base::Bind(
708 &RequestCoordinatorTest::EmptyCallbackFunction, 716 &RequestCoordinatorTest::EmptyCallbackFunction,
709 base::Unretained(this)); 717 base::Unretained(this));
710 EXPECT_TRUE(coordinator()->StartProcessing(device_conditions, callback)); 718 EXPECT_TRUE(coordinator()->StartProcessing(device_conditions, callback));
719 EXPECT_TRUE(is_starting());
711 720
712 // Let all the async parts of the start processing pipeline run to completion. 721 // Let all the async parts of the start processing pipeline run to completion.
713 PumpLoop(); 722 PumpLoop();
714 723
715 // Coordinator should now be busy. 724 // Coordinator should now be busy.
716 EXPECT_TRUE(is_busy()); 725 EXPECT_TRUE(is_busy());
726 EXPECT_FALSE(is_starting());
717 727
718 // Now we cancel it while the prerenderer is busy. 728 // Now we cancel it while the prerenderer is busy.
719 coordinator()->StopProcessing(); 729 coordinator()->StopProcessing();
720 730
721 // Let the async callbacks in the cancel run. 731 // Let the async callbacks in the cancel run.
722 PumpLoop(); 732 PumpLoop();
723 733
724 EXPECT_FALSE(is_busy()); 734 EXPECT_FALSE(is_busy());
725 735
726 // OfflinerDoneCallback will not end up getting called with status SAVED, 736 // OfflinerDoneCallback will not end up getting called with status SAVED,
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
801 // Advance the mock clock far enough to cause a watchdog timeout 811 // Advance the mock clock far enough to cause a watchdog timeout
802 AdvanceClockBy(base::TimeDelta::FromSeconds(kTestTimeoutSeconds + 1)); 812 AdvanceClockBy(base::TimeDelta::FromSeconds(kTestTimeoutSeconds + 1));
803 PumpLoop(); 813 PumpLoop();
804 814
805 // Wait for timeout to expire. Use a TaskRunner with a DelayedTaskRunner 815 // Wait for timeout to expire. Use a TaskRunner with a DelayedTaskRunner
806 // which won't time out immediately, so the watchdog thread doesn't kill valid 816 // which won't time out immediately, so the watchdog thread doesn't kill valid
807 // tasks too soon. 817 // tasks too soon.
808 WaitForCallback(); 818 WaitForCallback();
809 PumpLoop(); 819 PumpLoop();
810 820
821 EXPECT_FALSE(is_starting());
811 EXPECT_TRUE(OfflinerWasCanceled()); 822 EXPECT_TRUE(OfflinerWasCanceled());
812 EXPECT_EQ(Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED, 823 EXPECT_EQ(Offliner::RequestStatus::REQUEST_COORDINATOR_CANCELED,
813 last_offlining_status()); 824 last_offlining_status());
814 } 825 }
815 826
816 TEST_F(RequestCoordinatorTest, TimeBudgetExceeded) { 827 TEST_F(RequestCoordinatorTest, TimeBudgetExceeded) {
817 // Build two requests to use with the pre-renderer, and put it on the queue. 828 // Build two requests to use with the pre-renderer, and put it on the queue.
818 offline_pages::SavePageRequest request1( 829 offline_pages::SavePageRequest request1(
819 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested); 830 kRequestId1, kUrl1, kClientId1, base::Time::Now(), kUserRequested);
820 offline_pages::SavePageRequest request2( 831 offline_pages::SavePageRequest request2(
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
993 net::NetworkChangeNotifier::ConnectionType::CONNECTION_3G); 1004 net::NetworkChangeNotifier::ConnectionType::CONNECTION_3G);
994 1005
995 // Resume the request while connected. 1006 // Resume the request while connected.
996 coordinator()->ResumeRequests(request_ids); 1007 coordinator()->ResumeRequests(request_ids);
997 EXPECT_FALSE(is_busy()); 1008 EXPECT_FALSE(is_busy());
998 PumpLoop(); 1009 PumpLoop();
999 EXPECT_TRUE(is_busy()); 1010 EXPECT_TRUE(is_busy());
1000 } 1011 }
1001 1012
1002 } // namespace offline_pages 1013 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698