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

Side by Side Diff: mojo/edk/system/wait_set_dispatcher_unittest.cc

Issue 2096683003: Add yet another threaded test of WaitSetDispatcher. (Closed) Base URL: https://github.com/domokit/mojo.git@work792_wait_set_5.2-x-work791_wait_set_5.1-x-work790_wait_set_5
Patch Set: improve test and fix bug Created 4 years, 6 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
« no previous file with comments | « mojo/edk/system/wait_set_dispatcher.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
OLDNEW
« no previous file with comments | « mojo/edk/system/wait_set_dispatcher.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698