Commit 495fd992 authored by Gabriel Charette's avatar Gabriel Charette Committed by Commit Bot

[content test_utils] Fix RunAllTasksUntilIdle hang

See crbug.com/1035189 for explanation of issue and fix and why it ever
worked (ish).

R=carlscab@google.com

Bug: 1035189
Change-Id: Id1c5468e631c00ab2ea4de6065bd20a28dc8af0a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1972294
Commit-Queue: Gabriel Charette <gab@chromium.org>
Commit-Queue: Avi Drissman <avi@chromium.org>
Auto-Submit: Gabriel Charette <gab@chromium.org>
Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarCarlos Caballero <carlscab@google.com>
Cr-Commit-Position: refs/heads/master@{#726072}
parent fdbaa0aa
......@@ -14,6 +14,7 @@
#include "base/message_loop/message_loop_current.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/post_task.h"
#include "base/task/sequence_manager/sequence_manager.h"
......@@ -79,6 +80,12 @@ class TaskObserver : public base::TaskObserver {
void WillProcessTask(const base::PendingTask& pending_task,
bool was_blocked_or_low_priority) override {}
void DidProcessTask(const base::PendingTask& pending_task) override {
if (base::EndsWith(pending_task.posted_from.file_name(), "base/run_loop.cc",
base::CompareCase::SENSITIVE)) {
// Don't consider RunLoop internal tasks (i.e. QuitClosure() reposted by
// ProxyToTaskRunner() or RunLoop timeouts) as actual work.
return;
}
processed_ = true;
}
......@@ -148,9 +155,15 @@ void RunAllTasksUntilIdle() {
TaskObserver task_observer;
base::MessageLoopCurrent::Get()->AddTaskObserver(&task_observer);
base::RunLoop run_loop;
// This must use RunLoop::Type::kNestableTasksAllowed in case this
// RunAllTasksUntilIdle() call is nested inside an existing Run(). Without
// it, the QuitWhenIdleClosure() below would never run if it's posted from
// another thread (i.e.. by run_loop.cc's ProxyToTaskRunner).
base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed);
base::ThreadPoolInstance::Get()->FlushAsyncForTesting(
run_loop.QuitWhenIdleClosure());
run_loop.Run();
base::MessageLoopCurrent::Get()->RemoveTaskObserver(&task_observer);
......
// Copyright 2019 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 "content/public/test/test_utils.h"
#include "base/run_loop.h"
#include "base/task/post_task.h"
#include "base/test/bind_test_util.h"
#include "base/test/task_environment.h"
#include "base/threading/platform_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
// Regression test for crbug.com/1035189.
TEST(ContentTestUtils, NestedRunAllTasksUntilIdleWithPendingThreadPoolWork) {
base::test::TaskEnvironment task_environment;
bool thread_pool_task_completed = false;
base::PostTask(
FROM_HERE, {base::ThreadPool()}, base::BindLambdaForTesting([&]() {
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(200));
thread_pool_task_completed = true;
}));
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindLambdaForTesting([&]() {
// Nested RunAllTasksUntilIdle() (i.e. crbug.com/1035189).
content::RunAllTasksUntilIdle();
EXPECT_TRUE(thread_pool_task_completed);
run_loop.Quit();
}));
run_loop.Run();
EXPECT_TRUE(thread_pool_task_completed);
}
......@@ -1851,6 +1851,7 @@ test("content_unittests") {
"../public/test/no_renderer_crashes_assertion_unittest.cc",
"../public/test/permission_type_unittest.cc",
"../public/test/referrer_unittest.cc",
"../public/test/test_utils_unittest.cc",
"../renderer/categorized_worker_pool_unittest.cc",
"../renderer/child_frame_compositing_helper_unittest.cc",
"../renderer/compositor/layer_tree_view_unittest.cc",
......
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