| 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/simple_dispatcher.h" | 10 #include "mojo/system/simple_dispatcher.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "mojo/system/waiter_test_utils.h" | 21 #include "mojo/system/waiter_test_utils.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 23 |
| 24 namespace mojo { | 24 namespace mojo { |
| 25 namespace system { | 25 namespace system { |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 class MockSimpleDispatcher : public SimpleDispatcher { | 28 class MockSimpleDispatcher : public SimpleDispatcher { |
| 29 public: | 29 public: |
| 30 MockSimpleDispatcher() | 30 MockSimpleDispatcher() |
| 31 : satisfied_flags_(MOJO_WAIT_FLAG_NONE), | 31 : state_(MOJO_WAIT_FLAG_NONE, |
| 32 satisfiable_flags_(MOJO_WAIT_FLAG_READABLE | MOJO_WAIT_FLAG_WRITABLE) {} | 32 MOJO_WAIT_FLAG_READABLE | MOJO_WAIT_FLAG_WRITABLE) {} |
| 33 | 33 |
| 34 void SetSatisfiedFlags(MojoWaitFlags new_satisfied_flags) { | 34 void SetSatisfiedFlags(MojoWaitFlags new_satisfied_flags) { |
| 35 base::AutoLock locker(lock()); | 35 base::AutoLock locker(lock()); |
| 36 | 36 |
| 37 // Any new flags that are set should be satisfiable. | 37 // Any new flags that are set should be satisfiable. |
| 38 CHECK_EQ(new_satisfied_flags & ~satisfied_flags_, | 38 CHECK_EQ(new_satisfied_flags & ~state_.satisfied_flags, |
| 39 new_satisfied_flags & ~satisfied_flags_ & satisfiable_flags_); | 39 new_satisfied_flags & ~state_.satisfied_flags & |
| 40 state_.satisfiable_flags); |
| 40 | 41 |
| 41 if (new_satisfied_flags == satisfied_flags_) | 42 if (new_satisfied_flags == state_.satisfied_flags) |
| 42 return; | 43 return; |
| 43 | 44 |
| 44 satisfied_flags_ = new_satisfied_flags; | 45 state_.satisfied_flags = new_satisfied_flags; |
| 45 StateChangedNoLock(); | 46 WaitFlagsStateChangedNoLock(); |
| 46 } | 47 } |
| 47 | 48 |
| 48 void SetSatisfiableFlags(MojoWaitFlags new_satisfiable_flags) { | 49 void SetSatisfiableFlags(MojoWaitFlags new_satisfiable_flags) { |
| 49 base::AutoLock locker(lock()); | 50 base::AutoLock locker(lock()); |
| 50 | 51 |
| 51 if (new_satisfiable_flags == satisfiable_flags_) | 52 // Satisfied implies satisfiable. |
| 53 CHECK_EQ(new_satisfiable_flags & state_.satisfied_flags, |
| 54 state_.satisfied_flags); |
| 55 |
| 56 if (new_satisfiable_flags == state_.satisfiable_flags) |
| 52 return; | 57 return; |
| 53 | 58 |
| 54 satisfiable_flags_ = new_satisfiable_flags; | 59 state_.satisfiable_flags = new_satisfiable_flags; |
| 55 StateChangedNoLock(); | 60 WaitFlagsStateChangedNoLock(); |
| 56 } | 61 } |
| 57 | 62 |
| 58 virtual Type GetType() const OVERRIDE { | 63 virtual Type GetType() const OVERRIDE { |
| 59 return kTypeUnknown; | 64 return kTypeUnknown; |
| 60 } | 65 } |
| 61 | 66 |
| 62 private: | 67 private: |
| 63 friend class base::RefCountedThreadSafe<MockSimpleDispatcher>; | 68 friend class base::RefCountedThreadSafe<MockSimpleDispatcher>; |
| 64 virtual ~MockSimpleDispatcher() {} | 69 virtual ~MockSimpleDispatcher() {} |
| 65 | 70 |
| 66 virtual scoped_refptr<Dispatcher> | 71 virtual scoped_refptr<Dispatcher> |
| 67 CreateEquivalentDispatcherAndCloseImplNoLock() OVERRIDE { | 72 CreateEquivalentDispatcherAndCloseImplNoLock() OVERRIDE { |
| 68 scoped_refptr<MockSimpleDispatcher> rv(new MockSimpleDispatcher()); | 73 scoped_refptr<MockSimpleDispatcher> rv(new MockSimpleDispatcher()); |
| 69 rv->satisfied_flags_ = satisfied_flags_; | 74 rv->state_ = state_; |
| 70 rv->satisfiable_flags_ = satisfiable_flags_; | |
| 71 return scoped_refptr<Dispatcher>(rv.get()); | 75 return scoped_refptr<Dispatcher>(rv.get()); |
| 72 } | 76 } |
| 73 | 77 |
| 74 // |SimpleDispatcher| implementation: | 78 // |SimpleDispatcher| implementation: |
| 75 virtual MojoWaitFlags SatisfiedFlagsNoLock() const OVERRIDE { | 79 virtual WaitFlagsState GetWaitFlagsStateNoLock() const OVERRIDE { |
| 76 lock().AssertAcquired(); | 80 lock().AssertAcquired(); |
| 77 return satisfied_flags_; | 81 return state_; |
| 78 } | |
| 79 | |
| 80 virtual MojoWaitFlags SatisfiableFlagsNoLock() const OVERRIDE { | |
| 81 lock().AssertAcquired(); | |
| 82 return satisfiable_flags_; | |
| 83 } | 82 } |
| 84 | 83 |
| 85 // Protected by |lock()|: | 84 // Protected by |lock()|: |
| 86 MojoWaitFlags satisfied_flags_; | 85 WaitFlagsState state_; |
| 87 MojoWaitFlags satisfiable_flags_; | |
| 88 | 86 |
| 89 DISALLOW_COPY_AND_ASSIGN(MockSimpleDispatcher); | 87 DISALLOW_COPY_AND_ASSIGN(MockSimpleDispatcher); |
| 90 }; | 88 }; |
| 91 | 89 |
| 92 TEST(SimpleDispatcherTest, Basic) { | 90 TEST(SimpleDispatcherTest, Basic) { |
| 93 test::Stopwatch stopwatch; | 91 test::Stopwatch stopwatch; |
| 94 | 92 |
| 95 scoped_refptr<MockSimpleDispatcher> d(new MockSimpleDispatcher()); | 93 scoped_refptr<MockSimpleDispatcher> d(new MockSimpleDispatcher()); |
| 96 Waiter w; | 94 Waiter w; |
| 97 | 95 |
| (...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 EXPECT_TRUE(did_wait[i]); | 494 EXPECT_TRUE(did_wait[i]); |
| 497 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, result[i]); | 495 EXPECT_EQ(MOJO_RESULT_DEADLINE_EXCEEDED, result[i]); |
| 498 } | 496 } |
| 499 } | 497 } |
| 500 | 498 |
| 501 // TODO(vtl): Stress test? | 499 // TODO(vtl): Stress test? |
| 502 | 500 |
| 503 } // namespace | 501 } // namespace |
| 504 } // namespace system | 502 } // namespace system |
| 505 } // namespace mojo | 503 } // namespace mojo |
| OLD | NEW |