[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] | f7b98b3 | 2013-02-05 08:14:15 | [diff] [blame] | 11 | #include "base/run_loop.h" |
[email protected] | 44f9c95 | 2011-01-02 06:05:39 | [diff] [blame] | 12 | #include "base/synchronization/waitable_event.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame^] | 13 | #include "base/test/task_environment.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" |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 16 | #include "base/threading/thread_task_runner_handle.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 17 | #include "build/build_config.h" |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 18 | #include "testing/gtest/include/gtest/gtest.h" |
| 19 | |
[email protected] | 44f9c95 | 2011-01-02 06:05:39 | [diff] [blame] | 20 | namespace base { |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 21 | |
| 22 | namespace { |
| 23 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 24 | // The main thread types on which each waitable event should be tested. |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 25 | const test::TaskEnvironment::MainThreadType testing_main_threads[] = { |
| 26 | test::TaskEnvironment::MainThreadType::DEFAULT, |
| 27 | test::TaskEnvironment::MainThreadType::IO, |
[email protected] | 840246b | 2012-07-18 08:06:50 | [diff] [blame] | 28 | #if !defined(OS_IOS) // iOS does not allow direct running of the UI loop. |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 29 | test::TaskEnvironment::MainThreadType::UI, |
[email protected] | 840246b | 2012-07-18 08:06:50 | [diff] [blame] | 30 | #endif |
| 31 | }; |
| 32 | |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 33 | void QuitWhenSignaled(WaitableEvent* event) { |
Gabriel Charette | 53a9ef81 | 2017-07-26 12:36:23 | [diff] [blame] | 34 | RunLoop::QuitCurrentWhenIdleDeprecated(); |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 35 | } |
[email protected] | 1eaa5479 | 2013-01-31 23:14:27 | [diff] [blame] | 36 | |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 37 | class DecrementCountContainer { |
[email protected] | 1eaa5479 | 2013-01-31 23:14:27 | [diff] [blame] | 38 | public: |
Robert Sesek | b44f896 | 2017-06-30 21:21:13 | [diff] [blame] | 39 | explicit DecrementCountContainer(int* counter) : counter_(counter) {} |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 40 | void OnWaitableEventSignaled(WaitableEvent* object) { |
atuchin | 54d8315 | 2017-05-10 06:30:01 | [diff] [blame] | 41 | // NOTE: |object| may be already deleted. |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 42 | --(*counter_); |
| 43 | } |
Robert Sesek | b44f896 | 2017-06-30 21:21:13 | [diff] [blame] | 44 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 45 | private: |
| 46 | int* counter_; |
| 47 | }; |
| 48 | |
Robert Sesek | 3b333377 | 2017-06-30 18:21:56 | [diff] [blame] | 49 | } // namespace |
| 50 | |
| 51 | class WaitableEventWatcherTest |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 52 | : public testing::TestWithParam<test::TaskEnvironment::MainThreadType> {}; |
Robert Sesek | 3b333377 | 2017-06-30 18:21:56 | [diff] [blame] | 53 | |
Robert Sesek | cfab3ad | 2017-08-01 16:25:43 | [diff] [blame] | 54 | TEST_P(WaitableEventWatcherTest, BasicSignalManual) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 55 | test::TaskEnvironment task_environment(GetParam()); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 56 | |
| 57 | // A manual-reset event that is not yet signaled. |
gab | 75d7233 | 2016-06-01 21:15:33 | [diff] [blame] | 58 | WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, |
| 59 | WaitableEvent::InitialState::NOT_SIGNALED); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 60 | |
| 61 | WaitableEventWatcher watcher; |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 62 | watcher.StartWatching(&event, BindOnce(&QuitWhenSignaled), |
| 63 | SequencedTaskRunnerHandle::Get()); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 64 | |
| 65 | event.Signal(); |
| 66 | |
fdoray | 1022458 | 2016-06-30 18:17:39 | [diff] [blame] | 67 | RunLoop().Run(); |
Robert Sesek | cfab3ad | 2017-08-01 16:25:43 | [diff] [blame] | 68 | |
| 69 | EXPECT_TRUE(event.IsSignaled()); |
| 70 | } |
| 71 | |
| 72 | TEST_P(WaitableEventWatcherTest, BasicSignalAutomatic) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 73 | test::TaskEnvironment task_environment(GetParam()); |
Robert Sesek | cfab3ad | 2017-08-01 16:25:43 | [diff] [blame] | 74 | |
| 75 | WaitableEvent event(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 76 | WaitableEvent::InitialState::NOT_SIGNALED); |
| 77 | |
| 78 | WaitableEventWatcher watcher; |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 79 | watcher.StartWatching(&event, BindOnce(&QuitWhenSignaled), |
| 80 | SequencedTaskRunnerHandle::Get()); |
Robert Sesek | cfab3ad | 2017-08-01 16:25:43 | [diff] [blame] | 81 | |
| 82 | event.Signal(); |
| 83 | |
| 84 | RunLoop().Run(); |
| 85 | |
| 86 | // The WaitableEventWatcher consumes the event signal. |
| 87 | EXPECT_FALSE(event.IsSignaled()); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 88 | } |
| 89 | |
Robert Sesek | 3b333377 | 2017-06-30 18:21:56 | [diff] [blame] | 90 | TEST_P(WaitableEventWatcherTest, BasicCancel) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 91 | test::TaskEnvironment task_environment(GetParam()); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 92 | |
| 93 | // A manual-reset event that is not yet signaled. |
gab | 75d7233 | 2016-06-01 21:15:33 | [diff] [blame] | 94 | WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, |
| 95 | WaitableEvent::InitialState::NOT_SIGNALED); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 96 | |
| 97 | WaitableEventWatcher watcher; |
| 98 | |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 99 | watcher.StartWatching(&event, BindOnce(&QuitWhenSignaled), |
| 100 | SequencedTaskRunnerHandle::Get()); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 101 | |
| 102 | watcher.StopWatching(); |
| 103 | } |
| 104 | |
Robert Sesek | 3b333377 | 2017-06-30 18:21:56 | [diff] [blame] | 105 | TEST_P(WaitableEventWatcherTest, CancelAfterSet) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 106 | test::TaskEnvironment task_environment(GetParam()); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 107 | |
| 108 | // A manual-reset event that is not yet signaled. |
gab | 75d7233 | 2016-06-01 21:15:33 | [diff] [blame] | 109 | WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, |
| 110 | WaitableEvent::InitialState::NOT_SIGNALED); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 111 | |
| 112 | WaitableEventWatcher watcher; |
| 113 | |
| 114 | int counter = 1; |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 115 | DecrementCountContainer delegate(&counter); |
tzik | 130cfd0c | 2017-04-18 03:49:05 | [diff] [blame] | 116 | WaitableEventWatcher::EventCallback callback = BindOnce( |
| 117 | &DecrementCountContainer::OnWaitableEventSignaled, Unretained(&delegate)); |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 118 | watcher.StartWatching(&event, std::move(callback), |
| 119 | SequencedTaskRunnerHandle::Get()); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 120 | |
| 121 | event.Signal(); |
| 122 | |
| 123 | // Let the background thread do its business |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 124 | PlatformThread::Sleep(TimeDelta::FromMilliseconds(30)); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 125 | |
| 126 | watcher.StopWatching(); |
| 127 | |
[email protected] | f7b98b3 | 2013-02-05 08:14:15 | [diff] [blame] | 128 | RunLoop().RunUntilIdle(); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 129 | |
| 130 | // Our delegate should not have fired. |
| 131 | EXPECT_EQ(1, counter); |
| 132 | } |
| 133 | |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 134 | TEST_P(WaitableEventWatcherTest, OutlivesTaskEnvironment) { |
| 135 | // Simulate a task environment that dies before an WaitableEventWatcher. This |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 136 | // ordinarily doesn't happen when people use the Thread class, but it can |
| 137 | // happen when people use the Singleton pattern or atexit. |
gab | 75d7233 | 2016-06-01 21:15:33 | [diff] [blame] | 138 | WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, |
| 139 | WaitableEvent::InitialState::NOT_SIGNALED); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 140 | { |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 141 | std::unique_ptr<WaitableEventWatcher> watcher; |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 142 | { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 143 | test::TaskEnvironment task_environment(GetParam()); |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 144 | watcher = std::make_unique<WaitableEventWatcher>(); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 145 | |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 146 | watcher->StartWatching(&event, BindOnce(&QuitWhenSignaled), |
| 147 | SequencedTaskRunnerHandle::Get()); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
Robert Sesek | cfab3ad | 2017-08-01 16:25:43 | [diff] [blame] | 152 | TEST_P(WaitableEventWatcherTest, SignaledAtStartManual) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 153 | test::TaskEnvironment task_environment(GetParam()); |
Robert Sesek | b44f896 | 2017-06-30 21:21:13 | [diff] [blame] | 154 | |
| 155 | WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, |
| 156 | WaitableEvent::InitialState::SIGNALED); |
| 157 | |
| 158 | WaitableEventWatcher watcher; |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 159 | watcher.StartWatching(&event, BindOnce(&QuitWhenSignaled), |
| 160 | SequencedTaskRunnerHandle::Get()); |
Robert Sesek | b44f896 | 2017-06-30 21:21:13 | [diff] [blame] | 161 | |
| 162 | RunLoop().Run(); |
Robert Sesek | cfab3ad | 2017-08-01 16:25:43 | [diff] [blame] | 163 | |
| 164 | EXPECT_TRUE(event.IsSignaled()); |
| 165 | } |
| 166 | |
| 167 | TEST_P(WaitableEventWatcherTest, SignaledAtStartAutomatic) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 168 | test::TaskEnvironment task_environment(GetParam()); |
Robert Sesek | cfab3ad | 2017-08-01 16:25:43 | [diff] [blame] | 169 | |
| 170 | WaitableEvent event(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 171 | WaitableEvent::InitialState::SIGNALED); |
| 172 | |
| 173 | WaitableEventWatcher watcher; |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 174 | watcher.StartWatching(&event, BindOnce(&QuitWhenSignaled), |
| 175 | SequencedTaskRunnerHandle::Get()); |
Robert Sesek | cfab3ad | 2017-08-01 16:25:43 | [diff] [blame] | 176 | |
| 177 | RunLoop().Run(); |
| 178 | |
| 179 | // The watcher consumes the event signal. |
| 180 | EXPECT_FALSE(event.IsSignaled()); |
Robert Sesek | b44f896 | 2017-06-30 21:21:13 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | TEST_P(WaitableEventWatcherTest, StartWatchingInCallback) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 184 | test::TaskEnvironment task_environment(GetParam()); |
Robert Sesek | b44f896 | 2017-06-30 21:21:13 | [diff] [blame] | 185 | |
| 186 | WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, |
| 187 | WaitableEvent::InitialState::NOT_SIGNALED); |
| 188 | |
| 189 | WaitableEventWatcher watcher; |
| 190 | watcher.StartWatching( |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 191 | &event, |
| 192 | BindOnce( |
| 193 | [](WaitableEventWatcher* watcher, WaitableEvent* event) { |
| 194 | // |event| is manual, so the second watcher will run |
| 195 | // immediately. |
| 196 | watcher->StartWatching(event, BindOnce(&QuitWhenSignaled), |
| 197 | SequencedTaskRunnerHandle::Get()); |
| 198 | }, |
| 199 | &watcher), |
| 200 | SequencedTaskRunnerHandle::Get()); |
Robert Sesek | b44f896 | 2017-06-30 21:21:13 | [diff] [blame] | 201 | |
| 202 | event.Signal(); |
| 203 | |
| 204 | RunLoop().Run(); |
| 205 | } |
| 206 | |
Robert Sesek | cfab3ad | 2017-08-01 16:25:43 | [diff] [blame] | 207 | TEST_P(WaitableEventWatcherTest, MultipleWatchersManual) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 208 | test::TaskEnvironment task_environment(GetParam()); |
Robert Sesek | cfab3ad | 2017-08-01 16:25:43 | [diff] [blame] | 209 | |
| 210 | WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, |
| 211 | WaitableEvent::InitialState::NOT_SIGNALED); |
| 212 | |
| 213 | int counter1 = 0; |
| 214 | int counter2 = 0; |
| 215 | |
| 216 | auto callback = [](RunLoop* run_loop, int* counter, WaitableEvent* event) { |
| 217 | ++(*counter); |
| 218 | run_loop->QuitWhenIdle(); |
| 219 | }; |
| 220 | |
| 221 | RunLoop run_loop; |
| 222 | |
| 223 | WaitableEventWatcher watcher1; |
| 224 | watcher1.StartWatching( |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 225 | &event, BindOnce(callback, Unretained(&run_loop), Unretained(&counter1)), |
| 226 | SequencedTaskRunnerHandle::Get()); |
Robert Sesek | cfab3ad | 2017-08-01 16:25:43 | [diff] [blame] | 227 | |
| 228 | WaitableEventWatcher watcher2; |
| 229 | watcher2.StartWatching( |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 230 | &event, BindOnce(callback, Unretained(&run_loop), Unretained(&counter2)), |
| 231 | SequencedTaskRunnerHandle::Get()); |
Robert Sesek | cfab3ad | 2017-08-01 16:25:43 | [diff] [blame] | 232 | |
| 233 | event.Signal(); |
| 234 | run_loop.Run(); |
| 235 | |
| 236 | EXPECT_EQ(1, counter1); |
| 237 | EXPECT_EQ(1, counter2); |
| 238 | EXPECT_TRUE(event.IsSignaled()); |
| 239 | } |
| 240 | |
| 241 | // Tests that only one async waiter gets called back for an auto-reset event. |
| 242 | TEST_P(WaitableEventWatcherTest, MultipleWatchersAutomatic) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 243 | test::TaskEnvironment task_environment(GetParam()); |
Robert Sesek | cfab3ad | 2017-08-01 16:25:43 | [diff] [blame] | 244 | |
| 245 | WaitableEvent event(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 246 | WaitableEvent::InitialState::NOT_SIGNALED); |
| 247 | |
| 248 | int counter1 = 0; |
| 249 | int counter2 = 0; |
| 250 | |
| 251 | auto callback = [](RunLoop** run_loop, int* counter, WaitableEvent* event) { |
| 252 | ++(*counter); |
| 253 | (*run_loop)->QuitWhenIdle(); |
| 254 | }; |
| 255 | |
| 256 | // The same RunLoop instance cannot be Run more than once, and it is |
| 257 | // undefined which watcher will get called back first. Have the callback |
| 258 | // dereference this pointer to quit the loop, which will be updated on each |
| 259 | // Run. |
| 260 | RunLoop* current_run_loop; |
| 261 | |
| 262 | WaitableEventWatcher watcher1; |
| 263 | watcher1.StartWatching( |
| 264 | &event, |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 265 | BindOnce(callback, Unretained(¤t_run_loop), Unretained(&counter1)), |
| 266 | SequencedTaskRunnerHandle::Get()); |
Robert Sesek | cfab3ad | 2017-08-01 16:25:43 | [diff] [blame] | 267 | |
| 268 | WaitableEventWatcher watcher2; |
| 269 | watcher2.StartWatching( |
| 270 | &event, |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 271 | BindOnce(callback, Unretained(¤t_run_loop), Unretained(&counter2)), |
| 272 | SequencedTaskRunnerHandle::Get()); |
Robert Sesek | cfab3ad | 2017-08-01 16:25:43 | [diff] [blame] | 273 | |
| 274 | event.Signal(); |
| 275 | { |
| 276 | RunLoop run_loop; |
| 277 | current_run_loop = &run_loop; |
| 278 | run_loop.Run(); |
| 279 | } |
| 280 | |
| 281 | // Only one of the waiters should have been signaled. |
| 282 | EXPECT_TRUE((counter1 == 1) ^ (counter2 == 1)); |
| 283 | |
| 284 | EXPECT_FALSE(event.IsSignaled()); |
| 285 | |
| 286 | event.Signal(); |
| 287 | { |
| 288 | RunLoop run_loop; |
| 289 | current_run_loop = &run_loop; |
| 290 | run_loop.Run(); |
| 291 | } |
| 292 | |
| 293 | EXPECT_FALSE(event.IsSignaled()); |
| 294 | |
| 295 | // The other watcher should have been signaled. |
| 296 | EXPECT_EQ(1, counter1); |
| 297 | EXPECT_EQ(1, counter2); |
| 298 | } |
| 299 | |
Robert Sesek | 3b333377 | 2017-06-30 18:21:56 | [diff] [blame] | 300 | // To help detect errors around deleting WaitableEventWatcher, an additional |
| 301 | // bool parameter is used to test sleeping between watching and deletion. |
| 302 | class WaitableEventWatcherDeletionTest |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 303 | : public testing::TestWithParam< |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 304 | std::tuple<test::TaskEnvironment::MainThreadType, bool>> {}; |
Robert Sesek | 3b333377 | 2017-06-30 18:21:56 | [diff] [blame] | 305 | |
| 306 | TEST_P(WaitableEventWatcherDeletionTest, DeleteUnder) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 307 | test::TaskEnvironment::MainThreadType main_thread_type; |
Robert Sesek | 3b333377 | 2017-06-30 18:21:56 | [diff] [blame] | 308 | bool delay_after_delete; |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 309 | std::tie(main_thread_type, delay_after_delete) = GetParam(); |
Robert Sesek | 3b333377 | 2017-06-30 18:21:56 | [diff] [blame] | 310 | |
[email protected] | c891ab9 | 2009-03-26 18:28:19 | [diff] [blame] | 311 | // Delete the WaitableEvent out from under the Watcher. This is explictly |
| 312 | // allowed by the interface. |
| 313 | |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 314 | test::TaskEnvironment task_environment(main_thread_type); |
[email protected] | c891ab9 | 2009-03-26 18:28:19 | [diff] [blame] | 315 | |
| 316 | { |
| 317 | WaitableEventWatcher watcher; |
| 318 | |
atuchin | 54d8315 | 2017-05-10 06:30:01 | [diff] [blame] | 319 | auto* event = new WaitableEvent(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 320 | WaitableEvent::InitialState::NOT_SIGNALED); |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 321 | |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 322 | watcher.StartWatching(event, BindOnce(&QuitWhenSignaled), |
| 323 | SequencedTaskRunnerHandle::Get()); |
atuchin | 54d8315 | 2017-05-10 06:30:01 | [diff] [blame] | 324 | |
| 325 | if (delay_after_delete) { |
| 326 | // On Windows that sleep() improves the chance to catch some problems. |
| 327 | // It postpones the dtor |watcher| (which immediately cancel the waiting) |
| 328 | // and gives some time to run to a created background thread. |
| 329 | // Unfortunately, that thread is under OS control and we can't |
| 330 | // manipulate it directly. |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 331 | PlatformThread::Sleep(TimeDelta::FromMilliseconds(30)); |
atuchin | 54d8315 | 2017-05-10 06:30:01 | [diff] [blame] | 332 | } |
| 333 | |
[email protected] | c891ab9 | 2009-03-26 18:28:19 | [diff] [blame] | 334 | delete event; |
| 335 | } |
| 336 | } |
| 337 | |
Robert Sesek | 3b333377 | 2017-06-30 18:21:56 | [diff] [blame] | 338 | TEST_P(WaitableEventWatcherDeletionTest, SignalAndDelete) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 339 | test::TaskEnvironment::MainThreadType main_thread_type; |
Robert Sesek | 3b333377 | 2017-06-30 18:21:56 | [diff] [blame] | 340 | bool delay_after_delete; |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 341 | std::tie(main_thread_type, delay_after_delete) = GetParam(); |
Robert Sesek | 3b333377 | 2017-06-30 18:21:56 | [diff] [blame] | 342 | |
atuchin | 54d8315 | 2017-05-10 06:30:01 | [diff] [blame] | 343 | // Signal and immediately delete the WaitableEvent out from under the Watcher. |
| 344 | |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 345 | test::TaskEnvironment task_environment(main_thread_type); |
atuchin | 54d8315 | 2017-05-10 06:30:01 | [diff] [blame] | 346 | |
| 347 | { |
| 348 | WaitableEventWatcher watcher; |
| 349 | |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 350 | auto event = std::make_unique<WaitableEvent>( |
atuchin | 54d8315 | 2017-05-10 06:30:01 | [diff] [blame] | 351 | WaitableEvent::ResetPolicy::AUTOMATIC, |
| 352 | WaitableEvent::InitialState::NOT_SIGNALED); |
| 353 | |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 354 | watcher.StartWatching(event.get(), BindOnce(&QuitWhenSignaled), |
| 355 | SequencedTaskRunnerHandle::Get()); |
atuchin | 54d8315 | 2017-05-10 06:30:01 | [diff] [blame] | 356 | event->Signal(); |
| 357 | event.reset(); |
| 358 | |
| 359 | if (delay_after_delete) { |
| 360 | // On Windows that sleep() improves the chance to catch some problems. |
| 361 | // It postpones the dtor |watcher| (which immediately cancel the waiting) |
| 362 | // and gives some time to run to a created background thread. |
| 363 | // Unfortunately, that thread is under OS control and we can't |
| 364 | // manipulate it directly. |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 365 | PlatformThread::Sleep(TimeDelta::FromMilliseconds(30)); |
atuchin | 54d8315 | 2017-05-10 06:30:01 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | // Wait for the watcher callback. |
| 369 | RunLoop().Run(); |
| 370 | } |
| 371 | } |
| 372 | |
Robert Sesek | 6d38e78 | 2017-07-13 00:46:50 | [diff] [blame] | 373 | // Tests deleting the WaitableEventWatcher between signaling the event and |
| 374 | // when the callback should be run. |
| 375 | TEST_P(WaitableEventWatcherDeletionTest, DeleteWatcherBeforeCallback) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 376 | test::TaskEnvironment::MainThreadType main_thread_type; |
Robert Sesek | 6d38e78 | 2017-07-13 00:46:50 | [diff] [blame] | 377 | bool delay_after_delete; |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 378 | std::tie(main_thread_type, delay_after_delete) = GetParam(); |
Robert Sesek | 6d38e78 | 2017-07-13 00:46:50 | [diff] [blame] | 379 | |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 380 | test::TaskEnvironment task_environment(main_thread_type); |
Robert Sesek | 6d38e78 | 2017-07-13 00:46:50 | [diff] [blame] | 381 | scoped_refptr<SingleThreadTaskRunner> task_runner = |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 382 | ThreadTaskRunnerHandle::Get(); |
Robert Sesek | 6d38e78 | 2017-07-13 00:46:50 | [diff] [blame] | 383 | |
| 384 | // Flag used to esnure that the |watcher_callback| never runs. |
| 385 | bool did_callback = false; |
| 386 | |
| 387 | WaitableEvent event(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 388 | WaitableEvent::InitialState::NOT_SIGNALED); |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 389 | auto watcher = std::make_unique<WaitableEventWatcher>(); |
Robert Sesek | 6d38e78 | 2017-07-13 00:46:50 | [diff] [blame] | 390 | |
| 391 | // Queue up a series of tasks: |
| 392 | // 1. StartWatching the WaitableEvent |
| 393 | // 2. Signal the event (which will result in another task getting posted to |
| 394 | // the |task_runner|) |
| 395 | // 3. Delete the WaitableEventWatcher |
| 396 | // 4. WaitableEventWatcher callback should run (from #2) |
| 397 | |
| 398 | WaitableEventWatcher::EventCallback watcher_callback = BindOnce( |
| 399 | [](bool* did_callback, WaitableEvent*) { |
| 400 | *did_callback = true; |
| 401 | }, |
| 402 | Unretained(&did_callback)); |
| 403 | |
| 404 | task_runner->PostTask( |
| 405 | FROM_HERE, BindOnce(IgnoreResult(&WaitableEventWatcher::StartWatching), |
| 406 | Unretained(watcher.get()), Unretained(&event), |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 407 | std::move(watcher_callback), task_runner)); |
Robert Sesek | 6d38e78 | 2017-07-13 00:46:50 | [diff] [blame] | 408 | task_runner->PostTask(FROM_HERE, |
| 409 | BindOnce(&WaitableEvent::Signal, Unretained(&event))); |
| 410 | task_runner->DeleteSoon(FROM_HERE, std::move(watcher)); |
| 411 | if (delay_after_delete) { |
| 412 | task_runner->PostTask(FROM_HERE, BindOnce(&PlatformThread::Sleep, |
| 413 | TimeDelta::FromMilliseconds(30))); |
| 414 | } |
| 415 | |
| 416 | RunLoop().RunUntilIdle(); |
| 417 | |
| 418 | EXPECT_FALSE(did_callback); |
| 419 | } |
| 420 | |
Victor Costan | 033b9ac | 2019-01-29 00:52:16 | [diff] [blame] | 421 | INSTANTIATE_TEST_SUITE_P(, |
| 422 | WaitableEventWatcherTest, |
| 423 | testing::ValuesIn(testing_main_threads)); |
[email protected] | b57d33c5 | 2009-01-15 22:58:53 | [diff] [blame] | 424 | |
Victor Costan | 033b9ac | 2019-01-29 00:52:16 | [diff] [blame] | 425 | INSTANTIATE_TEST_SUITE_P( |
Robert Sesek | 3b333377 | 2017-06-30 18:21:56 | [diff] [blame] | 426 | , |
| 427 | WaitableEventWatcherDeletionTest, |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 428 | testing::Combine(testing::ValuesIn(testing_main_threads), testing::Bool())); |
[email protected] | 44f9c95 | 2011-01-02 06:05:39 | [diff] [blame] | 429 | |
| 430 | } // namespace base |