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

Unified Diff: base/task_scheduler/task_scheduler_impl_unittest.cc

Issue 1701343003: TaskScheduler [13] TaskSchedulerImpl (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@s_6_threadpool
Patch Set: self review Created 4 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: base/task_scheduler/task_scheduler_impl_unittest.cc
diff --git a/base/task_scheduler/task_scheduler_impl_unittest.cc b/base/task_scheduler/task_scheduler_impl_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..49aacdf526f82dba737d1504c2f1cbc515ae2799
--- /dev/null
+++ b/base/task_scheduler/task_scheduler_impl_unittest.cc
@@ -0,0 +1,183 @@
+// Copyright 2016 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 "base/task_scheduler/task_scheduler_impl.h"
+
+#include <stddef.h>
+
+#include <utility>
+#include <vector>
+
+#include "base/bind.h"
+#include "base/callback.h"
+#include "base/macros.h"
+#include "base/memory/ptr_util.h"
+#include "base/task_scheduler/task_traits.h"
+#include "base/task_scheduler/test_task_factory.h"
+#include "base/threading/platform_thread.h"
+#include "base/threading/simple_thread.h"
+#include "base/threading/thread.h"
+#include "base/threading/thread_restrictions.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace base {
+namespace internal {
+
+namespace {
+
+struct TraitsExecutionModePair {
+ TraitsExecutionModePair(const TaskTraits& traits,
+ ExecutionMode execution_mode)
+ : traits(traits), execution_mode(execution_mode) {}
+
+ const TaskTraits traits;
+ const ExecutionMode execution_mode;
+};
+
+class TaskSchedulerImplTest
+ : public testing::TestWithParam<TraitsExecutionModePair> {
+ protected:
+ TaskSchedulerImplTest() = default;
+
+ void TearDown() override { scheduler_.JoinForTesting(); }
+
+ TaskSchedulerImpl scheduler_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(TaskSchedulerImplTest);
+};
+
+// Returns whether I/O calls are allowed on the current thread.
+bool GetIOAllowed() {
+ // SetIOAllowed() is the only function that returns the current value of the
+ // I/O allowed bit. We don't use
+ // EXPECT_DCHECK_DEATH({ ThreadRestrictions::AssertIOAllowed(); }, "");
+ // to verify that I/O is disallowed because EXPECT_DCHECK_DEATH is slow on
+ // Windows.
gab 2016/04/28 19:29:02 I wouldn't even mention why we don't use EXPECT_DC
fdoray 2016/04/28 21:02:24 Removed comment. TaskSchedulerImplTest.PostTasks
+ const bool previous_value = ThreadRestrictions::SetIOAllowed(true);
+ ThreadRestrictions::SetIOAllowed(previous_value);
+ return previous_value;
+}
+
+void VerifyTaskEnvironement(const TaskTraits& traits) {
gab 2016/04/28 19:29:02 Add a comment about what this verifies and also ad
fdoray 2016/04/28 21:02:24 Done.
+ EXPECT_EQ((traits.priority() == TaskPriority::BACKGROUND)
gab 2016/04/28 19:29:02 Remove extra () for == check (the ternary operator
fdoray 2016/04/28 21:02:24 Done.
+ ? ThreadPriority::BACKGROUND
+ : ThreadPriority::NORMAL,
+ PlatformThread::GetCurrentThreadPriority());
+
+#if ENABLE_THREAD_RESTRICTIONS
+ // The #if above is required because GetIOAllowed() always returns true when
+ // !ENABLE_THREAD_RESTRICTIONS.
gab 2016/04/28 19:29:02 Instead return true from GetIOAllowed() when #if E
fdoray 2016/04/28 21:02:24 Clarified comment. GetIOAllowed() already returns
+ EXPECT_EQ(traits.with_file_io(), GetIOAllowed());
+#endif
+}
+
+class ThreadPostingTasks : public SimpleThread {
+ public:
+ // Creates a thread that posts Tasks to |scheduler| with |traits| and
+ // |execution_mode|.
+ ThreadPostingTasks(TaskSchedulerImpl* scheduler,
+ const TaskTraits& traits,
+ ExecutionMode execution_mode)
+ : SimpleThread("ThreadPostingTasks"),
+ traits_(traits),
+ factory_(scheduler->CreateTaskRunnerWithTraits(traits, execution_mode),
+ execution_mode) {}
+
+ void WaitForAllTasksToRun() { factory_.WaitForAllTasksToRun(); }
+
+ private:
+ void Run() override {
+ EXPECT_FALSE(factory_.task_runner()->RunsTasksOnCurrentThread());
+
+ const size_t kNumTasksPerThread = 150;
+ for (size_t i = 0; i < kNumTasksPerThread; ++i) {
+ factory_.PostTask(test::TestTaskFactory::PostNestedTask::NO,
+ Bind(&VerifyTaskEnvironement, traits_));
+ }
+ }
+
+ const TaskTraits traits_;
+ test::TestTaskFactory factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks);
+};
+
+// Returns a vector with a TraitsExecutionModePair for each valid
+// combination of {ExecutionMode, TaskPriority, WithFileIO()}.
+std::vector<TraitsExecutionModePair> GetTraitsExecutionModePairs() {
+ std::vector<TraitsExecutionModePair> params;
+
+ const ExecutionMode execution_modes[] = {ExecutionMode::PARALLEL,
+ ExecutionMode::SEQUENCED,
+ ExecutionMode::SINGLE_THREADED};
+
+ for (ExecutionMode execution_mode : execution_modes) {
+ for (size_t priority_index = static_cast<size_t>(TaskPriority::LOWEST);
+ priority_index <= static_cast<size_t>(TaskPriority::HIGHEST);
+ ++priority_index) {
+ const TaskPriority priority = static_cast<TaskPriority>(priority_index);
+ params.push_back(TraitsExecutionModePair(
+ TaskTraits().WithPriority(priority), execution_mode));
+ params.push_back(TraitsExecutionModePair(
+ TaskTraits().WithPriority(priority).WithFileIO(), execution_mode));
+ }
+ }
+
+ return params;
+}
+
+} // namespace
+
+// Verifies that Tasks posted with parameterized TaskTraits and ExecutionMode
+// run on a thread with the expected priority and I/O restrictions and respect
+// the characteristics of their ExecutionMode.
+TEST_P(TaskSchedulerImplTest, PostTasks) {
+ test::TestTaskFactory factory(
+ scheduler_.CreateTaskRunnerWithTraits(GetParam().traits,
+ GetParam().execution_mode),
+ GetParam().execution_mode);
+ EXPECT_FALSE(factory.task_runner()->RunsTasksOnCurrentThread());
+
+ const size_t kNumTasksPerTest = 150;
+ for (size_t i = 0; i < kNumTasksPerTest; ++i) {
+ factory.PostTask(test::TestTaskFactory::PostNestedTask::NO,
+ Bind(&VerifyTaskEnvironement, GetParam().traits));
+ }
+
+ factory.WaitForAllTasksToRun();
+}
+
+INSTANTIATE_TEST_CASE_P(OneTraitsExecutionModePair,
+ TaskSchedulerImplTest,
+ ::testing::ValuesIn(GetTraitsExecutionModePairs()));
+
+// Spawns threads that simultaneously post Tasks with various TaskTraits and
+// ExecutionMode. Verifies that each Task runs on a thread with the expected
+// priority and I/O restrictions and respects the characteristics of its
+// ExecutionMode.
+TEST(TaskSchedulerImplTest, MultipleTraitsExecutionModePairs) {
+ TaskSchedulerImpl scheduler;
+
+ std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks;
+ for (const auto& traits_execution_mode_pair : GetTraitsExecutionModePairs()) {
+ threads_posting_tasks.push_back(WrapUnique(
+ new ThreadPostingTasks(&scheduler, traits_execution_mode_pair.traits,
+ traits_execution_mode_pair.execution_mode)));
+ threads_posting_tasks.back()->Start();
+ }
+
+ for (const auto& thread : threads_posting_tasks) {
+ thread->WaitForAllTasksToRun();
+ thread->Join();
+ }
+
+ scheduler.JoinForTesting();
+}
+
+// TODO(fdoray): Add tests with Sequences that move around thread pools once
gab 2016/04/28 19:29:02 Please add an item for this in our internal tracke
fdoray 2016/04/28 21:02:24 Done.
+// child TaskRunners are supported.
+
+} // namespace internal
+} // namespace base
« base/task_scheduler/task_scheduler_impl.cc ('K') | « base/task_scheduler/task_scheduler_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698