Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(742)

Unified Diff: mojo/common/handle_watcher_unittest.cc

Issue 506353002: Make HandleWatcher watch on the same thread if the thread is running a MessagePumpMojo. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: updated unittests Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/common/handle_watcher.cc ('k') | mojo/common/message_pump_mojo.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/common/handle_watcher_unittest.cc
diff --git a/mojo/common/handle_watcher_unittest.cc b/mojo/common/handle_watcher_unittest.cc
index b734e99b7fc9564a5390c6dc418adada04da5616..d7b8b72eaa8f3bad503d81be3b5f3b6e07b080db 100644
--- a/mojo/common/handle_watcher_unittest.cc
+++ b/mojo/common/handle_watcher_unittest.cc
@@ -13,6 +13,7 @@
#include "base/run_loop.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/threading/thread.h"
+#include "mojo/common/message_pump_mojo.h"
#include "mojo/common/time_helper.h"
#include "mojo/public/cpp/system/core.h"
#include "mojo/public/cpp/test_support/test_utils.h"
@@ -22,6 +23,12 @@ namespace mojo {
namespace common {
namespace test {
+enum MessageLoopConfig {
+ MESSAGE_LOOP_CONFIG_DEFAULT = 0,
+ MESSAGE_LOOP_CONFIG_MOJO = 1,
+ MESSAGE_LOOP_CONFIG_COUNT = 2
+};
+
void ObserveCallback(bool* was_signaled,
MojoResult* result_observed,
MojoResult result) {
@@ -42,6 +49,21 @@ void DeleteWatcherAndForwardResult(
next_callback.Run(result);
}
+scoped_ptr<base::MessageLoop> CreateMessageLoop(MessageLoopConfig config) {
+ scoped_ptr<base::MessageLoop> loop;
+ switch (config) {
+ case MESSAGE_LOOP_CONFIG_DEFAULT:
+ loop.reset(new base::MessageLoop());
+ break;
+ case MESSAGE_LOOP_CONFIG_MOJO:
+ loop.reset(new base::MessageLoop(MessagePumpMojo::Create()));
+ break;
+ case MESSAGE_LOOP_CONFIG_COUNT:
+ break;
sky 2014/08/28 23:12:19 NOTREACHED
yzshen1 2014/08/29 00:23:45 This 'case' is removed entirely.
+ }
+ return loop.Pass();
+}
+
// Helper class to manage the callback and running the message loop waiting for
// message to be received. Typical usage is something like:
// Schedule callback returned from GetCallback().
@@ -105,12 +127,26 @@ class CallbackHelper {
class HandleWatcherTest : public testing::Test {
public:
- HandleWatcherTest() {}
+ HandleWatcherTest() : next_message_loop_config_(MESSAGE_LOOP_CONFIG_DEFAULT) {
+ }
virtual ~HandleWatcherTest() {
test::SetTickClockForTest(NULL);
}
protected:
+ bool NextTestConfig() {
+ if (next_message_loop_config_ == MESSAGE_LOOP_CONFIG_COUNT)
+ return false;
+
+ // We have to destroy the current message loop before we create a new one.
+ message_loop_.reset();
+
+ message_loop_ = CreateMessageLoop(next_message_loop_config_);
+ next_message_loop_config_ = static_cast<MessageLoopConfig>(
+ next_message_loop_config_ + 1);
+ return true;
+ }
+
void InstallTickClock() {
test::SetTickClockForTest(&tick_clock_);
}
@@ -119,223 +155,238 @@ class HandleWatcherTest : public testing::Test {
private:
base::ShadowingAtExitManager at_exit_;
- base::MessageLoop message_loop_;
+ scoped_ptr<base::MessageLoop> message_loop_;
+ MessageLoopConfig next_message_loop_config_;
DISALLOW_COPY_AND_ASSIGN(HandleWatcherTest);
};
// Trivial test case with a single handle to watch.
TEST_F(HandleWatcherTest, SingleHandler) {
- MessagePipe test_pipe;
- ASSERT_TRUE(test_pipe.handle0.is_valid());
- CallbackHelper callback_helper;
- HandleWatcher watcher;
- callback_helper.Start(&watcher, test_pipe.handle0.get());
- RunUntilIdle();
- EXPECT_FALSE(callback_helper.got_callback());
- EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe.handle1.get(),
- std::string()));
- callback_helper.RunUntilGotCallback();
- EXPECT_TRUE(callback_helper.got_callback());
+ while (NextTestConfig()) {
+ MessagePipe test_pipe;
+ ASSERT_TRUE(test_pipe.handle0.is_valid());
+ CallbackHelper callback_helper;
+ HandleWatcher watcher;
+ callback_helper.Start(&watcher, test_pipe.handle0.get());
+ RunUntilIdle();
+ EXPECT_FALSE(callback_helper.got_callback());
+ EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe.handle1.get(),
+ std::string()));
+ callback_helper.RunUntilGotCallback();
+ EXPECT_TRUE(callback_helper.got_callback());
+ }
}
// Creates three handles and notfies them in reverse order ensuring each one is
// notified appropriately.
TEST_F(HandleWatcherTest, ThreeHandles) {
- MessagePipe test_pipe1;
- MessagePipe test_pipe2;
- MessagePipe test_pipe3;
- CallbackHelper callback_helper1;
- CallbackHelper callback_helper2;
- CallbackHelper callback_helper3;
- ASSERT_TRUE(test_pipe1.handle0.is_valid());
- ASSERT_TRUE(test_pipe2.handle0.is_valid());
- ASSERT_TRUE(test_pipe3.handle0.is_valid());
-
- HandleWatcher watcher1;
- callback_helper1.Start(&watcher1, test_pipe1.handle0.get());
- RunUntilIdle();
- EXPECT_FALSE(callback_helper1.got_callback());
- EXPECT_FALSE(callback_helper2.got_callback());
- EXPECT_FALSE(callback_helper3.got_callback());
-
- HandleWatcher watcher2;
- callback_helper2.Start(&watcher2, test_pipe2.handle0.get());
- RunUntilIdle();
- EXPECT_FALSE(callback_helper1.got_callback());
- EXPECT_FALSE(callback_helper2.got_callback());
- EXPECT_FALSE(callback_helper3.got_callback());
-
- HandleWatcher watcher3;
- callback_helper3.Start(&watcher3, test_pipe3.handle0.get());
- RunUntilIdle();
- EXPECT_FALSE(callback_helper1.got_callback());
- EXPECT_FALSE(callback_helper2.got_callback());
- EXPECT_FALSE(callback_helper3.got_callback());
-
- // Write to 3 and make sure it's notified.
- EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe3.handle1.get(),
- std::string()));
- callback_helper3.RunUntilGotCallback();
- EXPECT_FALSE(callback_helper1.got_callback());
- EXPECT_FALSE(callback_helper2.got_callback());
- EXPECT_TRUE(callback_helper3.got_callback());
- callback_helper3.clear_callback();
-
- // Write to 1 and 3. Only 1 should be notified since 3 was is no longer
- // running.
- EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe1.handle1.get(),
- std::string()));
- EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe3.handle1.get(),
- std::string()));
- callback_helper1.RunUntilGotCallback();
- EXPECT_TRUE(callback_helper1.got_callback());
- EXPECT_FALSE(callback_helper2.got_callback());
- EXPECT_FALSE(callback_helper3.got_callback());
- callback_helper1.clear_callback();
-
- // Write to 1 and 2. Only 2 should be notified (since 1 was already notified).
- EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe1.handle1.get(),
- std::string()));
- EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe2.handle1.get(),
- std::string()));
- callback_helper2.RunUntilGotCallback();
- EXPECT_FALSE(callback_helper1.got_callback());
- EXPECT_TRUE(callback_helper2.got_callback());
- EXPECT_FALSE(callback_helper3.got_callback());
+ while (NextTestConfig()) {
sky 2014/08/28 23:12:19 Did you look into making these parameterized tests
yzshen1 2014/08/29 00:23:45 Great suggestion!
+ MessagePipe test_pipe1;
+ MessagePipe test_pipe2;
+ MessagePipe test_pipe3;
+ CallbackHelper callback_helper1;
+ CallbackHelper callback_helper2;
+ CallbackHelper callback_helper3;
+ ASSERT_TRUE(test_pipe1.handle0.is_valid());
+ ASSERT_TRUE(test_pipe2.handle0.is_valid());
+ ASSERT_TRUE(test_pipe3.handle0.is_valid());
+
+ HandleWatcher watcher1;
+ callback_helper1.Start(&watcher1, test_pipe1.handle0.get());
+ RunUntilIdle();
+ EXPECT_FALSE(callback_helper1.got_callback());
+ EXPECT_FALSE(callback_helper2.got_callback());
+ EXPECT_FALSE(callback_helper3.got_callback());
+
+ HandleWatcher watcher2;
+ callback_helper2.Start(&watcher2, test_pipe2.handle0.get());
+ RunUntilIdle();
+ EXPECT_FALSE(callback_helper1.got_callback());
+ EXPECT_FALSE(callback_helper2.got_callback());
+ EXPECT_FALSE(callback_helper3.got_callback());
+
+ HandleWatcher watcher3;
+ callback_helper3.Start(&watcher3, test_pipe3.handle0.get());
+ RunUntilIdle();
+ EXPECT_FALSE(callback_helper1.got_callback());
+ EXPECT_FALSE(callback_helper2.got_callback());
+ EXPECT_FALSE(callback_helper3.got_callback());
+
+ // Write to 3 and make sure it's notified.
+ EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe3.handle1.get(),
+ std::string()));
+ callback_helper3.RunUntilGotCallback();
+ EXPECT_FALSE(callback_helper1.got_callback());
+ EXPECT_FALSE(callback_helper2.got_callback());
+ EXPECT_TRUE(callback_helper3.got_callback());
+ callback_helper3.clear_callback();
+
+ // Write to 1 and 3. Only 1 should be notified since 3 was is no longer
+ // running.
+ EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe1.handle1.get(),
+ std::string()));
+ EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe3.handle1.get(),
+ std::string()));
+ callback_helper1.RunUntilGotCallback();
+ EXPECT_TRUE(callback_helper1.got_callback());
+ EXPECT_FALSE(callback_helper2.got_callback());
+ EXPECT_FALSE(callback_helper3.got_callback());
+ callback_helper1.clear_callback();
+
+ // Write to 1 and 2. Only 2 should be notified (since 1 was already
+ // notified).
+ EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe1.handle1.get(),
+ std::string()));
+ EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe2.handle1.get(),
+ std::string()));
+ callback_helper2.RunUntilGotCallback();
+ EXPECT_FALSE(callback_helper1.got_callback());
+ EXPECT_TRUE(callback_helper2.got_callback());
+ EXPECT_FALSE(callback_helper3.got_callback());
+ }
}
// Verifies Start() invoked a second time works.
TEST_F(HandleWatcherTest, Restart) {
- MessagePipe test_pipe1;
- MessagePipe test_pipe2;
- CallbackHelper callback_helper1;
- CallbackHelper callback_helper2;
- ASSERT_TRUE(test_pipe1.handle0.is_valid());
- ASSERT_TRUE(test_pipe2.handle0.is_valid());
-
- HandleWatcher watcher1;
- callback_helper1.Start(&watcher1, test_pipe1.handle0.get());
- RunUntilIdle();
- EXPECT_FALSE(callback_helper1.got_callback());
- EXPECT_FALSE(callback_helper2.got_callback());
-
- HandleWatcher watcher2;
- callback_helper2.Start(&watcher2, test_pipe2.handle0.get());
- RunUntilIdle();
- EXPECT_FALSE(callback_helper1.got_callback());
- EXPECT_FALSE(callback_helper2.got_callback());
-
- // Write to 1 and make sure it's notified.
- EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe1.handle1.get(),
- std::string()));
- callback_helper1.RunUntilGotCallback();
- EXPECT_TRUE(callback_helper1.got_callback());
- EXPECT_FALSE(callback_helper2.got_callback());
- callback_helper1.clear_callback();
- EXPECT_TRUE(mojo::test::DiscardMessage(test_pipe1.handle0.get()));
-
- // Write to 2 and make sure it's notified.
- EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe2.handle1.get(),
- std::string()));
- callback_helper2.RunUntilGotCallback();
- EXPECT_FALSE(callback_helper1.got_callback());
- EXPECT_TRUE(callback_helper2.got_callback());
- callback_helper2.clear_callback();
-
- // Listen on 1 again.
- callback_helper1.Start(&watcher1, test_pipe1.handle0.get());
- RunUntilIdle();
- EXPECT_FALSE(callback_helper1.got_callback());
- EXPECT_FALSE(callback_helper2.got_callback());
-
- // Write to 1 and make sure it's notified.
- EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe1.handle1.get(),
- std::string()));
- callback_helper1.RunUntilGotCallback();
- EXPECT_TRUE(callback_helper1.got_callback());
- EXPECT_FALSE(callback_helper2.got_callback());
+ while (NextTestConfig()) {
+ MessagePipe test_pipe1;
+ MessagePipe test_pipe2;
+ CallbackHelper callback_helper1;
+ CallbackHelper callback_helper2;
+ ASSERT_TRUE(test_pipe1.handle0.is_valid());
+ ASSERT_TRUE(test_pipe2.handle0.is_valid());
+
+ HandleWatcher watcher1;
+ callback_helper1.Start(&watcher1, test_pipe1.handle0.get());
+ RunUntilIdle();
+ EXPECT_FALSE(callback_helper1.got_callback());
+ EXPECT_FALSE(callback_helper2.got_callback());
+
+ HandleWatcher watcher2;
+ callback_helper2.Start(&watcher2, test_pipe2.handle0.get());
+ RunUntilIdle();
+ EXPECT_FALSE(callback_helper1.got_callback());
+ EXPECT_FALSE(callback_helper2.got_callback());
+
+ // Write to 1 and make sure it's notified.
+ EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe1.handle1.get(),
+ std::string()));
+ callback_helper1.RunUntilGotCallback();
+ EXPECT_TRUE(callback_helper1.got_callback());
+ EXPECT_FALSE(callback_helper2.got_callback());
+ callback_helper1.clear_callback();
+ EXPECT_TRUE(mojo::test::DiscardMessage(test_pipe1.handle0.get()));
+
+ // Write to 2 and make sure it's notified.
+ EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe2.handle1.get(),
+ std::string()));
+ callback_helper2.RunUntilGotCallback();
+ EXPECT_FALSE(callback_helper1.got_callback());
+ EXPECT_TRUE(callback_helper2.got_callback());
+ callback_helper2.clear_callback();
+
+ // Listen on 1 again.
+ callback_helper1.Start(&watcher1, test_pipe1.handle0.get());
+ RunUntilIdle();
+ EXPECT_FALSE(callback_helper1.got_callback());
+ EXPECT_FALSE(callback_helper2.got_callback());
+
+ // Write to 1 and make sure it's notified.
+ EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe1.handle1.get(),
+ std::string()));
+ callback_helper1.RunUntilGotCallback();
+ EXPECT_TRUE(callback_helper1.got_callback());
+ EXPECT_FALSE(callback_helper2.got_callback());
+ }
}
// Verifies deadline is honored.
TEST_F(HandleWatcherTest, Deadline) {
- InstallTickClock();
-
- MessagePipe test_pipe1;
- MessagePipe test_pipe2;
- MessagePipe test_pipe3;
- CallbackHelper callback_helper1;
- CallbackHelper callback_helper2;
- CallbackHelper callback_helper3;
- ASSERT_TRUE(test_pipe1.handle0.is_valid());
- ASSERT_TRUE(test_pipe2.handle0.is_valid());
- ASSERT_TRUE(test_pipe3.handle0.is_valid());
-
- // Add a watcher with an infinite timeout.
- HandleWatcher watcher1;
- callback_helper1.Start(&watcher1, test_pipe1.handle0.get());
- RunUntilIdle();
- EXPECT_FALSE(callback_helper1.got_callback());
- EXPECT_FALSE(callback_helper2.got_callback());
- EXPECT_FALSE(callback_helper3.got_callback());
-
- // Add another watcher wth a timeout of 500 microseconds.
- HandleWatcher watcher2;
- watcher2.Start(test_pipe2.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, 500,
- callback_helper2.GetCallback());
- RunUntilIdle();
- EXPECT_FALSE(callback_helper1.got_callback());
- EXPECT_FALSE(callback_helper2.got_callback());
- EXPECT_FALSE(callback_helper3.got_callback());
-
- // Advance the clock passed the deadline. We also have to start another
- // watcher to wake up the background thread.
- tick_clock_.Advance(base::TimeDelta::FromMicroseconds(501));
-
- HandleWatcher watcher3;
- callback_helper3.Start(&watcher3, test_pipe3.handle0.get());
-
- callback_helper2.RunUntilGotCallback();
- EXPECT_FALSE(callback_helper1.got_callback());
- EXPECT_TRUE(callback_helper2.got_callback());
- EXPECT_FALSE(callback_helper3.got_callback());
+ while (NextTestConfig()) {
+ InstallTickClock();
+
+ MessagePipe test_pipe1;
+ MessagePipe test_pipe2;
+ MessagePipe test_pipe3;
+ CallbackHelper callback_helper1;
+ CallbackHelper callback_helper2;
+ CallbackHelper callback_helper3;
+ ASSERT_TRUE(test_pipe1.handle0.is_valid());
+ ASSERT_TRUE(test_pipe2.handle0.is_valid());
+ ASSERT_TRUE(test_pipe3.handle0.is_valid());
+
+ // Add a watcher with an infinite timeout.
+ HandleWatcher watcher1;
+ callback_helper1.Start(&watcher1, test_pipe1.handle0.get());
+ RunUntilIdle();
+ EXPECT_FALSE(callback_helper1.got_callback());
+ EXPECT_FALSE(callback_helper2.got_callback());
+ EXPECT_FALSE(callback_helper3.got_callback());
+
+ // Add another watcher wth a timeout of 500 microseconds.
+ HandleWatcher watcher2;
+ watcher2.Start(test_pipe2.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, 500,
+ callback_helper2.GetCallback());
+ RunUntilIdle();
+ EXPECT_FALSE(callback_helper1.got_callback());
+ EXPECT_FALSE(callback_helper2.got_callback());
+ EXPECT_FALSE(callback_helper3.got_callback());
+
+ // Advance the clock passed the deadline. We also have to start another
+ // watcher to wake up the background thread.
+ tick_clock_.Advance(base::TimeDelta::FromMicroseconds(501));
+
+ HandleWatcher watcher3;
+ callback_helper3.Start(&watcher3, test_pipe3.handle0.get());
+
+ callback_helper2.RunUntilGotCallback();
+ EXPECT_FALSE(callback_helper1.got_callback());
+ EXPECT_TRUE(callback_helper2.got_callback());
+ EXPECT_FALSE(callback_helper3.got_callback());
+ }
}
TEST_F(HandleWatcherTest, DeleteInCallback) {
- MessagePipe test_pipe;
- CallbackHelper callback_helper;
-
- HandleWatcher* watcher = new HandleWatcher();
- callback_helper.StartWithCallback(watcher, test_pipe.handle1.get(),
- base::Bind(&DeleteWatcherAndForwardResult,
- watcher,
- callback_helper.GetCallback()));
- EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe.handle0.get(),
- std::string()));
- callback_helper.RunUntilGotCallback();
- EXPECT_TRUE(callback_helper.got_callback());
+ while (NextTestConfig()) {
+ MessagePipe test_pipe;
+ CallbackHelper callback_helper;
+
+ HandleWatcher* watcher = new HandleWatcher();
+ callback_helper.StartWithCallback(
+ watcher, test_pipe.handle1.get(),
+ base::Bind(&DeleteWatcherAndForwardResult, watcher,
+ callback_helper.GetCallback()));
+ EXPECT_TRUE(mojo::test::WriteTextMessage(test_pipe.handle0.get(),
+ std::string()));
+ callback_helper.RunUntilGotCallback();
+ EXPECT_TRUE(callback_helper.got_callback());
+ }
}
TEST(HandleWatcherCleanEnvironmentTest, AbortedOnMessageLoopDestruction) {
- bool was_signaled = false;
- MojoResult result = MOJO_RESULT_OK;
+ for (int i = 0; i < MESSAGE_LOOP_CONFIG_COUNT; ++i) {
+ bool was_signaled = false;
+ MojoResult result = MOJO_RESULT_OK;
- base::ShadowingAtExitManager at_exit;
- MessagePipe pipe;
- HandleWatcher watcher;
- {
- base::MessageLoop loop;
+ base::ShadowingAtExitManager at_exit;
+ MessagePipe pipe;
+ HandleWatcher watcher;
+ {
+ scoped_ptr<base::MessageLoop> loop(
+ CreateMessageLoop(static_cast<MessageLoopConfig>(i)));
- watcher.Start(pipe.handle0.get(),
- MOJO_HANDLE_SIGNAL_READABLE,
- MOJO_DEADLINE_INDEFINITE,
- base::Bind(&ObserveCallback, &was_signaled, &result));
+ watcher.Start(pipe.handle0.get(),
+ MOJO_HANDLE_SIGNAL_READABLE,
+ MOJO_DEADLINE_INDEFINITE,
+ base::Bind(&ObserveCallback, &was_signaled, &result));
- // Now, let the MessageLoop get torn down. We expect our callback to run.
- }
+ // Now, let the MessageLoop get torn down. We expect our callback to run.
+ }
- EXPECT_TRUE(was_signaled);
- EXPECT_EQ(MOJO_RESULT_ABORTED, result);
+ EXPECT_TRUE(was_signaled);
+ EXPECT_EQ(MOJO_RESULT_ABORTED, result);
+ }
}
void NeverReached(MojoResult result) {
@@ -423,7 +474,14 @@ TEST(HandleWatcherCleanEnvironmentTest, StressTest) {
// threads running at once.
for (int i = 0; i < kThreadCount; ++i) {
scoped_ptr<base::Thread> thread(new base::Thread("test thread"));
- thread->Start();
+ if (i % 2) {
+ base::Thread::Options thread_options;
+ thread_options.message_pump_factory =
+ base::Bind(&MessagePumpMojo::Create);
+ thread->StartWithOptions(thread_options);
+ } else {
+ thread->Start();
+ }
threads.push_back(thread.release());
}
for (int i = 0; i < kThreadCount; ++i) {
« no previous file with comments | « mojo/common/handle_watcher.cc ('k') | mojo/common/message_pump_mojo.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698