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

Unified Diff: mojo/public/cpp/system/tests/watcher_unittest.cc

Issue 2725133002: Mojo: Armed Watchers (Closed)
Patch Set: . Created 3 years, 10 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
Index: mojo/public/cpp/system/tests/watcher_unittest.cc
diff --git a/mojo/public/cpp/system/tests/watcher_unittest.cc b/mojo/public/cpp/system/tests/watcher_unittest.cc
index 9b59240c5dcdf82b865ed07f53a5c06ee79af17d..5a8cc08e79d85bc3b86b51d2c4c589b05b46175c 100644
--- a/mojo/public/cpp/system/tests/watcher_unittest.cc
+++ b/mojo/public/cpp/system/tests/watcher_unittest.cc
@@ -48,7 +48,7 @@ TEST_F(WatcherTest, WatchBasic) {
bool notified = false;
base::RunLoop run_loop;
- Watcher b_watcher(FROM_HERE);
+ Watcher b_watcher(FROM_HERE, Watcher::ArmingPolicy::AUTOMATIC);
EXPECT_EQ(MOJO_RESULT_OK,
b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
OnReady([&] (MojoResult result) {
@@ -71,7 +71,7 @@ TEST_F(WatcherTest, WatchUnsatisfiable) {
CreateMessagePipe(nullptr, &a, &b);
a.reset();
- Watcher b_watcher(FROM_HERE);
+ Watcher b_watcher(FROM_HERE, Watcher::ArmingPolicy::AUTOMATIC);
EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
NotReached()));
@@ -84,7 +84,7 @@ TEST_F(WatcherTest, WatchInvalidHandle) {
a.reset();
b.reset();
- Watcher b_watcher(FROM_HERE);
+ Watcher b_watcher(FROM_HERE, Watcher::ArmingPolicy::AUTOMATIC);
EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT,
b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
NotReached()));
@@ -96,7 +96,7 @@ TEST_F(WatcherTest, Cancel) {
CreateMessagePipe(nullptr, &a, &b);
base::RunLoop run_loop;
- Watcher b_watcher(FROM_HERE);
+ Watcher b_watcher(FROM_HERE, Watcher::ArmingPolicy::AUTOMATIC);
EXPECT_EQ(MOJO_RESULT_OK,
b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
NotReached()));
@@ -118,7 +118,7 @@ TEST_F(WatcherTest, CancelOnClose) {
CreateMessagePipe(nullptr, &a, &b);
base::RunLoop run_loop;
- Watcher b_watcher(FROM_HERE);
+ Watcher b_watcher(FROM_HERE, Watcher::ArmingPolicy::AUTOMATIC);
EXPECT_EQ(MOJO_RESULT_OK,
b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
OnReady([&] (MojoResult result) {
@@ -140,7 +140,7 @@ TEST_F(WatcherTest, CancelOnDestruction) {
CreateMessagePipe(nullptr, &a, &b);
base::RunLoop run_loop;
{
- Watcher b_watcher(FROM_HERE);
+ Watcher b_watcher(FROM_HERE, Watcher::ArmingPolicy::AUTOMATIC);
EXPECT_EQ(MOJO_RESULT_OK,
b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
NotReached()));
@@ -161,7 +161,7 @@ TEST_F(WatcherTest, CloseAndCancel) {
ScopedMessagePipeHandle a, b;
CreateMessagePipe(nullptr, &a, &b);
- Watcher b_watcher(FROM_HERE);
+ Watcher b_watcher(FROM_HERE, Watcher::ArmingPolicy::AUTOMATIC);
EXPECT_EQ(MOJO_RESULT_OK,
b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
OnReady([](MojoResult result) { FAIL(); })));
@@ -177,5 +177,99 @@ TEST_F(WatcherTest, CloseAndCancel) {
base::RunLoop().RunUntilIdle();
}
+TEST_F(WatcherTest, UnarmedCancel) {
+ ScopedMessagePipeHandle a, b;
+ CreateMessagePipe(nullptr, &a, &b);
+
+ Watcher b_watcher(FROM_HERE, Watcher::ArmingPolicy::MANUAL);
+ base::RunLoop loop;
+ EXPECT_EQ(MOJO_RESULT_OK,
+ b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
+ base::Bind(
+ [](base::RunLoop* loop, MojoResult result) {
+ EXPECT_EQ(result, MOJO_RESULT_CANCELLED);
+ loop->Quit();
+ },
+ &loop)));
+
+ // This message write will not wake up the watcher since the watcher isn't
+ // armed. Instead, the cancellation will dispatch due to the reset below.
+ EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0,
+ MOJO_WRITE_MESSAGE_FLAG_NONE));
+ b.reset();
+ loop.Run();
+}
+
+TEST_F(WatcherTest, ManualArming) {
+ ScopedMessagePipeHandle a, b;
+ CreateMessagePipe(nullptr, &a, &b);
+
+ Watcher b_watcher(FROM_HERE, Watcher::ArmingPolicy::MANUAL);
+ base::RunLoop loop;
+ EXPECT_EQ(MOJO_RESULT_OK,
+ b_watcher.Start(b.get(), MOJO_HANDLE_SIGNAL_READABLE,
+ base::Bind(
+ [](base::RunLoop* loop, MojoResult result) {
+ EXPECT_EQ(result, MOJO_RESULT_OK);
+ loop->Quit();
+ },
+ &loop)));
+ EXPECT_EQ(MOJO_RESULT_OK, b_watcher.Arm());
+
+ EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0,
+ MOJO_WRITE_MESSAGE_FLAG_NONE));
+ loop.Run();
+}
+
+TEST_F(WatcherTest, ManualArmOrNotifyWhileSignaled) {
+ ScopedMessagePipeHandle a, b;
+ CreateMessagePipe(nullptr, &a, &b);
+
+ base::RunLoop loop1;
+ Watcher b_watcher1(FROM_HERE, Watcher::ArmingPolicy::MANUAL);
+ bool notified1 = false;
+ EXPECT_EQ(MOJO_RESULT_OK,
+ b_watcher1.Start(
+ b.get(), MOJO_HANDLE_SIGNAL_READABLE,
+ base::Bind(
+ [](base::RunLoop* loop, bool* notified, MojoResult result) {
+ EXPECT_EQ(result, MOJO_RESULT_OK);
+ *notified = true;
+ loop->Quit();
+ },
+ &loop1, &notified1)));
+
+ base::RunLoop loop2;
+ Watcher b_watcher2(FROM_HERE, Watcher::ArmingPolicy::MANUAL);
+ bool notified2 = false;
+ EXPECT_EQ(MOJO_RESULT_OK,
+ b_watcher2.Start(
+ b.get(), MOJO_HANDLE_SIGNAL_READABLE,
+ base::Bind(
+ [](base::RunLoop* loop, bool* notified, MojoResult result) {
+ EXPECT_EQ(result, MOJO_RESULT_OK);
+ *notified = true;
+ loop->Quit();
+ },
+ &loop2, &notified2)));
+
+ // First ensure that |b| is readable.
+ EXPECT_EQ(MOJO_RESULT_OK, b_watcher1.Arm());
+ EXPECT_EQ(MOJO_RESULT_OK, WriteMessageRaw(a.get(), "hello", 5, nullptr, 0,
+ MOJO_WRITE_MESSAGE_FLAG_NONE));
+ loop1.Run();
+
+ EXPECT_TRUE(notified1);
+ EXPECT_FALSE(notified2);
+ notified1 = false;
+
+ // Now verify that ArmOrNotify results in a notification.
+ b_watcher2.ArmOrNotify();
+ loop2.Run();
+
+ EXPECT_FALSE(notified1);
+ EXPECT_TRUE(notified2);
+}
+
} // namespace
} // namespace mojo

Powered by Google App Engine
This is Rietveld 408576698