| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/synchronization/waitable_event_watcher.h" | 5 #include "base/synchronization/waitable_event_watcher.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 --(*counter_); | 41 --(*counter_); |
| 42 } | 42 } |
| 43 private: | 43 private: |
| 44 int* counter_; | 44 int* counter_; |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 void RunTest_BasicSignal(MessageLoop::Type message_loop_type) { | 47 void RunTest_BasicSignal(MessageLoop::Type message_loop_type) { |
| 48 MessageLoop message_loop(message_loop_type); | 48 MessageLoop message_loop(message_loop_type); |
| 49 | 49 |
| 50 // A manual-reset event that is not yet signaled. | 50 // A manual-reset event that is not yet signaled. |
| 51 WaitableEvent event(true, false); | 51 WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, |
| 52 WaitableEvent::InitialState::NOT_SIGNALED); |
| 52 | 53 |
| 53 WaitableEventWatcher watcher; | 54 WaitableEventWatcher watcher; |
| 54 EXPECT_TRUE(watcher.GetWatchedEvent() == NULL); | 55 EXPECT_TRUE(watcher.GetWatchedEvent() == NULL); |
| 55 | 56 |
| 56 watcher.StartWatching(&event, Bind(&QuitWhenSignaled)); | 57 watcher.StartWatching(&event, Bind(&QuitWhenSignaled)); |
| 57 EXPECT_EQ(&event, watcher.GetWatchedEvent()); | 58 EXPECT_EQ(&event, watcher.GetWatchedEvent()); |
| 58 | 59 |
| 59 event.Signal(); | 60 event.Signal(); |
| 60 | 61 |
| 61 MessageLoop::current()->Run(); | 62 MessageLoop::current()->Run(); |
| 62 | 63 |
| 63 EXPECT_TRUE(watcher.GetWatchedEvent() == NULL); | 64 EXPECT_TRUE(watcher.GetWatchedEvent() == NULL); |
| 64 } | 65 } |
| 65 | 66 |
| 66 void RunTest_BasicCancel(MessageLoop::Type message_loop_type) { | 67 void RunTest_BasicCancel(MessageLoop::Type message_loop_type) { |
| 67 MessageLoop message_loop(message_loop_type); | 68 MessageLoop message_loop(message_loop_type); |
| 68 | 69 |
| 69 // A manual-reset event that is not yet signaled. | 70 // A manual-reset event that is not yet signaled. |
| 70 WaitableEvent event(true, false); | 71 WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, |
| 72 WaitableEvent::InitialState::NOT_SIGNALED); |
| 71 | 73 |
| 72 WaitableEventWatcher watcher; | 74 WaitableEventWatcher watcher; |
| 73 | 75 |
| 74 watcher.StartWatching(&event, Bind(&QuitWhenSignaled)); | 76 watcher.StartWatching(&event, Bind(&QuitWhenSignaled)); |
| 75 | 77 |
| 76 watcher.StopWatching(); | 78 watcher.StopWatching(); |
| 77 } | 79 } |
| 78 | 80 |
| 79 void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type) { | 81 void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type) { |
| 80 MessageLoop message_loop(message_loop_type); | 82 MessageLoop message_loop(message_loop_type); |
| 81 | 83 |
| 82 // A manual-reset event that is not yet signaled. | 84 // A manual-reset event that is not yet signaled. |
| 83 WaitableEvent event(true, false); | 85 WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, |
| 86 WaitableEvent::InitialState::NOT_SIGNALED); |
| 84 | 87 |
| 85 WaitableEventWatcher watcher; | 88 WaitableEventWatcher watcher; |
| 86 | 89 |
| 87 int counter = 1; | 90 int counter = 1; |
| 88 DecrementCountContainer delegate(&counter); | 91 DecrementCountContainer delegate(&counter); |
| 89 WaitableEventWatcher::EventCallback callback = | 92 WaitableEventWatcher::EventCallback callback = |
| 90 Bind(&DecrementCountContainer::OnWaitableEventSignaled, | 93 Bind(&DecrementCountContainer::OnWaitableEventSignaled, |
| 91 Unretained(&delegate)); | 94 Unretained(&delegate)); |
| 92 watcher.StartWatching(&event, callback); | 95 watcher.StartWatching(&event, callback); |
| 93 | 96 |
| 94 event.Signal(); | 97 event.Signal(); |
| 95 | 98 |
| 96 // Let the background thread do its business | 99 // Let the background thread do its business |
| 97 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(30)); | 100 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(30)); |
| 98 | 101 |
| 99 watcher.StopWatching(); | 102 watcher.StopWatching(); |
| 100 | 103 |
| 101 RunLoop().RunUntilIdle(); | 104 RunLoop().RunUntilIdle(); |
| 102 | 105 |
| 103 // Our delegate should not have fired. | 106 // Our delegate should not have fired. |
| 104 EXPECT_EQ(1, counter); | 107 EXPECT_EQ(1, counter); |
| 105 } | 108 } |
| 106 | 109 |
| 107 void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type) { | 110 void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type) { |
| 108 // Simulate a MessageLoop that dies before an WaitableEventWatcher. This | 111 // Simulate a MessageLoop that dies before an WaitableEventWatcher. This |
| 109 // ordinarily doesn't happen when people use the Thread class, but it can | 112 // ordinarily doesn't happen when people use the Thread class, but it can |
| 110 // happen when people use the Singleton pattern or atexit. | 113 // happen when people use the Singleton pattern or atexit. |
| 111 WaitableEvent event(true, false); | 114 WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, |
| 115 WaitableEvent::InitialState::NOT_SIGNALED); |
| 112 { | 116 { |
| 113 WaitableEventWatcher watcher; | 117 WaitableEventWatcher watcher; |
| 114 { | 118 { |
| 115 MessageLoop message_loop(message_loop_type); | 119 MessageLoop message_loop(message_loop_type); |
| 116 | 120 |
| 117 watcher.StartWatching(&event, Bind(&QuitWhenSignaled)); | 121 watcher.StartWatching(&event, Bind(&QuitWhenSignaled)); |
| 118 } | 122 } |
| 119 } | 123 } |
| 120 } | 124 } |
| 121 | 125 |
| 122 void RunTest_DeleteUnder(MessageLoop::Type message_loop_type) { | 126 void RunTest_DeleteUnder(MessageLoop::Type message_loop_type) { |
| 123 // Delete the WaitableEvent out from under the Watcher. This is explictly | 127 // Delete the WaitableEvent out from under the Watcher. This is explictly |
| 124 // allowed by the interface. | 128 // allowed by the interface. |
| 125 | 129 |
| 126 MessageLoop message_loop(message_loop_type); | 130 MessageLoop message_loop(message_loop_type); |
| 127 | 131 |
| 128 { | 132 { |
| 129 WaitableEventWatcher watcher; | 133 WaitableEventWatcher watcher; |
| 130 | 134 |
| 131 WaitableEvent* event = new WaitableEvent(false, false); | 135 WaitableEvent* event = |
| 136 new WaitableEvent(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 137 WaitableEvent::InitialState::NOT_SIGNALED); |
| 132 | 138 |
| 133 watcher.StartWatching(event, Bind(&QuitWhenSignaled)); | 139 watcher.StartWatching(event, Bind(&QuitWhenSignaled)); |
| 134 delete event; | 140 delete event; |
| 135 } | 141 } |
| 136 } | 142 } |
| 137 | 143 |
| 138 } // namespace | 144 } // namespace |
| 139 | 145 |
| 140 //----------------------------------------------------------------------------- | 146 //----------------------------------------------------------------------------- |
| 141 | 147 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 169 #else | 175 #else |
| 170 #define MAYBE_DeleteUnder DeleteUnder | 176 #define MAYBE_DeleteUnder DeleteUnder |
| 171 #endif | 177 #endif |
| 172 TEST(WaitableEventWatcherTest, MAYBE_DeleteUnder) { | 178 TEST(WaitableEventWatcherTest, MAYBE_DeleteUnder) { |
| 173 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 179 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 174 RunTest_DeleteUnder(testing_message_loops[i]); | 180 RunTest_DeleteUnder(testing_message_loops[i]); |
| 175 } | 181 } |
| 176 } | 182 } |
| 177 | 183 |
| 178 } // namespace base | 184 } // namespace base |
| OLD | NEW |