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

Unified Diff: mojo/edk/embedder/embedder_unittest.cc

Issue 798993003: Add embedder::AsyncWait() (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 6 years 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/edk/embedder/embedder_unittest.cc
diff --git a/mojo/edk/embedder/embedder_unittest.cc b/mojo/edk/embedder/embedder_unittest.cc
index 03937489025e2008975704fcc27b0c9089c5a7a7..17c5ff90827014c03ff2aadbaa27519b5f73f207 100644
--- a/mojo/edk/embedder/embedder_unittest.cc
+++ b/mojo/edk/embedder/embedder_unittest.cc
@@ -13,6 +13,7 @@
#include "base/message_loop/message_loop.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/test_io_thread.h"
+#include "base/test/test_timeouts.h"
#include "mojo/edk/embedder/platform_channel_pair.h"
#include "mojo/edk/embedder/test_embedder.h"
#include "mojo/edk/system/test_utils.h"
@@ -160,6 +161,99 @@ TEST_F(EmbedderTest, ChannelsBasic) {
EXPECT_TRUE(test::Shutdown());
}
+class TestAsyncWaiter {
+ public:
+ TestAsyncWaiter() : event_(true, false), wait_result_(MOJO_RESULT_UNKNOWN) {}
+
+ void Awake(MojoResult result) {
+ base::AutoLock l(result_lock_);
+ wait_result_ = result;
+ event_.Signal();
+ }
+
+ bool TryWait() { return event_.TimedWait(TestTimeouts::action_timeout()); }
+
+ MojoResult wait_result() const {
+ base::AutoLock l(result_lock_);
+ return wait_result_;
+ }
+
+ private:
+ base::WaitableEvent event_;
+
+ mutable base::Lock result_lock_;
+ MojoResult wait_result_;
viettrungluu 2014/12/12 21:39:30 You should probably call this just "result_", or t
gmorrita 2014/12/12 22:56:27 Done.
+
+ DISALLOW_COPY_AND_ASSIGN(TestAsyncWaiter);
+};
+
+void ReadAndIgnore(MojoHandle pipe) {
viettrungluu 2014/12/12 21:39:30 Since you only use this function once (and not as
viettrungluu 2014/12/12 21:40:22 I'm not implying that you need to do that here, BT
gmorrita 2014/12/12 22:56:27 Did this for readability but apparently not worth
+ char buffer[1000] = {};
viettrungluu 2014/12/12 21:39:30 Since you're not actually going to look at |buffer
gmorrita 2014/12/12 22:56:27 Done.
+ uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer));
+ CHECK_EQ(MOJO_RESULT_OK,
+ MojoReadMessage(pipe, buffer, &num_bytes, nullptr, nullptr,
+ MOJO_READ_MESSAGE_FLAG_NONE));
+}
+
+void WriteHello(MojoHandle pipe) {
+ const char kHello[] = "hello";
viettrungluu 2014/12/12 21:39:30 nit: static const char
gmorrita 2014/12/12 22:56:27 Done.
+ CHECK_EQ(MOJO_RESULT_OK,
+ MojoWriteMessage(pipe, kHello, static_cast<uint32_t>(sizeof(kHello)),
+ nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE));
+}
+
+void CloseScopedHandle(ScopedMessagePipeHandle handle) {
+ // Do nothing and the destructor will close it.
+}
+
+TEST_F(EmbedderTest, AsyncWait) {
+ mojo::embedder::test::InitWithSimplePlatformSupport();
+
+ {
+ ScopedMessagePipeHandle client_mp;
+ ScopedMessagePipeHandle server_mp;
+ EXPECT_EQ(MOJO_RESULT_OK,
+ mojo::CreateMessagePipe(nullptr, &client_mp, &server_mp));
+
+ TestAsyncWaiter waiter;
+ EXPECT_EQ(MOJO_RESULT_OK,
+ AsyncWait(client_mp.get().value(), MOJO_HANDLE_SIGNAL_READABLE,
+ base::Bind(&TestAsyncWaiter::Awake,
+ base::Unretained(&waiter))));
+
+ test_io_thread()->task_runner()->PostTask(
+ FROM_HERE, base::Bind(&WriteHello, server_mp.get().value()));
+ EXPECT_TRUE(waiter.TryWait());
+ EXPECT_EQ(MOJO_RESULT_OK, waiter.wait_result());
+
+ // If message is in the queue, it does't allow us to wait.
+ TestAsyncWaiter waiter_that_doesnt_wait;
+ EXPECT_EQ(
+ MOJO_RESULT_ALREADY_EXISTS,
+ AsyncWait(client_mp.get().value(), MOJO_HANDLE_SIGNAL_READABLE,
+ base::Bind(&TestAsyncWaiter::Awake,
+ base::Unretained(&waiter_that_doesnt_wait))));
+
+ ReadAndIgnore(client_mp.get().value());
+
+ TestAsyncWaiter unsatisfiable_waiter;
+ EXPECT_EQ(MOJO_RESULT_OK,
+ AsyncWait(client_mp.get().value(), MOJO_HANDLE_SIGNAL_READABLE,
+ base::Bind(&TestAsyncWaiter::Awake,
+ base::Unretained(&unsatisfiable_waiter))));
+
+ test_io_thread()->task_runner()->PostTask(
+ FROM_HERE,
+ base::Bind(&CloseScopedHandle, base::Passed(server_mp.Pass())));
+
+ EXPECT_TRUE(unsatisfiable_waiter.TryWait());
+ EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
+ unsatisfiable_waiter.wait_result());
+ }
+
+ EXPECT_TRUE(test::Shutdown());
+}
+
TEST_F(EmbedderTest, ChannelsHandlePassing) {
mojo::embedder::test::InitWithSimplePlatformSupport();

Powered by Google App Engine
This is Rietveld 408576698