| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/task_scheduler/task_tracker.h" | 5 #include "base/task_scheduler/task_tracker.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #include "base/synchronization/waitable_event.h" | 23 #include "base/synchronization/waitable_event.h" |
| 24 #include "base/task_scheduler/scheduler_lock.h" | 24 #include "base/task_scheduler/scheduler_lock.h" |
| 25 #include "base/task_scheduler/task.h" | 25 #include "base/task_scheduler/task.h" |
| 26 #include "base/task_scheduler/task_traits.h" | 26 #include "base/task_scheduler/task_traits.h" |
| 27 #include "base/test/gtest_util.h" | 27 #include "base/test/gtest_util.h" |
| 28 #include "base/test/test_simple_task_runner.h" | 28 #include "base/test/test_simple_task_runner.h" |
| 29 #include "base/test/test_timeouts.h" | 29 #include "base/test/test_timeouts.h" |
| 30 #include "base/threading/platform_thread.h" | 30 #include "base/threading/platform_thread.h" |
| 31 #include "base/threading/sequenced_task_runner_handle.h" | 31 #include "base/threading/sequenced_task_runner_handle.h" |
| 32 #include "base/threading/simple_thread.h" | 32 #include "base/threading/simple_thread.h" |
| 33 #include "base/threading/thread.h" |
| 33 #include "base/threading/thread_restrictions.h" | 34 #include "base/threading/thread_restrictions.h" |
| 34 #include "base/threading/thread_task_runner_handle.h" | 35 #include "base/threading/thread_task_runner_handle.h" |
| 36 #include "build/build_config.h" |
| 35 #include "testing/gtest/include/gtest/gtest.h" | 37 #include "testing/gtest/include/gtest/gtest.h" |
| 36 | 38 |
| 37 namespace base { | 39 namespace base { |
| 38 namespace internal { | 40 namespace internal { |
| 39 | 41 |
| 40 namespace { | 42 namespace { |
| 41 | 43 |
| 42 constexpr size_t kLoadTestNumIterations = 100; | 44 constexpr size_t kLoadTestNumIterations = 100; |
| 43 | 45 |
| 44 // Invokes a closure asynchronously. | 46 // Invokes a closure asynchronously. |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 ThreadRestrictions::SetSingletonAllowed(previous_value_); | 133 ThreadRestrictions::SetSingletonAllowed(previous_value_); |
| 132 } | 134 } |
| 133 | 135 |
| 134 private: | 136 private: |
| 135 const bool previous_value_; | 137 const bool previous_value_; |
| 136 }; | 138 }; |
| 137 | 139 |
| 138 class TaskSchedulerTaskTrackerTest | 140 class TaskSchedulerTaskTrackerTest |
| 139 : public testing::TestWithParam<TaskShutdownBehavior> { | 141 : public testing::TestWithParam<TaskShutdownBehavior> { |
| 140 protected: | 142 protected: |
| 141 TaskSchedulerTaskTrackerTest() = default; | 143 TaskSchedulerTaskTrackerTest() |
| 144 #if defined(OS_POSIX) && !defined(OS_NACL_SFI) |
| 145 : service_thread_("TaskSchedulerServiceThread") |
| 146 #endif |
| 147 { |
| 148 } |
| 149 |
| 150 void SetUp() override { |
| 151 #if defined(OS_POSIX) && !defined(OS_NACL_SFI) |
| 152 constexpr size_t kDefaultStackSize = 0; |
| 153 ASSERT_TRUE(service_thread_.StartWithOptions( |
| 154 Thread::Options(MessageLoop::TYPE_IO, kDefaultStackSize))); |
| 155 #endif |
| 156 tracker_ = MakeUnique<TaskTracker>( |
| 157 #if defined(OS_POSIX) && !defined(OS_NACL_SFI) |
| 158 static_cast<MessageLoopForIO*>(service_thread_.message_loop()) |
| 159 #endif |
| 160 ); |
| 161 } |
| 142 | 162 |
| 143 // Creates a task with |shutdown_behavior|. | 163 // Creates a task with |shutdown_behavior|. |
| 144 std::unique_ptr<Task> CreateTask(TaskShutdownBehavior shutdown_behavior) { | 164 std::unique_ptr<Task> CreateTask(TaskShutdownBehavior shutdown_behavior) { |
| 145 return MakeUnique<Task>( | 165 return MakeUnique<Task>( |
| 146 FROM_HERE, | 166 FROM_HERE, |
| 147 Bind(&TaskSchedulerTaskTrackerTest::RunTaskCallback, Unretained(this)), | 167 Bind(&TaskSchedulerTaskTrackerTest::RunTaskCallback, Unretained(this)), |
| 148 TaskTraits().WithShutdownBehavior(shutdown_behavior), TimeDelta()); | 168 TaskTraits().WithShutdownBehavior(shutdown_behavior), TimeDelta()); |
| 149 } | 169 } |
| 150 | 170 |
| 151 // Calls tracker_->Shutdown() on a new thread. When this returns, Shutdown() | 171 // Calls tracker_->Shutdown() on a new thread. When this returns, Shutdown() |
| 152 // method has been entered on the new thread, but it hasn't necessarily | 172 // method has been entered on the new thread, but it hasn't necessarily |
| 153 // returned. | 173 // returned. |
| 154 void CallShutdownAsync() { | 174 void CallShutdownAsync() { |
| 155 ASSERT_FALSE(thread_calling_shutdown_); | 175 ASSERT_FALSE(thread_calling_shutdown_); |
| 156 thread_calling_shutdown_.reset(new CallbackThread( | 176 thread_calling_shutdown_.reset(new CallbackThread( |
| 157 Bind(&TaskTracker::Shutdown, Unretained(&tracker_)))); | 177 Bind(&TaskTracker::Shutdown, Unretained(tracker_.get())))); |
| 158 thread_calling_shutdown_->Start(); | 178 thread_calling_shutdown_->Start(); |
| 159 while (!tracker_.HasShutdownStarted()) | 179 while (!tracker_->HasShutdownStarted()) |
| 160 PlatformThread::YieldCurrentThread(); | 180 PlatformThread::YieldCurrentThread(); |
| 161 } | 181 } |
| 162 | 182 |
| 163 void WaitForAsyncIsShutdownComplete() { | 183 void WaitForAsyncIsShutdownComplete() { |
| 164 ASSERT_TRUE(thread_calling_shutdown_); | 184 ASSERT_TRUE(thread_calling_shutdown_); |
| 165 thread_calling_shutdown_->Join(); | 185 thread_calling_shutdown_->Join(); |
| 166 EXPECT_TRUE(thread_calling_shutdown_->has_returned()); | 186 EXPECT_TRUE(thread_calling_shutdown_->has_returned()); |
| 167 EXPECT_TRUE(tracker_.IsShutdownComplete()); | 187 EXPECT_TRUE(tracker_->IsShutdownComplete()); |
| 168 } | 188 } |
| 169 | 189 |
| 170 void VerifyAsyncShutdownInProgress() { | 190 void VerifyAsyncShutdownInProgress() { |
| 171 ASSERT_TRUE(thread_calling_shutdown_); | 191 ASSERT_TRUE(thread_calling_shutdown_); |
| 172 EXPECT_FALSE(thread_calling_shutdown_->has_returned()); | 192 EXPECT_FALSE(thread_calling_shutdown_->has_returned()); |
| 173 EXPECT_TRUE(tracker_.HasShutdownStarted()); | 193 EXPECT_TRUE(tracker_->HasShutdownStarted()); |
| 174 EXPECT_FALSE(tracker_.IsShutdownComplete()); | 194 EXPECT_FALSE(tracker_->IsShutdownComplete()); |
| 175 } | 195 } |
| 176 | 196 |
| 177 // Calls tracker_->Flush() on a new thread. | 197 // Calls tracker_->Flush() on a new thread. |
| 178 void CallFlushAsync() { | 198 void CallFlushAsync() { |
| 179 ASSERT_FALSE(thread_calling_flush_); | 199 ASSERT_FALSE(thread_calling_flush_); |
| 180 thread_calling_flush_.reset( | 200 thread_calling_flush_.reset(new CallbackThread( |
| 181 new CallbackThread(Bind(&TaskTracker::Flush, Unretained(&tracker_)))); | 201 Bind(&TaskTracker::Flush, Unretained(tracker_.get())))); |
| 182 thread_calling_flush_->Start(); | 202 thread_calling_flush_->Start(); |
| 183 } | 203 } |
| 184 | 204 |
| 185 void WaitForAsyncFlushReturned() { | 205 void WaitForAsyncFlushReturned() { |
| 186 ASSERT_TRUE(thread_calling_flush_); | 206 ASSERT_TRUE(thread_calling_flush_); |
| 187 thread_calling_flush_->Join(); | 207 thread_calling_flush_->Join(); |
| 188 EXPECT_TRUE(thread_calling_flush_->has_returned()); | 208 EXPECT_TRUE(thread_calling_flush_->has_returned()); |
| 189 } | 209 } |
| 190 | 210 |
| 191 void VerifyAsyncFlushInProgress() { | 211 void VerifyAsyncFlushInProgress() { |
| 192 ASSERT_TRUE(thread_calling_flush_); | 212 ASSERT_TRUE(thread_calling_flush_); |
| 193 EXPECT_FALSE(thread_calling_flush_->has_returned()); | 213 EXPECT_FALSE(thread_calling_flush_->has_returned()); |
| 194 } | 214 } |
| 195 | 215 |
| 196 size_t NumTasksExecuted() { | 216 size_t NumTasksExecuted() { |
| 197 AutoSchedulerLock auto_lock(lock_); | 217 AutoSchedulerLock auto_lock(lock_); |
| 198 return num_tasks_executed_; | 218 return num_tasks_executed_; |
| 199 } | 219 } |
| 200 | 220 |
| 201 TaskTracker tracker_; | 221 #if defined(OS_POSIX) && !defined(OS_NACL_SFI) |
| 222 private: |
| 223 Thread service_thread_; |
| 224 #endif |
| 225 |
| 226 protected: |
| 227 std::unique_ptr<TaskTracker> tracker_; |
| 202 | 228 |
| 203 private: | 229 private: |
| 204 void RunTaskCallback() { | 230 void RunTaskCallback() { |
| 205 AutoSchedulerLock auto_lock(lock_); | 231 AutoSchedulerLock auto_lock(lock_); |
| 206 ++num_tasks_executed_; | 232 ++num_tasks_executed_; |
| 207 } | 233 } |
| 208 | 234 |
| 209 std::unique_ptr<CallbackThread> thread_calling_shutdown_; | 235 std::unique_ptr<CallbackThread> thread_calling_shutdown_; |
| 210 std::unique_ptr<CallbackThread> thread_calling_flush_; | 236 std::unique_ptr<CallbackThread> thread_calling_flush_; |
| 211 | 237 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 240 SCOPED_TRACE(""); \ | 266 SCOPED_TRACE(""); \ |
| 241 VerifyAsyncFlushInProgress(); \ | 267 VerifyAsyncFlushInProgress(); \ |
| 242 } while (false) | 268 } while (false) |
| 243 | 269 |
| 244 } // namespace | 270 } // namespace |
| 245 | 271 |
| 246 TEST_P(TaskSchedulerTaskTrackerTest, WillPostAndRunBeforeShutdown) { | 272 TEST_P(TaskSchedulerTaskTrackerTest, WillPostAndRunBeforeShutdown) { |
| 247 std::unique_ptr<Task> task(CreateTask(GetParam())); | 273 std::unique_ptr<Task> task(CreateTask(GetParam())); |
| 248 | 274 |
| 249 // Inform |task_tracker_| that |task| will be posted. | 275 // Inform |task_tracker_| that |task| will be posted. |
| 250 EXPECT_TRUE(tracker_.WillPostTask(task.get())); | 276 EXPECT_TRUE(tracker_->WillPostTask(task.get())); |
| 251 | 277 |
| 252 // Run the task. | 278 // Run the task. |
| 253 EXPECT_EQ(0U, NumTasksExecuted()); | 279 EXPECT_EQ(0U, NumTasksExecuted()); |
| 254 EXPECT_TRUE(tracker_.RunTask(std::move(task), SequenceToken::Create())); | 280 EXPECT_TRUE(tracker_->RunTask(std::move(task), SequenceToken::Create())); |
| 255 EXPECT_EQ(1U, NumTasksExecuted()); | 281 EXPECT_EQ(1U, NumTasksExecuted()); |
| 256 | 282 |
| 257 // Shutdown() shouldn't block. | 283 // Shutdown() shouldn't block. |
| 258 tracker_.Shutdown(); | 284 tracker_->Shutdown(); |
| 259 } | 285 } |
| 260 | 286 |
| 261 TEST_P(TaskSchedulerTaskTrackerTest, WillPostAndRunLongTaskBeforeShutdown) { | 287 TEST_P(TaskSchedulerTaskTrackerTest, WillPostAndRunLongTaskBeforeShutdown) { |
| 262 // Create a task that will block until |event| is signaled. | 288 // Create a task that will block until |event| is signaled. |
| 263 WaitableEvent event(WaitableEvent::ResetPolicy::AUTOMATIC, | 289 WaitableEvent event(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 264 WaitableEvent::InitialState::NOT_SIGNALED); | 290 WaitableEvent::InitialState::NOT_SIGNALED); |
| 265 auto blocked_task = base::MakeUnique<Task>( | 291 auto blocked_task = base::MakeUnique<Task>( |
| 266 FROM_HERE, Bind(&WaitableEvent::Wait, Unretained(&event)), | 292 FROM_HERE, Bind(&WaitableEvent::Wait, Unretained(&event)), |
| 267 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta()); | 293 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta()); |
| 268 | 294 |
| 269 // Inform |task_tracker_| that |blocked_task| will be posted. | 295 // Inform |task_tracker_| that |blocked_task| will be posted. |
| 270 EXPECT_TRUE(tracker_.WillPostTask(blocked_task.get())); | 296 EXPECT_TRUE(tracker_->WillPostTask(blocked_task.get())); |
| 271 | 297 |
| 272 // Run the task asynchronouly. | 298 // Run the task asynchronouly. |
| 273 ThreadPostingAndRunningTask thread_running_task( | 299 ThreadPostingAndRunningTask thread_running_task( |
| 274 &tracker_, std::move(blocked_task), | 300 tracker_.get(), std::move(blocked_task), |
| 275 ThreadPostingAndRunningTask::Action::RUN, false); | 301 ThreadPostingAndRunningTask::Action::RUN, false); |
| 276 thread_running_task.Start(); | 302 thread_running_task.Start(); |
| 277 | 303 |
| 278 // Initiate shutdown while the task is running. | 304 // Initiate shutdown while the task is running. |
| 279 CallShutdownAsync(); | 305 CallShutdownAsync(); |
| 280 | 306 |
| 281 if (GetParam() == TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) { | 307 if (GetParam() == TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) { |
| 282 // Shutdown should complete even with a CONTINUE_ON_SHUTDOWN in progress. | 308 // Shutdown should complete even with a CONTINUE_ON_SHUTDOWN in progress. |
| 283 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); | 309 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
| 284 } else { | 310 } else { |
| 285 // Shutdown should block with any non CONTINUE_ON_SHUTDOWN task in progress. | 311 // Shutdown should block with any non CONTINUE_ON_SHUTDOWN task in progress. |
| 286 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); | 312 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); |
| 287 } | 313 } |
| 288 | 314 |
| 289 // Unblock the task. | 315 // Unblock the task. |
| 290 event.Signal(); | 316 event.Signal(); |
| 291 thread_running_task.Join(); | 317 thread_running_task.Join(); |
| 292 | 318 |
| 293 // Shutdown should now complete for a non CONTINUE_ON_SHUTDOWN task. | 319 // Shutdown should now complete for a non CONTINUE_ON_SHUTDOWN task. |
| 294 if (GetParam() != TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) | 320 if (GetParam() != TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) |
| 295 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); | 321 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
| 296 } | 322 } |
| 297 | 323 |
| 298 TEST_P(TaskSchedulerTaskTrackerTest, WillPostBeforeShutdownRunDuringShutdown) { | 324 TEST_P(TaskSchedulerTaskTrackerTest, WillPostBeforeShutdownRunDuringShutdown) { |
| 299 // Inform |task_tracker_| that a task will be posted. | 325 // Inform |task_tracker_| that a task will be posted. |
| 300 std::unique_ptr<Task> task(CreateTask(GetParam())); | 326 std::unique_ptr<Task> task(CreateTask(GetParam())); |
| 301 EXPECT_TRUE(tracker_.WillPostTask(task.get())); | 327 EXPECT_TRUE(tracker_->WillPostTask(task.get())); |
| 302 | 328 |
| 303 // Inform |task_tracker_| that a BLOCK_SHUTDOWN task will be posted just to | 329 // Inform |task_tracker_| that a BLOCK_SHUTDOWN task will be posted just to |
| 304 // block shutdown. | 330 // block shutdown. |
| 305 std::unique_ptr<Task> block_shutdown_task( | 331 std::unique_ptr<Task> block_shutdown_task( |
| 306 CreateTask(TaskShutdownBehavior::BLOCK_SHUTDOWN)); | 332 CreateTask(TaskShutdownBehavior::BLOCK_SHUTDOWN)); |
| 307 EXPECT_TRUE(tracker_.WillPostTask(block_shutdown_task.get())); | 333 EXPECT_TRUE(tracker_->WillPostTask(block_shutdown_task.get())); |
| 308 | 334 |
| 309 // Call Shutdown() asynchronously. | 335 // Call Shutdown() asynchronously. |
| 310 CallShutdownAsync(); | 336 CallShutdownAsync(); |
| 311 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); | 337 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); |
| 312 | 338 |
| 313 // Try to run |task|. It should only run it it's BLOCK_SHUTDOWN. Otherwise it | 339 // Try to run |task|. It should only run it it's BLOCK_SHUTDOWN. Otherwise it |
| 314 // should be discarded. | 340 // should be discarded. |
| 315 EXPECT_EQ(0U, NumTasksExecuted()); | 341 EXPECT_EQ(0U, NumTasksExecuted()); |
| 316 const bool should_run = GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN; | 342 const bool should_run = GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN; |
| 317 EXPECT_EQ(should_run, | 343 EXPECT_EQ(should_run, |
| 318 tracker_.RunTask(std::move(task), SequenceToken::Create())); | 344 tracker_->RunTask(std::move(task), SequenceToken::Create())); |
| 319 EXPECT_EQ(should_run ? 1U : 0U, NumTasksExecuted()); | 345 EXPECT_EQ(should_run ? 1U : 0U, NumTasksExecuted()); |
| 320 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); | 346 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); |
| 321 | 347 |
| 322 // Unblock shutdown by running the remaining BLOCK_SHUTDOWN task. | 348 // Unblock shutdown by running the remaining BLOCK_SHUTDOWN task. |
| 323 EXPECT_TRUE(tracker_.RunTask(std::move(block_shutdown_task), | 349 EXPECT_TRUE(tracker_->RunTask(std::move(block_shutdown_task), |
| 324 SequenceToken::Create())); | 350 SequenceToken::Create())); |
| 325 EXPECT_EQ(should_run ? 2U : 1U, NumTasksExecuted()); | 351 EXPECT_EQ(should_run ? 2U : 1U, NumTasksExecuted()); |
| 326 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); | 352 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
| 327 } | 353 } |
| 328 | 354 |
| 329 TEST_P(TaskSchedulerTaskTrackerTest, WillPostBeforeShutdownRunAfterShutdown) { | 355 TEST_P(TaskSchedulerTaskTrackerTest, WillPostBeforeShutdownRunAfterShutdown) { |
| 330 // Inform |task_tracker_| that a task will be posted. | 356 // Inform |task_tracker_| that a task will be posted. |
| 331 std::unique_ptr<Task> task(CreateTask(GetParam())); | 357 std::unique_ptr<Task> task(CreateTask(GetParam())); |
| 332 EXPECT_TRUE(tracker_.WillPostTask(task.get())); | 358 EXPECT_TRUE(tracker_->WillPostTask(task.get())); |
| 333 | 359 |
| 334 // Call Shutdown() asynchronously. | 360 // Call Shutdown() asynchronously. |
| 335 CallShutdownAsync(); | 361 CallShutdownAsync(); |
| 336 EXPECT_EQ(0U, NumTasksExecuted()); | 362 EXPECT_EQ(0U, NumTasksExecuted()); |
| 337 | 363 |
| 338 if (GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN) { | 364 if (GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN) { |
| 339 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); | 365 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); |
| 340 | 366 |
| 341 // Run the task to unblock shutdown. | 367 // Run the task to unblock shutdown. |
| 342 EXPECT_TRUE(tracker_.RunTask(std::move(task), SequenceToken::Create())); | 368 EXPECT_TRUE(tracker_->RunTask(std::move(task), SequenceToken::Create())); |
| 343 EXPECT_EQ(1U, NumTasksExecuted()); | 369 EXPECT_EQ(1U, NumTasksExecuted()); |
| 344 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); | 370 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
| 345 | 371 |
| 346 // It is not possible to test running a BLOCK_SHUTDOWN task posted before | 372 // It is not possible to test running a BLOCK_SHUTDOWN task posted before |
| 347 // shutdown after shutdown because Shutdown() won't return if there are | 373 // shutdown after shutdown because Shutdown() won't return if there are |
| 348 // pending BLOCK_SHUTDOWN tasks. | 374 // pending BLOCK_SHUTDOWN tasks. |
| 349 } else { | 375 } else { |
| 350 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); | 376 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
| 351 | 377 |
| 352 // The task shouldn't be allowed to run after shutdown. | 378 // The task shouldn't be allowed to run after shutdown. |
| 353 EXPECT_FALSE(tracker_.RunTask(std::move(task), SequenceToken::Create())); | 379 EXPECT_FALSE(tracker_->RunTask(std::move(task), SequenceToken::Create())); |
| 354 EXPECT_EQ(0U, NumTasksExecuted()); | 380 EXPECT_EQ(0U, NumTasksExecuted()); |
| 355 } | 381 } |
| 356 } | 382 } |
| 357 | 383 |
| 358 TEST_P(TaskSchedulerTaskTrackerTest, WillPostAndRunDuringShutdown) { | 384 TEST_P(TaskSchedulerTaskTrackerTest, WillPostAndRunDuringShutdown) { |
| 359 // Inform |task_tracker_| that a BLOCK_SHUTDOWN task will be posted just to | 385 // Inform |task_tracker_| that a BLOCK_SHUTDOWN task will be posted just to |
| 360 // block shutdown. | 386 // block shutdown. |
| 361 std::unique_ptr<Task> block_shutdown_task( | 387 std::unique_ptr<Task> block_shutdown_task( |
| 362 CreateTask(TaskShutdownBehavior::BLOCK_SHUTDOWN)); | 388 CreateTask(TaskShutdownBehavior::BLOCK_SHUTDOWN)); |
| 363 EXPECT_TRUE(tracker_.WillPostTask(block_shutdown_task.get())); | 389 EXPECT_TRUE(tracker_->WillPostTask(block_shutdown_task.get())); |
| 364 | 390 |
| 365 // Call Shutdown() asynchronously. | 391 // Call Shutdown() asynchronously. |
| 366 CallShutdownAsync(); | 392 CallShutdownAsync(); |
| 367 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); | 393 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); |
| 368 | 394 |
| 369 if (GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN) { | 395 if (GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN) { |
| 370 // Inform |task_tracker_| that a BLOCK_SHUTDOWN task will be posted. | 396 // Inform |task_tracker_| that a BLOCK_SHUTDOWN task will be posted. |
| 371 std::unique_ptr<Task> task(CreateTask(GetParam())); | 397 std::unique_ptr<Task> task(CreateTask(GetParam())); |
| 372 EXPECT_TRUE(tracker_.WillPostTask(task.get())); | 398 EXPECT_TRUE(tracker_->WillPostTask(task.get())); |
| 373 | 399 |
| 374 // Run the BLOCK_SHUTDOWN task. | 400 // Run the BLOCK_SHUTDOWN task. |
| 375 EXPECT_EQ(0U, NumTasksExecuted()); | 401 EXPECT_EQ(0U, NumTasksExecuted()); |
| 376 EXPECT_TRUE(tracker_.RunTask(std::move(task), SequenceToken::Create())); | 402 EXPECT_TRUE(tracker_->RunTask(std::move(task), SequenceToken::Create())); |
| 377 EXPECT_EQ(1U, NumTasksExecuted()); | 403 EXPECT_EQ(1U, NumTasksExecuted()); |
| 378 } else { | 404 } else { |
| 379 // It shouldn't be allowed to post a non BLOCK_SHUTDOWN task. | 405 // It shouldn't be allowed to post a non BLOCK_SHUTDOWN task. |
| 380 std::unique_ptr<Task> task(CreateTask(GetParam())); | 406 std::unique_ptr<Task> task(CreateTask(GetParam())); |
| 381 EXPECT_FALSE(tracker_.WillPostTask(task.get())); | 407 EXPECT_FALSE(tracker_->WillPostTask(task.get())); |
| 382 | 408 |
| 383 // Don't try to run the task, because it wasn't allowed to be posted. | 409 // Don't try to run the task, because it wasn't allowed to be posted. |
| 384 } | 410 } |
| 385 | 411 |
| 386 // Unblock shutdown by running |block_shutdown_task|. | 412 // Unblock shutdown by running |block_shutdown_task|. |
| 387 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); | 413 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); |
| 388 EXPECT_TRUE(tracker_.RunTask(std::move(block_shutdown_task), | 414 EXPECT_TRUE(tracker_->RunTask(std::move(block_shutdown_task), |
| 389 SequenceToken::Create())); | 415 SequenceToken::Create())); |
| 390 EXPECT_EQ(GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN ? 2U : 1U, | 416 EXPECT_EQ(GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN ? 2U : 1U, |
| 391 NumTasksExecuted()); | 417 NumTasksExecuted()); |
| 392 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); | 418 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
| 393 } | 419 } |
| 394 | 420 |
| 395 TEST_P(TaskSchedulerTaskTrackerTest, WillPostAfterShutdown) { | 421 TEST_P(TaskSchedulerTaskTrackerTest, WillPostAfterShutdown) { |
| 396 tracker_.Shutdown(); | 422 tracker_->Shutdown(); |
| 397 | 423 |
| 398 std::unique_ptr<Task> task(CreateTask(GetParam())); | 424 std::unique_ptr<Task> task(CreateTask(GetParam())); |
| 399 | 425 |
| 400 // |task_tracker_| shouldn't allow a task to be posted after shutdown. | 426 // |task_tracker_| shouldn't allow a task to be posted after shutdown. |
| 401 if (GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN) { | 427 if (GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN) { |
| 402 EXPECT_DCHECK_DEATH({ tracker_.WillPostTask(task.get()); }); | 428 EXPECT_DCHECK_DEATH({ tracker_->WillPostTask(task.get()); }); |
| 403 } else { | 429 } else { |
| 404 EXPECT_FALSE(tracker_.WillPostTask(task.get())); | 430 EXPECT_FALSE(tracker_->WillPostTask(task.get())); |
| 405 } | 431 } |
| 406 } | 432 } |
| 407 | 433 |
| 408 // Verify that BLOCK_SHUTDOWN and SKIP_ON_SHUTDOWN tasks can | 434 // Verify that BLOCK_SHUTDOWN and SKIP_ON_SHUTDOWN tasks can |
| 409 // AssertSingletonAllowed() but CONTINUE_ON_SHUTDOWN tasks can't. | 435 // AssertSingletonAllowed() but CONTINUE_ON_SHUTDOWN tasks can't. |
| 410 TEST_P(TaskSchedulerTaskTrackerTest, SingletonAllowed) { | 436 TEST_P(TaskSchedulerTaskTrackerTest, SingletonAllowed) { |
| 411 const bool can_use_singletons = | 437 const bool can_use_singletons = |
| 412 (GetParam() != TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN); | 438 (GetParam() != TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN); |
| 413 | 439 |
| 414 TaskTracker tracker; | |
| 415 std::unique_ptr<Task> task( | 440 std::unique_ptr<Task> task( |
| 416 new Task(FROM_HERE, Bind(&ThreadRestrictions::AssertSingletonAllowed), | 441 new Task(FROM_HERE, Bind(&ThreadRestrictions::AssertSingletonAllowed), |
| 417 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta())); | 442 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta())); |
| 418 EXPECT_TRUE(tracker.WillPostTask(task.get())); | 443 EXPECT_TRUE(tracker_->WillPostTask(task.get())); |
| 419 | 444 |
| 420 // Set the singleton allowed bit to the opposite of what it is expected to be | 445 // Set the singleton allowed bit to the opposite of what it is expected to be |
| 421 // when |tracker| runs |task| to verify that |tracker| actually sets the | 446 // when |tracker| runs |task| to verify that |tracker| actually sets the |
| 422 // correct value. | 447 // correct value. |
| 423 ScopedSetSingletonAllowed scoped_singleton_allowed(!can_use_singletons); | 448 ScopedSetSingletonAllowed scoped_singleton_allowed(!can_use_singletons); |
| 424 | 449 |
| 425 // Running the task should fail iff the task isn't allowed to use singletons. | 450 // Running the task should fail iff the task isn't allowed to use singletons. |
| 426 if (can_use_singletons) { | 451 if (can_use_singletons) { |
| 427 EXPECT_TRUE(tracker.RunTask(std::move(task), SequenceToken::Create())); | 452 EXPECT_TRUE(tracker_->RunTask(std::move(task), SequenceToken::Create())); |
| 428 } else { | 453 } else { |
| 429 EXPECT_DCHECK_DEATH( | 454 EXPECT_DCHECK_DEATH( |
| 430 { tracker.RunTask(std::move(task), SequenceToken::Create()); }); | 455 { tracker_->RunTask(std::move(task), SequenceToken::Create()); }); |
| 431 } | 456 } |
| 432 } | 457 } |
| 433 | 458 |
| 434 static void RunTaskRunnerHandleVerificationTask( | 459 static void RunTaskRunnerHandleVerificationTask( |
| 435 TaskTracker* tracker, | 460 TaskTracker* tracker, |
| 436 std::unique_ptr<Task> verify_task) { | 461 std::unique_ptr<Task> verify_task) { |
| 437 // Pretend |verify_task| is posted to respect TaskTracker's contract. | 462 // Pretend |verify_task| is posted to respect TaskTracker's contract. |
| 438 EXPECT_TRUE(tracker->WillPostTask(verify_task.get())); | 463 EXPECT_TRUE(tracker->WillPostTask(verify_task.get())); |
| 439 | 464 |
| 440 // Confirm that the test conditions are right (no TaskRunnerHandles set | 465 // Confirm that the test conditions are right (no TaskRunnerHandles set |
| (...skipping 14 matching lines...) Expand all Loading... |
| 455 EXPECT_FALSE(SequencedTaskRunnerHandle::IsSet()); | 480 EXPECT_FALSE(SequencedTaskRunnerHandle::IsSet()); |
| 456 } | 481 } |
| 457 | 482 |
| 458 TEST_P(TaskSchedulerTaskTrackerTest, TaskRunnerHandleIsNotSetOnParallel) { | 483 TEST_P(TaskSchedulerTaskTrackerTest, TaskRunnerHandleIsNotSetOnParallel) { |
| 459 // Create a task that will verify that TaskRunnerHandles are not set in its | 484 // Create a task that will verify that TaskRunnerHandles are not set in its |
| 460 // scope per no TaskRunner ref being set to it. | 485 // scope per no TaskRunner ref being set to it. |
| 461 std::unique_ptr<Task> verify_task( | 486 std::unique_ptr<Task> verify_task( |
| 462 new Task(FROM_HERE, Bind(&VerifyNoTaskRunnerHandle), | 487 new Task(FROM_HERE, Bind(&VerifyNoTaskRunnerHandle), |
| 463 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta())); | 488 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta())); |
| 464 | 489 |
| 465 RunTaskRunnerHandleVerificationTask(&tracker_, std::move(verify_task)); | 490 RunTaskRunnerHandleVerificationTask(tracker_.get(), std::move(verify_task)); |
| 466 } | 491 } |
| 467 | 492 |
| 468 static void VerifySequencedTaskRunnerHandle( | 493 static void VerifySequencedTaskRunnerHandle( |
| 469 const SequencedTaskRunner* expected_task_runner) { | 494 const SequencedTaskRunner* expected_task_runner) { |
| 470 EXPECT_FALSE(ThreadTaskRunnerHandle::IsSet()); | 495 EXPECT_FALSE(ThreadTaskRunnerHandle::IsSet()); |
| 471 EXPECT_TRUE(SequencedTaskRunnerHandle::IsSet()); | 496 EXPECT_TRUE(SequencedTaskRunnerHandle::IsSet()); |
| 472 EXPECT_EQ(expected_task_runner, SequencedTaskRunnerHandle::Get()); | 497 EXPECT_EQ(expected_task_runner, SequencedTaskRunnerHandle::Get()); |
| 473 } | 498 } |
| 474 | 499 |
| 475 TEST_P(TaskSchedulerTaskTrackerTest, | 500 TEST_P(TaskSchedulerTaskTrackerTest, |
| 476 SequencedTaskRunnerHandleIsSetOnSequenced) { | 501 SequencedTaskRunnerHandleIsSetOnSequenced) { |
| 477 scoped_refptr<SequencedTaskRunner> test_task_runner(new TestSimpleTaskRunner); | 502 scoped_refptr<SequencedTaskRunner> test_task_runner(new TestSimpleTaskRunner); |
| 478 | 503 |
| 479 // Create a task that will verify that SequencedTaskRunnerHandle is properly | 504 // Create a task that will verify that SequencedTaskRunnerHandle is properly |
| 480 // set to |test_task_runner| in its scope per |sequenced_task_runner_ref| | 505 // set to |test_task_runner| in its scope per |sequenced_task_runner_ref| |
| 481 // being set to it. | 506 // being set to it. |
| 482 std::unique_ptr<Task> verify_task( | 507 std::unique_ptr<Task> verify_task( |
| 483 new Task(FROM_HERE, Bind(&VerifySequencedTaskRunnerHandle, | 508 new Task(FROM_HERE, Bind(&VerifySequencedTaskRunnerHandle, |
| 484 base::Unretained(test_task_runner.get())), | 509 base::Unretained(test_task_runner.get())), |
| 485 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta())); | 510 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta())); |
| 486 verify_task->sequenced_task_runner_ref = test_task_runner; | 511 verify_task->sequenced_task_runner_ref = test_task_runner; |
| 487 | 512 |
| 488 RunTaskRunnerHandleVerificationTask(&tracker_, std::move(verify_task)); | 513 RunTaskRunnerHandleVerificationTask(tracker_.get(), std::move(verify_task)); |
| 489 } | 514 } |
| 490 | 515 |
| 491 static void VerifyThreadTaskRunnerHandle( | 516 static void VerifyThreadTaskRunnerHandle( |
| 492 const SingleThreadTaskRunner* expected_task_runner) { | 517 const SingleThreadTaskRunner* expected_task_runner) { |
| 493 EXPECT_TRUE(ThreadTaskRunnerHandle::IsSet()); | 518 EXPECT_TRUE(ThreadTaskRunnerHandle::IsSet()); |
| 494 // SequencedTaskRunnerHandle inherits ThreadTaskRunnerHandle for thread. | 519 // SequencedTaskRunnerHandle inherits ThreadTaskRunnerHandle for thread. |
| 495 EXPECT_TRUE(SequencedTaskRunnerHandle::IsSet()); | 520 EXPECT_TRUE(SequencedTaskRunnerHandle::IsSet()); |
| 496 EXPECT_EQ(expected_task_runner, ThreadTaskRunnerHandle::Get()); | 521 EXPECT_EQ(expected_task_runner, ThreadTaskRunnerHandle::Get()); |
| 497 } | 522 } |
| 498 | 523 |
| 499 TEST_P(TaskSchedulerTaskTrackerTest, | 524 TEST_P(TaskSchedulerTaskTrackerTest, |
| 500 ThreadTaskRunnerHandleIsSetOnSingleThreaded) { | 525 ThreadTaskRunnerHandleIsSetOnSingleThreaded) { |
| 501 scoped_refptr<SingleThreadTaskRunner> test_task_runner( | 526 scoped_refptr<SingleThreadTaskRunner> test_task_runner( |
| 502 new TestSimpleTaskRunner); | 527 new TestSimpleTaskRunner); |
| 503 | 528 |
| 504 // Create a task that will verify that ThreadTaskRunnerHandle is properly set | 529 // Create a task that will verify that ThreadTaskRunnerHandle is properly set |
| 505 // to |test_task_runner| in its scope per |single_thread_task_runner_ref| | 530 // to |test_task_runner| in its scope per |single_thread_task_runner_ref| |
| 506 // being set on it. | 531 // being set on it. |
| 507 std::unique_ptr<Task> verify_task( | 532 std::unique_ptr<Task> verify_task( |
| 508 new Task(FROM_HERE, Bind(&VerifyThreadTaskRunnerHandle, | 533 new Task(FROM_HERE, Bind(&VerifyThreadTaskRunnerHandle, |
| 509 base::Unretained(test_task_runner.get())), | 534 base::Unretained(test_task_runner.get())), |
| 510 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta())); | 535 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta())); |
| 511 verify_task->single_thread_task_runner_ref = test_task_runner; | 536 verify_task->single_thread_task_runner_ref = test_task_runner; |
| 512 | 537 |
| 513 RunTaskRunnerHandleVerificationTask(&tracker_, std::move(verify_task)); | 538 RunTaskRunnerHandleVerificationTask(tracker_.get(), std::move(verify_task)); |
| 514 } | 539 } |
| 515 | 540 |
| 516 TEST_P(TaskSchedulerTaskTrackerTest, FlushPendingDelayedTask) { | 541 TEST_P(TaskSchedulerTaskTrackerTest, FlushPendingDelayedTask) { |
| 517 const Task delayed_task(FROM_HERE, Bind(&DoNothing), | 542 const Task delayed_task(FROM_HERE, Bind(&DoNothing), |
| 518 TaskTraits().WithShutdownBehavior(GetParam()), | 543 TaskTraits().WithShutdownBehavior(GetParam()), |
| 519 TimeDelta::FromDays(1)); | 544 TimeDelta::FromDays(1)); |
| 520 tracker_.WillPostTask(&delayed_task); | 545 tracker_->WillPostTask(&delayed_task); |
| 521 // Flush() should return even if the delayed task didn't run. | 546 // Flush() should return even if the delayed task didn't run. |
| 522 tracker_.Flush(); | 547 tracker_->Flush(); |
| 523 } | 548 } |
| 524 | 549 |
| 525 TEST_P(TaskSchedulerTaskTrackerTest, FlushPendingUndelayedTask) { | 550 TEST_P(TaskSchedulerTaskTrackerTest, FlushPendingUndelayedTask) { |
| 526 auto undelayed_task = base::MakeUnique<Task>( | 551 auto undelayed_task = base::MakeUnique<Task>( |
| 527 FROM_HERE, Bind(&DoNothing), | 552 FROM_HERE, Bind(&DoNothing), |
| 528 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta()); | 553 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta()); |
| 529 tracker_.WillPostTask(undelayed_task.get()); | 554 tracker_->WillPostTask(undelayed_task.get()); |
| 530 | 555 |
| 531 // Flush() shouldn't return before the undelayed task runs. | 556 // Flush() shouldn't return before the undelayed task runs. |
| 532 CallFlushAsync(); | 557 CallFlushAsync(); |
| 533 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | 558 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); |
| 534 VERIFY_ASYNC_FLUSH_IN_PROGRESS(); | 559 VERIFY_ASYNC_FLUSH_IN_PROGRESS(); |
| 535 | 560 |
| 536 // Flush() should return after the undelayed task runs. | 561 // Flush() should return after the undelayed task runs. |
| 537 tracker_.RunTask(std::move(undelayed_task), SequenceToken::Create()); | 562 tracker_->RunTask(std::move(undelayed_task), SequenceToken::Create()); |
| 538 WAIT_FOR_ASYNC_FLUSH_RETURNED(); | 563 WAIT_FOR_ASYNC_FLUSH_RETURNED(); |
| 539 } | 564 } |
| 540 | 565 |
| 541 TEST_P(TaskSchedulerTaskTrackerTest, PostTaskDuringFlush) { | 566 TEST_P(TaskSchedulerTaskTrackerTest, PostTaskDuringFlush) { |
| 542 auto undelayed_task = base::MakeUnique<Task>( | 567 auto undelayed_task = base::MakeUnique<Task>( |
| 543 FROM_HERE, Bind(&DoNothing), | 568 FROM_HERE, Bind(&DoNothing), |
| 544 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta()); | 569 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta()); |
| 545 tracker_.WillPostTask(undelayed_task.get()); | 570 tracker_->WillPostTask(undelayed_task.get()); |
| 546 | 571 |
| 547 // Flush() shouldn't return before the undelayed task runs. | 572 // Flush() shouldn't return before the undelayed task runs. |
| 548 CallFlushAsync(); | 573 CallFlushAsync(); |
| 549 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | 574 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); |
| 550 VERIFY_ASYNC_FLUSH_IN_PROGRESS(); | 575 VERIFY_ASYNC_FLUSH_IN_PROGRESS(); |
| 551 | 576 |
| 552 // Simulate posting another undelayed task. | 577 // Simulate posting another undelayed task. |
| 553 auto other_undelayed_task = base::MakeUnique<Task>( | 578 auto other_undelayed_task = base::MakeUnique<Task>( |
| 554 FROM_HERE, Bind(&DoNothing), | 579 FROM_HERE, Bind(&DoNothing), |
| 555 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta()); | 580 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta()); |
| 556 tracker_.WillPostTask(other_undelayed_task.get()); | 581 tracker_->WillPostTask(other_undelayed_task.get()); |
| 557 | 582 |
| 558 // Run the first undelayed task. | 583 // Run the first undelayed task. |
| 559 tracker_.RunTask(std::move(undelayed_task), SequenceToken::Create()); | 584 tracker_->RunTask(std::move(undelayed_task), SequenceToken::Create()); |
| 560 | 585 |
| 561 // Flush() shouldn't return before the second undelayed task runs. | 586 // Flush() shouldn't return before the second undelayed task runs. |
| 562 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | 587 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); |
| 563 VERIFY_ASYNC_FLUSH_IN_PROGRESS(); | 588 VERIFY_ASYNC_FLUSH_IN_PROGRESS(); |
| 564 | 589 |
| 565 // Flush() should return after the second undelayed task runs. | 590 // Flush() should return after the second undelayed task runs. |
| 566 tracker_.RunTask(std::move(other_undelayed_task), SequenceToken::Create()); | 591 tracker_->RunTask(std::move(other_undelayed_task), SequenceToken::Create()); |
| 567 WAIT_FOR_ASYNC_FLUSH_RETURNED(); | 592 WAIT_FOR_ASYNC_FLUSH_RETURNED(); |
| 568 } | 593 } |
| 569 | 594 |
| 570 TEST_P(TaskSchedulerTaskTrackerTest, RunDelayedTaskDuringFlush) { | 595 TEST_P(TaskSchedulerTaskTrackerTest, RunDelayedTaskDuringFlush) { |
| 571 // Simulate posting a delayed and an undelayed task. | 596 // Simulate posting a delayed and an undelayed task. |
| 572 auto delayed_task = base::MakeUnique<Task>( | 597 auto delayed_task = base::MakeUnique<Task>( |
| 573 FROM_HERE, Bind(&DoNothing), | 598 FROM_HERE, Bind(&DoNothing), |
| 574 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta::FromDays(1)); | 599 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta::FromDays(1)); |
| 575 tracker_.WillPostTask(delayed_task.get()); | 600 tracker_->WillPostTask(delayed_task.get()); |
| 576 auto undelayed_task = base::MakeUnique<Task>( | 601 auto undelayed_task = base::MakeUnique<Task>( |
| 577 FROM_HERE, Bind(&DoNothing), | 602 FROM_HERE, Bind(&DoNothing), |
| 578 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta()); | 603 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta()); |
| 579 tracker_.WillPostTask(undelayed_task.get()); | 604 tracker_->WillPostTask(undelayed_task.get()); |
| 580 | 605 |
| 581 // Flush() shouldn't return before the undelayed task runs. | 606 // Flush() shouldn't return before the undelayed task runs. |
| 582 CallFlushAsync(); | 607 CallFlushAsync(); |
| 583 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | 608 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); |
| 584 VERIFY_ASYNC_FLUSH_IN_PROGRESS(); | 609 VERIFY_ASYNC_FLUSH_IN_PROGRESS(); |
| 585 | 610 |
| 586 // Run the delayed task. | 611 // Run the delayed task. |
| 587 tracker_.RunTask(std::move(delayed_task), SequenceToken::Create()); | 612 tracker_->RunTask(std::move(delayed_task), SequenceToken::Create()); |
| 588 | 613 |
| 589 // Flush() shouldn't return since there is still a pending undelayed | 614 // Flush() shouldn't return since there is still a pending undelayed |
| 590 // task. | 615 // task. |
| 591 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | 616 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); |
| 592 VERIFY_ASYNC_FLUSH_IN_PROGRESS(); | 617 VERIFY_ASYNC_FLUSH_IN_PROGRESS(); |
| 593 | 618 |
| 594 // Run the undelayed task. | 619 // Run the undelayed task. |
| 595 tracker_.RunTask(std::move(undelayed_task), SequenceToken::Create()); | 620 tracker_->RunTask(std::move(undelayed_task), SequenceToken::Create()); |
| 596 | 621 |
| 597 // Flush() should now return. | 622 // Flush() should now return. |
| 598 WAIT_FOR_ASYNC_FLUSH_RETURNED(); | 623 WAIT_FOR_ASYNC_FLUSH_RETURNED(); |
| 599 } | 624 } |
| 600 | 625 |
| 601 TEST_P(TaskSchedulerTaskTrackerTest, FlushAfterShutdown) { | 626 TEST_P(TaskSchedulerTaskTrackerTest, FlushAfterShutdown) { |
| 602 if (GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN) | 627 if (GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN) |
| 603 return; | 628 return; |
| 604 | 629 |
| 605 // Simulate posting a task. | 630 // Simulate posting a task. |
| 606 auto undelayed_task = base::MakeUnique<Task>( | 631 auto undelayed_task = base::MakeUnique<Task>( |
| 607 FROM_HERE, Bind(&DoNothing), | 632 FROM_HERE, Bind(&DoNothing), |
| 608 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta()); | 633 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta()); |
| 609 tracker_.WillPostTask(undelayed_task.get()); | 634 tracker_->WillPostTask(undelayed_task.get()); |
| 610 | 635 |
| 611 // Shutdown() should return immediately since there are no pending | 636 // Shutdown() should return immediately since there are no pending |
| 612 // BLOCK_SHUTDOWN tasks. | 637 // BLOCK_SHUTDOWN tasks. |
| 613 tracker_.Shutdown(); | 638 tracker_->Shutdown(); |
| 614 | 639 |
| 615 // Flush() should return immediately after shutdown, even if an | 640 // Flush() should return immediately after shutdown, even if an |
| 616 // undelayed task hasn't run. | 641 // undelayed task hasn't run. |
| 617 tracker_.Flush(); | 642 tracker_->Flush(); |
| 618 } | 643 } |
| 619 | 644 |
| 620 TEST_P(TaskSchedulerTaskTrackerTest, ShutdownDuringFlush) { | 645 TEST_P(TaskSchedulerTaskTrackerTest, ShutdownDuringFlush) { |
| 621 if (GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN) | 646 if (GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN) |
| 622 return; | 647 return; |
| 623 | 648 |
| 624 // Simulate posting a task. | 649 // Simulate posting a task. |
| 625 auto undelayed_task = base::MakeUnique<Task>( | 650 auto undelayed_task = base::MakeUnique<Task>( |
| 626 FROM_HERE, Bind(&DoNothing), | 651 FROM_HERE, Bind(&DoNothing), |
| 627 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta()); | 652 TaskTraits().WithShutdownBehavior(GetParam()), TimeDelta()); |
| 628 tracker_.WillPostTask(undelayed_task.get()); | 653 tracker_->WillPostTask(undelayed_task.get()); |
| 629 | 654 |
| 630 // Flush() shouldn't return before the undelayed task runs or | 655 // Flush() shouldn't return before the undelayed task runs or |
| 631 // shutdown completes. | 656 // shutdown completes. |
| 632 CallFlushAsync(); | 657 CallFlushAsync(); |
| 633 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | 658 PlatformThread::Sleep(TestTimeouts::tiny_timeout()); |
| 634 VERIFY_ASYNC_FLUSH_IN_PROGRESS(); | 659 VERIFY_ASYNC_FLUSH_IN_PROGRESS(); |
| 635 | 660 |
| 636 // Shutdown() should return immediately since there are no pending | 661 // Shutdown() should return immediately since there are no pending |
| 637 // BLOCK_SHUTDOWN tasks. | 662 // BLOCK_SHUTDOWN tasks. |
| 638 tracker_.Shutdown(); | 663 tracker_->Shutdown(); |
| 639 | 664 |
| 640 // Flush() should now return, even if an undelayed task hasn't run. | 665 // Flush() should now return, even if an undelayed task hasn't run. |
| 641 WAIT_FOR_ASYNC_FLUSH_RETURNED(); | 666 WAIT_FOR_ASYNC_FLUSH_RETURNED(); |
| 642 } | 667 } |
| 643 | 668 |
| 644 INSTANTIATE_TEST_CASE_P( | 669 INSTANTIATE_TEST_CASE_P( |
| 645 ContinueOnShutdown, | 670 ContinueOnShutdown, |
| 646 TaskSchedulerTaskTrackerTest, | 671 TaskSchedulerTaskTrackerTest, |
| 647 ::testing::Values(TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN)); | 672 ::testing::Values(TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN)); |
| 648 INSTANTIATE_TEST_CASE_P( | 673 INSTANTIATE_TEST_CASE_P( |
| (...skipping 13 matching lines...) Expand all Loading... |
| 662 | 687 |
| 663 } // namespace | 688 } // namespace |
| 664 | 689 |
| 665 // Verify that SequenceToken::GetForCurrentThread() returns the Sequence's token | 690 // Verify that SequenceToken::GetForCurrentThread() returns the Sequence's token |
| 666 // when a Task runs. | 691 // when a Task runs. |
| 667 TEST_F(TaskSchedulerTaskTrackerTest, CurrentSequenceToken) { | 692 TEST_F(TaskSchedulerTaskTrackerTest, CurrentSequenceToken) { |
| 668 const SequenceToken sequence_token(SequenceToken::Create()); | 693 const SequenceToken sequence_token(SequenceToken::Create()); |
| 669 auto task = base::MakeUnique<Task>(FROM_HERE, | 694 auto task = base::MakeUnique<Task>(FROM_HERE, |
| 670 Bind(&ExpectSequenceToken, sequence_token), | 695 Bind(&ExpectSequenceToken, sequence_token), |
| 671 TaskTraits(), TimeDelta()); | 696 TaskTraits(), TimeDelta()); |
| 672 tracker_.WillPostTask(task.get()); | 697 tracker_->WillPostTask(task.get()); |
| 673 | 698 |
| 674 EXPECT_FALSE(SequenceToken::GetForCurrentThread().IsValid()); | 699 EXPECT_FALSE(SequenceToken::GetForCurrentThread().IsValid()); |
| 675 EXPECT_TRUE(tracker_.RunTask(std::move(task), sequence_token)); | 700 EXPECT_TRUE(tracker_->RunTask(std::move(task), sequence_token)); |
| 676 EXPECT_FALSE(SequenceToken::GetForCurrentThread().IsValid()); | 701 EXPECT_FALSE(SequenceToken::GetForCurrentThread().IsValid()); |
| 677 } | 702 } |
| 678 | 703 |
| 679 TEST_F(TaskSchedulerTaskTrackerTest, LoadWillPostAndRunBeforeShutdown) { | 704 TEST_F(TaskSchedulerTaskTrackerTest, LoadWillPostAndRunBeforeShutdown) { |
| 680 // Post and run tasks asynchronously. | 705 // Post and run tasks asynchronously. |
| 681 std::vector<std::unique_ptr<ThreadPostingAndRunningTask>> threads; | 706 std::vector<std::unique_ptr<ThreadPostingAndRunningTask>> threads; |
| 682 | 707 |
| 683 for (size_t i = 0; i < kLoadTestNumIterations; ++i) { | 708 for (size_t i = 0; i < kLoadTestNumIterations; ++i) { |
| 684 threads.push_back(MakeUnique<ThreadPostingAndRunningTask>( | 709 threads.push_back(MakeUnique<ThreadPostingAndRunningTask>( |
| 685 &tracker_, CreateTask(TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN), | 710 tracker_.get(), CreateTask(TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN), |
| 686 ThreadPostingAndRunningTask::Action::WILL_POST_AND_RUN, true)); | 711 ThreadPostingAndRunningTask::Action::WILL_POST_AND_RUN, true)); |
| 687 threads.back()->Start(); | 712 threads.back()->Start(); |
| 688 | 713 |
| 689 threads.push_back(MakeUnique<ThreadPostingAndRunningTask>( | 714 threads.push_back(MakeUnique<ThreadPostingAndRunningTask>( |
| 690 &tracker_, CreateTask(TaskShutdownBehavior::SKIP_ON_SHUTDOWN), | 715 tracker_.get(), CreateTask(TaskShutdownBehavior::SKIP_ON_SHUTDOWN), |
| 691 ThreadPostingAndRunningTask::Action::WILL_POST_AND_RUN, true)); | 716 ThreadPostingAndRunningTask::Action::WILL_POST_AND_RUN, true)); |
| 692 threads.back()->Start(); | 717 threads.back()->Start(); |
| 693 | 718 |
| 694 threads.push_back(MakeUnique<ThreadPostingAndRunningTask>( | 719 threads.push_back(MakeUnique<ThreadPostingAndRunningTask>( |
| 695 &tracker_, CreateTask(TaskShutdownBehavior::BLOCK_SHUTDOWN), | 720 tracker_.get(), CreateTask(TaskShutdownBehavior::BLOCK_SHUTDOWN), |
| 696 ThreadPostingAndRunningTask::Action::WILL_POST_AND_RUN, true)); | 721 ThreadPostingAndRunningTask::Action::WILL_POST_AND_RUN, true)); |
| 697 threads.back()->Start(); | 722 threads.back()->Start(); |
| 698 } | 723 } |
| 699 | 724 |
| 700 for (const auto& thread : threads) | 725 for (const auto& thread : threads) |
| 701 thread->Join(); | 726 thread->Join(); |
| 702 | 727 |
| 703 // Expect all tasks to be executed. | 728 // Expect all tasks to be executed. |
| 704 EXPECT_EQ(kLoadTestNumIterations * 3, NumTasksExecuted()); | 729 EXPECT_EQ(kLoadTestNumIterations * 3, NumTasksExecuted()); |
| 705 | 730 |
| 706 // Should return immediately because no tasks are blocking shutdown. | 731 // Should return immediately because no tasks are blocking shutdown. |
| 707 tracker_.Shutdown(); | 732 tracker_->Shutdown(); |
| 708 } | 733 } |
| 709 | 734 |
| 710 TEST_F(TaskSchedulerTaskTrackerTest, | 735 TEST_F(TaskSchedulerTaskTrackerTest, |
| 711 LoadWillPostBeforeShutdownAndRunDuringShutdown) { | 736 LoadWillPostBeforeShutdownAndRunDuringShutdown) { |
| 712 // Post tasks asynchronously. | 737 // Post tasks asynchronously. |
| 713 std::vector<std::unique_ptr<Task>> tasks; | 738 std::vector<std::unique_ptr<Task>> tasks; |
| 714 std::vector<std::unique_ptr<ThreadPostingAndRunningTask>> post_threads; | 739 std::vector<std::unique_ptr<ThreadPostingAndRunningTask>> post_threads; |
| 715 | 740 |
| 716 for (size_t i = 0; i < kLoadTestNumIterations; ++i) { | 741 for (size_t i = 0; i < kLoadTestNumIterations; ++i) { |
| 717 tasks.push_back(CreateTask(TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN)); | 742 tasks.push_back(CreateTask(TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN)); |
| 718 post_threads.push_back(MakeUnique<ThreadPostingAndRunningTask>( | 743 post_threads.push_back(MakeUnique<ThreadPostingAndRunningTask>( |
| 719 &tracker_, tasks.back().get(), | 744 tracker_.get(), tasks.back().get(), |
| 720 ThreadPostingAndRunningTask::Action::WILL_POST, true)); | 745 ThreadPostingAndRunningTask::Action::WILL_POST, true)); |
| 721 post_threads.back()->Start(); | 746 post_threads.back()->Start(); |
| 722 | 747 |
| 723 tasks.push_back(CreateTask(TaskShutdownBehavior::SKIP_ON_SHUTDOWN)); | 748 tasks.push_back(CreateTask(TaskShutdownBehavior::SKIP_ON_SHUTDOWN)); |
| 724 post_threads.push_back(MakeUnique<ThreadPostingAndRunningTask>( | 749 post_threads.push_back(MakeUnique<ThreadPostingAndRunningTask>( |
| 725 &tracker_, tasks.back().get(), | 750 tracker_.get(), tasks.back().get(), |
| 726 ThreadPostingAndRunningTask::Action::WILL_POST, true)); | 751 ThreadPostingAndRunningTask::Action::WILL_POST, true)); |
| 727 post_threads.back()->Start(); | 752 post_threads.back()->Start(); |
| 728 | 753 |
| 729 tasks.push_back(CreateTask(TaskShutdownBehavior::BLOCK_SHUTDOWN)); | 754 tasks.push_back(CreateTask(TaskShutdownBehavior::BLOCK_SHUTDOWN)); |
| 730 post_threads.push_back(MakeUnique<ThreadPostingAndRunningTask>( | 755 post_threads.push_back(MakeUnique<ThreadPostingAndRunningTask>( |
| 731 &tracker_, tasks.back().get(), | 756 tracker_.get(), tasks.back().get(), |
| 732 ThreadPostingAndRunningTask::Action::WILL_POST, true)); | 757 ThreadPostingAndRunningTask::Action::WILL_POST, true)); |
| 733 post_threads.back()->Start(); | 758 post_threads.back()->Start(); |
| 734 } | 759 } |
| 735 | 760 |
| 736 for (const auto& thread : post_threads) | 761 for (const auto& thread : post_threads) |
| 737 thread->Join(); | 762 thread->Join(); |
| 738 | 763 |
| 739 // Call Shutdown() asynchronously. | 764 // Call Shutdown() asynchronously. |
| 740 CallShutdownAsync(); | 765 CallShutdownAsync(); |
| 741 | 766 |
| 742 // Run tasks asynchronously. | 767 // Run tasks asynchronously. |
| 743 std::vector<std::unique_ptr<ThreadPostingAndRunningTask>> run_threads; | 768 std::vector<std::unique_ptr<ThreadPostingAndRunningTask>> run_threads; |
| 744 | 769 |
| 745 for (auto& task : tasks) { | 770 for (auto& task : tasks) { |
| 746 run_threads.push_back(MakeUnique<ThreadPostingAndRunningTask>( | 771 run_threads.push_back(MakeUnique<ThreadPostingAndRunningTask>( |
| 747 &tracker_, std::move(task), ThreadPostingAndRunningTask::Action::RUN, | 772 tracker_.get(), std::move(task), |
| 748 false)); | 773 ThreadPostingAndRunningTask::Action::RUN, false)); |
| 749 run_threads.back()->Start(); | 774 run_threads.back()->Start(); |
| 750 } | 775 } |
| 751 | 776 |
| 752 for (const auto& thread : run_threads) | 777 for (const auto& thread : run_threads) |
| 753 thread->Join(); | 778 thread->Join(); |
| 754 | 779 |
| 755 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); | 780 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
| 756 | 781 |
| 757 // Expect BLOCK_SHUTDOWN tasks to have been executed. | 782 // Expect BLOCK_SHUTDOWN tasks to have been executed. |
| 758 EXPECT_EQ(kLoadTestNumIterations, NumTasksExecuted()); | 783 EXPECT_EQ(kLoadTestNumIterations, NumTasksExecuted()); |
| 759 } | 784 } |
| 760 | 785 |
| 761 TEST_F(TaskSchedulerTaskTrackerTest, LoadWillPostAndRunDuringShutdown) { | 786 TEST_F(TaskSchedulerTaskTrackerTest, LoadWillPostAndRunDuringShutdown) { |
| 762 // Inform |task_tracker_| that a BLOCK_SHUTDOWN task will be posted just to | 787 // Inform |task_tracker_| that a BLOCK_SHUTDOWN task will be posted just to |
| 763 // block shutdown. | 788 // block shutdown. |
| 764 std::unique_ptr<Task> block_shutdown_task( | 789 std::unique_ptr<Task> block_shutdown_task( |
| 765 CreateTask(TaskShutdownBehavior::BLOCK_SHUTDOWN)); | 790 CreateTask(TaskShutdownBehavior::BLOCK_SHUTDOWN)); |
| 766 EXPECT_TRUE(tracker_.WillPostTask(block_shutdown_task.get())); | 791 EXPECT_TRUE(tracker_->WillPostTask(block_shutdown_task.get())); |
| 767 | 792 |
| 768 // Call Shutdown() asynchronously. | 793 // Call Shutdown() asynchronously. |
| 769 CallShutdownAsync(); | 794 CallShutdownAsync(); |
| 770 | 795 |
| 771 // Post and run tasks asynchronously. | 796 // Post and run tasks asynchronously. |
| 772 std::vector<std::unique_ptr<ThreadPostingAndRunningTask>> threads; | 797 std::vector<std::unique_ptr<ThreadPostingAndRunningTask>> threads; |
| 773 | 798 |
| 774 for (size_t i = 0; i < kLoadTestNumIterations; ++i) { | 799 for (size_t i = 0; i < kLoadTestNumIterations; ++i) { |
| 775 threads.push_back(MakeUnique<ThreadPostingAndRunningTask>( | 800 threads.push_back(MakeUnique<ThreadPostingAndRunningTask>( |
| 776 &tracker_, CreateTask(TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN), | 801 tracker_.get(), CreateTask(TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN), |
| 777 ThreadPostingAndRunningTask::Action::WILL_POST_AND_RUN, false)); | 802 ThreadPostingAndRunningTask::Action::WILL_POST_AND_RUN, false)); |
| 778 threads.back()->Start(); | 803 threads.back()->Start(); |
| 779 | 804 |
| 780 threads.push_back(MakeUnique<ThreadPostingAndRunningTask>( | 805 threads.push_back(MakeUnique<ThreadPostingAndRunningTask>( |
| 781 &tracker_, CreateTask(TaskShutdownBehavior::SKIP_ON_SHUTDOWN), | 806 tracker_.get(), CreateTask(TaskShutdownBehavior::SKIP_ON_SHUTDOWN), |
| 782 ThreadPostingAndRunningTask::Action::WILL_POST_AND_RUN, false)); | 807 ThreadPostingAndRunningTask::Action::WILL_POST_AND_RUN, false)); |
| 783 threads.back()->Start(); | 808 threads.back()->Start(); |
| 784 | 809 |
| 785 threads.push_back(MakeUnique<ThreadPostingAndRunningTask>( | 810 threads.push_back(MakeUnique<ThreadPostingAndRunningTask>( |
| 786 &tracker_, CreateTask(TaskShutdownBehavior::BLOCK_SHUTDOWN), | 811 tracker_.get(), CreateTask(TaskShutdownBehavior::BLOCK_SHUTDOWN), |
| 787 ThreadPostingAndRunningTask::Action::WILL_POST_AND_RUN, true)); | 812 ThreadPostingAndRunningTask::Action::WILL_POST_AND_RUN, true)); |
| 788 threads.back()->Start(); | 813 threads.back()->Start(); |
| 789 } | 814 } |
| 790 | 815 |
| 791 for (const auto& thread : threads) | 816 for (const auto& thread : threads) |
| 792 thread->Join(); | 817 thread->Join(); |
| 793 | 818 |
| 794 // Expect BLOCK_SHUTDOWN tasks to have been executed. | 819 // Expect BLOCK_SHUTDOWN tasks to have been executed. |
| 795 EXPECT_EQ(kLoadTestNumIterations, NumTasksExecuted()); | 820 EXPECT_EQ(kLoadTestNumIterations, NumTasksExecuted()); |
| 796 | 821 |
| 797 // Shutdown() shouldn't return before |block_shutdown_task| is executed. | 822 // Shutdown() shouldn't return before |block_shutdown_task| is executed. |
| 798 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); | 823 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); |
| 799 | 824 |
| 800 // Unblock shutdown by running |block_shutdown_task|. | 825 // Unblock shutdown by running |block_shutdown_task|. |
| 801 EXPECT_TRUE(tracker_.RunTask(std::move(block_shutdown_task), | 826 EXPECT_TRUE(tracker_->RunTask(std::move(block_shutdown_task), |
| 802 SequenceToken::Create())); | 827 SequenceToken::Create())); |
| 803 EXPECT_EQ(kLoadTestNumIterations + 1, NumTasksExecuted()); | 828 EXPECT_EQ(kLoadTestNumIterations + 1, NumTasksExecuted()); |
| 804 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); | 829 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
| 805 } | 830 } |
| 806 | 831 |
| 807 } // namespace internal | 832 } // namespace internal |
| 808 } // namespace base | 833 } // namespace base |
| OLD | NEW |