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

Side by Side Diff: mojo/system/message_pipe_dispatcher_unittest.cc

Issue 419973005: Convert WriteMessage...() to use the new user pointer handling (see r285350). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « mojo/system/message_pipe_dispatcher.cc ('k') | mojo/system/message_pipe_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 // NOTE(vtl): Some of these tests are inherently flaky (e.g., if run on a 5 // NOTE(vtl): Some of these tests are inherently flaky (e.g., if run on a
6 // heavily-loaded system). Sorry. |test::EpsilonTimeout()| may be increased to 6 // heavily-loaded system). Sorry. |test::EpsilonTimeout()| may be increased to
7 // increase tolerance and reduce observed flakiness (though doing so reduces the 7 // increase tolerance and reduce observed flakiness (though doing so reduces the
8 // meaningfulness of the test). 8 // meaningfulness of the test).
9 9
10 #include "mojo/system/message_pipe_dispatcher.h" 10 #include "mojo/system/message_pipe_dispatcher.h"
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 d0->AddWaiter(&w, MOJO_HANDLE_SIGNAL_WRITABLE, 0)); 56 d0->AddWaiter(&w, MOJO_HANDLE_SIGNAL_WRITABLE, 0));
57 // Shouldn't need to remove the waiter (it was not added). 57 // Shouldn't need to remove the waiter (it was not added).
58 58
59 // Add a readable waiter to |d0|, then make it readable (by writing to 59 // Add a readable waiter to |d0|, then make it readable (by writing to
60 // |d1|), then wait. 60 // |d1|), then wait.
61 w.Init(); 61 w.Init();
62 ASSERT_EQ(MOJO_RESULT_OK, 62 ASSERT_EQ(MOJO_RESULT_OK,
63 d0->AddWaiter(&w, MOJO_HANDLE_SIGNAL_READABLE, 1)); 63 d0->AddWaiter(&w, MOJO_HANDLE_SIGNAL_READABLE, 1));
64 buffer[0] = 123456789; 64 buffer[0] = 123456789;
65 EXPECT_EQ(MOJO_RESULT_OK, 65 EXPECT_EQ(MOJO_RESULT_OK,
66 d1->WriteMessage(buffer, kBufferSize, 66 d1->WriteMessage(UserPointer<const void>(buffer), kBufferSize,
67 NULL, 67 NULL, MOJO_WRITE_MESSAGE_FLAG_NONE));
68 MOJO_WRITE_MESSAGE_FLAG_NONE));
69 stopwatch.Start(); 68 stopwatch.Start();
70 EXPECT_EQ(MOJO_RESULT_OK, w.Wait(MOJO_DEADLINE_INDEFINITE, &context)); 69 EXPECT_EQ(MOJO_RESULT_OK, w.Wait(MOJO_DEADLINE_INDEFINITE, &context));
71 EXPECT_EQ(1u, context); 70 EXPECT_EQ(1u, context);
72 EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout()); 71 EXPECT_LT(stopwatch.Elapsed(), test::EpsilonTimeout());
73 d0->RemoveWaiter(&w); 72 d0->RemoveWaiter(&w);
74 73
75 // Try adding a readable waiter when already readable (from above). 74 // Try adding a readable waiter when already readable (from above).
76 w.Init(); 75 w.Init();
77 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 76 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
78 d0->AddWaiter(&w, MOJO_HANDLE_SIGNAL_READABLE, 2)); 77 d0->AddWaiter(&w, MOJO_HANDLE_SIGNAL_READABLE, 2));
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 MessagePipeDispatcher::kDefaultCreateOptions)); 120 MessagePipeDispatcher::kDefaultCreateOptions));
122 scoped_refptr<MessagePipeDispatcher> d1(new MessagePipeDispatcher( 121 scoped_refptr<MessagePipeDispatcher> d1(new MessagePipeDispatcher(
123 MessagePipeDispatcher::kDefaultCreateOptions)); 122 MessagePipeDispatcher::kDefaultCreateOptions));
124 { 123 {
125 scoped_refptr<MessagePipe> mp(new MessagePipe()); 124 scoped_refptr<MessagePipe> mp(new MessagePipe());
126 d0->Init(mp, 0); 125 d0->Init(mp, 0);
127 d1->Init(mp, 1); 126 d1->Init(mp, 1);
128 } 127 }
129 128
130 // |WriteMessage|: 129 // |WriteMessage|:
130 // Huge buffer size.
131 EXPECT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED,
132 d0->WriteMessage(UserPointer<const void>(buffer),
133 std::numeric_limits<uint32_t>::max(), NULL,
134 MOJO_WRITE_MESSAGE_FLAG_NONE));
135
136 EXPECT_EQ(MOJO_RESULT_OK, d0->Close());
137 EXPECT_EQ(MOJO_RESULT_OK, d1->Close());
138 }
139
140 // These test invalid arguments that should cause death if we're being paranoid
141 // about checking arguments (which we would want to do if, e.g., we were in a
142 // true "kernel" situation, but we might not want to do otherwise for
143 // performance reasons). Probably blatant errors like passing in null pointers
144 // (for required pointer arguments) will still cause death, but perhaps not
145 // predictably.
146 TEST(MessagePipeDispatcherTest, InvalidParamsDeath) {
147 const char kMemoryCheckFailedRegex[] = "Check failed";
148
149 scoped_refptr<MessagePipeDispatcher> d0(new MessagePipeDispatcher(
150 MessagePipeDispatcher::kDefaultCreateOptions));
151 scoped_refptr<MessagePipeDispatcher> d1(new MessagePipeDispatcher(
152 MessagePipeDispatcher::kDefaultCreateOptions));
153 {
154 scoped_refptr<MessagePipe> mp(new MessagePipe());
155 d0->Init(mp, 0);
156 d1->Init(mp, 1);
157 }
158
159 // |WriteMessage|:
131 // Null buffer with nonzero buffer size. 160 // Null buffer with nonzero buffer size.
132 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, 161 EXPECT_DEATH_IF_SUPPORTED(
133 d0->WriteMessage(NULL, 1, 162 d0->WriteMessage(NullUserPointer(), 1, NULL,
134 NULL, 163 MOJO_WRITE_MESSAGE_FLAG_NONE),
164 kMemoryCheckFailedRegex);
165
166 // |ReadMessage|:
167 // Null buffer with nonzero buffer size.
168 // First write something so that we actually have something to read.
169 EXPECT_EQ(MOJO_RESULT_OK,
170 d1->WriteMessage(UserPointer<const void>("x"), 1, NULL,
135 MOJO_WRITE_MESSAGE_FLAG_NONE)); 171 MOJO_WRITE_MESSAGE_FLAG_NONE));
136 // Huge buffer size. 172 uint32_t buffer_size = 1;
137 EXPECT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED, 173 EXPECT_DEATH_IF_SUPPORTED(
138 d0->WriteMessage(buffer, std::numeric_limits<uint32_t>::max(), 174 d0->ReadMessage(NullUserPointer(), MakeUserPointer(&buffer_size), 0, NULL,
139 NULL, 175 MOJO_READ_MESSAGE_FLAG_NONE),
140 MOJO_WRITE_MESSAGE_FLAG_NONE)); 176 kMemoryCheckFailedRegex);
141 177
142 EXPECT_EQ(MOJO_RESULT_OK, d0->Close()); 178 EXPECT_EQ(MOJO_RESULT_OK, d0->Close());
143 EXPECT_EQ(MOJO_RESULT_OK, d1->Close()); 179 EXPECT_EQ(MOJO_RESULT_OK, d1->Close());
144 } 180 }
145 181
146 // Test what happens when one end is closed (single-threaded test). 182 // Test what happens when one end is closed (single-threaded test).
147 TEST(MessagePipeDispatcherTest, BasicClosed) { 183 TEST(MessagePipeDispatcherTest, BasicClosed) {
148 int32_t buffer[1]; 184 int32_t buffer[1];
149 const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer)); 185 const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer));
150 uint32_t buffer_size; 186 uint32_t buffer_size;
151 187
152 // Run this test both with |d0| as port 0, |d1| as port 1 and vice versa. 188 // Run this test both with |d0| as port 0, |d1| as port 1 and vice versa.
153 for (unsigned i = 0; i < 2; i++) { 189 for (unsigned i = 0; i < 2; i++) {
154 scoped_refptr<MessagePipeDispatcher> d0(new MessagePipeDispatcher( 190 scoped_refptr<MessagePipeDispatcher> d0(new MessagePipeDispatcher(
155 MessagePipeDispatcher::kDefaultCreateOptions)); 191 MessagePipeDispatcher::kDefaultCreateOptions));
156 scoped_refptr<MessagePipeDispatcher> d1(new MessagePipeDispatcher( 192 scoped_refptr<MessagePipeDispatcher> d1(new MessagePipeDispatcher(
157 MessagePipeDispatcher::kDefaultCreateOptions)); 193 MessagePipeDispatcher::kDefaultCreateOptions));
158 { 194 {
159 scoped_refptr<MessagePipe> mp(new MessagePipe()); 195 scoped_refptr<MessagePipe> mp(new MessagePipe());
160 d0->Init(mp, i); // 0, 1. 196 d0->Init(mp, i); // 0, 1.
161 d1->Init(mp, i ^ 1); // 1, 0. 197 d1->Init(mp, i ^ 1); // 1, 0.
162 } 198 }
163 Waiter w; 199 Waiter w;
164 200
165 // Write (twice) to |d1|. 201 // Write (twice) to |d1|.
166 buffer[0] = 123456789; 202 buffer[0] = 123456789;
167 EXPECT_EQ(MOJO_RESULT_OK, 203 EXPECT_EQ(MOJO_RESULT_OK,
168 d1->WriteMessage(buffer, kBufferSize, 204 d1->WriteMessage(UserPointer<const void>(buffer), kBufferSize,
169 NULL, 205 NULL, MOJO_WRITE_MESSAGE_FLAG_NONE));
170 MOJO_WRITE_MESSAGE_FLAG_NONE));
171 buffer[0] = 234567890; 206 buffer[0] = 234567890;
172 EXPECT_EQ(MOJO_RESULT_OK, 207 EXPECT_EQ(MOJO_RESULT_OK,
173 d1->WriteMessage(buffer, kBufferSize, 208 d1->WriteMessage(UserPointer<const void>(buffer), kBufferSize,
174 NULL, 209 NULL, MOJO_WRITE_MESSAGE_FLAG_NONE));
175 MOJO_WRITE_MESSAGE_FLAG_NONE));
176 210
177 // Try waiting for readable on |d0|; should fail (already satisfied). 211 // Try waiting for readable on |d0|; should fail (already satisfied).
178 w.Init(); 212 w.Init();
179 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, 213 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS,
180 d0->AddWaiter(&w, MOJO_HANDLE_SIGNAL_READABLE, 0)); 214 d0->AddWaiter(&w, MOJO_HANDLE_SIGNAL_READABLE, 0));
181 215
182 // Try reading from |d1|; should fail (nothing to read). 216 // Try reading from |d1|; should fail (nothing to read).
183 buffer[0] = 0; 217 buffer[0] = 0;
184 buffer_size = kBufferSize; 218 buffer_size = kBufferSize;
185 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT, 219 EXPECT_EQ(MOJO_RESULT_SHOULD_WAIT,
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 buffer[0] = 0; 269 buffer[0] = 0;
236 buffer_size = kBufferSize; 270 buffer_size = kBufferSize;
237 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 271 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
238 d0->ReadMessage(UserPointer<void>(buffer), 272 d0->ReadMessage(UserPointer<void>(buffer),
239 MakeUserPointer(&buffer_size), 0, NULL, 273 MakeUserPointer(&buffer_size), 0, NULL,
240 MOJO_READ_MESSAGE_FLAG_NONE)); 274 MOJO_READ_MESSAGE_FLAG_NONE));
241 275
242 // Try writing to |d0|; should fail (other end closed). 276 // Try writing to |d0|; should fail (other end closed).
243 buffer[0] = 345678901; 277 buffer[0] = 345678901;
244 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, 278 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
245 d0->WriteMessage(buffer, kBufferSize, 279 d0->WriteMessage(UserPointer<const void>(buffer), kBufferSize,
246 NULL, 280 NULL, MOJO_WRITE_MESSAGE_FLAG_NONE));
247 MOJO_WRITE_MESSAGE_FLAG_NONE));
248 281
249 EXPECT_EQ(MOJO_RESULT_OK, d0->Close()); 282 EXPECT_EQ(MOJO_RESULT_OK, d0->Close());
250 } 283 }
251 } 284 }
252 285
253 #if defined(OS_WIN) 286 #if defined(OS_WIN)
254 // http://crbug.com/396386 287 // http://crbug.com/396386
255 #define MAYBE_BasicThreaded DISABLED_BasicThreaded 288 #define MAYBE_BasicThreaded DISABLED_BasicThreaded
256 #else 289 #else
257 #define MAYBE_BasicThreaded BasicThreaded 290 #define MAYBE_BasicThreaded BasicThreaded
(...skipping 26 matching lines...) Expand all
284 MOJO_HANDLE_SIGNAL_READABLE, 317 MOJO_HANDLE_SIGNAL_READABLE,
285 MOJO_DEADLINE_INDEFINITE, 318 MOJO_DEADLINE_INDEFINITE,
286 1, 319 1,
287 &did_wait, &result, &context); 320 &did_wait, &result, &context);
288 stopwatch.Start(); 321 stopwatch.Start();
289 thread.Start(); 322 thread.Start();
290 base::PlatformThread::Sleep(2 * test::EpsilonTimeout()); 323 base::PlatformThread::Sleep(2 * test::EpsilonTimeout());
291 // Wake it up by writing to |d0|. 324 // Wake it up by writing to |d0|.
292 buffer[0] = 123456789; 325 buffer[0] = 123456789;
293 EXPECT_EQ(MOJO_RESULT_OK, 326 EXPECT_EQ(MOJO_RESULT_OK,
294 d0->WriteMessage(buffer, kBufferSize, 327 d0->WriteMessage(UserPointer<const void>(buffer), kBufferSize,
295 NULL, 328 NULL, MOJO_WRITE_MESSAGE_FLAG_NONE));
296 MOJO_WRITE_MESSAGE_FLAG_NONE));
297 } // Joins the thread. 329 } // Joins the thread.
298 elapsed = stopwatch.Elapsed(); 330 elapsed = stopwatch.Elapsed();
299 EXPECT_GT(elapsed, (2-1) * test::EpsilonTimeout()); 331 EXPECT_GT(elapsed, (2-1) * test::EpsilonTimeout());
300 EXPECT_LT(elapsed, (2+1) * test::EpsilonTimeout()); 332 EXPECT_LT(elapsed, (2+1) * test::EpsilonTimeout());
301 EXPECT_TRUE(did_wait); 333 EXPECT_TRUE(did_wait);
302 EXPECT_EQ(MOJO_RESULT_OK, result); 334 EXPECT_EQ(MOJO_RESULT_OK, result);
303 EXPECT_EQ(1u, context); 335 EXPECT_EQ(1u, context);
304 336
305 // Now |d1| is already readable. Try waiting for it again. 337 // Now |d1| is already readable. Try waiting for it again.
306 { 338 {
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
414 buffer[i] = static_cast<unsigned char>(i); 446 buffer[i] = static_cast<unsigned char>(i);
415 447
416 // Number of messages to write. 448 // Number of messages to write.
417 *messages_written_ = static_cast<size_t>(base::RandInt(1000, 6000)); 449 *messages_written_ = static_cast<size_t>(base::RandInt(1000, 6000));
418 450
419 // Write messages. 451 // Write messages.
420 for (size_t i = 0; i < *messages_written_; i++) { 452 for (size_t i = 0; i < *messages_written_; i++) {
421 uint32_t bytes_to_write = static_cast<uint32_t>( 453 uint32_t bytes_to_write = static_cast<uint32_t>(
422 base::RandInt(1, static_cast<int>(kMaxMessageSize))); 454 base::RandInt(1, static_cast<int>(kMaxMessageSize)));
423 EXPECT_EQ(MOJO_RESULT_OK, 455 EXPECT_EQ(MOJO_RESULT_OK,
424 write_dispatcher_->WriteMessage(buffer, bytes_to_write, 456 write_dispatcher_->WriteMessage(UserPointer<const void>(buffer),
425 NULL, 457 bytes_to_write, NULL,
426 MOJO_WRITE_MESSAGE_FLAG_NONE)); 458 MOJO_WRITE_MESSAGE_FLAG_NONE));
427 *bytes_written_ += bytes_to_write; 459 *bytes_written_ += bytes_to_write;
428 } 460 }
429 461
430 // Write one last "quit" message. 462 // Write one last "quit" message.
431 EXPECT_EQ(MOJO_RESULT_OK, 463 EXPECT_EQ(MOJO_RESULT_OK,
432 write_dispatcher_->WriteMessage("quit", 4, 464 write_dispatcher_->WriteMessage(UserPointer<const void>("quit"),
433 NULL, 465 4, NULL,
434 MOJO_WRITE_MESSAGE_FLAG_NONE)); 466 MOJO_WRITE_MESSAGE_FLAG_NONE));
435 } 467 }
436 468
437 const scoped_refptr<Dispatcher> write_dispatcher_; 469 const scoped_refptr<Dispatcher> write_dispatcher_;
438 size_t* const messages_written_; 470 size_t* const messages_written_;
439 size_t* const bytes_written_; 471 size_t* const bytes_written_;
440 472
441 DISALLOW_COPY_AND_ASSIGN(WriterThread); 473 DISALLOW_COPY_AND_ASSIGN(WriterThread);
442 }; 474 };
443 475
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
587 EXPECT_EQ(total_messages_written, total_messages_read); 619 EXPECT_EQ(total_messages_written, total_messages_read);
588 EXPECT_EQ(total_bytes_written, total_bytes_read); 620 EXPECT_EQ(total_bytes_written, total_bytes_read);
589 621
590 EXPECT_EQ(MOJO_RESULT_OK, d_write->Close()); 622 EXPECT_EQ(MOJO_RESULT_OK, d_write->Close());
591 EXPECT_EQ(MOJO_RESULT_OK, d_read->Close()); 623 EXPECT_EQ(MOJO_RESULT_OK, d_read->Close());
592 } 624 }
593 625
594 } // namespace 626 } // namespace
595 } // namespace system 627 } // namespace system
596 } // namespace mojo 628 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/system/message_pipe_dispatcher.cc ('k') | mojo/system/message_pipe_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698