| 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 <memory> | 7 #include <memory> |
| 8 #include <queue> | |
| 9 | 8 |
| 10 #include "base/bind.h" | 9 #include "base/bind.h" |
| 11 #include "base/logging.h" | 10 #include "base/logging.h" |
| 12 #include "base/macros.h" | 11 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 14 #include "base/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
| 15 #include "base/task_scheduler/task.h" | 14 #include "base/task_scheduler/task.h" |
| 16 #include "base/task_scheduler/task_traits.h" | 15 #include "base/task_scheduler/task_traits.h" |
| 17 #include "base/task_scheduler/test_utils.h" | 16 #include "base/task_scheduler/test_utils.h" |
| 18 #include "base/threading/platform_thread.h" | 17 #include "base/threading/platform_thread.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 TaskSchedulerTaskTrackerTest() = default; | 67 TaskSchedulerTaskTrackerTest() = default; |
| 69 | 68 |
| 70 // Creates a task with |shutdown_behavior|. | 69 // Creates a task with |shutdown_behavior|. |
| 71 std::unique_ptr<Task> CreateTask(TaskShutdownBehavior shutdown_behavior) { | 70 std::unique_ptr<Task> CreateTask(TaskShutdownBehavior shutdown_behavior) { |
| 72 return WrapUnique(new Task( | 71 return WrapUnique(new Task( |
| 73 FROM_HERE, | 72 FROM_HERE, |
| 74 Bind(&TaskSchedulerTaskTrackerTest::RunTaskCallback, Unretained(this)), | 73 Bind(&TaskSchedulerTaskTrackerTest::RunTaskCallback, Unretained(this)), |
| 75 TaskTraits().WithShutdownBehavior(shutdown_behavior))); | 74 TaskTraits().WithShutdownBehavior(shutdown_behavior))); |
| 76 } | 75 } |
| 77 | 76 |
| 78 // Tries to post |task| via |tracker_|. If |tracker_| approves the operation, | |
| 79 // |task| is added to |posted_tasks_|. | |
| 80 void PostTaskViaTracker(std::unique_ptr<Task> task) { | |
| 81 tracker_.PostTask( | |
| 82 Bind(&TaskSchedulerTaskTrackerTest::PostTaskCallback, Unretained(this)), | |
| 83 std::move(task)); | |
| 84 } | |
| 85 | |
| 86 // Tries to run the next task in |posted_tasks_| via |tracker_|. | |
| 87 void RunNextPostedTaskViaTracker() { | |
| 88 ASSERT_FALSE(posted_tasks_.empty()); | |
| 89 tracker_.RunTask(posted_tasks_.front().get()); | |
| 90 posted_tasks_.pop(); | |
| 91 } | |
| 92 | |
| 93 // Calls tracker_->Shutdown() on a new thread. When this returns, Shutdown() | 77 // Calls tracker_->Shutdown() on a new thread. When this returns, Shutdown() |
| 94 // method has been entered on the new thread, but it hasn't necessarily | 78 // method has been entered on the new thread, but it hasn't necessarily |
| 95 // returned. | 79 // returned. |
| 96 void CallShutdownAsync() { | 80 void CallShutdownAsync() { |
| 97 ASSERT_FALSE(thread_calling_shutdown_); | 81 ASSERT_FALSE(thread_calling_shutdown_); |
| 98 thread_calling_shutdown_.reset(new ThreadCallingShutdown(&tracker_)); | 82 thread_calling_shutdown_.reset(new ThreadCallingShutdown(&tracker_)); |
| 99 thread_calling_shutdown_->Start(); | 83 thread_calling_shutdown_->Start(); |
| 100 while (!tracker_.IsShuttingDownForTesting() && | 84 while (!tracker_.IsShuttingDownForTesting() && |
| 101 !tracker_.shutdown_completed()) { | 85 !tracker_.shutdown_completed()) { |
| 102 PlatformThread::YieldCurrentThread(); | 86 PlatformThread::YieldCurrentThread(); |
| 103 } | 87 } |
| 104 } | 88 } |
| 105 | 89 |
| 106 void WaitForAsyncShutdownCompleted() { | 90 void WaitForAsyncShutdownCompleted() { |
| 107 ASSERT_TRUE(thread_calling_shutdown_); | 91 ASSERT_TRUE(thread_calling_shutdown_); |
| 108 thread_calling_shutdown_->Join(); | 92 thread_calling_shutdown_->Join(); |
| 109 EXPECT_TRUE(thread_calling_shutdown_->has_returned()); | 93 EXPECT_TRUE(thread_calling_shutdown_->has_returned()); |
| 110 EXPECT_TRUE(tracker_.shutdown_completed()); | 94 EXPECT_TRUE(tracker_.shutdown_completed()); |
| 111 } | 95 } |
| 112 | 96 |
| 113 void VerifyAsyncShutdownInProgress() { | 97 void VerifyAsyncShutdownInProgress() { |
| 114 ASSERT_TRUE(thread_calling_shutdown_); | 98 ASSERT_TRUE(thread_calling_shutdown_); |
| 115 EXPECT_FALSE(thread_calling_shutdown_->has_returned()); | 99 EXPECT_FALSE(thread_calling_shutdown_->has_returned()); |
| 116 EXPECT_FALSE(tracker_.shutdown_completed()); | 100 EXPECT_FALSE(tracker_.shutdown_completed()); |
| 117 EXPECT_TRUE(tracker_.IsShuttingDownForTesting()); | 101 EXPECT_TRUE(tracker_.IsShuttingDownForTesting()); |
| 118 } | 102 } |
| 119 | 103 |
| 120 TaskTracker tracker_; | 104 TaskTracker tracker_; |
| 121 size_t num_tasks_executed_ = 0; | 105 size_t num_tasks_executed_ = 0; |
| 122 std::queue<std::unique_ptr<Task>> posted_tasks_; | |
| 123 | 106 |
| 124 private: | 107 private: |
| 125 void PostTaskCallback(std::unique_ptr<Task> task) { | |
| 126 posted_tasks_.push(std::move(task)); | |
| 127 } | |
| 128 | |
| 129 void RunTaskCallback() { ++num_tasks_executed_; } | 108 void RunTaskCallback() { ++num_tasks_executed_; } |
| 130 | 109 |
| 131 std::unique_ptr<ThreadCallingShutdown> thread_calling_shutdown_; | 110 std::unique_ptr<ThreadCallingShutdown> thread_calling_shutdown_; |
| 132 | 111 |
| 133 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerTaskTrackerTest); | 112 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerTaskTrackerTest); |
| 134 }; | 113 }; |
| 135 | 114 |
| 136 #define WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED() \ | 115 #define WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED() \ |
| 137 do { \ | 116 do { \ |
| 138 SCOPED_TRACE(""); \ | 117 SCOPED_TRACE(""); \ |
| 139 WaitForAsyncShutdownCompleted(); \ | 118 WaitForAsyncShutdownCompleted(); \ |
| 140 } while (false) | 119 } while (false) |
| 141 | 120 |
| 142 #define VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS() \ | 121 #define VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS() \ |
| 143 do { \ | 122 do { \ |
| 144 SCOPED_TRACE(""); \ | 123 SCOPED_TRACE(""); \ |
| 145 VerifyAsyncShutdownInProgress(); \ | 124 VerifyAsyncShutdownInProgress(); \ |
| 146 } while (false) | 125 } while (false) |
| 147 | 126 |
| 148 } // namespace | 127 } // namespace |
| 149 | 128 |
| 150 TEST_P(TaskSchedulerTaskTrackerTest, PostAndRunBeforeShutdown) { | 129 TEST_P(TaskSchedulerTaskTrackerTest, WillPostAndRunBeforeShutdown) { |
| 151 std::unique_ptr<Task> task_to_post(CreateTask(GetParam())); | 130 std::unique_ptr<Task> task(CreateTask(GetParam())); |
| 152 const Task* task_to_post_raw = task_to_post.get(); | |
| 153 | 131 |
| 154 // Post the task. | 132 // Inform |task_tracker_| that |task| will be posted. |
| 155 EXPECT_TRUE(posted_tasks_.empty()); | 133 EXPECT_TRUE(tracker_.WillPostTask(task.get())); |
| 156 PostTaskViaTracker(std::move(task_to_post)); | |
| 157 EXPECT_EQ(1U, posted_tasks_.size()); | |
| 158 EXPECT_EQ(task_to_post_raw, posted_tasks_.front().get()); | |
| 159 | 134 |
| 160 // Run the posted task. | 135 // Run the task. |
| 161 EXPECT_EQ(0U, num_tasks_executed_); | 136 EXPECT_EQ(0U, num_tasks_executed_); |
| 162 RunNextPostedTaskViaTracker(); | 137 tracker_.RunTask(task.get()); |
| 163 EXPECT_EQ(1U, num_tasks_executed_); | 138 EXPECT_EQ(1U, num_tasks_executed_); |
| 164 | 139 |
| 165 // Shutdown() shouldn't block. | 140 // Shutdown() shouldn't block. |
| 166 tracker_.Shutdown(); | 141 tracker_.Shutdown(); |
| 167 } | 142 } |
| 168 | 143 |
| 169 TEST_P(TaskSchedulerTaskTrackerTest, PostAndRunLongTaskBeforeShutdown) { | 144 TEST_P(TaskSchedulerTaskTrackerTest, WillPostAndRunLongTaskBeforeShutdown) { |
| 170 // Post a task that will block until |event| is signaled. | 145 // Create a task that will block until |event| is signaled. |
| 171 EXPECT_TRUE(posted_tasks_.empty()); | |
| 172 WaitableEvent event(false, false); | 146 WaitableEvent event(false, false); |
| 173 PostTaskViaTracker(WrapUnique( | 147 std::unique_ptr<Task> blocked_task( |
| 174 new Task(FROM_HERE, Bind(&WaitableEvent::Wait, Unretained(&event)), | 148 new Task(FROM_HERE, Bind(&WaitableEvent::Wait, Unretained(&event)), |
| 175 TaskTraits().WithShutdownBehavior(GetParam())))); | 149 TaskTraits().WithShutdownBehavior(GetParam()))); |
| 176 EXPECT_EQ(1U, posted_tasks_.size()); | 150 |
| 151 // Inform |task_tracker_| that |blocked_task| will be posted. |
| 152 EXPECT_TRUE(tracker_.WillPostTask(blocked_task.get())); |
| 177 | 153 |
| 178 // Run the task asynchronouly. | 154 // Run the task asynchronouly. |
| 179 ThreadRunningTask thread_running_task(&tracker_, posted_tasks_.front().get()); | 155 ThreadRunningTask thread_running_task(&tracker_, blocked_task.get()); |
| 180 thread_running_task.Start(); | 156 thread_running_task.Start(); |
| 181 | 157 |
| 182 // Initiate shutdown while the task is running. | 158 // Initiate shutdown while the task is running. |
| 183 CallShutdownAsync(); | 159 CallShutdownAsync(); |
| 184 | 160 |
| 185 if (GetParam() == TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) { | 161 if (GetParam() == TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) { |
| 186 // Shutdown should complete even with a CONTINUE_ON_SHUTDOWN in progress. | 162 // Shutdown should complete even with a CONTINUE_ON_SHUTDOWN in progress. |
| 187 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); | 163 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
| 188 } else { | 164 } else { |
| 189 // Shutdown should block with any non CONTINUE_ON_SHUTDOWN task in progress. | 165 // Shutdown should block with any non CONTINUE_ON_SHUTDOWN task in progress. |
| 190 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); | 166 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); |
| 191 } | 167 } |
| 192 | 168 |
| 193 // Unblock the task. | 169 // Unblock the task. |
| 194 event.Signal(); | 170 event.Signal(); |
| 195 thread_running_task.Join(); | 171 thread_running_task.Join(); |
| 196 | 172 |
| 197 // Shutdown should now complete for a non CONTINUE_ON_SHUTDOWN task. | 173 // Shutdown should now complete for a non CONTINUE_ON_SHUTDOWN task. |
| 198 if (GetParam() != TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) | 174 if (GetParam() != TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) |
| 199 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); | 175 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
| 200 } | 176 } |
| 201 | 177 |
| 202 TEST_P(TaskSchedulerTaskTrackerTest, PostBeforeShutdownRunDuringShutdown) { | 178 TEST_P(TaskSchedulerTaskTrackerTest, WillPostBeforeShutdownRunDuringShutdown) { |
| 203 std::unique_ptr<Task> task_to_post(CreateTask(GetParam())); | 179 // Inform |task_tracker_| that a task will be posted. |
| 204 const Task* task_to_post_raw = task_to_post.get(); | 180 std::unique_ptr<Task> task(CreateTask(GetParam())); |
| 181 EXPECT_TRUE(tracker_.WillPostTask(task.get())); |
| 205 | 182 |
| 206 // Post the task. | 183 // Inform |task_tracker_| that a BLOCK_SHUTDOWN task will be posted just to |
| 207 EXPECT_TRUE(posted_tasks_.empty()); | 184 // block shutdown. |
| 208 PostTaskViaTracker(std::move(task_to_post)); | 185 std::unique_ptr<Task> block_shutdown_task( |
| 209 EXPECT_EQ(1U, posted_tasks_.size()); | 186 CreateTask(TaskShutdownBehavior::BLOCK_SHUTDOWN)); |
| 210 EXPECT_EQ(task_to_post_raw, posted_tasks_.front().get()); | 187 EXPECT_TRUE(tracker_.WillPostTask(block_shutdown_task.get())); |
| 211 | |
| 212 // Post a BLOCK_SHUTDOWN task just to block shutdown. | |
| 213 PostTaskViaTracker(CreateTask(TaskShutdownBehavior::BLOCK_SHUTDOWN)); | |
| 214 | 188 |
| 215 // Call Shutdown() asynchronously. | 189 // Call Shutdown() asynchronously. |
| 216 CallShutdownAsync(); | 190 CallShutdownAsync(); |
| 217 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); | 191 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); |
| 218 | 192 |
| 219 // Try to run the first posted task. Only BLOCK_SHUTDOWN tasks should run, | 193 // Try to run |task|. It should only run it it's BLOCK_SHUTDOWN. Otherwise it |
| 220 // others should be discarded. | 194 // should be discarded. |
| 221 EXPECT_EQ(0U, num_tasks_executed_); | 195 EXPECT_EQ(0U, num_tasks_executed_); |
| 222 RunNextPostedTaskViaTracker(); | 196 tracker_.RunTask(task.get()); |
| 223 EXPECT_EQ(GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN ? 1U : 0U, | 197 EXPECT_EQ(GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN ? 1U : 0U, |
| 224 num_tasks_executed_); | 198 num_tasks_executed_); |
| 225 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); | 199 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); |
| 226 | 200 |
| 227 // Unblock shutdown by running the remaining BLOCK_SHUTDOWN task. | 201 // Unblock shutdown by running the remaining BLOCK_SHUTDOWN task. |
| 228 RunNextPostedTaskViaTracker(); | 202 tracker_.RunTask(block_shutdown_task.get()); |
| 229 EXPECT_EQ(GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN ? 2U : 1U, | 203 EXPECT_EQ(GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN ? 2U : 1U, |
| 230 num_tasks_executed_); | 204 num_tasks_executed_); |
| 231 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); | 205 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
| 232 } | 206 } |
| 233 | 207 |
| 234 TEST_P(TaskSchedulerTaskTrackerTest, PostBeforeShutdownRunAfterShutdown) { | 208 TEST_P(TaskSchedulerTaskTrackerTest, WillPostBeforeShutdownRunAfterShutdown) { |
| 235 std::unique_ptr<Task> task_to_post(CreateTask(GetParam())); | 209 // Inform |task_tracker_| that a task will be posted. |
| 236 const Task* task_to_post_raw = task_to_post.get(); | 210 std::unique_ptr<Task> task(CreateTask(GetParam())); |
| 237 | 211 EXPECT_TRUE(tracker_.WillPostTask(task.get())); |
| 238 // Post the task. | |
| 239 EXPECT_TRUE(posted_tasks_.empty()); | |
| 240 PostTaskViaTracker(std::move(task_to_post)); | |
| 241 EXPECT_EQ(1U, posted_tasks_.size()); | |
| 242 EXPECT_EQ(task_to_post_raw, posted_tasks_.front().get()); | |
| 243 | 212 |
| 244 // Call Shutdown() asynchronously. | 213 // Call Shutdown() asynchronously. |
| 245 CallShutdownAsync(); | 214 CallShutdownAsync(); |
| 246 EXPECT_EQ(0U, num_tasks_executed_); | 215 EXPECT_EQ(0U, num_tasks_executed_); |
| 247 | 216 |
| 248 if (GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN) { | 217 if (GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN) { |
| 249 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); | 218 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); |
| 250 | 219 |
| 251 // Run the task to unblock shutdown. | 220 // Run the task to unblock shutdown. |
| 252 RunNextPostedTaskViaTracker(); | 221 tracker_.RunTask(task.get()); |
| 253 EXPECT_EQ(1U, num_tasks_executed_); | 222 EXPECT_EQ(1U, num_tasks_executed_); |
| 254 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); | 223 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
| 255 | 224 |
| 256 // It is not possible to test running a BLOCK_SHUTDOWN task posted before | 225 // It is not possible to test running a BLOCK_SHUTDOWN task posted before |
| 257 // shutdown after shutdown because Shutdown() won't return if there are | 226 // shutdown after shutdown because Shutdown() won't return if there are |
| 258 // pending BLOCK_SHUTDOWN tasks. | 227 // pending BLOCK_SHUTDOWN tasks. |
| 259 } else { | 228 } else { |
| 260 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); | 229 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
| 261 | 230 |
| 262 // The task shouldn't be allowed to run after shutdown. | 231 // The task shouldn't be allowed to run after shutdown. |
| 263 RunNextPostedTaskViaTracker(); | 232 tracker_.RunTask(task.get()); |
| 264 EXPECT_EQ(0U, num_tasks_executed_); | 233 EXPECT_EQ(0U, num_tasks_executed_); |
| 265 } | 234 } |
| 266 } | 235 } |
| 267 | 236 |
| 268 TEST_P(TaskSchedulerTaskTrackerTest, PostAndRunDuringShutdown) { | 237 TEST_P(TaskSchedulerTaskTrackerTest, WillPostAndRunDuringShutdown) { |
| 269 // Post a BLOCK_SHUTDOWN task just to block shutdown. | 238 // Inform |task_tracker_| that a BLOCK_SHUTDOWN task will be posted just to |
| 270 PostTaskViaTracker(CreateTask(TaskShutdownBehavior::BLOCK_SHUTDOWN)); | 239 // block shutdown. |
| 271 std::unique_ptr<Task> block_shutdown_task = std::move(posted_tasks_.front()); | 240 std::unique_ptr<Task> block_shutdown_task( |
| 272 posted_tasks_.pop(); | 241 CreateTask(TaskShutdownBehavior::BLOCK_SHUTDOWN)); |
| 242 EXPECT_TRUE(tracker_.WillPostTask(block_shutdown_task.get())); |
| 273 | 243 |
| 274 // Call Shutdown() asynchronously. | 244 // Call Shutdown() asynchronously. |
| 275 CallShutdownAsync(); | 245 CallShutdownAsync(); |
| 276 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); | 246 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); |
| 277 | 247 |
| 278 if (GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN) { | 248 if (GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN) { |
| 279 // Post a BLOCK_SHUTDOWN task. This should succeed. | 249 // Inform |task_tracker_| that a BLOCK_SHUTDOWN task will be posted. |
| 280 EXPECT_TRUE(posted_tasks_.empty()); | 250 std::unique_ptr<Task> task(CreateTask(GetParam())); |
| 281 PostTaskViaTracker(CreateTask(GetParam())); | 251 EXPECT_TRUE(tracker_.WillPostTask(task.get())); |
| 282 EXPECT_EQ(1U, posted_tasks_.size()); | |
| 283 | 252 |
| 284 // Run the BLOCK_SHUTDOWN task. This should succeed. | 253 // Run the BLOCK_SHUTDOWN task. |
| 285 EXPECT_EQ(0U, num_tasks_executed_); | 254 EXPECT_EQ(0U, num_tasks_executed_); |
| 286 RunNextPostedTaskViaTracker(); | 255 tracker_.RunTask(task.get()); |
| 287 EXPECT_EQ(1U, num_tasks_executed_); | 256 EXPECT_EQ(1U, num_tasks_executed_); |
| 288 } else { | 257 } else { |
| 289 // It shouldn't be possible to post a non BLOCK_SHUTDOWN task. | 258 // It shouldn't be allowed to post a non BLOCK_SHUTDOWN task. |
| 290 PostTaskViaTracker(CreateTask(GetParam())); | 259 std::unique_ptr<Task> task(CreateTask(GetParam())); |
| 291 EXPECT_TRUE(posted_tasks_.empty()); | 260 EXPECT_FALSE(tracker_.WillPostTask(task.get())); |
| 292 | 261 |
| 293 // Don't try to run the task, because it hasn't been posted successfully. | 262 // Don't try to run the task, because it wasn't allowed to be posted. |
| 294 } | 263 } |
| 295 | 264 |
| 296 // Unblock shutdown by running the BLOCK_SHUTDOWN task posted at the beginning | 265 // Unblock shutdown by running |block_shutdown_task|. |
| 297 // of the test. | |
| 298 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); | 266 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); |
| 299 tracker_.RunTask(block_shutdown_task.get()); | 267 tracker_.RunTask(block_shutdown_task.get()); |
| 300 EXPECT_EQ(GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN ? 2U : 1U, | 268 EXPECT_EQ(GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN ? 2U : 1U, |
| 301 num_tasks_executed_); | 269 num_tasks_executed_); |
| 302 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); | 270 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
| 303 } | 271 } |
| 304 | 272 |
| 305 TEST_P(TaskSchedulerTaskTrackerTest, PostAfterShutdown) { | 273 TEST_P(TaskSchedulerTaskTrackerTest, WillPostAfterShutdown) { |
| 306 // It is not possible to post a task after shutdown. | |
| 307 tracker_.Shutdown(); | 274 tracker_.Shutdown(); |
| 308 EXPECT_TRUE(posted_tasks_.empty()); | |
| 309 | 275 |
| 276 std::unique_ptr<Task> task(CreateTask(GetParam())); |
| 277 |
| 278 // |task_tracker_| shouldn't allow a task to be posted after shutdown. |
| 310 if (GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN) { | 279 if (GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN) { |
| 311 EXPECT_DCHECK_DEATH({ PostTaskViaTracker(CreateTask(GetParam())); }, ""); | 280 EXPECT_DCHECK_DEATH({ tracker_.WillPostTask(task.get()); }, ""); |
| 312 } else { | 281 } else { |
| 313 PostTaskViaTracker(CreateTask(GetParam())); | 282 EXPECT_FALSE(tracker_.WillPostTask(task.get())); |
| 314 } | 283 } |
| 315 | |
| 316 EXPECT_TRUE(posted_tasks_.empty()); | |
| 317 } | 284 } |
| 318 | 285 |
| 319 INSTANTIATE_TEST_CASE_P( | 286 INSTANTIATE_TEST_CASE_P( |
| 320 ContinueOnShutdown, | 287 ContinueOnShutdown, |
| 321 TaskSchedulerTaskTrackerTest, | 288 TaskSchedulerTaskTrackerTest, |
| 322 ::testing::Values(TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN)); | 289 ::testing::Values(TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN)); |
| 323 INSTANTIATE_TEST_CASE_P( | 290 INSTANTIATE_TEST_CASE_P( |
| 324 SkipOnShutdown, | 291 SkipOnShutdown, |
| 325 TaskSchedulerTaskTrackerTest, | 292 TaskSchedulerTaskTrackerTest, |
| 326 ::testing::Values(TaskShutdownBehavior::SKIP_ON_SHUTDOWN)); | 293 ::testing::Values(TaskShutdownBehavior::SKIP_ON_SHUTDOWN)); |
| 327 INSTANTIATE_TEST_CASE_P( | 294 INSTANTIATE_TEST_CASE_P( |
| 328 BlockShutdown, | 295 BlockShutdown, |
| 329 TaskSchedulerTaskTrackerTest, | 296 TaskSchedulerTaskTrackerTest, |
| 330 ::testing::Values(TaskShutdownBehavior::BLOCK_SHUTDOWN)); | 297 ::testing::Values(TaskShutdownBehavior::BLOCK_SHUTDOWN)); |
| 331 | 298 |
| 332 } // namespace internal | 299 } // namespace internal |
| 333 } // namespace base | 300 } // namespace base |
| OLD | NEW |