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

Unified Diff: base/task_scheduler/scheduler_thread_pool_impl_unittest.cc

Issue 1906083002: TaskScheduler: Remove base/task_scheduler/utils.h/.cc (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@sched_2_stack
Patch Set: rebase 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/scheduler_thread_pool_impl_unittest.cc
diff --git a/base/task_scheduler/scheduler_thread_pool_unittest.cc b/base/task_scheduler/scheduler_thread_pool_impl_unittest.cc
similarity index 79%
rename from base/task_scheduler/scheduler_thread_pool_unittest.cc
rename to base/task_scheduler/scheduler_thread_pool_impl_unittest.cc
index 7e1ec5c2cb1195864e6e812072b13f7a540ad4e6..d949b4ad64f4bdc429be9b05063dff265dc8ad5e 100644
--- a/base/task_scheduler/scheduler_thread_pool_unittest.cc
+++ b/base/task_scheduler/scheduler_thread_pool_impl_unittest.cc
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "base/task_scheduler/scheduler_thread_pool.h"
+#include "base/task_scheduler/scheduler_thread_pool_impl.h"
#include <stddef.h>
@@ -35,15 +35,30 @@ const size_t kNumThreadsInThreadPool = 4;
const size_t kNumThreadsPostingTasks = 4;
const size_t kNumTasksPostedPerThread = 150;
-class TaskSchedulerThreadPoolTest
+class TestDelayedTaskManager : public DelayedTaskManager {
+ public:
+ TestDelayedTaskManager() : DelayedTaskManager(Bind(&DoNothing)) {}
+
+ void SetCurrentTime(TimeTicks now) { now_ = now; }
+
+ // DelayedTaskManager:
+ TimeTicks Now() const override { return now_; }
+
+ private:
+ TimeTicks now_ = TimeTicks::Now();
+
+ DISALLOW_COPY_AND_ASSIGN(TestDelayedTaskManager);
+};
+
+class TaskSchedulerThreadPoolImplTest
: public testing::TestWithParam<ExecutionMode> {
protected:
- TaskSchedulerThreadPoolTest() : delayed_task_manager_(Bind(&DoNothing)) {}
+ TaskSchedulerThreadPoolImplTest() = default;
void SetUp() override {
- thread_pool_ = SchedulerThreadPool::CreateThreadPool(
+ thread_pool_ = SchedulerThreadPoolImpl::CreateThreadPool(
ThreadPriority::NORMAL, kNumThreadsInThreadPool,
- Bind(&TaskSchedulerThreadPoolTest::EnqueueSequenceCallback,
+ Bind(&TaskSchedulerThreadPoolImplTest::EnqueueSequenceCallback,
Unretained(this)),
&task_tracker_, &delayed_task_manager_);
ASSERT_TRUE(thread_pool_);
@@ -54,7 +69,10 @@ class TaskSchedulerThreadPoolTest
thread_pool_->JoinForTesting();
}
- std::unique_ptr<SchedulerThreadPool> thread_pool_;
+ std::unique_ptr<SchedulerThreadPoolImpl> thread_pool_;
+
+ TaskTracker task_tracker_;
+ TestDelayedTaskManager delayed_task_manager_;
private:
void EnqueueSequenceCallback(scoped_refptr<Sequence> sequence) {
@@ -65,17 +83,15 @@ class TaskSchedulerThreadPoolTest
thread_pool_->EnqueueSequence(std::move(sequence), sort_key);
}
- TaskTracker task_tracker_;
- DelayedTaskManager delayed_task_manager_;
-
- DISALLOW_COPY_AND_ASSIGN(TaskSchedulerThreadPoolTest);
+ DISALLOW_COPY_AND_ASSIGN(TaskSchedulerThreadPoolImplTest);
};
class TaskFactory {
public:
// Constructs a TaskFactory that posts tasks with |execution_mode| to
// |thread_pool|.
- TaskFactory(SchedulerThreadPool* thread_pool, ExecutionMode execution_mode)
+ TaskFactory(SchedulerThreadPoolImpl* thread_pool,
+ ExecutionMode execution_mode)
: cv_(&lock_),
task_runner_(thread_pool->CreateTaskRunnerWithTraits(TaskTraits(),
execution_mode)),
@@ -163,7 +179,7 @@ class ThreadPostingTasks : public SimpleThread {
// thread wait until all worker threads in |thread_pool| are idle before
// posting a new task. If |post_nested_task| is true, each task posted by this
// thread posts another task when it runs.
- ThreadPostingTasks(SchedulerThreadPool* thread_pool,
+ ThreadPostingTasks(SchedulerThreadPoolImpl* thread_pool,
ExecutionMode execution_mode,
bool wait_for_all_threads_idle,
bool post_nested_task)
@@ -188,7 +204,7 @@ class ThreadPostingTasks : public SimpleThread {
}
}
- SchedulerThreadPool* const thread_pool_;
+ SchedulerThreadPoolImpl* const thread_pool_;
const scoped_refptr<TaskRunner> task_runner_;
const bool wait_for_all_threads_idle_;
const bool post_nested_task_;
@@ -197,7 +213,18 @@ class ThreadPostingTasks : public SimpleThread {
DISALLOW_COPY_AND_ASSIGN(ThreadPostingTasks);
};
-TEST_P(TaskSchedulerThreadPoolTest, PostTasks) {
+void ShouldNotRunCallback() {
+ ADD_FAILURE() << "Ran a task that shouldn't run.";
+}
+
+void SignalEventCallback(WaitableEvent* event) {
+ DCHECK(event);
+ event->Signal();
+}
+
+} // namespace
+
+TEST_P(TaskSchedulerThreadPoolImplTest, PostTasks) {
// Create threads to post tasks.
std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks;
for (size_t i = 0; i < kNumThreadsPostingTasks; ++i) {
@@ -222,7 +249,7 @@ TEST_P(TaskSchedulerThreadPoolTest, PostTasks) {
thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
}
-TEST_P(TaskSchedulerThreadPoolTest, PostTasksWaitAllThreadsIdle) {
+TEST_P(TaskSchedulerThreadPoolImplTest, PostTasksWaitAllThreadsIdle) {
// Create threads to post tasks. To verify that worker threads can sleep and
// be woken up when new tasks are posted, wait for all threads to become idle
// before posting a new task.
@@ -249,7 +276,7 @@ TEST_P(TaskSchedulerThreadPoolTest, PostTasksWaitAllThreadsIdle) {
thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
}
-TEST_P(TaskSchedulerThreadPoolTest, NestedPostTasks) {
+TEST_P(TaskSchedulerThreadPoolImplTest, NestedPostTasks) {
// Create threads to post tasks. Each task posted by these threads will post
// another task when it runs.
std::vector<std::unique_ptr<ThreadPostingTasks>> threads_posting_tasks;
@@ -275,7 +302,7 @@ TEST_P(TaskSchedulerThreadPoolTest, NestedPostTasks) {
thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
}
-TEST_P(TaskSchedulerThreadPoolTest, PostTasksWithOneAvailableThread) {
+TEST_P(TaskSchedulerThreadPoolImplTest, PostTasksWithOneAvailableThread) {
// Post tasks to keep all threads busy except one until |event| is signaled.
// Use different factories so that tasks are added to different sequences and
// can run simultaneously when the execution mode is SEQUENCED.
@@ -303,7 +330,7 @@ TEST_P(TaskSchedulerThreadPoolTest, PostTasksWithOneAvailableThread) {
thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
}
-TEST_P(TaskSchedulerThreadPoolTest, Saturate) {
+TEST_P(TaskSchedulerThreadPoolImplTest, Saturate) {
// Verify that it is possible to have |kNumThreadsInThreadPool|
// tasks/sequences running simultaneously. Use different factories so that
// tasks are added to different sequences and can run simultaneously when the
@@ -325,13 +352,47 @@ TEST_P(TaskSchedulerThreadPoolTest, Saturate) {
thread_pool_->WaitForAllWorkerThreadsIdleForTesting();
}
+// Verify that a Task can't be posted after shutdown.
+TEST_P(TaskSchedulerThreadPoolImplTest, PostTaskAfterShutdown) {
+ task_tracker_.Shutdown();
+ EXPECT_FALSE(
+ thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam())
gab 2016/04/25 17:52:43 Create the TaskRunner before calling Shutdown() to
fdoray 2016/04/25 19:14:51 Done.
+ ->PostTask(FROM_HERE, Bind(&ShouldNotRunCallback)));
+}
+
+// Verify that a Task posted with a delay is added to the DelayedTaskManager and
+// doesn't run before its delay expires.
+TEST_P(TaskSchedulerThreadPoolImplTest, PostDelayedTask) {
+ EXPECT_TRUE(delayed_task_manager_.GetDelayedRunTime().is_null());
+
+ // Post a delayed task.
+ WaitableEvent task_ran(true, false);
+ EXPECT_TRUE(thread_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam())
+ ->PostDelayedTask(FROM_HERE, Bind(&SignalEventCallback,
+ Unretained(&task_ran)),
+ TimeDelta::FromSeconds(10)));
+
+ // The task should have been added to the DelayedTaskManager.
+ EXPECT_FALSE(delayed_task_manager_.GetDelayedRunTime().is_null());
+
+ // The task shouldn't run.
+ EXPECT_FALSE(task_ran.IsSignaled());
+
+ // Fast-forward time and post tasks that are ripe for execution.
+ delayed_task_manager_.SetCurrentTime(
+ delayed_task_manager_.GetDelayedRunTime());
+ delayed_task_manager_.PostReadyTasks();
+
+ // The task should run.
+ task_ran.Wait();
+}
+
INSTANTIATE_TEST_CASE_P(Parallel,
- TaskSchedulerThreadPoolTest,
+ TaskSchedulerThreadPoolImplTest,
::testing::Values(ExecutionMode::PARALLEL));
INSTANTIATE_TEST_CASE_P(Sequenced,
- TaskSchedulerThreadPoolTest,
+ TaskSchedulerThreadPoolImplTest,
::testing::Values(ExecutionMode::SEQUENCED));
-} // namespace
} // namespace internal
} // namespace base

Powered by Google App Engine
This is Rietveld 408576698