Commit 4ed468c1 authored by Gabriel Charette's avatar Gabriel Charette Committed by Commit Bot

[content test_utils] Fix RunAllPendingInMessageLoop(BrowserThread::IO) under REAL_IO_THREAD

See bug for details.

Bug: 1035604
Change-Id: I071fce979094d291171a7fba4b90da2bf70ce711
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1974585Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarCarlos Caballero <carlscab@google.com>
Commit-Queue: Gabriel Charette <gab@chromium.org>
Cr-Commit-Position: refs/heads/master@{#726416}
parent f83f4f4b
......@@ -246,8 +246,8 @@ void BrowserTaskExecutor::ResetForTesting() {
// static
void BrowserTaskExecutor::PostFeatureListSetup() {
DCHECK(g_browser_task_executor);
DCHECK(g_browser_task_executor->ui_thread_executor_);
DCHECK(g_browser_task_executor->io_thread_executor_);
DCHECK(g_browser_task_executor->browser_ui_thread_handle_);
DCHECK(g_browser_task_executor->browser_io_thread_handle_);
g_browser_task_executor->browser_ui_thread_handle_
->PostFeatureListInitializationSetup();
g_browser_task_executor->browser_io_thread_handle_
......@@ -285,12 +285,6 @@ void BrowserTaskExecutor::RunAllPendingTasksOnThreadForTesting(
->ScheduleRunAllPendingTasksForTesting(run_loop.QuitClosure());
break;
case BrowserThread::IO: {
// In tests there may not be a functional IO thread.
if (!g_browser_task_executor->io_thread_executor_ ||
!g_browser_task_executor->io_thread_executor_
->HasDelegateForTesting()) {
return;
}
g_browser_task_executor->browser_io_thread_handle_
->ScheduleRunAllPendingTasksForTesting(run_loop.QuitClosure());
break;
......
......@@ -230,8 +230,6 @@ class CONTENT_EXPORT BrowserTaskExecutor : public BaseBrowserTaskExecutor {
return std::move(browser_io_thread_delegate_);
}
bool HasDelegateForTesting() const { return !!browser_io_thread_delegate_; }
private:
BrowserThread::ID GetCurrentThreadID() const override;
......
......@@ -9,6 +9,9 @@
#include "base/test/bind_test_util.h"
#include "base/test/task_environment.h"
#include "base/threading/platform_thread.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
// Regression test for crbug.com/1035189.
......@@ -18,7 +21,7 @@ TEST(ContentTestUtils, NestedRunAllTasksUntilIdleWithPendingThreadPoolWork) {
bool thread_pool_task_completed = false;
base::PostTask(
FROM_HERE, {base::ThreadPool()}, base::BindLambdaForTesting([&]() {
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(200));
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
thread_pool_task_completed = true;
}));
......@@ -35,3 +38,94 @@ TEST(ContentTestUtils, NestedRunAllTasksUntilIdleWithPendingThreadPoolWork) {
run_loop.Run();
EXPECT_TRUE(thread_pool_task_completed);
}
// Regression test for crbug.com/1035604.
TEST(ContentTestUtils, FlushRealIOThread) {
content::BrowserTaskEnvironment task_environment{
content::BrowserTaskEnvironment::REAL_IO_THREAD};
bool io_task_completed = false;
base::PostTask(
FROM_HERE, {content::BrowserThread::IO},
base::BindLambdaForTesting([&]() {
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
io_task_completed = true;
}));
content::RunAllPendingInMessageLoop(content::BrowserThread::IO);
EXPECT_TRUE(io_task_completed);
}
TEST(ContentTestUtils, NestedFlushRealIOThread) {
content::BrowserTaskEnvironment task_environment{
content::BrowserTaskEnvironment::REAL_IO_THREAD};
bool io_task_completed = false;
base::PostTask(
FROM_HERE, {content::BrowserThread::IO},
base::BindLambdaForTesting([&]() {
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
io_task_completed = true;
}));
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindLambdaForTesting([&]() {
content::RunAllPendingInMessageLoop(content::BrowserThread::IO);
EXPECT_TRUE(io_task_completed);
run_loop.Quit();
}));
run_loop.Run();
EXPECT_TRUE(io_task_completed);
}
TEST(ContentTestUtils, FlushRealIOThreadWithPendingBestEffortTask) {
content::BrowserTaskEnvironment task_environment{
content::BrowserTaskEnvironment::REAL_IO_THREAD};
bool io_task_completed = false;
base::PostTask(
FROM_HERE, {content::BrowserThread::IO, base::TaskPriority::BEST_EFFORT},
base::BindLambdaForTesting([&]() {
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
io_task_completed = true;
}));
content::RunAllPendingInMessageLoop(content::BrowserThread::IO);
EXPECT_TRUE(io_task_completed);
}
// Same as FlushRealIOThreadWithPendingBestEffortTask but when BrowserThread::IO
// is multiplexed with BrowserThread::UI on the main thread (i.e. the default
// under BrowserTaskEnvironment).
TEST(ContentTestUtils, FlushFakeIOThread) {
content::BrowserTaskEnvironment task_environment;
bool io_task_completed = false;
base::PostTask(
FROM_HERE, {content::BrowserThread::IO, base::TaskPriority::BEST_EFFORT},
base::BindLambdaForTesting([&]() {
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
io_task_completed = true;
}));
content::RunAllPendingInMessageLoop(content::BrowserThread::IO);
EXPECT_TRUE(io_task_completed);
}
TEST(ContentTestUtils, FlushUIThread) {
content::BrowserTaskEnvironment task_environment;
bool ui_task_completed = false;
base::PostTask(
FROM_HERE, {content::BrowserThread::UI, base::TaskPriority::BEST_EFFORT},
base::BindLambdaForTesting([&]() {
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
ui_task_completed = true;
}));
content::RunAllPendingInMessageLoop(content::BrowserThread::UI);
EXPECT_TRUE(ui_task_completed);
}
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