Skip to content

Commit

Permalink
Use ScopedTaskEnvironment instead of MessageLoop
Browse files Browse the repository at this point in the history
MessageLoop will go away, eventually.

ScopedTaskEnvironment will per default start a ThreadPool, which should
be fine in most of the cases. If you belive your test needs to make sure
that no ThreadPool runs let me know and I will update the patch.

BUG=891670
This CL was uploaded by git cl split.

R=yuweih@chromium.org

Change-Id: Ic007c72adc58a2169301ad9611a21fc28ac849f6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1636353
Auto-Submit: Carlos Caballero <carlscab@google.com>
Commit-Queue: Yuwei Huang <yuweih@chromium.org>
Reviewed-by: Yuwei Huang <yuweih@chromium.org>
Cr-Commit-Position: refs/heads/master@{#664775}
  • Loading branch information
carlscabgro authored and Commit Bot committed May 30, 2019
1 parent e4e020b commit 1df57eb
Show file tree
Hide file tree
Showing 42 changed files with 163 additions and 147 deletions.
14 changes: 8 additions & 6 deletions remoting/base/auto_thread_task_runner_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/base/auto_thread_task_runner.h"

#include "base/bind.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "remoting/base/auto_thread_task_runner.h"
#include "base/test/scoped_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {
Expand All @@ -22,15 +23,16 @@ namespace remoting {

TEST(AutoThreadTaskRunnerTest, StartAndStop) {
// Create a task runner.
base::MessageLoop message_loop;
base::test::ScopedTaskEnvironment scoped_task_environment;
base::RunLoop run_loop;
scoped_refptr<AutoThreadTaskRunner> task_runner = new AutoThreadTaskRunner(
message_loop.task_runner(), run_loop.QuitClosure());
scoped_task_environment.GetMainThreadTaskRunner(),
run_loop.QuitClosure());

// Post a task to make sure it is executed.
bool success = false;
message_loop.task_runner()->PostTask(FROM_HERE,
base::BindOnce(&SetFlagTask, &success));
scoped_task_environment.GetMainThreadTaskRunner()->PostTask(
FROM_HERE, base::BindOnce(&SetFlagTask, &success));

task_runner = NULL;
run_loop.Run();
Expand Down
12 changes: 6 additions & 6 deletions remoting/base/auto_thread_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/scoped_native_library.h"
#include "base/single_thread_task_runner.h"
#include "base/test/scoped_task_environment.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"

Expand Down Expand Up @@ -56,20 +56,20 @@ namespace remoting {
class AutoThreadTest : public testing::Test {
public:
void RunMessageLoop() {
// Release |main_task_runner_|, then run |message_loop_| until other
// references created in tests are gone. We also post a delayed quit
// Release |main_task_runner_|, then run |scoped_task_environment_| until
// other references created in tests are gone. We also post a delayed quit
// task to |message_loop_| so the test will not hang on failure.
main_task_runner_ = NULL;
base::RunLoop run_loop;
quit_closure_ = run_loop.QuitClosure();
message_loop_.task_runner()->PostDelayedTask(
scoped_task_environment_.GetMainThreadTaskRunner()->PostDelayedTask(
FROM_HERE, run_loop.QuitClosure(), base::TimeDelta::FromSeconds(5));
run_loop.Run();
}

void SetUp() override {
main_task_runner_ = new AutoThreadTaskRunner(
message_loop_.task_runner(),
scoped_task_environment_.GetMainThreadTaskRunner(),
base::Bind(&AutoThreadTest::QuitMainMessageLoop,
base::Unretained(this)));
}
Expand All @@ -82,7 +82,7 @@ class AutoThreadTest : public testing::Test {
protected:
void QuitMainMessageLoop() { std::move(quit_closure_).Run(); }

base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
base::OnceClosure quit_closure_;
scoped_refptr<AutoThreadTaskRunner> main_task_runner_;
};
Expand Down
4 changes: 2 additions & 2 deletions remoting/base/buffered_socket_writer_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include <stdlib.h>

#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/test/scoped_task_environment.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/log/net_log.h"
Expand Down Expand Up @@ -158,7 +158,7 @@ class BufferedSocketWriterTest : public testing::Test {
VerifyWrittenData();
}

base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
net::NetLog net_log_;
SocketDataProvider socket_data_provider_;
std::unique_ptr<net::StreamSocket> socket_;
Expand Down
4 changes: 2 additions & 2 deletions remoting/base/oauth_token_getter_proxy_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread.h"
#include "base/threading/thread_checker.h"
#include "base/threading/thread_task_runner_handle.h"
Expand Down Expand Up @@ -135,7 +135,7 @@ class OAuthTokenGetterProxyTest : public testing::Test {

std::unique_ptr<TokenCallbackResult> expected_callback_result_;

base::MessageLoop main_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;

DISALLOW_COPY_AND_ASSIGN(OAuthTokenGetterProxyTest);
};
Expand Down
4 changes: 2 additions & 2 deletions remoting/base/telemetry_log_writer_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#include "base/containers/circular_deque.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/test/scoped_task_environment.h"
#include "net/http/http_status_code.h"
#include "remoting/base/chromoting_event.h"
#include "remoting/base/fake_oauth_token_getter.h"
Expand Down Expand Up @@ -139,7 +139,7 @@ class TelemetryLogWriterTest : public testing::Test {

private:
int id_ = 0;
base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
};

// Test workflow: add request -> log event -> respond request.
Expand Down
9 changes: 5 additions & 4 deletions remoting/client/display/gl_renderer_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread_task_runner_handle.h"
#include "remoting/client/display/fake_canvas.h"
#include "remoting/client/display/gl_renderer_delegate.h"
Expand Down Expand Up @@ -125,7 +125,7 @@ class GlRendererTest : public testing::Test {
return on_desktop_frame_processed_call_count_;
}

base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
std::unique_ptr<GlRenderer> renderer_;
FakeGlRendererDelegate delegate_;

Expand Down Expand Up @@ -160,7 +160,7 @@ void GlRendererTest::SetDesktopFrameWithSize(const webrtc::DesktopSize& size) {
void GlRendererTest::PostSetDesktopFrameTasks(const webrtc::DesktopSize& size,
int count) {
for (int i = 0; i < count; i++) {
message_loop_.task_runner()->PostTask(
scoped_task_environment_.GetMainThreadTaskRunner()->PostTask(
FROM_HERE, base::BindOnce(&GlRendererTest::SetDesktopFrameWithSize,
base::Unretained(this), size));
}
Expand All @@ -172,7 +172,8 @@ void GlRendererTest::OnDesktopFrameProcessed() {

void GlRendererTest::RunTasksInCurrentQueue() {
base::RunLoop run_loop;
message_loop_.task_runner()->PostTask(FROM_HERE, run_loop.QuitClosure());
scoped_task_environment_.GetMainThreadTaskRunner()->PostTask(
FROM_HERE, run_loop.QuitClosure());
run_loop.Run();
}

Expand Down
4 changes: 2 additions & 2 deletions remoting/client/queued_task_poster_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread.h"
#include "base/threading/thread_task_runner_handle.h"
#include "testing/gtest/include/gtest/gtest.h"
Expand Down Expand Up @@ -42,7 +42,7 @@ class QueuedTaskPosterTest : public testing::Test {
void AssertSequenceNotStarted();

base::Thread target_thread_;
base::MessageLoop main_message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
bool sequence_started_ = false;
};

Expand Down
4 changes: 2 additions & 2 deletions remoting/client/software_video_renderer_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#include <vector>

#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread.h"
#include "remoting/client/client_context.h"
#include "remoting/codec/video_encoder_verbatim.h"
Expand Down Expand Up @@ -143,7 +143,7 @@ class SoftwareVideoRendererTest : public ::testing::Test {
}

protected:
base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
ClientContext context_;

TestFrameConsumer frame_consumer_;
Expand Down
13 changes: 7 additions & 6 deletions remoting/host/client_session_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

#include "base/bind.h"
#include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/test/scoped_task_environment.h"
#include "build/build_config.h"
#include "remoting/base/auto_thread_task_runner.h"
#include "remoting/base/constants.h"
Expand Down Expand Up @@ -169,7 +169,7 @@ class ClientSessionTest : public testing::Test {
int curr_display_;

// Message loop that will process all ClientSession tasks.
base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;

// AutoThreadTaskRunner on which |client_session_| will be run.
scoped_refptr<AutoThreadTaskRunner> task_runner_;
Expand Down Expand Up @@ -200,12 +200,13 @@ class ClientSessionTest : public testing::Test {
};

void ClientSessionTest::SetUp() {
// Arrange to run |message_loop_| until no components depend on it.
// Arrange to run |scoped_task_environment_| until no components depend on it.
task_runner_ = new AutoThreadTaskRunner(
message_loop_.task_runner(), run_loop_.QuitClosure());
scoped_task_environment_.GetMainThreadTaskRunner(),
run_loop_.QuitClosure());

desktop_environment_factory_.reset(
new FakeDesktopEnvironmentFactory(message_loop_.task_runner()));
desktop_environment_factory_.reset(new FakeDesktopEnvironmentFactory(
scoped_task_environment_.GetMainThreadTaskRunner()));
desktop_environment_options_ = DesktopEnvironmentOptions::CreateDefault();
}

Expand Down
13 changes: 8 additions & 5 deletions remoting/host/desktop_session_agent_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
#include "base/location.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread_task_runner_handle.h"
#include "ipc/ipc_channel_proxy.h"
#include "ipc/ipc_listener.h"
Expand Down Expand Up @@ -94,17 +94,20 @@ class DesktopSessionAgentTest : public ::testing::Test {
void Shutdown();

protected:
base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
base::RunLoop run_loop_;
scoped_refptr<AutoThreadTaskRunner> task_runner_;
scoped_refptr<DesktopSessionAgent> agent_;
};

DesktopSessionAgentTest::DesktopSessionAgentTest()
: task_runner_(new AutoThreadTaskRunner(
message_loop_.task_runner(), run_loop_.QuitClosure())),
agent_(new DesktopSessionAgent(
task_runner_, task_runner_, task_runner_, task_runner_)) {}
scoped_task_environment_.GetMainThreadTaskRunner(),
run_loop_.QuitClosure())),
agent_(new DesktopSessionAgent(task_runner_,
task_runner_,
task_runner_,
task_runner_)) {}

void DesktopSessionAgentTest::Shutdown() {
task_runner_ = nullptr;
Expand Down
14 changes: 7 additions & 7 deletions remoting/host/host_change_notification_listener_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

#include "base/bind.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/scoped_task_environment.h"
#include "remoting/base/constants.h"
#include "remoting/signaling/mock_signal_strategy.h"
#include "remoting/signaling/signaling_address.h"
Expand Down Expand Up @@ -89,7 +89,7 @@ class HostChangeNotificationListenerTest : public testing::Test {
std::set<SignalStrategy::Listener*> signal_strategy_listeners_;
std::unique_ptr<HostChangeNotificationListener>
host_change_notification_listener_;
base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
};

TEST_F(HostChangeNotificationListenerTest, ReceiveValidNotification) {
Expand All @@ -99,7 +99,7 @@ TEST_F(HostChangeNotificationListenerTest, ReceiveValidNotification) {
GetNotificationStanza("delete", kHostId, kTestBotJid);
host_change_notification_listener_->OnSignalStrategyIncomingStanza(
stanza.get());
message_loop_.task_runner()->PostTask(
scoped_task_environment_.GetMainThreadTaskRunner()->PostTask(
FROM_HERE, base::RunLoop::QuitCurrentWhenIdleClosureDeprecated());
base::RunLoop().Run();
}
Expand All @@ -112,7 +112,7 @@ TEST_F(HostChangeNotificationListenerTest, ReceiveNotificationBeforeDelete) {
host_change_notification_listener_->OnSignalStrategyIncomingStanza(
stanza.get());
host_change_notification_listener_.reset();
message_loop_.task_runner()->PostTask(
scoped_task_environment_.GetMainThreadTaskRunner()->PostTask(
FROM_HERE, base::RunLoop::QuitCurrentWhenIdleClosureDeprecated());
base::RunLoop().Run();
}
Expand All @@ -125,7 +125,7 @@ TEST_F(HostChangeNotificationListenerTest, ReceiveInvalidHostIdNotification) {
GetNotificationStanza("delete", "1", kTestBotJid);
host_change_notification_listener_->OnSignalStrategyIncomingStanza(
stanza.get());
message_loop_.task_runner()->PostTask(
scoped_task_environment_.GetMainThreadTaskRunner()->PostTask(
FROM_HERE, base::RunLoop::QuitCurrentWhenIdleClosureDeprecated());
base::RunLoop().Run();
}
Expand All @@ -137,7 +137,7 @@ TEST_F(HostChangeNotificationListenerTest, ReceiveInvalidBotJidNotification) {
"delete", kHostId, "notremotingbot@bot.talk.google.com");
host_change_notification_listener_->OnSignalStrategyIncomingStanza(
stanza.get());
message_loop_.task_runner()->PostTask(
scoped_task_environment_.GetMainThreadTaskRunner()->PostTask(
FROM_HERE, base::RunLoop::QuitCurrentWhenIdleClosureDeprecated());
base::RunLoop().Run();
}
Expand All @@ -149,7 +149,7 @@ TEST_F(HostChangeNotificationListenerTest, ReceiveNonDeleteNotification) {
GetNotificationStanza("update", kHostId, kTestBotJid);
host_change_notification_listener_->OnSignalStrategyIncomingStanza(
stanza.get());
message_loop_.task_runner()->PostTask(
scoped_task_environment_.GetMainThreadTaskRunner()->PostTask(
FROM_HERE, base::RunLoop::QuitCurrentWhenIdleClosureDeprecated());
base::RunLoop().Run();
}
Expand Down
4 changes: 2 additions & 2 deletions remoting/host/host_status_logger_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

#include "remoting/host/host_status_logger.h"

#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/test/scoped_task_environment.h"
#include "remoting/host/host_status_monitor.h"
#include "remoting/signaling/mock_signal_strategy.h"
#include "remoting/signaling/xmpp_log_to_server.h"
Expand Down Expand Up @@ -138,7 +138,7 @@ class HostStatusLoggerTest : public testing::Test {
}

protected:
base::MessageLoop message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
MockSignalStrategy signal_strategy_;
std::unique_ptr<XmppLogToServer> log_to_server_;
std::unique_ptr<HostStatusLogger> host_status_logger_;
Expand Down
Loading

0 comments on commit 1df57eb

Please sign in to comment.