summaryrefslogtreecommitdiffstats
path: root/base/test
diff options
context:
space:
mode:
authorakalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-12-19 22:18:29 +0000
committerakalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-12-19 22:18:29 +0000
commita8582b16e600e031fbc30bd8adc6f005df042852 (patch)
treedb01d9f32349a72bb592ddacc29675f8ecec209f /base/test
parent6fb2d79e22fe40d888faeedff76ebb95fc012cfe (diff)
downloadchromium_src-a8582b16e600e031fbc30bd8adc6f005df042852.zip
chromium_src-a8582b16e600e031fbc30bd8adc6f005df042852.tar.gz
chromium_src-a8582b16e600e031fbc30bd8adc6f005df042852.tar.bz2
Unify various test TaskRunner implementations
Remove various ad-hoc test implementations of TaskRunner and implement TestSimpleTaskRunner instead. Add TestPendingTask class for use by test TaskRunner implementations. Clean up TestTaskRunner implementation in net/ and make it use TestPendingTask. BUG=165806 Review URL: https://codereview.chromium.org/11554036 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@174016 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/test')
-rw-r--r--base/test/test_pending_task.cc35
-rw-r--r--base/test/test_pending_task.h58
-rw-r--r--base/test/test_simple_task_runner.cc72
-rw-r--r--base/test/test_simple_task_runner.h82
4 files changed, 247 insertions, 0 deletions
diff --git a/base/test/test_pending_task.cc b/base/test/test_pending_task.cc
new file mode 100644
index 0000000..cb8412c
--- /dev/null
+++ b/base/test/test_pending_task.cc
@@ -0,0 +1,35 @@
+// Copyright (c) 2012 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/test/test_pending_task.h"
+
+namespace base {
+
+TestPendingTask::TestPendingTask() : nestability(NESTABLE) {}
+
+TestPendingTask::TestPendingTask(
+ const tracked_objects::Location& location,
+ const Closure& task,
+ TimeTicks post_time,
+ TimeDelta delay,
+ TestNestability nestability)
+ : location(location),
+ task(task),
+ post_time(post_time),
+ delay(delay),
+ nestability(nestability) {}
+
+TimeTicks TestPendingTask::GetTimeToRun() const {
+ return post_time + delay;
+}
+
+bool TestPendingTask::ShouldRunBefore(const TestPendingTask& other) const {
+ if (nestability != other.nestability)
+ return (nestability == NESTABLE);
+ return GetTimeToRun() < other.GetTimeToRun();
+}
+
+TestPendingTask::~TestPendingTask() {}
+
+} // namespace base
diff --git a/base/test/test_pending_task.h b/base/test/test_pending_task.h
new file mode 100644
index 0000000..c4a83bd
--- /dev/null
+++ b/base/test/test_pending_task.h
@@ -0,0 +1,58 @@
+// Copyright (c) 2012 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 BASE_TEST_TEST_PENDING_TASK_H_
+#define BASE_TEST_TEST_PENDING_TASK_H_
+
+#include "base/callback.h"
+#include "base/location.h"
+#include "base/time.h"
+
+namespace base {
+
+// TestPendingTask is a helper class for test TaskRunner
+// implementations. See test_simple_task_runner.h for example usage.
+
+struct TestPendingTask {
+ enum TestNestability { NESTABLE, NON_NESTABLE };
+
+ TestPendingTask();
+ TestPendingTask(const tracked_objects::Location& location,
+ const Closure& task,
+ TimeTicks post_time,
+ TimeDelta delay,
+ TestNestability nestability);
+ ~TestPendingTask();
+
+ // Returns post_time + delay.
+ TimeTicks GetTimeToRun() const;
+
+ // Returns true if this task is nestable and |other| isn't, or if
+ // this task's time to run is strictly earlier than |other|'s time
+ // to run.
+ //
+ // Note that two tasks may both have the same nestability and delay.
+ // In that case, the caller must use some other criterion (probably
+ // the position in some queue) to break the tie. Conveniently, the
+ // following STL functions already do so:
+ //
+ // - std::min_element
+ // - std::stable_sort
+ //
+ // but the following STL functions don't:
+ //
+ // - std::max_element
+ // - std::sort.
+ bool ShouldRunBefore(const TestPendingTask& other) const;
+
+ tracked_objects::Location location;
+ Closure task;
+ TimeTicks post_time;
+ TimeDelta delay;
+ TestNestability nestability;
+};
+
+} // namespace base
+
+#endif // BASE_TEST_TEST_TASK_RUNNER_H_
diff --git a/base/test/test_simple_task_runner.cc b/base/test/test_simple_task_runner.cc
new file mode 100644
index 0000000..440d6a1
--- /dev/null
+++ b/base/test/test_simple_task_runner.cc
@@ -0,0 +1,72 @@
+// Copyright (c) 2012 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/test/test_simple_task_runner.h"
+
+#include "base/logging.h"
+
+namespace base {
+
+TestSimpleTaskRunner::TestSimpleTaskRunner() {}
+
+TestSimpleTaskRunner::~TestSimpleTaskRunner() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+}
+
+bool TestSimpleTaskRunner::PostDelayedTask(
+ const tracked_objects::Location& from_here,
+ const Closure& task,
+ TimeDelta delay) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ pending_tasks_.push_back(
+ TestPendingTask(from_here, task, TimeTicks(), delay,
+ TestPendingTask::NESTABLE));
+ return true;
+}
+
+bool TestSimpleTaskRunner::PostNonNestableDelayedTask(
+ const tracked_objects::Location& from_here,
+ const Closure& task,
+ TimeDelta delay) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ pending_tasks_.push_back(
+ TestPendingTask(from_here, task, TimeTicks(), delay,
+ TestPendingTask::NON_NESTABLE));
+ return true;
+}
+
+bool TestSimpleTaskRunner::RunsTasksOnCurrentThread() const {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ return true;
+}
+
+const std::deque<TestPendingTask>&
+TestSimpleTaskRunner::GetPendingTasks() const {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ return pending_tasks_;
+}
+
+void TestSimpleTaskRunner::ClearPendingTasks() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ pending_tasks_.clear();
+}
+
+void TestSimpleTaskRunner::RunPendingTasks() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ // Swap with a local variable to avoid re-entrancy problems.
+ std::deque<TestPendingTask> tasks_to_run;
+ tasks_to_run.swap(pending_tasks_);
+ for (std::deque<TestPendingTask>::iterator it = tasks_to_run.begin();
+ it != tasks_to_run.end(); ++it) {
+ it->task.Run();
+ }
+}
+
+void TestSimpleTaskRunner::RunUntilIdle() {
+ while (!pending_tasks_.empty()) {
+ RunPendingTasks();
+ }
+}
+
+} // namespace base
diff --git a/base/test/test_simple_task_runner.h b/base/test/test_simple_task_runner.h
new file mode 100644
index 0000000..88113be
--- /dev/null
+++ b/base/test/test_simple_task_runner.h
@@ -0,0 +1,82 @@
+// Copyright (c) 2012 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 BASE_TEST_TEST_SIMPLE_TASK_RUNNER_H_
+#define BASE_TEST_TEST_SIMPLE_TASK_RUNNER_H_
+
+#include <deque>
+
+#include "base/basictypes.h"
+#include "base/compiler_specific.h"
+#include "base/single_thread_task_runner.h"
+#include "base/test/test_pending_task.h"
+#include "base/threading/thread_checker.h"
+
+namespace base {
+
+// TestSimpleTaskRunner is a simple TaskRunner implementation that can
+// be used for testing. It implements SingleThreadTaskRunner as that
+// interface implements SequencedTaskRunner, which in turn implements
+// TaskRunner, so TestSimpleTaskRunner can be passed in to a function
+// that accepts any *TaskRunner object.
+//
+// TestSimpleTaskRunner has the following properties which make it simple:
+//
+// - It is non-thread safe; all member functions must be called on
+// the same thread.
+// - Tasks are simply stored in a queue in FIFO order, ignoring delay
+// and nestability.
+// - Tasks aren't guaranteed to be destroyed immediately after
+// they're run.
+//
+// However, TestSimpleTaskRunner allows for reentrancy, in that it
+// handles the running of tasks that in turn call back into itself
+// (e.g., to post more tasks).
+//
+// If you need more complicated properties, consider using this class
+// as a template for writing a test TaskRunner implementation using
+// TestPendingTask.
+//
+// Note that, like any TaskRunner, TestSimpleTaskRunner is
+// ref-counted.
+class TestSimpleTaskRunner : public SingleThreadTaskRunner {
+ public:
+ TestSimpleTaskRunner();
+
+ // SingleThreadTaskRunner implementation.
+ virtual bool PostDelayedTask(const tracked_objects::Location& from_here,
+ const Closure& task,
+ TimeDelta delay) OVERRIDE;
+ virtual bool PostNonNestableDelayedTask(
+ const tracked_objects::Location& from_here,
+ const Closure& task,
+ TimeDelta delay) OVERRIDE;
+
+ virtual bool RunsTasksOnCurrentThread() const OVERRIDE;
+
+ const std::deque<TestPendingTask>& GetPendingTasks() const;
+
+ // Clears the queue of pending tasks without running them.
+ void ClearPendingTasks();
+
+ // Runs each current pending task in order and clears the queue.
+ // Any tasks posted by the tasks are not run.
+ void RunPendingTasks();
+
+ // Runs pending tasks until the queue is empty.
+ void RunUntilIdle();
+
+ protected:
+ virtual ~TestSimpleTaskRunner();
+
+ private:
+ ThreadChecker thread_checker_;
+ std::deque<TestPendingTask> pending_tasks_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestSimpleTaskRunner);
+};
+
+} // namespace base
+
+#endif // BASE_TEST_TEST_SIMPLE_TASK_RUNNER_H_