Chromium Code Reviews| Index: mojo/edk/system/watch_unittest.cc |
| diff --git a/mojo/edk/system/watch_unittest.cc b/mojo/edk/system/watch_unittest.cc |
| index 7b3137f97c411d01d91f0f412b3a97d2ed33d2fc..6ae2994c49eba44c16f04d6dc83799a056e79c4d 100644 |
| --- a/mojo/edk/system/watch_unittest.cc |
| +++ b/mojo/edk/system/watch_unittest.cc |
| @@ -5,8 +5,11 @@ |
| #include <functional> |
| #include "base/macros.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/threading/thread_task_runner_handle.h" |
| #include "mojo/edk/system/request_context.h" |
| #include "mojo/edk/test/mojo_test_base.h" |
| #include "mojo/public/c/system/functions.h" |
| @@ -29,7 +32,7 @@ class WatchHelper { |
| using Callback = |
| std::function<void(MojoResult result, MojoHandleSignalsState state)>; |
| - WatchHelper() {} |
| + WatchHelper() : task_runner_(base::ThreadTaskRunnerHandle::Get()) {} |
| ~WatchHelper() { |
| CHECK(!watching_); |
| } |
| @@ -55,27 +58,32 @@ class WatchHelper { |
| watching_ = false; |
| } |
| - void ExpectSystemNotifications() { expect_system_notifications_ = true; } |
| - |
| private: |
| static void OnNotify(uintptr_t context, |
| MojoResult result, |
| MojoHandleSignalsState state, |
| MojoWatchNotificationFlags flags) { |
| WatchHelper* watcher = reinterpret_cast<WatchHelper*>(context); |
| + watcher->task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&NotifyOnMainThread, context, result, state, flags)); |
| + } |
| + |
| + static void NotifyOnMainThread(uintptr_t context, |
| + MojoResult result, |
| + MojoHandleSignalsState state, |
| + MojoWatchNotificationFlags flags) { |
| + WatchHelper* watcher = reinterpret_cast<WatchHelper*>(context); |
| CHECK(watcher->watching_); |
| if (result == MOJO_RESULT_CANCELLED) |
| watcher->watching_ = false; |
| - CHECK_EQ(flags, watcher->expect_system_notifications_? |
| - MOJO_WATCH_NOTIFICATION_FLAG_FROM_SYSTEM : |
| - MOJO_WATCH_NOTIFICATION_FLAG_NONE); |
| watcher->callback_(result, state); |
| } |
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| bool watching_ = false; |
| MojoHandle handle_; |
| Callback callback_; |
| - bool expect_system_notifications_ = false; |
| DISALLOW_COPY_AND_ASSIGN(WatchHelper); |
| }; |
| @@ -125,9 +133,12 @@ TEST_F(WatchTest, NotifyUnsatisfiable) { |
| base::RunLoop loop; |
| WatchHelper b_watcher; |
| + base::ThreadChecker tc; |
| b_watcher.Watch( |
| b, MOJO_HANDLE_SIGNAL_READABLE, |
| [&] (MojoResult result, MojoHandleSignalsState state) { |
| + DCHECK(tc.CalledOnValidThread()); |
| + LOG(ERROR) <<"YES CALLED ."; |
|
Anand Mistry (off Chromium)
2016/07/15 09:55:04
Remember to remove before submitting.
Ken Rockot(use gerrit already)
2016/07/15 13:55:40
Done
|
| EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); |
| EXPECT_EQ(0u, |
| state.satisfied_signals & MOJO_HANDLE_SIGNAL_READABLE); |
| @@ -249,6 +260,7 @@ TEST_F(WatchTest, WatchWhileSatisfied) { |
| WriteMessage(a, "hey"); |
| bool signaled = false; |
| WatchHelper b_watcher; |
| + base::RunLoop loop; |
| b_watcher.Watch( |
| b, MOJO_HANDLE_SIGNAL_READABLE, |
| [&] (MojoResult result, MojoHandleSignalsState state) { |
| @@ -256,7 +268,9 @@ TEST_F(WatchTest, WatchWhileSatisfied) { |
| EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE, |
| state.satisfied_signals & MOJO_HANDLE_SIGNAL_READABLE); |
| signaled = true; |
| + loop.Quit(); |
| }); |
| + loop.Run(); |
| EXPECT_TRUE(signaled); |
| b_watcher.Cancel(); |
| @@ -419,6 +433,7 @@ TEST_F(WatchTest, NestedCancellation) { |
| CreateMessagePipe(&a, &b); |
| CreateMessagePipe(&c, &d); |
| + base::RunLoop loop; |
| bool a_watcher_run = false; |
| WatchHelper a_watcher; |
| a_watcher.Watch( |
| @@ -438,8 +453,9 @@ TEST_F(WatchTest, NestedCancellation) { |
| // ...but this should prevent that notification from dispatching because |
| // |a_watcher| is now cancelled. |
| a_watcher.Cancel(); |
| + |
| + loop.Quit(); |
| }); |
| - c_watcher.ExpectSystemNotifications(); |
| { |
| // Force "system" notifications for the synchronous behavior required to |
| @@ -451,6 +467,8 @@ TEST_F(WatchTest, NestedCancellation) { |
| CloseHandle(d); |
| } |
| + loop.Run(); |
| + |
| EXPECT_FALSE(a_watcher.is_watching()); |
| EXPECT_FALSE(a_watcher_run); |