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

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

Issue 2754083003: Mojo EDK: Circulate MojoArmWatcher outputs to avoid starvation (Closed)
Patch Set: . Created 3 years, 9 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
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 #include <stdint.h> 5 #include <stdint.h>
6 6
7 #include <map> 7 #include <map>
8 #include <memory> 8 #include <memory>
9 #include <set>
9 10
10 #include "base/bind.h" 11 #include "base/bind.h"
11 #include "base/callback.h" 12 #include "base/callback.h"
12 #include "base/macros.h" 13 #include "base/macros.h"
13 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
14 #include "base/synchronization/waitable_event.h" 15 #include "base/synchronization/waitable_event.h"
15 #include "base/threading/platform_thread.h" 16 #include "base/threading/platform_thread.h"
16 #include "base/threading/simple_thread.h" 17 #include "base/threading/simple_thread.h"
17 #include "base/time/time.h" 18 #include "base/time/time.h"
18 #include "mojo/edk/test/mojo_test_base.h" 19 #include "mojo/edk/test/mojo_test_base.h"
(...skipping 1559 matching lines...) Expand 10 before | Expand all | Expand 10 after
1578 context = helper.CreateContextWithCancel(WatchHelper::ContextCallback(), 1579 context = helper.CreateContextWithCancel(WatchHelper::ContextCallback(),
1579 signal_event); 1580 signal_event);
1580 EXPECT_EQ(MOJO_RESULT_OK, 1581 EXPECT_EQ(MOJO_RESULT_OK,
1581 MojoWatch(w, b, MOJO_HANDLE_SIGNAL_READABLE, context)); 1582 MojoWatch(w, b, MOJO_HANDLE_SIGNAL_READABLE, context));
1582 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(w)); 1583 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(w));
1583 event.Wait(); 1584 event.Wait();
1584 1585
1585 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b)); 1586 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(b));
1586 } 1587 }
1587 1588
1589 TEST_F(WatcherTest, ArmFailureCirculation) {
1590 // Sanity check to ensure that all ready handles will eventually be returned
1591 // over a finite number of calls to MojoArmWatcher().
1592
1593 constexpr size_t kNumTestPipes = 100;
1594 constexpr size_t kNumTestHandles = kNumTestPipes * 2;
1595 MojoHandle handles[kNumTestHandles];
1596
1597 // Create a bunch of pipes and make sure they're all readable.
1598 for (size_t i = 0; i < kNumTestPipes; ++i) {
1599 CreateMessagePipe(&handles[i], &handles[i + kNumTestPipes]);
1600 WriteMessage(handles[i], "hey");
1601 WriteMessage(handles[i + kNumTestPipes], "hay");
1602 WaitForSignals(handles[i], MOJO_HANDLE_SIGNAL_READABLE);
1603 WaitForSignals(handles[i + kNumTestPipes], MOJO_HANDLE_SIGNAL_READABLE);
1604 }
1605
1606 // Create a watcher and watch all of them.
1607 MojoHandle w;
1608 EXPECT_EQ(MOJO_RESULT_OK, MojoCreateWatcher(&ExpectOnlyCancel, &w));
1609 for (size_t i = 0; i < kNumTestHandles; ++i) {
1610 EXPECT_EQ(MOJO_RESULT_OK,
1611 MojoWatch(w, handles[i], MOJO_HANDLE_SIGNAL_READABLE, i));
1612 }
1613
1614 // Keep trying to arm |w| until every watch gets an entry in |ready_contexts|.
1615 // If MojoArmWatcher() is well-behaved, this should terminate eventually.
1616 std::set<uintptr_t> ready_contexts;
1617 while (ready_contexts.size() < kNumTestHandles) {
1618 uint32_t num_ready_contexts = 1;
1619 uintptr_t ready_context;
1620 MojoResult ready_result;
1621 MojoHandleSignalsState ready_state;
1622 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION,
1623 MojoArmWatcher(w, &num_ready_contexts, &ready_context,
1624 &ready_result, &ready_state));
1625 EXPECT_EQ(1u, num_ready_contexts);
1626 EXPECT_EQ(MOJO_RESULT_OK, ready_result);
1627 ready_contexts.insert(ready_context);
1628 }
1629
1630 for (size_t i = 0; i < kNumTestHandles; ++i)
1631 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(handles[i]));
1632 EXPECT_EQ(MOJO_RESULT_OK, MojoClose(w));
1633 }
1634
1588 } // namespace 1635 } // namespace
1589 } // namespace edk 1636 } // namespace edk
1590 } // namespace mojo 1637 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698