Index: base/test/test_mock_time_task_runner.cc |
diff --git a/base/test/test_mock_time_task_runner.cc b/base/test/test_mock_time_task_runner.cc |
index eaaceb6338166859c6da399b8de5edd30e665836..4987436a70f07009b38c0cc3651ba66cb5725184 100644 |
--- a/base/test/test_mock_time_task_runner.cc |
+++ b/base/test/test_mock_time_task_runner.cc |
@@ -120,6 +120,16 @@ TestMockTimeTaskRunner::TestOrderedPendingTask::operator=( |
// TestMockTimeTaskRunner ----------------------------------------------------- |
+// TODO(gab): This should also set the SequenceToken for the current thread. |
+// Ref. TestMockTimeTaskRunner::RunsTasksOnCurrentThread(). |
+TestMockTimeTaskRunner::ScopedContext::ScopedContext( |
+ scoped_refptr<TestMockTimeTaskRunner> scope) |
+ : task_runner_handle_(scope) { |
danakj
2017/02/16 17:57:24
move()
gab
2017/02/16 21:03:36
Can't because... (next comment)
|
+ scope->RunUntilIdle(); |
danakj
2017/02/16 17:57:24
then task_runner_handle_->
gab
2017/02/16 21:03:36
Wanted to do that too (and move above) but can't b
|
+} |
+ |
+TestMockTimeTaskRunner::ScopedContext::~ScopedContext() = default; |
+ |
bool TestMockTimeTaskRunner::TemporalOrder::operator()( |
const TestOrderedPendingTask& first_task, |
const TestOrderedPendingTask& second_task) const { |
@@ -213,6 +223,9 @@ TimeDelta TestMockTimeTaskRunner::NextPendingTaskDelay() const { |
: tasks_.top().GetTimeToRun() - now_ticks_; |
} |
+// TODO(gab): Combine |thread_checker_| with a SequenceToken to differentiate |
+// between tasks running in the scope of this TestMockTimeTaskRunner and other |
+// task runners sharing this thread. http://crbug.com/631186 |
bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const { |
return thread_checker_.CalledOnValidThread(); |
} |
@@ -253,6 +266,16 @@ void TestMockTimeTaskRunner::OnAfterTaskRun() { |
void TestMockTimeTaskRunner::ProcessAllTasksNoLaterThan(TimeDelta max_delta) { |
DCHECK_GE(max_delta, TimeDelta()); |
+ |
+ // Multiple test task runners can share the same thread for determinism in |
+ // unit tests. Make sure this TestMockTimeTaskRunner's tasks run in its scope. |
+ std::unique_ptr<ThreadTaskRunnerHandle::NestedForTesting> handle_override; |
+ if (!ThreadTaskRunnerHandle::IsSet() || |
+ ThreadTaskRunnerHandle::Get() != this) { |
+ handle_override = |
+ MakeUnique<ThreadTaskRunnerHandle::NestedForTesting>(this); |
+ } |
+ |
const TimeTicks original_now_ticks = now_ticks_; |
while (!IsElapsingStopped()) { |
OnBeforeSelectingTask(); |