Commit 0b20ee6c authored by Gabriel Charette's avatar Gabriel Charette Committed by Commit Bot

Add "Testing Components Which Post Tasks" docs

This is the complimentary testing docs to the Threading and Tasks usage
docs.

Updated in-code comments to reflect the current reality better too.

R=fdoray@chromium.org

Bug: 1002654
Change-Id: Ic9f4227e462ddc7180a1a6f3005935f1203ff064
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1798985
Auto-Submit: Gabriel Charette <gab@chromium.org>
Reviewed-by: default avatarEtienne Pierre-Doray <etiennep@chromium.org>
Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Reviewed-by: default avatarElly Fong-Jones <ellyjones@chromium.org>
Commit-Queue: Gabriel Charette <gab@chromium.org>
Cr-Commit-Position: refs/heads/master@{#697592}
parent ca03ee3b
...@@ -12,12 +12,11 @@ ...@@ -12,12 +12,11 @@
namespace base { namespace base {
// ATTENTION: Prefer TaskEnvironment::ThreadPoolExecutionMode::QUEUED and // ATTENTION: Prefer SingleThreadTaskEnvironment or TaskEnvironment w/
// a task runner obtained from base/task/post_task.h over this class. A // ThreadPoolExecutionMode::QUEUED over this class. A NullTaskRunner might seem
// NullTaskRunner might seem appealing, but not running tasks is under-testing // appealing, but not running tasks is under-testing the potential side-effects
// the side-effects of the code under tests. ThreadPoolExecutionMode::QUEUED // of the code under tests. All tests should be okay if tasks born from their
// will delay execution until the end of the test (if not requested earlier) but // actions are run or deleted at a later point.
// will at least exercise the tasks posted as a side-effect of the test.
// //
// Helper class for tests that need to provide an implementation of a // Helper class for tests that need to provide an implementation of a
// *TaskRunner class but don't actually care about tasks being run. // *TaskRunner class but don't actually care about tasks being run.
......
...@@ -22,9 +22,9 @@ class SingleThreadTaskRunner; ...@@ -22,9 +22,9 @@ class SingleThreadTaskRunner;
// ScopedMockTimeMessageLoopTaskRunner, the underlying TestMockTimeTaskRunner's // ScopedMockTimeMessageLoopTaskRunner, the underlying TestMockTimeTaskRunner's
// methods must be used instead to pump tasks. // methods must be used instead to pump tasks.
// //
// DEPRECATED: Use a TestMockTimeTaskRunner::Type::kBoundToThread instead of a // Note: Use TaskEnvironment + TimeSource::MOCK_TIME instead of this in unit
// MessageLoop + ScopedMockTimeMessageLoopTaskRunner. // tests. In browser tests you unfortunately still need this at the moment to
// TODO(gab): Remove usage of this API and delete it. // mock delayed tasks on the main thread...
class ScopedMockTimeMessageLoopTaskRunner { class ScopedMockTimeMessageLoopTaskRunner {
public: public:
ScopedMockTimeMessageLoopTaskRunner(); ScopedMockTimeMessageLoopTaskRunner();
......
...@@ -20,10 +20,10 @@ class TimeDelta; ...@@ -20,10 +20,10 @@ class TimeDelta;
// ATTENTION: Prefer using base::test::TaskEnvironment and a task runner // ATTENTION: Prefer using base::test::TaskEnvironment and a task runner
// obtained from base/task/post_task.h over this class. This class isn't as // obtained from base/task/post_task.h over this class. This class isn't as
// "simple" as it seems specifically because its attempt at being simple makes // "simple" as it seems specifically because it runs tasks in a surprising order
// it run tasks in a surprising order (delays aren't respected and nesting // (delays aren't respected and nesting doesn't behave as usual). Should you
// doesn't behave as usual). Should you prefer to flush all tasks regardless of // prefer to flush all tasks regardless of delays,
// delays, TaskEnvironment::TimeSource::MOCK_TIME and // TaskEnvironment::TimeSource::MOCK_TIME and
// TaskEnvironment::FastForwardUntilNoTasksRemain() have you covered. // TaskEnvironment::FastForwardUntilNoTasksRemain() have you covered.
// //
// TestSimpleTaskRunner is a simple TaskRunner implementation that can // TestSimpleTaskRunner is a simple TaskRunner implementation that can
......
...@@ -23,8 +23,10 @@ LazyInstance<ThreadLocalPointer<SequencedTaskRunnerHandle>>::Leaky ...@@ -23,8 +23,10 @@ LazyInstance<ThreadLocalPointer<SequencedTaskRunnerHandle>>::Leaky
const scoped_refptr<SequencedTaskRunner>& SequencedTaskRunnerHandle::Get() { const scoped_refptr<SequencedTaskRunner>& SequencedTaskRunnerHandle::Get() {
const SequencedTaskRunnerHandle* current = const SequencedTaskRunnerHandle* current =
sequenced_task_runner_tls.Pointer()->Get(); sequenced_task_runner_tls.Pointer()->Get();
CHECK(current) << "Error: This caller requires a sequenced context (i.e. the " CHECK(current)
"current task needs to run from a SequencedTaskRunner)."; << "Error: This caller requires a sequenced context (i.e. the current "
"task needs to run from a SequencedTaskRunner). If you're in a test "
"refer to //docs/threading_and_tasks_testing.md.";
return current->task_runner_; return current->task_runner_;
} }
......
...@@ -26,9 +26,10 @@ base::LazyInstance<base::ThreadLocalPointer<ThreadTaskRunnerHandle>>::Leaky ...@@ -26,9 +26,10 @@ base::LazyInstance<base::ThreadLocalPointer<ThreadTaskRunnerHandle>>::Leaky
const scoped_refptr<SingleThreadTaskRunner>& ThreadTaskRunnerHandle::Get() { const scoped_refptr<SingleThreadTaskRunner>& ThreadTaskRunnerHandle::Get() {
const ThreadTaskRunnerHandle* current = const ThreadTaskRunnerHandle* current =
thread_task_runner_tls.Pointer()->Get(); thread_task_runner_tls.Pointer()->Get();
CHECK(current) << "Error: This caller requires a single-threaded context " CHECK(current)
"(i.e. the current task needs to run from a " << "Error: This caller requires a single-threaded context (i.e. the "
"SingleThreadTaskRunner)."; "current task needs to run from a SingleThreadTaskRunner). If you're "
"in a test refer to //docs/threading_and_tasks_testing.md.";
return current->task_runner_; return current->task_runner_;
} }
......
...@@ -38,8 +38,8 @@ This documentation assumes familiarity with computer science ...@@ -38,8 +38,8 @@ This documentation assumes familiarity with computer science
dedicated task queue until Quit(). You should pretty much never be creating dedicated task queue until Quit(). You should pretty much never be creating
your own `base::Thread`'s. your own `base::Thread`'s.
* **Thread pool**: A pool of physical threads with a shared task queue. In * **Thread pool**: A pool of physical threads with a shared task queue. In
Chrome, this is `base::ThreadPoolInstance`. There's exactly one instance per Chrome Chrome, this is `base::ThreadPoolInstance`. There's exactly one instance per
process, it serves tasks posted through Chrome process, it serves tasks posted through
[`base/task/post_task.h`](https://cs.chromium.org/chromium/src/base/task/post_task.h) [`base/task/post_task.h`](https://cs.chromium.org/chromium/src/base/task/post_task.h)
and as such you should rarely need to use the `base::ThreadPoolInstance` API and as such you should rarely need to use the `base::ThreadPoolInstance` API
directly (more on posting tasks later). directly (more on posting tasks later).
...@@ -625,11 +625,14 @@ cancelable_task_tracker.TryCancelAll(); ...@@ -625,11 +625,14 @@ cancelable_task_tracker.TryCancelAll();
## Testing ## Testing
For more details see [Testing Components Which Post
Tasks](threading_and_tasks_testing.md).
To test code that uses `base::ThreadTaskRunnerHandle`, To test code that uses `base::ThreadTaskRunnerHandle`,
`base::SequencedTaskRunnerHandle` or a function in `base::SequencedTaskRunnerHandle` or a function in
[`base/task/post_task.h`](https://cs.chromium.org/chromium/src/base/task/post_task.h), [`base/task/post_task.h`](https://cs.chromium.org/chromium/src/base/task/post_task.h),
instantiate a instantiate a
[`base::test::TaskEnvironment`](https://cs.chromium.org/chromium/src/base/test/scoped_task_environment.h) [`base::test::TaskEnvironment`](https://cs.chromium.org/chromium/src/base/test/task_environment.h)
for the scope of the test. If you need BrowserThreads, use for the scope of the test. If you need BrowserThreads, use
`content::BrowserTaskEnvironment` instead of `content::BrowserTaskEnvironment` instead of
`base::test::TaskEnvironment`. `base::test::TaskEnvironment`.
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment