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

Side by Side Diff: third_party/WebKit/Source/platform/TimerTest.cpp

Issue 1441073006: Move throttling of background timers into the renderer scheduler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Try again for MSVC Created 5 years, 1 month 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 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 "config.h" 5 #include "config.h"
6 #include "platform/Timer.h" 6 #include "platform/Timer.h"
7 7
8 #include "public/platform/Platform.h" 8 #include "public/platform/Platform.h"
9 #include "public/platform/WebScheduler.h" 9 #include "public/platform/WebScheduler.h"
10 #include "public/platform/WebThread.h" 10 #include "public/platform/WebThread.h"
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 { 128 {
129 return &m_timerWebTaskRunner; 129 return &m_timerWebTaskRunner;
130 } 130 }
131 131
132 WebTaskRunner* loadingTaskRunner() override 132 WebTaskRunner* loadingTaskRunner() override
133 { 133 {
134 ASSERT_NOT_REACHED(); 134 ASSERT_NOT_REACHED();
135 return nullptr; 135 return nullptr;
136 } 136 }
137 137
138 void postTimerTaskAt(const WebTraceLocation&, WebTaskRunner::Task* task, dou ble monotonicTime) override
139 {
140 m_timerTasks.push(DelayedTask(task, (monotonicTime - monotonicallyIncrea singTime()) * 1000));
141 }
142
143 void runUntilIdle() 138 void runUntilIdle()
144 { 139 {
145 while (m_timerTasks.size()) { 140 while (m_timerTasks.size()) {
146 gCurrentTimeSecs = m_timerTasks.top().runTimeSeconds(); 141 gCurrentTimeSecs = m_timerTasks.top().runTimeSeconds();
147 m_timerTasks.top().run(); 142 m_timerTasks.top().run();
148 m_timerTasks.pop(); 143 m_timerTasks.pop();
149 } 144 }
150 } 145 }
151 146
152 void runUntilIdleOrDeadlinePassed(double deadline) 147 void runUntilIdleOrDeadlinePassed(double deadline)
(...skipping 543 matching lines...) Expand 10 before | Expand all | Expand 10 after
696 { 691 {
697 Timer<TimerTest> timer(this, &TimerTest::countingTask); 692 Timer<TimerTest> timer(this, &TimerTest::countingTask);
698 timer.startRepeating(20, BLINK_FROM_HERE); 693 timer.startRepeating(20, BLINK_FROM_HERE);
699 694
700 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval()); 695 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval());
701 } 696 }
702 697
703 TEST_F(TimerTest, AugmentRepeatInterval) 698 TEST_F(TimerTest, AugmentRepeatInterval)
704 { 699 {
705 Timer<TimerTest> timer(this, &TimerTest::countingTask); 700 Timer<TimerTest> timer(this, &TimerTest::countingTask);
701 fprintf(stderr, "m_startTime = %f\n", m_startTime);
Sami 2015/11/17 10:12:21 Might want to remove this :)
alex clarke (OOO till 29th) 2015/11/23 17:13:46 Done.
706 timer.startRepeating(10, BLINK_FROM_HERE); 702 timer.startRepeating(10, BLINK_FROM_HERE);
707 EXPECT_FLOAT_EQ(10.0, timer.repeatInterval()); 703 EXPECT_FLOAT_EQ(10.0, timer.repeatInterval());
708 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval()); 704 EXPECT_FLOAT_EQ(10.0, timer.nextFireInterval());
709 705
710 advanceTimeBy(2.0); 706 advanceTimeBy(2.0);
711 timer.augmentRepeatInterval(10); 707 timer.augmentRepeatInterval(10);
712 708
713 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval()); 709 EXPECT_FLOAT_EQ(20.0, timer.repeatInterval());
714 EXPECT_FLOAT_EQ(18.0, timer.nextFireInterval()); 710 EXPECT_FLOAT_EQ(18.0, timer.nextFireInterval());
715 711
716 runUntilIdleOrDeadlinePassed(m_startTime + 50.0); 712 runUntilIdleOrDeadlinePassed(m_startTime + 50.0);
717 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 20.0, m_startTime + 40.0)) ; 713 EXPECT_THAT(m_runTimes, ElementsAre(m_startTime + 20.0, m_startTime + 40.0)) ;
718 } 714 }
719 715
720 class MockTimerWithAlignment : public TimerBase {
721 public:
722 MockTimerWithAlignment() : m_lastFireTime(0.0), m_alignedFireTime(0.0) { }
723
724 void fired() override
725 {
726 }
727
728 double alignedFireTime(double fireTime) const override
729 {
730 m_lastFireTime = fireTime;
731 return m_alignedFireTime;
732 }
733
734 void setAlignedFireTime(double alignedFireTime)
735 {
736 m_alignedFireTime = alignedFireTime;
737 }
738
739 double lastFireTime() const
740 {
741 return m_lastFireTime;
742 }
743
744 private:
745 mutable double m_lastFireTime;
746 double m_alignedFireTime;
747 };
748
749 TEST_F(TimerTest, TimerAlignment_OneShotZero)
750 {
751 MockTimerWithAlignment timer;
752 timer.setAlignedFireTime(m_startTime + 1.0);
753
754 timer.start(0.0, 0.0, BLINK_FROM_HERE);
755
756 // The nextFireInterval gets overrriden.
757 EXPECT_FLOAT_EQ(1.0, timer.nextFireInterval());
758 EXPECT_FLOAT_EQ(0.0, timer.nextUnalignedFireInterval());
759 EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime());
760 }
761
762 TEST_F(TimerTest, TimerAlignment_OneShotNonZero)
763 {
764 MockTimerWithAlignment timer;
765 timer.setAlignedFireTime(m_startTime + 1.0);
766
767 timer.start(0.5, 0.0, BLINK_FROM_HERE);
768
769 // The nextFireInterval gets overrriden.
770 EXPECT_FLOAT_EQ(1.0, timer.nextFireInterval());
771 EXPECT_FLOAT_EQ(0.5, timer.nextUnalignedFireInterval());
772 EXPECT_FLOAT_EQ(m_startTime + 0.5, timer.lastFireTime());
773 }
774
775 TEST_F(TimerTest, DidChangeAlignmentInterval)
776 {
777 MockTimerWithAlignment timer;
778 timer.setAlignedFireTime(m_startTime + 1.0);
779
780 timer.start(0.0, 0.0, BLINK_FROM_HERE);
781
782 EXPECT_FLOAT_EQ(1.0, timer.nextFireInterval());
783 EXPECT_FLOAT_EQ(0.0, timer.nextUnalignedFireInterval());
784 EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime());
785
786 timer.setAlignedFireTime(m_startTime);
787 timer.didChangeAlignmentInterval(monotonicallyIncreasingTime());
788
789 EXPECT_FLOAT_EQ(0.0, timer.nextFireInterval());
790 EXPECT_FLOAT_EQ(0.0, timer.nextUnalignedFireInterval());
791 EXPECT_FLOAT_EQ(m_startTime, timer.lastFireTime());
792 }
793
794 TEST_F(TimerTest, RepeatingTimerDoesNotDrift) 716 TEST_F(TimerTest, RepeatingTimerDoesNotDrift)
795 { 717 {
796 Timer<TimerTest> timer(this, &TimerTest::recordNextFireTimeTask); 718 Timer<TimerTest> timer(this, &TimerTest::recordNextFireTimeTask);
797 timer.startRepeating(2.0, BLINK_FROM_HERE); 719 timer.startRepeating(2.0, BLINK_FROM_HERE);
798 720
799 ASSERT(hasOneTimerTask()); 721 ASSERT(hasOneTimerTask());
800 recordNextFireTimeTask(&timer); // Next scheduled task to run at m_startTime + 2.0 722 recordNextFireTimeTask(&timer); // Next scheduled task to run at m_startTime + 2.0
801 723
802 // Simulate timer firing early. Next scheduled task to run at m_startTime + 4.0 724 // Simulate timer firing early. Next scheduled task to run at m_startTime + 4.0
803 advanceTimeBy(1.9); 725 advanceTimeBy(1.9);
(...skipping 24 matching lines...) Expand all
828 m_startTime + 6.0, 750 m_startTime + 6.0,
829 m_startTime + 8.0, 751 m_startTime + 8.0,
830 m_startTime + 10.0, 752 m_startTime + 10.0,
831 m_startTime + 14.0, 753 m_startTime + 14.0,
832 m_startTime + 18.0, 754 m_startTime + 18.0,
833 m_startTime + 28.0)); 755 m_startTime + 28.0));
834 } 756 }
835 757
836 } // namespace 758 } // namespace
837 } // namespace blink 759 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698