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::EpsilonDeadline()| may be increased to | 6 // heavily-loaded system). Sorry. |test::EpsilonDeadline()| 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/edk/system/message_pipe_dispatcher.h" | 10 #include "mojo/edk/system/message_pipe_dispatcher.h" |
11 | 11 |
12 #include <string.h> | 12 #include <string.h> |
13 | 13 |
14 #include <limits> | 14 #include <limits> |
| 15 #include <utility> |
15 | 16 |
16 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
17 #include "base/memory/scoped_vector.h" | 18 #include "base/memory/scoped_vector.h" |
18 #include "mojo/edk/system/message_pipe.h" | 19 #include "mojo/edk/system/message_pipe.h" |
19 #include "mojo/edk/system/test_utils.h" | 20 #include "mojo/edk/system/test_utils.h" |
20 #include "mojo/edk/system/waiter.h" | 21 #include "mojo/edk/system/waiter.h" |
21 #include "mojo/edk/system/waiter_test_utils.h" | 22 #include "mojo/edk/system/waiter_test_utils.h" |
22 #include "mojo/edk/test/simple_test_thread.h" | 23 #include "mojo/edk/test/simple_test_thread.h" |
23 #include "mojo/public/cpp/system/macros.h" | 24 #include "mojo/public/cpp/system/macros.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 13 matching lines...) Expand all Loading... |
38 uint32_t buffer_size; | 39 uint32_t buffer_size; |
39 | 40 |
40 // Run this test both with |d0| as port 0, |d1| as port 1 and vice versa. | 41 // Run this test both with |d0| as port 0, |d1| as port 1 and vice versa. |
41 for (unsigned i = 0; i < 2; i++) { | 42 for (unsigned i = 0; i < 2; i++) { |
42 scoped_refptr<MessagePipeDispatcher> d0 = MessagePipeDispatcher::Create( | 43 scoped_refptr<MessagePipeDispatcher> d0 = MessagePipeDispatcher::Create( |
43 MessagePipeDispatcher::kDefaultCreateOptions); | 44 MessagePipeDispatcher::kDefaultCreateOptions); |
44 EXPECT_EQ(Dispatcher::Type::MESSAGE_PIPE, d0->GetType()); | 45 EXPECT_EQ(Dispatcher::Type::MESSAGE_PIPE, d0->GetType()); |
45 scoped_refptr<MessagePipeDispatcher> d1 = MessagePipeDispatcher::Create( | 46 scoped_refptr<MessagePipeDispatcher> d1 = MessagePipeDispatcher::Create( |
46 MessagePipeDispatcher::kDefaultCreateOptions); | 47 MessagePipeDispatcher::kDefaultCreateOptions); |
47 { | 48 { |
48 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalLocal()); | 49 auto mp = MessagePipe::CreateLocalLocal(); |
49 d0->Init(mp, i); // 0, 1. | 50 d0->Init(mp.Clone(), i); // 0, 1. |
50 d1->Init(mp, i ^ 1); // 1, 0. | 51 d1->Init(std::move(mp), i ^ 1); // 1, 0. |
51 } | 52 } |
52 Waiter w; | 53 Waiter w; |
53 uint32_t context = 0; | 54 uint32_t context = 0; |
54 HandleSignalsState hss; | 55 HandleSignalsState hss; |
55 | 56 |
56 // Try adding a writable waiter when already writable. | 57 // Try adding a writable waiter when already writable. |
57 w.Init(); | 58 w.Init(); |
58 hss = HandleSignalsState(); | 59 hss = HandleSignalsState(); |
59 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, | 60 EXPECT_EQ(MOJO_RESULT_ALREADY_EXISTS, |
60 d0->AddAwakable(&w, MOJO_HANDLE_SIGNAL_WRITABLE, 0, &hss)); | 61 d0->AddAwakable(&w, MOJO_HANDLE_SIGNAL_WRITABLE, 0, &hss)); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 } | 150 } |
150 | 151 |
151 TEST(MessagePipeDispatcherTest, InvalidParams) { | 152 TEST(MessagePipeDispatcherTest, InvalidParams) { |
152 char buffer[1]; | 153 char buffer[1]; |
153 | 154 |
154 scoped_refptr<MessagePipeDispatcher> d0 = MessagePipeDispatcher::Create( | 155 scoped_refptr<MessagePipeDispatcher> d0 = MessagePipeDispatcher::Create( |
155 MessagePipeDispatcher::kDefaultCreateOptions); | 156 MessagePipeDispatcher::kDefaultCreateOptions); |
156 scoped_refptr<MessagePipeDispatcher> d1 = MessagePipeDispatcher::Create( | 157 scoped_refptr<MessagePipeDispatcher> d1 = MessagePipeDispatcher::Create( |
157 MessagePipeDispatcher::kDefaultCreateOptions); | 158 MessagePipeDispatcher::kDefaultCreateOptions); |
158 { | 159 { |
159 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalLocal()); | 160 auto mp = MessagePipe::CreateLocalLocal(); |
160 d0->Init(mp, 0); | 161 d0->Init(mp.Clone(), 0); |
161 d1->Init(mp, 1); | 162 d1->Init(std::move(mp), 1); |
162 } | 163 } |
163 | 164 |
164 // |WriteMessage|: | 165 // |WriteMessage|: |
165 // Huge buffer size. | 166 // Huge buffer size. |
166 EXPECT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED, | 167 EXPECT_EQ(MOJO_RESULT_RESOURCE_EXHAUSTED, |
167 d0->WriteMessage(UserPointer<const void>(buffer), | 168 d0->WriteMessage(UserPointer<const void>(buffer), |
168 std::numeric_limits<uint32_t>::max(), nullptr, | 169 std::numeric_limits<uint32_t>::max(), nullptr, |
169 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 170 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
170 | 171 |
171 EXPECT_EQ(MOJO_RESULT_OK, d0->Close()); | 172 EXPECT_EQ(MOJO_RESULT_OK, d0->Close()); |
172 EXPECT_EQ(MOJO_RESULT_OK, d1->Close()); | 173 EXPECT_EQ(MOJO_RESULT_OK, d1->Close()); |
173 } | 174 } |
174 | 175 |
175 // These test invalid arguments that should cause death if we're being paranoid | 176 // These test invalid arguments that should cause death if we're being paranoid |
176 // about checking arguments (which we would want to do if, e.g., we were in a | 177 // about checking arguments (which we would want to do if, e.g., we were in a |
177 // true "kernel" situation, but we might not want to do otherwise for | 178 // true "kernel" situation, but we might not want to do otherwise for |
178 // performance reasons). Probably blatant errors like passing in null pointers | 179 // performance reasons). Probably blatant errors like passing in null pointers |
179 // (for required pointer arguments) will still cause death, but perhaps not | 180 // (for required pointer arguments) will still cause death, but perhaps not |
180 // predictably. | 181 // predictably. |
181 TEST(MessagePipeDispatcherTest, InvalidParamsDeath) { | 182 TEST(MessagePipeDispatcherTest, InvalidParamsDeath) { |
182 const char kMemoryCheckFailedRegex[] = "Check failed"; | 183 const char kMemoryCheckFailedRegex[] = "Check failed"; |
183 | 184 |
184 scoped_refptr<MessagePipeDispatcher> d0 = MessagePipeDispatcher::Create( | 185 scoped_refptr<MessagePipeDispatcher> d0 = MessagePipeDispatcher::Create( |
185 MessagePipeDispatcher::kDefaultCreateOptions); | 186 MessagePipeDispatcher::kDefaultCreateOptions); |
186 scoped_refptr<MessagePipeDispatcher> d1 = MessagePipeDispatcher::Create( | 187 scoped_refptr<MessagePipeDispatcher> d1 = MessagePipeDispatcher::Create( |
187 MessagePipeDispatcher::kDefaultCreateOptions); | 188 MessagePipeDispatcher::kDefaultCreateOptions); |
188 { | 189 { |
189 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalLocal()); | 190 auto mp = MessagePipe::CreateLocalLocal(); |
190 d0->Init(mp, 0); | 191 d0->Init(mp.Clone(), 0); |
191 d1->Init(mp, 1); | 192 d1->Init(std::move(mp), 1); |
192 } | 193 } |
193 | 194 |
194 // |WriteMessage|: | 195 // |WriteMessage|: |
195 // Null buffer with nonzero buffer size. | 196 // Null buffer with nonzero buffer size. |
196 EXPECT_DEATH_IF_SUPPORTED(d0->WriteMessage(NullUserPointer(), 1, nullptr, | 197 EXPECT_DEATH_IF_SUPPORTED(d0->WriteMessage(NullUserPointer(), 1, nullptr, |
197 MOJO_WRITE_MESSAGE_FLAG_NONE), | 198 MOJO_WRITE_MESSAGE_FLAG_NONE), |
198 kMemoryCheckFailedRegex); | 199 kMemoryCheckFailedRegex); |
199 | 200 |
200 // |ReadMessage|: | 201 // |ReadMessage|: |
201 // Null buffer with nonzero buffer size. | 202 // Null buffer with nonzero buffer size. |
(...skipping 17 matching lines...) Expand all Loading... |
219 const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer)); | 220 const uint32_t kBufferSize = static_cast<uint32_t>(sizeof(buffer)); |
220 uint32_t buffer_size; | 221 uint32_t buffer_size; |
221 | 222 |
222 // Run this test both with |d0| as port 0, |d1| as port 1 and vice versa. | 223 // Run this test both with |d0| as port 0, |d1| as port 1 and vice versa. |
223 for (unsigned i = 0; i < 2; i++) { | 224 for (unsigned i = 0; i < 2; i++) { |
224 scoped_refptr<MessagePipeDispatcher> d0 = MessagePipeDispatcher::Create( | 225 scoped_refptr<MessagePipeDispatcher> d0 = MessagePipeDispatcher::Create( |
225 MessagePipeDispatcher::kDefaultCreateOptions); | 226 MessagePipeDispatcher::kDefaultCreateOptions); |
226 scoped_refptr<MessagePipeDispatcher> d1 = MessagePipeDispatcher::Create( | 227 scoped_refptr<MessagePipeDispatcher> d1 = MessagePipeDispatcher::Create( |
227 MessagePipeDispatcher::kDefaultCreateOptions); | 228 MessagePipeDispatcher::kDefaultCreateOptions); |
228 { | 229 { |
229 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalLocal()); | 230 auto mp = MessagePipe::CreateLocalLocal(); |
230 d0->Init(mp, i); // 0, 1. | 231 d0->Init(mp.Clone(), i); // 0, 1. |
231 d1->Init(mp, i ^ 1); // 1, 0. | 232 d1->Init(std::move(mp), i ^ 1); // 1, 0. |
232 } | 233 } |
233 Waiter w; | 234 Waiter w; |
234 HandleSignalsState hss; | 235 HandleSignalsState hss; |
235 | 236 |
236 // Write (twice) to |d1|. | 237 // Write (twice) to |d1|. |
237 buffer[0] = 123456789; | 238 buffer[0] = 123456789; |
238 EXPECT_EQ(MOJO_RESULT_OK, | 239 EXPECT_EQ(MOJO_RESULT_OK, |
239 d1->WriteMessage(UserPointer<const void>(buffer), kBufferSize, | 240 d1->WriteMessage(UserPointer<const void>(buffer), kBufferSize, |
240 nullptr, MOJO_WRITE_MESSAGE_FLAG_NONE)); | 241 nullptr, MOJO_WRITE_MESSAGE_FLAG_NONE)); |
241 buffer[0] = 234567890; | 242 buffer[0] = 234567890; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 uint32_t context; | 350 uint32_t context; |
350 HandleSignalsState hss; | 351 HandleSignalsState hss; |
351 | 352 |
352 // Run this test both with |d0| as port 0, |d1| as port 1 and vice versa. | 353 // Run this test both with |d0| as port 0, |d1| as port 1 and vice versa. |
353 for (unsigned i = 0; i < 2; i++) { | 354 for (unsigned i = 0; i < 2; i++) { |
354 scoped_refptr<MessagePipeDispatcher> d0 = MessagePipeDispatcher::Create( | 355 scoped_refptr<MessagePipeDispatcher> d0 = MessagePipeDispatcher::Create( |
355 MessagePipeDispatcher::kDefaultCreateOptions); | 356 MessagePipeDispatcher::kDefaultCreateOptions); |
356 scoped_refptr<MessagePipeDispatcher> d1 = MessagePipeDispatcher::Create( | 357 scoped_refptr<MessagePipeDispatcher> d1 = MessagePipeDispatcher::Create( |
357 MessagePipeDispatcher::kDefaultCreateOptions); | 358 MessagePipeDispatcher::kDefaultCreateOptions); |
358 { | 359 { |
359 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalLocal()); | 360 auto mp = MessagePipe::CreateLocalLocal(); |
360 d0->Init(mp, i); // 0, 1. | 361 d0->Init(mp.Clone(), i); // 0, 1. |
361 d1->Init(mp, i ^ 1); // 1, 0. | 362 d1->Init(std::move(mp), i ^ 1); // 1, 0. |
362 } | 363 } |
363 | 364 |
364 // Wait for readable on |d1|, which will become readable after some time. | 365 // Wait for readable on |d1|, which will become readable after some time. |
365 { | 366 { |
366 test::WaiterThread thread(d1, MOJO_HANDLE_SIGNAL_READABLE, | 367 test::WaiterThread thread(d1, MOJO_HANDLE_SIGNAL_READABLE, |
367 MOJO_DEADLINE_INDEFINITE, 1, &did_wait, &result, | 368 MOJO_DEADLINE_INDEFINITE, 1, &did_wait, &result, |
368 &context, &hss); | 369 &context, &hss); |
369 stopwatch.Start(); | 370 stopwatch.Start(); |
370 thread.Start(); | 371 thread.Start(); |
371 test::Sleep(2 * test::EpsilonDeadline()); | 372 test::Sleep(2 * test::EpsilonDeadline()); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
432 | 433 |
433 EXPECT_EQ(MOJO_RESULT_OK, d1->Close()); | 434 EXPECT_EQ(MOJO_RESULT_OK, d1->Close()); |
434 } | 435 } |
435 | 436 |
436 for (unsigned i = 0; i < 2; i++) { | 437 for (unsigned i = 0; i < 2; i++) { |
437 scoped_refptr<MessagePipeDispatcher> d0 = MessagePipeDispatcher::Create( | 438 scoped_refptr<MessagePipeDispatcher> d0 = MessagePipeDispatcher::Create( |
438 MessagePipeDispatcher::kDefaultCreateOptions); | 439 MessagePipeDispatcher::kDefaultCreateOptions); |
439 scoped_refptr<MessagePipeDispatcher> d1 = MessagePipeDispatcher::Create( | 440 scoped_refptr<MessagePipeDispatcher> d1 = MessagePipeDispatcher::Create( |
440 MessagePipeDispatcher::kDefaultCreateOptions); | 441 MessagePipeDispatcher::kDefaultCreateOptions); |
441 { | 442 { |
442 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalLocal()); | 443 auto mp = MessagePipe::CreateLocalLocal(); |
443 d0->Init(mp, i); // 0, 1. | 444 d0->Init(mp.Clone(), i); // 0, 1. |
444 d1->Init(mp, i ^ 1); // 1, 0. | 445 d1->Init(std::move(mp), i ^ 1); // 1, 0. |
445 } | 446 } |
446 | 447 |
447 // Wait for readable on |d1| and close |d1| after some time, which should | 448 // Wait for readable on |d1| and close |d1| after some time, which should |
448 // cancel that wait. | 449 // cancel that wait. |
449 { | 450 { |
450 test::WaiterThread thread(d1, MOJO_HANDLE_SIGNAL_READABLE, | 451 test::WaiterThread thread(d1, MOJO_HANDLE_SIGNAL_READABLE, |
451 MOJO_DEADLINE_INDEFINITE, 4, &did_wait, &result, | 452 MOJO_DEADLINE_INDEFINITE, 4, &did_wait, &result, |
452 &context, &hss); | 453 &context, &hss); |
453 stopwatch.Start(); | 454 stopwatch.Start(); |
454 thread.Start(); | 455 thread.Start(); |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
610 | 611 |
611 TEST(MessagePipeDispatcherTest, Stress) { | 612 TEST(MessagePipeDispatcherTest, Stress) { |
612 static const size_t kNumWriters = 30; | 613 static const size_t kNumWriters = 30; |
613 static const size_t kNumReaders = kNumWriters; | 614 static const size_t kNumReaders = kNumWriters; |
614 | 615 |
615 scoped_refptr<MessagePipeDispatcher> d_write = MessagePipeDispatcher::Create( | 616 scoped_refptr<MessagePipeDispatcher> d_write = MessagePipeDispatcher::Create( |
616 MessagePipeDispatcher::kDefaultCreateOptions); | 617 MessagePipeDispatcher::kDefaultCreateOptions); |
617 scoped_refptr<MessagePipeDispatcher> d_read = MessagePipeDispatcher::Create( | 618 scoped_refptr<MessagePipeDispatcher> d_read = MessagePipeDispatcher::Create( |
618 MessagePipeDispatcher::kDefaultCreateOptions); | 619 MessagePipeDispatcher::kDefaultCreateOptions); |
619 { | 620 { |
620 scoped_refptr<MessagePipe> mp(MessagePipe::CreateLocalLocal()); | 621 auto mp = MessagePipe::CreateLocalLocal(); |
621 d_write->Init(mp, 0); | 622 d_write->Init(mp.Clone(), 0); |
622 d_read->Init(mp, 1); | 623 d_read->Init(std::move(mp), 1); |
623 } | 624 } |
624 | 625 |
625 size_t messages_written[kNumWriters]; | 626 size_t messages_written[kNumWriters]; |
626 size_t bytes_written[kNumWriters]; | 627 size_t bytes_written[kNumWriters]; |
627 size_t messages_read[kNumReaders]; | 628 size_t messages_read[kNumReaders]; |
628 size_t bytes_read[kNumReaders]; | 629 size_t bytes_read[kNumReaders]; |
629 { | 630 { |
630 // Make writers. | 631 // Make writers. |
631 ScopedVector<WriterThread> writers; | 632 ScopedVector<WriterThread> writers; |
632 for (size_t i = 0; i < kNumWriters; i++) { | 633 for (size_t i = 0; i < kNumWriters; i++) { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
672 EXPECT_EQ(total_messages_written, total_messages_read); | 673 EXPECT_EQ(total_messages_written, total_messages_read); |
673 EXPECT_EQ(total_bytes_written, total_bytes_read); | 674 EXPECT_EQ(total_bytes_written, total_bytes_read); |
674 | 675 |
675 EXPECT_EQ(MOJO_RESULT_OK, d_write->Close()); | 676 EXPECT_EQ(MOJO_RESULT_OK, d_write->Close()); |
676 EXPECT_EQ(MOJO_RESULT_OK, d_read->Close()); | 677 EXPECT_EQ(MOJO_RESULT_OK, d_read->Close()); |
677 } | 678 } |
678 | 679 |
679 } // namespace | 680 } // namespace |
680 } // namespace system | 681 } // namespace system |
681 } // namespace mojo | 682 } // namespace mojo |
OLD | NEW |