diff options
author | skyostil <skyostil@chromium.org> | 2015-06-04 04:42:05 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-06-04 11:42:28 +0000 |
commit | ae456cc035b8bb76c11d47f28b2efab3dea0c26d (patch) | |
tree | ef7ce71f226cde948885c2a6a2a432821e66df18 /components/scheduler | |
parent | 78edcd5e7f6eace6313e22625163b61c96371495 (diff) | |
download | chromium_src-ae456cc035b8bb76c11d47f28b2efab3dea0c26d.zip chromium_src-ae456cc035b8bb76c11d47f28b2efab3dea0c26d.tar.gz chromium_src-ae456cc035b8bb76c11d47f28b2efab3dea0c26d.tar.bz2 |
scheduler: Always create a real scheduler in unit tests
Previously we would create a dummy scheduler in any test which uses
TestBlinkWebUnitTestSupport without first initializing a message loop.
This causes problems because the dummy scheduler ignores all tasks it
is given.
This patch makes the tests more realistic by always creating a real
renderer scheduler regardless of whether we have a message loop or not.
This is achieved by lazily binding the scheduler to the message loop
the first time it is needed.
Longer term we would like to refactor these test suites to ensure Blink
always has a valid message loop when it is initialized, but this will
involve rewiring several tests.
BUG=463143,495659
Committed: https://crrev.com/087644f1eab41927823a1a2fc2df08bd4e10fe18
Cr-Commit-Position: refs/heads/master@{#332685}
Review URL: https://codereview.chromium.org/1152623008
Cr-Commit-Position: refs/heads/master@{#332818}
Diffstat (limited to 'components/scheduler')
-rw-r--r-- | components/scheduler/BUILD.gn | 11 | ||||
-rw-r--r-- | components/scheduler/scheduler.gyp | 11 | ||||
-rw-r--r-- | components/scheduler/scheduler.gypi | 4 | ||||
-rw-r--r-- | components/scheduler/test/DEPS | 3 | ||||
-rw-r--r-- | components/scheduler/test/lazy_scheduler_message_loop_delegate_for_tests.cc | 86 | ||||
-rw-r--r-- | components/scheduler/test/lazy_scheduler_message_loop_delegate_for_tests.h | 57 |
6 files changed, 172 insertions, 0 deletions
diff --git a/components/scheduler/BUILD.gn b/components/scheduler/BUILD.gn index f55b542..587516f 100644 --- a/components/scheduler/BUILD.gn +++ b/components/scheduler/BUILD.gn @@ -60,3 +60,14 @@ source_set("unit_tests") { "//testing/gtest", ] } + +# GYP version: components/scheduler.gypi:scheduler_test_support +static_library("test_support") { + sources = rebase_path(scheduler_gypi_values.scheduler_test_support_sources, + ".", + "//components/scheduler") + + deps = [ + "//base", + ] +} diff --git a/components/scheduler/scheduler.gyp b/components/scheduler/scheduler.gyp index e77be85..cffc072 100644 --- a/components/scheduler/scheduler.gyp +++ b/components/scheduler/scheduler.gyp @@ -50,5 +50,16 @@ '../../third_party/WebKit/public/blink.gyp:blink', ], }, + { + # GN version: //components/scheduler:test_support + 'target_name': 'scheduler_test_support', + 'type': 'static_library', + 'include_dirs': [ + '../..', + ], + 'sources': [ + '<@(scheduler_test_support_sources)', + ], + }, ], } diff --git a/components/scheduler/scheduler.gypi b/components/scheduler/scheduler.gypi index 684688c..8132725 100644 --- a/components/scheduler/scheduler.gypi +++ b/components/scheduler/scheduler.gypi @@ -58,5 +58,9 @@ 'renderer/webthread_impl_for_renderer_scheduler.h', 'scheduler_export.h', ], + 'scheduler_test_support_sources': [ + 'test/lazy_scheduler_message_loop_delegate_for_tests.cc', + 'test/lazy_scheduler_message_loop_delegate_for_tests.h', + ], }, } diff --git a/components/scheduler/test/DEPS b/components/scheduler/test/DEPS new file mode 100644 index 0000000..7fba883 --- /dev/null +++ b/components/scheduler/test/DEPS @@ -0,0 +1,3 @@ +include_rules = [ + "+components/scheduler/child", +] diff --git a/components/scheduler/test/lazy_scheduler_message_loop_delegate_for_tests.cc b/components/scheduler/test/lazy_scheduler_message_loop_delegate_for_tests.cc new file mode 100644 index 0000000..999d856 --- /dev/null +++ b/components/scheduler/test/lazy_scheduler_message_loop_delegate_for_tests.cc @@ -0,0 +1,86 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/scheduler/test/lazy_scheduler_message_loop_delegate_for_tests.h" + +namespace scheduler { + +// static +scoped_refptr<LazySchedulerMessageLoopDelegateForTests> +LazySchedulerMessageLoopDelegateForTests::Create() { + return make_scoped_refptr(new LazySchedulerMessageLoopDelegateForTests()); +} + +LazySchedulerMessageLoopDelegateForTests:: + LazySchedulerMessageLoopDelegateForTests() + : message_loop_(base::MessageLoop::current()), + thread_id_(base::PlatformThread::CurrentId()) { +} + +LazySchedulerMessageLoopDelegateForTests:: + ~LazySchedulerMessageLoopDelegateForTests() { +} + +base::MessageLoop* LazySchedulerMessageLoopDelegateForTests::EnsureMessageLoop() + const { + if (message_loop_) + return message_loop_; + DCHECK(RunsTasksOnCurrentThread()); + message_loop_ = base::MessageLoop::current(); + DCHECK(message_loop_); + for (auto& observer : pending_observers_) { + message_loop_->AddTaskObserver(observer); + } + pending_observers_.clear(); + return message_loop_; +} + +bool LazySchedulerMessageLoopDelegateForTests::HasMessageLoop() const { + return message_loop_ != nullptr; +} + +bool LazySchedulerMessageLoopDelegateForTests::PostDelayedTask( + const tracked_objects::Location& from_here, + const base::Closure& task, + base::TimeDelta delay) { + return EnsureMessageLoop()->task_runner()->PostDelayedTask(from_here, task, + delay); +} + +bool LazySchedulerMessageLoopDelegateForTests::PostNonNestableDelayedTask( + const tracked_objects::Location& from_here, + const base::Closure& task, + base::TimeDelta delay) { + return EnsureMessageLoop()->task_runner()->PostNonNestableDelayedTask( + from_here, task, delay); +} + +bool LazySchedulerMessageLoopDelegateForTests::RunsTasksOnCurrentThread() + const { + return thread_id_ == base::PlatformThread::CurrentId(); +} + +bool LazySchedulerMessageLoopDelegateForTests::IsNested() const { + return EnsureMessageLoop()->IsNested(); +} + +void LazySchedulerMessageLoopDelegateForTests::AddTaskObserver( + base::MessageLoop::TaskObserver* task_observer) { + if (!HasMessageLoop()) { + pending_observers_.insert(task_observer); + return; + } + EnsureMessageLoop()->AddTaskObserver(task_observer); +} + +void LazySchedulerMessageLoopDelegateForTests::RemoveTaskObserver( + base::MessageLoop::TaskObserver* task_observer) { + if (!HasMessageLoop()) { + pending_observers_.erase(task_observer); + return; + } + EnsureMessageLoop()->RemoveTaskObserver(task_observer); +} + +} // namespace scheduler diff --git a/components/scheduler/test/lazy_scheduler_message_loop_delegate_for_tests.h b/components/scheduler/test/lazy_scheduler_message_loop_delegate_for_tests.h new file mode 100644 index 0000000..a727a6d --- /dev/null +++ b/components/scheduler/test/lazy_scheduler_message_loop_delegate_for_tests.h @@ -0,0 +1,57 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef COMPONENTS_SCHEDULER_TEST_LAZY_SCHEDULER_MESSAGE_LOOP_DELEGATE_FOR_TESTS_H_ +#define COMPONENTS_SCHEDULER_TEST_LAZY_SCHEDULER_MESSAGE_LOOP_DELEGATE_FOR_TESTS_H_ + +#include "base/message_loop/message_loop.h" +#include "components/scheduler/child/nestable_single_thread_task_runner.h" + +namespace scheduler { + +// This class connects the scheduler to a MessageLoop, but unlike +// SchedulerMessageLoopDelegate it allows the message loop to be created lazily +// after the scheduler has been brought up. This is needed in testing scenarios +// where Blink is initialized before a MessageLoop has been created. +// +// TODO(skyostil): Fix the relevant test suites and remove this class +// (crbug.com/495659). +class LazySchedulerMessageLoopDelegateForTests + : public NestableSingleThreadTaskRunner { + public: + static scoped_refptr<LazySchedulerMessageLoopDelegateForTests> Create(); + + // NestableSingleThreadTaskRunner implementation + bool PostDelayedTask(const tracked_objects::Location& from_here, + const base::Closure& task, + base::TimeDelta delay) override; + bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, + const base::Closure& task, + base::TimeDelta delay) override; + bool RunsTasksOnCurrentThread() const override; + bool IsNested() const override; + void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer) override; + void RemoveTaskObserver( + base::MessageLoop::TaskObserver* task_observer) override; + + private: + LazySchedulerMessageLoopDelegateForTests(); + ~LazySchedulerMessageLoopDelegateForTests() override; + + bool HasMessageLoop() const; + base::MessageLoop* EnsureMessageLoop() const; + + mutable base::MessageLoop* message_loop_; + base::PlatformThreadId thread_id_; + + // Task observers which have not yet been registered to a message loop. Not + // owned. + mutable base::hash_set<base::MessageLoop::TaskObserver*> pending_observers_; + + DISALLOW_COPY_AND_ASSIGN(LazySchedulerMessageLoopDelegateForTests); +}; + +} // namespace scheduler + +#endif // COMPONENTS_SCHEDULER_TEST_LAZY_SCHEDULER_MESSAGE_LOOP_DELEGATE_FOR_TESTS_H_ |