[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | f7b98b3 | 2013-02-05 08:14:15 | [diff] [blame] | 5 | #include "base/synchronization/waitable_event_watcher.h" |
| 6 | |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 7 | #include "base/bind.h" |
| 8 | #include "base/callback.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 9 | #include "base/macros.h" |
atuchin | 54d8315 | 2017-05-10 06:30:01 | [diff] [blame^] | 10 | #include "base/memory/ptr_util.h" |
[email protected] | 495cad9 | 2013-07-18 08:12:40 | [diff] [blame] | 11 | #include "base/message_loop/message_loop.h" |
[email protected] | f7b98b3 | 2013-02-05 08:14:15 | [diff] [blame] | 12 | #include "base/run_loop.h" |
[email protected] | 44f9c95 | 2011-01-02 06:05:39 | [diff] [blame] | 13 | #include "base/synchronization/waitable_event.h" |
[email protected] | ce072a7 | 2010-12-31 20:02:16 | [diff] [blame] | 14 | #include "base/threading/platform_thread.h" |
atuchin | 54d8315 | 2017-05-10 06:30:01 | [diff] [blame^] | 15 | #include "base/threading/sequenced_task_runner_handle.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 16 | #include "build/build_config.h" |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | |
[email protected] | 44f9c95 | 2011-01-02 06:05:39 | [diff] [blame] | 19 | namespace base { |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 20 | |
| 21 | namespace { |
| 22 | |
[email protected] | 840246b | 2012-07-18 08:06:50 | [diff] [blame] | 23 | // The message loops on which each waitable event timer should be tested. |
| 24 | const MessageLoop::Type testing_message_loops[] = { |
| 25 | MessageLoop::TYPE_DEFAULT, |
| 26 | MessageLoop::TYPE_IO, |
| 27 | #if !defined(OS_IOS) // iOS does not allow direct running of the UI loop. |
| 28 | MessageLoop::TYPE_UI, |
| 29 | #endif |
| 30 | }; |
| 31 | |
| 32 | const int kNumTestingMessageLoops = arraysize(testing_message_loops); |
| 33 | |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 34 | void QuitWhenSignaled(WaitableEvent* event) { |
| 35 | MessageLoop::current()->QuitWhenIdle(); |
| 36 | } |
[email protected] | 1eaa5479 | 2013-01-31 23:14:27 | [diff] [blame] | 37 | |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 38 | class DecrementCountContainer { |
[email protected] | 1eaa5479 | 2013-01-31 23:14:27 | [diff] [blame] | 39 | public: |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 40 | explicit DecrementCountContainer(int* counter) : counter_(counter) { |
[email protected] | 1eaa5479 | 2013-01-31 23:14:27 | [diff] [blame] | 41 | } |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 42 | void OnWaitableEventSignaled(WaitableEvent* object) { |
atuchin | 54d8315 | 2017-05-10 06:30:01 | [diff] [blame^] | 43 | // NOTE: |object| may be already deleted. |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 44 | --(*counter_); |
| 45 | } |
| 46 | private: |
| 47 | int* counter_; |
| 48 | }; |
| 49 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 50 | void RunTest_BasicSignal(MessageLoop::Type message_loop_type) { |
| 51 | MessageLoop message_loop(message_loop_type); |
| 52 | |
| 53 | // A manual-reset event that is not yet signaled. |
gab | 75d7233 | 2016-06-01 21:15:33 | [diff] [blame] | 54 | WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, |
| 55 | WaitableEvent::InitialState::NOT_SIGNALED); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 56 | |
| 57 | WaitableEventWatcher watcher; |
tzik | 130cfd0c | 2017-04-18 03:49:05 | [diff] [blame] | 58 | watcher.StartWatching(&event, BindOnce(&QuitWhenSignaled)); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 59 | |
| 60 | event.Signal(); |
| 61 | |
fdoray | 1022458 | 2016-06-30 18:17:39 | [diff] [blame] | 62 | RunLoop().Run(); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void RunTest_BasicCancel(MessageLoop::Type message_loop_type) { |
| 66 | MessageLoop message_loop(message_loop_type); |
| 67 | |
| 68 | // A manual-reset event that is not yet signaled. |
gab | 75d7233 | 2016-06-01 21:15:33 | [diff] [blame] | 69 | WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, |
| 70 | WaitableEvent::InitialState::NOT_SIGNALED); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 71 | |
| 72 | WaitableEventWatcher watcher; |
| 73 | |
tzik | 130cfd0c | 2017-04-18 03:49:05 | [diff] [blame] | 74 | watcher.StartWatching(&event, BindOnce(&QuitWhenSignaled)); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 75 | |
| 76 | watcher.StopWatching(); |
| 77 | } |
| 78 | |
| 79 | void RunTest_CancelAfterSet(MessageLoop::Type message_loop_type) { |
| 80 | MessageLoop message_loop(message_loop_type); |
| 81 | |
| 82 | // A manual-reset event that is not yet signaled. |
gab | 75d7233 | 2016-06-01 21:15:33 | [diff] [blame] | 83 | WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, |
| 84 | WaitableEvent::InitialState::NOT_SIGNALED); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 85 | |
| 86 | WaitableEventWatcher watcher; |
| 87 | |
| 88 | int counter = 1; |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 89 | DecrementCountContainer delegate(&counter); |
tzik | 130cfd0c | 2017-04-18 03:49:05 | [diff] [blame] | 90 | WaitableEventWatcher::EventCallback callback = BindOnce( |
| 91 | &DecrementCountContainer::OnWaitableEventSignaled, Unretained(&delegate)); |
| 92 | watcher.StartWatching(&event, std::move(callback)); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 93 | |
| 94 | event.Signal(); |
| 95 | |
| 96 | // Let the background thread do its business |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 97 | base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(30)); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 98 | |
| 99 | watcher.StopWatching(); |
| 100 | |
[email protected] | f7b98b3 | 2013-02-05 08:14:15 | [diff] [blame] | 101 | RunLoop().RunUntilIdle(); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 102 | |
| 103 | // Our delegate should not have fired. |
| 104 | EXPECT_EQ(1, counter); |
| 105 | } |
| 106 | |
| 107 | void RunTest_OutlivesMessageLoop(MessageLoop::Type message_loop_type) { |
| 108 | // Simulate a MessageLoop that dies before an WaitableEventWatcher. This |
| 109 | // ordinarily doesn't happen when people use the Thread class, but it can |
| 110 | // happen when people use the Singleton pattern or atexit. |
gab | 75d7233 | 2016-06-01 21:15:33 | [diff] [blame] | 111 | WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, |
| 112 | WaitableEvent::InitialState::NOT_SIGNALED); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 113 | { |
| 114 | WaitableEventWatcher watcher; |
| 115 | { |
| 116 | MessageLoop message_loop(message_loop_type); |
| 117 | |
tzik | 130cfd0c | 2017-04-18 03:49:05 | [diff] [blame] | 118 | watcher.StartWatching(&event, BindOnce(&QuitWhenSignaled)); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
atuchin | 54d8315 | 2017-05-10 06:30:01 | [diff] [blame^] | 123 | void RunTest_DeleteUnder(MessageLoop::Type message_loop_type, |
| 124 | bool delay_after_delete) { |
[email protected] | c891ab9 | 2009-03-26 18:28:19 | [diff] [blame] | 125 | // Delete the WaitableEvent out from under the Watcher. This is explictly |
| 126 | // allowed by the interface. |
| 127 | |
| 128 | MessageLoop message_loop(message_loop_type); |
| 129 | |
| 130 | { |
| 131 | WaitableEventWatcher watcher; |
| 132 | |
atuchin | 54d8315 | 2017-05-10 06:30:01 | [diff] [blame^] | 133 | auto* event = new WaitableEvent(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 134 | WaitableEvent::InitialState::NOT_SIGNALED); |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 135 | |
tzik | 130cfd0c | 2017-04-18 03:49:05 | [diff] [blame] | 136 | watcher.StartWatching(event, BindOnce(&QuitWhenSignaled)); |
atuchin | 54d8315 | 2017-05-10 06:30:01 | [diff] [blame^] | 137 | |
| 138 | if (delay_after_delete) { |
| 139 | // On Windows that sleep() improves the chance to catch some problems. |
| 140 | // It postpones the dtor |watcher| (which immediately cancel the waiting) |
| 141 | // and gives some time to run to a created background thread. |
| 142 | // Unfortunately, that thread is under OS control and we can't |
| 143 | // manipulate it directly. |
| 144 | base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(30)); |
| 145 | } |
| 146 | |
[email protected] | c891ab9 | 2009-03-26 18:28:19 | [diff] [blame] | 147 | delete event; |
| 148 | } |
| 149 | } |
| 150 | |
atuchin | 54d8315 | 2017-05-10 06:30:01 | [diff] [blame^] | 151 | void RunTest_SignalAndDelete(MessageLoop::Type message_loop_type, |
| 152 | bool delay_after_delete) { |
| 153 | // Signal and immediately delete the WaitableEvent out from under the Watcher. |
| 154 | |
| 155 | MessageLoop message_loop(message_loop_type); |
| 156 | |
| 157 | { |
| 158 | WaitableEventWatcher watcher; |
| 159 | |
| 160 | auto event = base::MakeUnique<WaitableEvent>( |
| 161 | WaitableEvent::ResetPolicy::AUTOMATIC, |
| 162 | WaitableEvent::InitialState::NOT_SIGNALED); |
| 163 | |
| 164 | watcher.StartWatching(event.get(), BindOnce(&QuitWhenSignaled)); |
| 165 | event->Signal(); |
| 166 | event.reset(); |
| 167 | |
| 168 | if (delay_after_delete) { |
| 169 | // On Windows that sleep() improves the chance to catch some problems. |
| 170 | // It postpones the dtor |watcher| (which immediately cancel the waiting) |
| 171 | // and gives some time to run to a created background thread. |
| 172 | // Unfortunately, that thread is under OS control and we can't |
| 173 | // manipulate it directly. |
| 174 | base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(30)); |
| 175 | } |
| 176 | |
| 177 | // Wait for the watcher callback. |
| 178 | RunLoop().Run(); |
| 179 | } |
| 180 | } |
| 181 | |
[email protected] | b57d33c5 | 2009-01-15 22:58:53 | [diff] [blame] | 182 | } // namespace |
| 183 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 184 | //----------------------------------------------------------------------------- |
| 185 | |
[email protected] | 9480d2ab | 2009-01-15 22:52:38 | [diff] [blame] | 186 | TEST(WaitableEventWatcherTest, BasicSignal) { |
[email protected] | 840246b | 2012-07-18 08:06:50 | [diff] [blame] | 187 | for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 188 | RunTest_BasicSignal(testing_message_loops[i]); |
| 189 | } |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 190 | } |
| 191 | |
[email protected] | 9480d2ab | 2009-01-15 22:52:38 | [diff] [blame] | 192 | TEST(WaitableEventWatcherTest, BasicCancel) { |
[email protected] | 840246b | 2012-07-18 08:06:50 | [diff] [blame] | 193 | for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 194 | RunTest_BasicCancel(testing_message_loops[i]); |
| 195 | } |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 196 | } |
| 197 | |
[email protected] | 9480d2ab | 2009-01-15 22:52:38 | [diff] [blame] | 198 | TEST(WaitableEventWatcherTest, CancelAfterSet) { |
[email protected] | 840246b | 2012-07-18 08:06:50 | [diff] [blame] | 199 | for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 200 | RunTest_CancelAfterSet(testing_message_loops[i]); |
| 201 | } |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 202 | } |
| 203 | |
[email protected] | 9480d2ab | 2009-01-15 22:52:38 | [diff] [blame] | 204 | TEST(WaitableEventWatcherTest, OutlivesMessageLoop) { |
[email protected] | 840246b | 2012-07-18 08:06:50 | [diff] [blame] | 205 | for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 206 | RunTest_OutlivesMessageLoop(testing_message_loops[i]); |
| 207 | } |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 208 | } |
[email protected] | c891ab9 | 2009-03-26 18:28:19 | [diff] [blame] | 209 | |
mattm | 5cc71c0e | 2016-07-06 23:56:26 | [diff] [blame] | 210 | TEST(WaitableEventWatcherTest, DeleteUnder) { |
[email protected] | 840246b | 2012-07-18 08:06:50 | [diff] [blame] | 211 | for (int i = 0; i < kNumTestingMessageLoops; i++) { |
atuchin | 54d8315 | 2017-05-10 06:30:01 | [diff] [blame^] | 212 | RunTest_DeleteUnder(testing_message_loops[i], false); |
| 213 | RunTest_DeleteUnder(testing_message_loops[i], true); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | TEST(WaitableEventWatcherTest, SignalAndDelete) { |
| 218 | for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 219 | RunTest_SignalAndDelete(testing_message_loops[i], false); |
| 220 | RunTest_SignalAndDelete(testing_message_loops[i], true); |
[email protected] | 840246b | 2012-07-18 08:06:50 | [diff] [blame] | 221 | } |
[email protected] | c891ab9 | 2009-03-26 18:28:19 | [diff] [blame] | 222 | } |
[email protected] | 44f9c95 | 2011-01-02 06:05:39 | [diff] [blame] | 223 | |
| 224 | } // namespace base |