| OLD | NEW |
| 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 19 matching lines...) Expand all Loading... |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 TEST(MessagePipeDispatcherTest, Basic) { | 32 TEST(MessagePipeDispatcherTest, Basic) { |
| 33 test::Stopwatch stopwatch; | 33 test::Stopwatch stopwatch; |
| 34 int32_t buffer[1]; | 34 int32_t buffer[1]; |
| 35 const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer)); | 35 const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer)); |
| 36 uint32_t buffer_size; | 36 uint32_t buffer_size; |
| 37 | 37 |
| 38 // Run this test both with |d0| as port 0, |d1| as port 1 and vice versa. | 38 // Run this test both with |d0| as port 0, |d1| as port 1 and vice versa. |
| 39 for (unsigned i = 0; i < 2; i++) { | 39 for (unsigned i = 0; i < 2; i++) { |
| 40 scoped_refptr<MessagePipeDispatcher> d0(new MessagePipeDispatcher()); | 40 scoped_refptr<MessagePipeDispatcher> d0(new MessagePipeDispatcher( |
| 41 MessagePipeDispatcher::kDefaultCreateOptions)); |
| 41 EXPECT_EQ(Dispatcher::kTypeMessagePipe, d0->GetType()); | 42 EXPECT_EQ(Dispatcher::kTypeMessagePipe, d0->GetType()); |
| 42 scoped_refptr<MessagePipeDispatcher> d1(new MessagePipeDispatcher()); | 43 scoped_refptr<MessagePipeDispatcher> d1(new MessagePipeDispatcher( |
| 44 MessagePipeDispatcher::kDefaultCreateOptions)); |
| 43 { | 45 { |
| 44 scoped_refptr<MessagePipe> mp(new MessagePipe()); | 46 scoped_refptr<MessagePipe> mp(new MessagePipe()); |
| 45 d0->Init(mp, i); // 0, 1. | 47 d0->Init(mp, i); // 0, 1. |
| 46 d1->Init(mp, i ^ 1); // 1, 0. | 48 d1->Init(mp, i ^ 1); // 1, 0. |
| 47 } | 49 } |
| 48 Waiter w; | 50 Waiter w; |
| 49 | 51 |
| 50 // Try adding a writable waiter when already writable. | 52 // Try adding a writable waiter when already writable. |
| 51 w.Init(); | 53 w.Init(); |
| 52 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, | 54 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 d0->RemoveWaiter(&w); | 108 d0->RemoveWaiter(&w); |
| 107 | 109 |
| 108 EXPECT_EQ(MOJO_RESULT_OK, d0->Close()); | 110 EXPECT_EQ(MOJO_RESULT_OK, d0->Close()); |
| 109 EXPECT_EQ(MOJO_RESULT_OK, d1->Close()); | 111 EXPECT_EQ(MOJO_RESULT_OK, d1->Close()); |
| 110 } | 112 } |
| 111 } | 113 } |
| 112 | 114 |
| 113 TEST(MessagePipeDispatcherTest, InvalidParams) { | 115 TEST(MessagePipeDispatcherTest, InvalidParams) { |
| 114 char buffer[1]; | 116 char buffer[1]; |
| 115 | 117 |
| 116 scoped_refptr<MessagePipeDispatcher> d0(new MessagePipeDispatcher()); | 118 scoped_refptr<MessagePipeDispatcher> d0(new MessagePipeDispatcher( |
| 117 scoped_refptr<MessagePipeDispatcher> d1(new MessagePipeDispatcher()); | 119 MessagePipeDispatcher::kDefaultCreateOptions)); |
| 120 scoped_refptr<MessagePipeDispatcher> d1(new MessagePipeDispatcher( |
| 121 MessagePipeDispatcher::kDefaultCreateOptions)); |
| 118 { | 122 { |
| 119 scoped_refptr<MessagePipe> mp(new MessagePipe()); | 123 scoped_refptr<MessagePipe> mp(new MessagePipe()); |
| 120 d0->Init(mp, 0); | 124 d0->Init(mp, 0); |
| 121 d1->Init(mp, 1); | 125 d1->Init(mp, 1); |
| 122 } | 126 } |
| 123 | 127 |
| 124 // |WriteMessage|: | 128 // |WriteMessage|: |
| 125 // Null buffer with nonzero buffer size. | 129 // Null buffer with nonzero buffer size. |
| 126 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, | 130 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, |
| 127 d0->WriteMessage(NULL, 1, | 131 d0->WriteMessage(NULL, 1, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 146 } | 150 } |
| 147 | 151 |
| 148 // Test what happens when one end is closed (single-threaded test). | 152 // Test what happens when one end is closed (single-threaded test). |
| 149 TEST(MessagePipeDispatcherTest, BasicClosed) { | 153 TEST(MessagePipeDispatcherTest, BasicClosed) { |
| 150 int32_t buffer[1]; | 154 int32_t buffer[1]; |
| 151 const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer)); | 155 const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer)); |
| 152 uint32_t buffer_size; | 156 uint32_t buffer_size; |
| 153 | 157 |
| 154 // Run this test both with |d0| as port 0, |d1| as port 1 and vice versa. | 158 // Run this test both with |d0| as port 0, |d1| as port 1 and vice versa. |
| 155 for (unsigned i = 0; i < 2; i++) { | 159 for (unsigned i = 0; i < 2; i++) { |
| 156 scoped_refptr<MessagePipeDispatcher> d0(new MessagePipeDispatcher()); | 160 scoped_refptr<MessagePipeDispatcher> d0(new MessagePipeDispatcher( |
| 157 scoped_refptr<MessagePipeDispatcher> d1(new MessagePipeDispatcher()); | 161 MessagePipeDispatcher::kDefaultCreateOptions)); |
| 162 scoped_refptr<MessagePipeDispatcher> d1(new MessagePipeDispatcher( |
| 163 MessagePipeDispatcher::kDefaultCreateOptions)); |
| 158 { | 164 { |
| 159 scoped_refptr<MessagePipe> mp(new MessagePipe()); | 165 scoped_refptr<MessagePipe> mp(new MessagePipe()); |
| 160 d0->Init(mp, i); // 0, 1. | 166 d0->Init(mp, i); // 0, 1. |
| 161 d1->Init(mp, i ^ 1); // 1, 0. | 167 d1->Init(mp, i ^ 1); // 1, 0. |
| 162 } | 168 } |
| 163 Waiter w; | 169 Waiter w; |
| 164 | 170 |
| 165 // Write (twice) to |d1|. | 171 // Write (twice) to |d1|. |
| 166 buffer[0] = 123456789; | 172 buffer[0] = 123456789; |
| 167 EXPECT_EQ(MOJO_RESULT_OK, | 173 EXPECT_EQ(MOJO_RESULT_OK, |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 test::Stopwatch stopwatch; | 260 test::Stopwatch stopwatch; |
| 255 int32_t buffer[1]; | 261 int32_t buffer[1]; |
| 256 const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer)); | 262 const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer)); |
| 257 uint32_t buffer_size; | 263 uint32_t buffer_size; |
| 258 base::TimeDelta elapsed; | 264 base::TimeDelta elapsed; |
| 259 bool did_wait; | 265 bool did_wait; |
| 260 MojoResult result; | 266 MojoResult result; |
| 261 | 267 |
| 262 // Run this test both with |d0| as port 0, |d1| as port 1 and vice versa. | 268 // Run this test both with |d0| as port 0, |d1| as port 1 and vice versa. |
| 263 for (unsigned i = 0; i < 2; i++) { | 269 for (unsigned i = 0; i < 2; i++) { |
| 264 scoped_refptr<MessagePipeDispatcher> d0(new MessagePipeDispatcher()); | 270 scoped_refptr<MessagePipeDispatcher> d0(new MessagePipeDispatcher( |
| 265 scoped_refptr<MessagePipeDispatcher> d1(new MessagePipeDispatcher()); | 271 MessagePipeDispatcher::kDefaultCreateOptions)); |
| 272 scoped_refptr<MessagePipeDispatcher> d1(new MessagePipeDispatcher( |
| 273 MessagePipeDispatcher::kDefaultCreateOptions)); |
| 266 { | 274 { |
| 267 scoped_refptr<MessagePipe> mp(new MessagePipe()); | 275 scoped_refptr<MessagePipe> mp(new MessagePipe()); |
| 268 d0->Init(mp, i); // 0, 1. | 276 d0->Init(mp, i); // 0, 1. |
| 269 d1->Init(mp, i ^ 1); // 1, 0. | 277 d1->Init(mp, i ^ 1); // 1, 0. |
| 270 } | 278 } |
| 271 | 279 |
| 272 // Wait for readable on |d1|, which will become readable after some time. | 280 // Wait for readable on |d1|, which will become readable after some time. |
| 273 { | 281 { |
| 274 test::WaiterThread thread(d1, | 282 test::WaiterThread thread(d1, |
| 275 MOJO_WAIT_FLAG_READABLE, | 283 MOJO_WAIT_FLAG_READABLE, |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 elapsed = stopwatch.Elapsed(); | 340 elapsed = stopwatch.Elapsed(); |
| 333 EXPECT_GT(elapsed, (2-1) * test::EpsilonTimeout()); | 341 EXPECT_GT(elapsed, (2-1) * test::EpsilonTimeout()); |
| 334 EXPECT_LT(elapsed, (2+1) * test::EpsilonTimeout()); | 342 EXPECT_LT(elapsed, (2+1) * test::EpsilonTimeout()); |
| 335 EXPECT_TRUE(did_wait); | 343 EXPECT_TRUE(did_wait); |
| 336 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); | 344 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); |
| 337 | 345 |
| 338 EXPECT_EQ(MOJO_RESULT_OK, d1->Close()); | 346 EXPECT_EQ(MOJO_RESULT_OK, d1->Close()); |
| 339 } | 347 } |
| 340 | 348 |
| 341 for (unsigned i = 0; i < 2; i++) { | 349 for (unsigned i = 0; i < 2; i++) { |
| 342 scoped_refptr<MessagePipeDispatcher> d0(new MessagePipeDispatcher()); | 350 scoped_refptr<MessagePipeDispatcher> d0(new MessagePipeDispatcher( |
| 343 scoped_refptr<MessagePipeDispatcher> d1(new MessagePipeDispatcher()); | 351 MessagePipeDispatcher::kDefaultCreateOptions)); |
| 352 scoped_refptr<MessagePipeDispatcher> d1(new MessagePipeDispatcher( |
| 353 MessagePipeDispatcher::kDefaultCreateOptions)); |
| 344 { | 354 { |
| 345 scoped_refptr<MessagePipe> mp(new MessagePipe()); | 355 scoped_refptr<MessagePipe> mp(new MessagePipe()); |
| 346 d0->Init(mp, i); // 0, 1. | 356 d0->Init(mp, i); // 0, 1. |
| 347 d1->Init(mp, i ^ 1); // 1, 0. | 357 d1->Init(mp, i ^ 1); // 1, 0. |
| 348 } | 358 } |
| 349 | 359 |
| 350 // Wait for readable on |d1| and close |d1| after some time, which should | 360 // Wait for readable on |d1| and close |d1| after some time, which should |
| 351 // cancel that wait. | 361 // cancel that wait. |
| 352 { | 362 { |
| 353 test::WaiterThread thread(d1, | 363 test::WaiterThread thread(d1, |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 size_t* const messages_read_; | 516 size_t* const messages_read_; |
| 507 size_t* const bytes_read_; | 517 size_t* const bytes_read_; |
| 508 | 518 |
| 509 DISALLOW_COPY_AND_ASSIGN(ReaderThread); | 519 DISALLOW_COPY_AND_ASSIGN(ReaderThread); |
| 510 }; | 520 }; |
| 511 | 521 |
| 512 TEST(MessagePipeDispatcherTest, Stress) { | 522 TEST(MessagePipeDispatcherTest, Stress) { |
| 513 static const size_t kNumWriters = 30; | 523 static const size_t kNumWriters = 30; |
| 514 static const size_t kNumReaders = kNumWriters; | 524 static const size_t kNumReaders = kNumWriters; |
| 515 | 525 |
| 516 scoped_refptr<MessagePipeDispatcher> d_write(new MessagePipeDispatcher()); | 526 scoped_refptr<MessagePipeDispatcher> d_write(new MessagePipeDispatcher( |
| 517 scoped_refptr<MessagePipeDispatcher> d_read(new MessagePipeDispatcher()); | 527 MessagePipeDispatcher::kDefaultCreateOptions)); |
| 528 scoped_refptr<MessagePipeDispatcher> d_read(new MessagePipeDispatcher( |
| 529 MessagePipeDispatcher::kDefaultCreateOptions)); |
| 518 { | 530 { |
| 519 scoped_refptr<MessagePipe> mp(new MessagePipe()); | 531 scoped_refptr<MessagePipe> mp(new MessagePipe()); |
| 520 d_write->Init(mp, 0); | 532 d_write->Init(mp, 0); |
| 521 d_read->Init(mp, 1); | 533 d_read->Init(mp, 1); |
| 522 } | 534 } |
| 523 | 535 |
| 524 size_t messages_written[kNumWriters]; | 536 size_t messages_written[kNumWriters]; |
| 525 size_t bytes_written[kNumWriters]; | 537 size_t bytes_written[kNumWriters]; |
| 526 size_t messages_read[kNumReaders]; | 538 size_t messages_read[kNumReaders]; |
| 527 size_t bytes_read[kNumReaders]; | 539 size_t bytes_read[kNumReaders]; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 571 EXPECT_EQ(total_messages_written, total_messages_read); | 583 EXPECT_EQ(total_messages_written, total_messages_read); |
| 572 EXPECT_EQ(total_bytes_written, total_bytes_read); | 584 EXPECT_EQ(total_bytes_written, total_bytes_read); |
| 573 | 585 |
| 574 EXPECT_EQ(MOJO_RESULT_OK, d_write->Close()); | 586 EXPECT_EQ(MOJO_RESULT_OK, d_write->Close()); |
| 575 EXPECT_EQ(MOJO_RESULT_OK, d_read->Close()); | 587 EXPECT_EQ(MOJO_RESULT_OK, d_read->Close()); |
| 576 } | 588 } |
| 577 | 589 |
| 578 } // namespace | 590 } // namespace |
| 579 } // namespace system | 591 } // namespace system |
| 580 } // namespace mojo | 592 } // namespace mojo |
| OLD | NEW |