[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 | |
Greg Thompson | 96a3910 | 2021-03-16 10:11:08 | [diff] [blame] | 207 | // Disabled due to flakes; see https://crbug.com/1188547. |
| 208 | TEST_P(WaitableEventWatcherTest, DISABLED_MultipleWatchersManual) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 209 | test::TaskEnvironment task_environment(GetParam()); |
Robert Sesek | cfab3ad | 2017-08-01 16:25:43 | [diff] [blame] | 210 | |
| 211 | WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, |
| 212 | WaitableEvent::InitialState::NOT_SIGNALED); |
| 213 | |
| 214 | int counter1 = 0; |
| 215 | int counter2 = 0; |
| 216 | |
| 217 | auto callback = [](RunLoop* run_loop, int* counter, WaitableEvent* event) { |
| 218 | ++(*counter); |
| 219 | run_loop->QuitWhenIdle(); |
| 220 | }; |
| 221 | |
| 222 | RunLoop run_loop; |
| 223 | |
| 224 | WaitableEventWatcher watcher1; |
| 225 | watcher1.StartWatching( |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 226 | &event, BindOnce(callback, Unretained(&run_loop), Unretained(&counter1)), |
| 227 | SequencedTaskRunnerHandle::Get()); |
Robert Sesek | cfab3ad | 2017-08-01 16:25:43 | [diff] [blame] | 228 | |
| 229 | WaitableEventWatcher watcher2; |
| 230 | watcher2.StartWatching( |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 231 | &event, BindOnce(callback, Unretained(&run_loop), Unretained(&counter2)), |
| 232 | SequencedTaskRunnerHandle::Get()); |
Robert Sesek | cfab3ad | 2017-08-01 16:25:43 | [diff] [blame] | 233 | |
| 234 | event.Signal(); |
| 235 | run_loop.Run(); |
| 236 | |
| 237 | EXPECT_EQ(1, counter1); |
| 238 | EXPECT_EQ(1, counter2); |
| 239 | EXPECT_TRUE(event.IsSignaled()); |
| 240 | } |
| 241 | |
| 242 | // Tests that only one async waiter gets called back for an auto-reset event. |
| 243 | TEST_P(WaitableEventWatcherTest, MultipleWatchersAutomatic) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 244 | test::TaskEnvironment task_environment(GetParam()); |
Robert Sesek | cfab3ad | 2017-08-01 16:25:43 | [diff] [blame] | 245 | |
| 246 | WaitableEvent event(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 247 | WaitableEvent::InitialState::NOT_SIGNALED); |
| 248 | |
| 249 | int counter1 = 0; |
| 250 | int counter2 = 0; |
| 251 | |
| 252 | auto callback = [](RunLoop** run_loop, int* counter, WaitableEvent* event) { |
| 253 | ++(*counter); |
| 254 | (*run_loop)->QuitWhenIdle(); |
| 255 | }; |
| 256 | |
| 257 | // The same RunLoop instance cannot be Run more than once, and it is |
| 258 | // undefined which watcher will get called back first. Have the callback |
| 259 | // dereference this pointer to quit the loop, which will be updated on each |
| 260 | // Run. |
| 261 | RunLoop* current_run_loop; |
| 262 | |
| 263 | WaitableEventWatcher watcher1; |
| 264 | watcher1.StartWatching( |
| 265 | &event, |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 266 | BindOnce(callback, Unretained(¤t_run_loop), Unretained(&counter1)), |
| 267 | SequencedTaskRunnerHandle::Get()); |
Robert Sesek | cfab3ad | 2017-08-01 16:25:43 | [diff] [blame] | 268 | |
| 269 | WaitableEventWatcher watcher2; |
| 270 | watcher2.StartWatching( |
| 271 | &event, |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 272 | BindOnce(callback, Unretained(¤t_run_loop), Unretained(&counter2)), |
| 273 | SequencedTaskRunnerHandle::Get()); |
Robert Sesek | cfab3ad | 2017-08-01 16:25:43 | [diff] [blame] | 274 | |
| 275 | event.Signal(); |
| 276 | { |
| 277 | RunLoop run_loop; |
| 278 | current_run_loop = &run_loop; |
| 279 | run_loop.Run(); |
| 280 | } |
| 281 | |
| 282 | // Only one of the waiters should have been signaled. |
| 283 | EXPECT_TRUE((counter1 == 1) ^ (counter2 == 1)); |
| 284 | |
| 285 | EXPECT_FALSE(event.IsSignaled()); |
| 286 | |
| 287 | event.Signal(); |
| 288 | { |
| 289 | RunLoop run_loop; |
| 290 | current_run_loop = &run_loop; |
| 291 | run_loop.Run(); |
| 292 | } |
| 293 | |
| 294 | EXPECT_FALSE(event.IsSignaled()); |
| 295 | |
| 296 | // The other watcher should have been signaled. |
| 297 | EXPECT_EQ(1, counter1); |
| 298 | EXPECT_EQ(1, counter2); |
| 299 | } |
| 300 | |
Robert Sesek | 3b333377 | 2017-06-30 18:21:56 | [diff] [blame] | 301 | // To help detect errors around deleting WaitableEventWatcher, an additional |
| 302 | // bool parameter is used to test sleeping between watching and deletion. |
| 303 | class WaitableEventWatcherDeletionTest |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 304 | : public testing::TestWithParam< |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 305 | std::tuple<test::TaskEnvironment::MainThreadType, bool>> {}; |
Robert Sesek | 3b333377 | 2017-06-30 18:21:56 | [diff] [blame] | 306 | |
| 307 | TEST_P(WaitableEventWatcherDeletionTest, DeleteUnder) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 308 | test::TaskEnvironment::MainThreadType main_thread_type; |
Robert Sesek | 3b333377 | 2017-06-30 18:21:56 | [diff] [blame] | 309 | bool delay_after_delete; |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 310 | std::tie(main_thread_type, delay_after_delete) = GetParam(); |
Robert Sesek | 3b333377 | 2017-06-30 18:21:56 | [diff] [blame] | 311 | |
[email protected] | c891ab9 | 2009-03-26 18:28:19 | [diff] [blame] | 312 | // Delete the WaitableEvent out from under the Watcher. This is explictly |
| 313 | // allowed by the interface. |
| 314 | |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 315 | test::TaskEnvironment task_environment(main_thread_type); |
[email protected] | c891ab9 | 2009-03-26 18:28:19 | [diff] [blame] | 316 | |
| 317 | { |
| 318 | WaitableEventWatcher watcher; |
| 319 | |
atuchin | 54d8315 | 2017-05-10 06:30:01 | [diff] [blame] | 320 | auto* event = new WaitableEvent(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 321 | WaitableEvent::InitialState::NOT_SIGNALED); |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 322 | |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 323 | watcher.StartWatching(event, BindOnce(&QuitWhenSignaled), |
| 324 | SequencedTaskRunnerHandle::Get()); |
atuchin | 54d8315 | 2017-05-10 06:30:01 | [diff] [blame] | 325 | |
| 326 | if (delay_after_delete) { |
| 327 | // On Windows that sleep() improves the chance to catch some problems. |
| 328 | // It postpones the dtor |watcher| (which immediately cancel the waiting) |
| 329 | // and gives some time to run to a created background thread. |
| 330 | // Unfortunately, that thread is under OS control and we can't |
| 331 | // manipulate it directly. |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 332 | PlatformThread::Sleep(TimeDelta::FromMilliseconds(30)); |
atuchin | 54d8315 | 2017-05-10 06:30:01 | [diff] [blame] | 333 | } |
| 334 | |
[email protected] | c891ab9 | 2009-03-26 18:28:19 | [diff] [blame] | 335 | delete event; |
| 336 | } |
| 337 | } |
| 338 | |
Robert Sesek | 3b333377 | 2017-06-30 18:21:56 | [diff] [blame] | 339 | TEST_P(WaitableEventWatcherDeletionTest, SignalAndDelete) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 340 | test::TaskEnvironment::MainThreadType main_thread_type; |
Robert Sesek | 3b333377 | 2017-06-30 18:21:56 | [diff] [blame] | 341 | bool delay_after_delete; |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 342 | std::tie(main_thread_type, delay_after_delete) = GetParam(); |
Robert Sesek | 3b333377 | 2017-06-30 18:21:56 | [diff] [blame] | 343 | |
atuchin | 54d8315 | 2017-05-10 06:30:01 | [diff] [blame] | 344 | // Signal and immediately delete the WaitableEvent out from under the Watcher. |
| 345 | |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 346 | test::TaskEnvironment task_environment(main_thread_type); |
atuchin | 54d8315 | 2017-05-10 06:30:01 | [diff] [blame] | 347 | |
| 348 | { |
| 349 | WaitableEventWatcher watcher; |
| 350 | |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 351 | auto event = std::make_unique<WaitableEvent>( |
atuchin | 54d8315 | 2017-05-10 06:30:01 | [diff] [blame] | 352 | WaitableEvent::ResetPolicy::AUTOMATIC, |
| 353 | WaitableEvent::InitialState::NOT_SIGNALED); |
| 354 | |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 355 | watcher.StartWatching(event.get(), BindOnce(&QuitWhenSignaled), |
| 356 | SequencedTaskRunnerHandle::Get()); |
atuchin | 54d8315 | 2017-05-10 06:30:01 | [diff] [blame] | 357 | event->Signal(); |
| 358 | event.reset(); |
| 359 | |
| 360 | if (delay_after_delete) { |
| 361 | // On Windows that sleep() improves the chance to catch some problems. |
| 362 | // It postpones the dtor |watcher| (which immediately cancel the waiting) |
| 363 | // and gives some time to run to a created background thread. |
| 364 | // Unfortunately, that thread is under OS control and we can't |
| 365 | // manipulate it directly. |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 366 | PlatformThread::Sleep(TimeDelta::FromMilliseconds(30)); |
atuchin | 54d8315 | 2017-05-10 06:30:01 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | // Wait for the watcher callback. |
| 370 | RunLoop().Run(); |
| 371 | } |
| 372 | } |
| 373 | |
Robert Sesek | 6d38e78 | 2017-07-13 00:46:50 | [diff] [blame] | 374 | // Tests deleting the WaitableEventWatcher between signaling the event and |
| 375 | // when the callback should be run. |
| 376 | TEST_P(WaitableEventWatcherDeletionTest, DeleteWatcherBeforeCallback) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 377 | test::TaskEnvironment::MainThreadType main_thread_type; |
Robert Sesek | 6d38e78 | 2017-07-13 00:46:50 | [diff] [blame] | 378 | bool delay_after_delete; |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 379 | std::tie(main_thread_type, delay_after_delete) = GetParam(); |
Robert Sesek | 6d38e78 | 2017-07-13 00:46:50 | [diff] [blame] | 380 | |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 381 | test::TaskEnvironment task_environment(main_thread_type); |
Robert Sesek | 6d38e78 | 2017-07-13 00:46:50 | [diff] [blame] | 382 | scoped_refptr<SingleThreadTaskRunner> task_runner = |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 383 | ThreadTaskRunnerHandle::Get(); |
Robert Sesek | 6d38e78 | 2017-07-13 00:46:50 | [diff] [blame] | 384 | |
| 385 | // Flag used to esnure that the |watcher_callback| never runs. |
| 386 | bool did_callback = false; |
| 387 | |
| 388 | WaitableEvent event(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 389 | WaitableEvent::InitialState::NOT_SIGNALED); |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 390 | auto watcher = std::make_unique<WaitableEventWatcher>(); |
Robert Sesek | 6d38e78 | 2017-07-13 00:46:50 | [diff] [blame] | 391 | |
| 392 | // Queue up a series of tasks: |
| 393 | // 1. StartWatching the WaitableEvent |
| 394 | // 2. Signal the event (which will result in another task getting posted to |
| 395 | // the |task_runner|) |
| 396 | // 3. Delete the WaitableEventWatcher |
| 397 | // 4. WaitableEventWatcher callback should run (from #2) |
| 398 | |
| 399 | WaitableEventWatcher::EventCallback watcher_callback = BindOnce( |
| 400 | [](bool* did_callback, WaitableEvent*) { |
| 401 | *did_callback = true; |
| 402 | }, |
| 403 | Unretained(&did_callback)); |
| 404 | |
| 405 | task_runner->PostTask( |
| 406 | FROM_HERE, BindOnce(IgnoreResult(&WaitableEventWatcher::StartWatching), |
| 407 | Unretained(watcher.get()), Unretained(&event), |
Hajime Hoshi | 138652c9 | 2018-01-12 15:11:44 | [diff] [blame] | 408 | std::move(watcher_callback), task_runner)); |
Robert Sesek | 6d38e78 | 2017-07-13 00:46:50 | [diff] [blame] | 409 | task_runner->PostTask(FROM_HERE, |
| 410 | BindOnce(&WaitableEvent::Signal, Unretained(&event))); |
| 411 | task_runner->DeleteSoon(FROM_HERE, std::move(watcher)); |
| 412 | if (delay_after_delete) { |
| 413 | task_runner->PostTask(FROM_HERE, BindOnce(&PlatformThread::Sleep, |
| 414 | TimeDelta::FromMilliseconds(30))); |
| 415 | } |
| 416 | |
| 417 | RunLoop().RunUntilIdle(); |
| 418 | |
| 419 | EXPECT_FALSE(did_callback); |
| 420 | } |
| 421 | |
Ilia Samsonov | a2885f7 | 2019-11-20 22:16:58 | [diff] [blame] | 422 | INSTANTIATE_TEST_SUITE_P(All, |
Victor Costan | 033b9ac | 2019-01-29 00:52:16 | [diff] [blame] | 423 | WaitableEventWatcherTest, |
| 424 | testing::ValuesIn(testing_main_threads)); |
[email protected] | b57d33c5 | 2009-01-15 22:58:53 | [diff] [blame] | 425 | |
Victor Costan | 033b9ac | 2019-01-29 00:52:16 | [diff] [blame] | 426 | INSTANTIATE_TEST_SUITE_P( |
Ilia Samsonov | a2885f7 | 2019-11-20 22:16:58 | [diff] [blame] | 427 | All, |
Robert Sesek | 3b333377 | 2017-06-30 18:21:56 | [diff] [blame] | 428 | WaitableEventWatcherDeletionTest, |
Sami Kyostila | 3f49cb57 | 2018-11-19 13:01:09 | [diff] [blame] | 429 | testing::Combine(testing::ValuesIn(testing_main_threads), testing::Bool())); |
[email protected] | 44f9c95 | 2011-01-02 06:05:39 | [diff] [blame] | 430 | |
| 431 | } // namespace base |