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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "mojo/edk/embedder/embedder.h" 5 #include "mojo/edk/embedder/embedder.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
14 #include "base/synchronization/waitable_event.h" 14 #include "base/synchronization/waitable_event.h"
15 #include "base/test/test_io_thread.h" 15 #include "base/test/test_io_thread.h"
16 #include "base/test/test_timeouts.h"
16 #include "mojo/edk/embedder/platform_channel_pair.h" 17 #include "mojo/edk/embedder/platform_channel_pair.h"
17 #include "mojo/edk/embedder/test_embedder.h" 18 #include "mojo/edk/embedder/test_embedder.h"
18 #include "mojo/edk/system/test_utils.h" 19 #include "mojo/edk/system/test_utils.h"
19 #include "mojo/edk/test/multiprocess_test_helper.h" 20 #include "mojo/edk/test/multiprocess_test_helper.h"
20 #include "mojo/public/c/system/core.h" 21 #include "mojo/public/c/system/core.h"
21 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
22 23
23 namespace mojo { 24 namespace mojo {
24 namespace embedder { 25 namespace embedder {
25 namespace { 26 namespace {
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 // the server and client channels were completely created). 154 // the server and client channels were completely created).
154 server_channel.WaitForChannelCreationCompletion(); 155 server_channel.WaitForChannelCreationCompletion();
155 client_channel.WaitForChannelCreationCompletion(); 156 client_channel.WaitForChannelCreationCompletion();
156 EXPECT_TRUE(server_channel.channel_info()); 157 EXPECT_TRUE(server_channel.channel_info());
157 EXPECT_TRUE(client_channel.channel_info()); 158 EXPECT_TRUE(client_channel.channel_info());
158 } 159 }
159 160
160 EXPECT_TRUE(test::Shutdown()); 161 EXPECT_TRUE(test::Shutdown());
161 } 162 }
162 163
164 class TestAsyncWaiter {
165 public:
166 TestAsyncWaiter() : event_(true, false), wait_result_(MOJO_RESULT_UNKNOWN) {}
167
168 void Awake(MojoResult result) {
169 base::AutoLock l(result_lock_);
170 wait_result_ = result;
171 event_.Signal();
172 }
173
174 bool TryWait() { return event_.TimedWait(TestTimeouts::action_timeout()); }
175
176 MojoResult wait_result() const {
177 base::AutoLock l(result_lock_);
178 return wait_result_;
179 }
180
181 private:
182 base::WaitableEvent event_;
183
184 mutable base::Lock result_lock_;
185 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.
186
187 DISALLOW_COPY_AND_ASSIGN(TestAsyncWaiter);
188 };
189
190 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
191 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.
192 uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer));
193 CHECK_EQ(MOJO_RESULT_OK,
194 MojoReadMessage(pipe, buffer, &num_bytes, nullptr, nullptr,
195 MOJO_READ_MESSAGE_FLAG_NONE));
196 }
197
198 void WriteHello(MojoHandle pipe) {
199 const char kHello[] = "hello";
viettrungluu 2014/12/12 21:39:30 nit: static const char
gmorrita 2014/12/12 22:56:27 Done.
200 CHECK_EQ(MOJO_RESULT_OK,
201 MojoWriteMessage(pipe, kHello, static_cast<uint32_t>(sizeof(kHello)),
202 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE));
203 }
204
205 void CloseScopedHandle(ScopedMessagePipeHandle handle) {
206 // Do nothing and the destructor will close it.
207 }
208
209 TEST_F(EmbedderTest, AsyncWait) {
210 mojo::embedder::test::InitWithSimplePlatformSupport();
211
212 {
213 ScopedMessagePipeHandle client_mp;
214 ScopedMessagePipeHandle server_mp;
215 EXPECT_EQ(MOJO_RESULT_OK,
216 mojo::CreateMessagePipe(nullptr, &client_mp, &server_mp));
217
218 TestAsyncWaiter waiter;
219 EXPECT_EQ(MOJO_RESULT_OK,
220 AsyncWait(client_mp.get().value(), MOJO_HANDLE_SIGNAL_READABLE,
221 base::Bind(&TestAsyncWaiter::Awake,
222 base::Unretained(&waiter))));
223
224 test_io_thread()->task_runner()->PostTask(
225 FROM_HERE, base::Bind(&WriteHello, server_mp.get().value()));
226 EXPECT_TRUE(waiter.TryWait());
227 EXPECT_EQ(MOJO_RESULT_OK, waiter.wait_result());
228
229 // If message is in the queue, it does't allow us to wait.
230 TestAsyncWaiter waiter_that_doesnt_wait;
231 EXPECT_EQ(
232 MOJO_RESULT_ALREADY_EXISTS,
233 AsyncWait(client_mp.get().value(), MOJO_HANDLE_SIGNAL_READABLE,
234 base::Bind(&TestAsyncWaiter::Awake,
235 base::Unretained(&waiter_that_doesnt_wait))));
236
237 ReadAndIgnore(client_mp.get().value());
238
239 TestAsyncWaiter unsatisfiable_waiter;
240 EXPECT_EQ(MOJO_RESULT_OK,
241 AsyncWait(client_mp.get().value(), MOJO_HANDLE_SIGNAL_READABLE,
242 base::Bind(&TestAsyncWaiter::Awake,
243 base::Unretained(&unsatisfiable_waiter))));
244
245 test_io_thread()->task_runner()->PostTask(
246 FROM_HERE,
247 base::Bind(&CloseScopedHandle, base::Passed(server_mp.Pass())));
248
249 EXPECT_TRUE(unsatisfiable_waiter.TryWait());
250 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
251 unsatisfiable_waiter.wait_result());
252 }
253
254 EXPECT_TRUE(test::Shutdown());
255 }
256
163 TEST_F(EmbedderTest, ChannelsHandlePassing) { 257 TEST_F(EmbedderTest, ChannelsHandlePassing) {
164 mojo::embedder::test::InitWithSimplePlatformSupport(); 258 mojo::embedder::test::InitWithSimplePlatformSupport();
165 259
166 { 260 {
167 PlatformChannelPair channel_pair; 261 PlatformChannelPair channel_pair;
168 ScopedTestChannel server_channel(test_io_thread()->task_runner(), 262 ScopedTestChannel server_channel(test_io_thread()->task_runner(),
169 channel_pair.PassServerHandle()); 263 channel_pair.PassServerHandle());
170 MojoHandle server_mp = server_channel.bootstrap_message_pipe(); 264 MojoHandle server_mp = server_channel.bootstrap_message_pipe();
171 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID); 265 EXPECT_NE(server_mp, MOJO_HANDLE_INVALID);
172 ScopedTestChannel client_channel(test_io_thread()->task_runner(), 266 ScopedTestChannel client_channel(test_io_thread()->task_runner(),
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after
545 639
546 EXPECT_TRUE(test::Shutdown()); 640 EXPECT_TRUE(test::Shutdown());
547 } 641 }
548 642
549 // TODO(vtl): Test immediate write & close. 643 // TODO(vtl): Test immediate write & close.
550 // TODO(vtl): Test broken-connection cases. 644 // TODO(vtl): Test broken-connection cases.
551 645
552 } // namespace 646 } // namespace
553 } // namespace embedder 647 } // namespace embedder
554 } // namespace mojo 648 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698