OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "platform/scheduler/child/worker_global_scope_scheduler.h" |
| 6 |
| 7 #include "base/macros.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/test/simple_test_tick_clock.h" |
| 10 #include "base/test/test_simple_task_runner.h" |
| 11 #include "platform/scheduler/base/test_time_source.h" |
| 12 #include "platform/scheduler/child/scheduler_tqm_delegate_for_test.h" |
| 13 #include "platform/scheduler/child/worker_scheduler_impl.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 using testing::ElementsAreArray; |
| 18 |
| 19 namespace blink { |
| 20 namespace scheduler { |
| 21 |
| 22 namespace { |
| 23 |
| 24 void AppendToVectorTestTask(std::vector<std::string>* vector, |
| 25 std::string value) { |
| 26 vector->push_back(value); |
| 27 } |
| 28 |
| 29 } // namespace |
| 30 |
| 31 class WorkerGlobalScopeSchedulerTest : public testing::Test { |
| 32 public: |
| 33 WorkerGlobalScopeSchedulerTest() |
| 34 : clock_(new base::SimpleTestTickClock()), |
| 35 mock_task_runner_(new base::TestSimpleTaskRunner()), |
| 36 main_task_runner_(SchedulerTqmDelegateForTest::Create( |
| 37 mock_task_runner_, |
| 38 base::WrapUnique(new TestTimeSource(clock_.get())))), |
| 39 scheduler_(new WorkerSchedulerImpl(main_task_runner_)) { |
| 40 clock_->Advance(base::TimeDelta::FromMicroseconds(5000)); |
| 41 } |
| 42 |
| 43 ~WorkerGlobalScopeSchedulerTest() override {} |
| 44 |
| 45 void SetUp() override { |
| 46 scheduler_->Init(); |
| 47 global_scope_scheduler_ = |
| 48 base::MakeUnique<WorkerGlobalScopeScheduler>(scheduler_.get()); |
| 49 } |
| 50 |
| 51 void RunUntilIdle() { mock_task_runner_->RunUntilIdle(); } |
| 52 |
| 53 // Helper for posting a task. |
| 54 void PostTestTask(std::vector<std::string>* run_order, |
| 55 const std::string& task_descriptor) { |
| 56 global_scope_scheduler_->UnthrottledTaskRunner()->PostTask( |
| 57 FROM_HERE, WTF::Bind(&AppendToVectorTestTask, |
| 58 WTF::Unretained(run_order), task_descriptor)); |
| 59 } |
| 60 |
| 61 protected: |
| 62 std::unique_ptr<base::SimpleTestTickClock> clock_; |
| 63 scoped_refptr<base::TestSimpleTaskRunner> mock_task_runner_; |
| 64 |
| 65 scoped_refptr<SchedulerTqmDelegate> main_task_runner_; |
| 66 std::unique_ptr<WorkerSchedulerImpl> scheduler_; |
| 67 std::unique_ptr<WorkerGlobalScopeScheduler> global_scope_scheduler_; |
| 68 |
| 69 DISALLOW_COPY_AND_ASSIGN(WorkerGlobalScopeSchedulerTest); |
| 70 }; |
| 71 |
| 72 TEST_F(WorkerGlobalScopeSchedulerTest, TestPostTasks) { |
| 73 std::vector<std::string> run_order; |
| 74 PostTestTask(&run_order, "T1"); |
| 75 PostTestTask(&run_order, "T2"); |
| 76 RunUntilIdle(); |
| 77 PostTestTask(&run_order, "T3"); |
| 78 RunUntilIdle(); |
| 79 EXPECT_THAT(run_order, testing::ElementsAre("T1", "T2", "T3")); |
| 80 |
| 81 // Tasks should not run after the scheduler is disposed of. |
| 82 global_scope_scheduler_->Dispose(); |
| 83 run_order.clear(); |
| 84 PostTestTask(&run_order, "T4"); |
| 85 PostTestTask(&run_order, "T5"); |
| 86 RunUntilIdle(); |
| 87 EXPECT_TRUE(run_order.empty()); |
| 88 } |
| 89 |
| 90 } // namespace scheduler |
| 91 } // namespace blink |
OLD | NEW |