| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/edk/system/wait_set_dispatcher.h" | 10 #include "mojo/edk/system/wait_set_dispatcher.h" |
| 11 | 11 |
| 12 #include <thread> | 12 #include <thread> |
| 13 #include <vector> |
| 13 | 14 |
| 14 #include "mojo/edk/platform/test_stopwatch.h" | 15 #include "mojo/edk/platform/test_stopwatch.h" |
| 15 #include "mojo/edk/platform/thread_utils.h" | 16 #include "mojo/edk/platform/thread_utils.h" |
| 16 #include "mojo/edk/system/mock_simple_dispatcher.h" | 17 #include "mojo/edk/system/mock_simple_dispatcher.h" |
| 17 #include "mojo/edk/system/test/timeouts.h" | 18 #include "mojo/edk/system/test/timeouts.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 20 |
| 20 using mojo::platform::test::Stopwatch; | 21 using mojo::platform::test::Stopwatch; |
| 21 using mojo::platform::ThreadSleep; | 22 using mojo::platform::ThreadSleep; |
| 22 using mojo::util::MakeRefCounted; | 23 using mojo::util::MakeRefCounted; |
| (...skipping 562 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 585 MOJO_RESULT_FAILED_PRECONDITION, | 586 MOJO_RESULT_FAILED_PRECONDITION, |
| 586 d_member->GetHandleSignalsState())); | 587 d_member->GetHandleSignalsState())); |
| 587 | 588 |
| 588 t.join(); | 589 t.join(); |
| 589 } | 590 } |
| 590 | 591 |
| 591 EXPECT_EQ(MOJO_RESULT_OK, d_member->Close()); | 592 EXPECT_EQ(MOJO_RESULT_OK, d_member->Close()); |
| 592 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); | 593 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); |
| 593 } | 594 } |
| 594 | 595 |
| 595 // TODO(vtl): Test waiting on multiple threads. | 596 TEST(WaitSetDispatcherTest, BasicThreaded3) { |
| 597 static constexpr auto kNone = MOJO_HANDLE_SIGNAL_NONE; |
| 598 static constexpr auto kR = MOJO_HANDLE_SIGNAL_READABLE; |
| 599 |
| 600 const auto epsilon = test::EpsilonTimeout(); |
| 601 |
| 602 Stopwatch stopwatch; |
| 603 |
| 604 auto d = WaitSetDispatcher::Create(WaitSetDispatcher::kDefaultCreateOptions); |
| 605 auto d_member = MakeRefCounted<test::MockSimpleDispatcher>(kNone, kR); |
| 606 |
| 607 { |
| 608 // Add an entry. |
| 609 EXPECT_EQ(MOJO_RESULT_OK, |
| 610 d->WaitSetAdd(NullUserPointer(), d_member.Clone(), kR, 123u)); |
| 611 |
| 612 // Wait on a bunch of threads. We'll trigger on the main thread. |
| 613 std::vector<std::thread> threads; |
| 614 for (size_t i = 0; i < 4; i++) { |
| 615 threads.push_back(std::thread([epsilon, d, d_member]() { |
| 616 uint32_t num_results = 10u; |
| 617 MojoWaitSetResult results[10] = {}; |
| 618 EXPECT_EQ(MOJO_RESULT_OK, |
| 619 d->WaitSetWait(5 * epsilon, MakeUserPointer(&num_results), |
| 620 MakeUserPointer(results), NullUserPointer())); |
| 621 EXPECT_EQ(1u, num_results); |
| 622 EXPECT_TRUE(CheckHasResult(num_results, results, 123u, kR, |
| 623 MOJO_RESULT_OK, |
| 624 d_member->GetHandleSignalsState())); |
| 625 })); |
| 626 } |
| 627 |
| 628 // Sleep a bit, to try to ensure that all the threads are already waiting. |
| 629 ThreadSleep(epsilon); |
| 630 |
| 631 // Trigger the entry. |
| 632 d_member->SetSatisfiedSignals(kR); |
| 633 |
| 634 for (auto& t : threads) |
| 635 t.join(); |
| 636 } |
| 637 |
| 638 EXPECT_EQ(MOJO_RESULT_OK, d_member->Close()); |
| 639 EXPECT_EQ(MOJO_RESULT_OK, d->Close()); |
| 640 } |
| 641 |
| 596 // TODO(vtl): Stress tests. | 642 // TODO(vtl): Stress tests. |
| 597 | 643 |
| 598 // TODO(vtl): Test options validation for "create" and "add" (not that there's | 644 // TODO(vtl): Test options validation for "create" and "add" (not that there's |
| 599 // much to test). | 645 // much to test). |
| 600 | 646 |
| 601 } // namespace | 647 } // namespace |
| 602 } // namespace system | 648 } // namespace system |
| 603 } // namespace mojo | 649 } // namespace mojo |
| OLD | NEW |