Chromium Code Reviews| 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, PostAndRunBeforeShutdown) { |
| 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 // Post the task. |
|
danakj
2016/04/06 19:38:30
update comment
robliao
2016/04/06 19:42:33
Maybe update the test names too.
fdoray
2016/04/06 20:59:27
Done.
| |
| 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 posted 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, PostAndRunLongTaskBeforeShutdown) { |
| 170 // Post a task that will block until |event| is signaled. | 145 // Post 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 EXPECT_TRUE(tracker_.WillPostTask(blocked_task.get())); |
| 177 | 151 |
| 178 // Run the task asynchronouly. | 152 // Run the task asynchronouly. |
| 179 ThreadRunningTask thread_running_task(&tracker_, posted_tasks_.front().get()); | 153 ThreadRunningTask thread_running_task(&tracker_, blocked_task.get()); |
| 180 thread_running_task.Start(); | 154 thread_running_task.Start(); |
| 181 | 155 |
| 182 // Initiate shutdown while the task is running. | 156 // Initiate shutdown while the task is running. |
| 183 CallShutdownAsync(); | 157 CallShutdownAsync(); |
| 184 | 158 |
| 185 if (GetParam() == TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) { | 159 if (GetParam() == TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) { |
| 186 // Shutdown should complete even with a CONTINUE_ON_SHUTDOWN in progress. | 160 // Shutdown should complete even with a CONTINUE_ON_SHUTDOWN in progress. |
| 187 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); | 161 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
| 188 } else { | 162 } else { |
| 189 // Shutdown should block with any non CONTINUE_ON_SHUTDOWN task in progress. | 163 // Shutdown should block with any non CONTINUE_ON_SHUTDOWN task in progress. |
| 190 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); | 164 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); |
| 191 } | 165 } |
| 192 | 166 |
| 193 // Unblock the task. | 167 // Unblock the task. |
| 194 event.Signal(); | 168 event.Signal(); |
| 195 thread_running_task.Join(); | 169 thread_running_task.Join(); |
| 196 | 170 |
| 197 // Shutdown should now complete for a non CONTINUE_ON_SHUTDOWN task. | 171 // Shutdown should now complete for a non CONTINUE_ON_SHUTDOWN task. |
| 198 if (GetParam() != TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) | 172 if (GetParam() != TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) |
| 199 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); | 173 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
| 200 } | 174 } |
| 201 | 175 |
| 202 TEST_P(TaskSchedulerTaskTrackerTest, PostBeforeShutdownRunDuringShutdown) { | 176 TEST_P(TaskSchedulerTaskTrackerTest, PostBeforeShutdownRunDuringShutdown) { |
| 203 std::unique_ptr<Task> task_to_post(CreateTask(GetParam())); | 177 // Post a task. |
|
danakj
2016/04/06 19:38:30
update comment?
fdoray
2016/04/06 20:59:27
Done.
| |
| 204 const Task* task_to_post_raw = task_to_post.get(); | 178 std::unique_ptr<Task> task(CreateTask(GetParam())); |
| 205 | 179 EXPECT_TRUE(tracker_.WillPostTask(task.get())); |
| 206 // Post the task. | |
| 207 EXPECT_TRUE(posted_tasks_.empty()); | |
| 208 PostTaskViaTracker(std::move(task_to_post)); | |
| 209 EXPECT_EQ(1U, posted_tasks_.size()); | |
| 210 EXPECT_EQ(task_to_post_raw, posted_tasks_.front().get()); | |
| 211 | 180 |
| 212 // Post a BLOCK_SHUTDOWN task just to block shutdown. | 181 // Post a BLOCK_SHUTDOWN task just to block shutdown. |
|
danakj
2016/04/06 19:38:30
and here, "Post" isn't strictly right.
fdoray
2016/04/06 20:59:27
Done.
| |
| 213 PostTaskViaTracker(CreateTask(TaskShutdownBehavior::BLOCK_SHUTDOWN)); | 182 std::unique_ptr<Task> block_shutdown_task( |
| 183 CreateTask(TaskShutdownBehavior::BLOCK_SHUTDOWN)); | |
| 184 EXPECT_TRUE(tracker_.WillPostTask(block_shutdown_task.get())); | |
| 214 | 185 |
| 215 // Call Shutdown() asynchronously. | 186 // Call Shutdown() asynchronously. |
| 216 CallShutdownAsync(); | 187 CallShutdownAsync(); |
| 217 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); | 188 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); |
| 218 | 189 |
| 219 // Try to run the first posted task. Only BLOCK_SHUTDOWN tasks should run, | 190 // Try to run the first posted task. Only BLOCK_SHUTDOWN tasks should run, |
| 220 // others should be discarded. | 191 // others should be discarded. |
| 221 EXPECT_EQ(0U, num_tasks_executed_); | 192 EXPECT_EQ(0U, num_tasks_executed_); |
| 222 RunNextPostedTaskViaTracker(); | 193 tracker_.RunTask(task.get()); |
| 223 EXPECT_EQ(GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN ? 1U : 0U, | 194 EXPECT_EQ(GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN ? 1U : 0U, |
| 224 num_tasks_executed_); | 195 num_tasks_executed_); |
| 225 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); | 196 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); |
| 226 | 197 |
| 227 // Unblock shutdown by running the remaining BLOCK_SHUTDOWN task. | 198 // Unblock shutdown by running the remaining BLOCK_SHUTDOWN task. |
| 228 RunNextPostedTaskViaTracker(); | 199 tracker_.RunTask(block_shutdown_task.get()); |
| 229 EXPECT_EQ(GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN ? 2U : 1U, | 200 EXPECT_EQ(GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN ? 2U : 1U, |
| 230 num_tasks_executed_); | 201 num_tasks_executed_); |
| 231 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); | 202 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
| 232 } | 203 } |
| 233 | 204 |
| 234 TEST_P(TaskSchedulerTaskTrackerTest, PostBeforeShutdownRunAfterShutdown) { | 205 TEST_P(TaskSchedulerTaskTrackerTest, PostBeforeShutdownRunAfterShutdown) { |
| 235 std::unique_ptr<Task> task_to_post(CreateTask(GetParam())); | 206 // Post a task. |
|
robliao
2016/04/06 19:42:33
Same here.
fdoray
2016/04/06 20:59:27
Done.
| |
| 236 const Task* task_to_post_raw = task_to_post.get(); | 207 std::unique_ptr<Task> task(CreateTask(GetParam())); |
| 237 | 208 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 | 209 |
| 244 // Call Shutdown() asynchronously. | 210 // Call Shutdown() asynchronously. |
| 245 CallShutdownAsync(); | 211 CallShutdownAsync(); |
| 246 EXPECT_EQ(0U, num_tasks_executed_); | 212 EXPECT_EQ(0U, num_tasks_executed_); |
| 247 | 213 |
| 248 if (GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN) { | 214 if (GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN) { |
| 249 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); | 215 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); |
| 250 | 216 |
| 251 // Run the task to unblock shutdown. | 217 // Run the task to unblock shutdown. |
| 252 RunNextPostedTaskViaTracker(); | 218 tracker_.RunTask(task.get()); |
| 253 EXPECT_EQ(1U, num_tasks_executed_); | 219 EXPECT_EQ(1U, num_tasks_executed_); |
| 254 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); | 220 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
| 255 | 221 |
| 256 // It is not possible to test running a BLOCK_SHUTDOWN task posted before | 222 // 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 | 223 // shutdown after shutdown because Shutdown() won't return if there are |
| 258 // pending BLOCK_SHUTDOWN tasks. | 224 // pending BLOCK_SHUTDOWN tasks. |
| 259 } else { | 225 } else { |
| 260 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); | 226 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
| 261 | 227 |
| 262 // The task shouldn't be allowed to run after shutdown. | 228 // The task shouldn't be allowed to run after shutdown. |
| 263 RunNextPostedTaskViaTracker(); | 229 tracker_.RunTask(task.get()); |
| 264 EXPECT_EQ(0U, num_tasks_executed_); | 230 EXPECT_EQ(0U, num_tasks_executed_); |
| 265 } | 231 } |
| 266 } | 232 } |
| 267 | 233 |
| 268 TEST_P(TaskSchedulerTaskTrackerTest, PostAndRunDuringShutdown) { | 234 TEST_P(TaskSchedulerTaskTrackerTest, PostAndRunDuringShutdown) { |
| 269 // Post a BLOCK_SHUTDOWN task just to block shutdown. | 235 // Post a BLOCK_SHUTDOWN task just to block shutdown. |
| 270 PostTaskViaTracker(CreateTask(TaskShutdownBehavior::BLOCK_SHUTDOWN)); | 236 std::unique_ptr<Task> block_shutdown_task( |
| 271 std::unique_ptr<Task> block_shutdown_task = std::move(posted_tasks_.front()); | 237 CreateTask(TaskShutdownBehavior::BLOCK_SHUTDOWN)); |
| 272 posted_tasks_.pop(); | 238 EXPECT_TRUE(tracker_.WillPostTask(block_shutdown_task.get())); |
| 273 | 239 |
| 274 // Call Shutdown() asynchronously. | 240 // Call Shutdown() asynchronously. |
| 275 CallShutdownAsync(); | 241 CallShutdownAsync(); |
| 276 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); | 242 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); |
| 277 | 243 |
| 278 if (GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN) { | 244 if (GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN) { |
| 279 // Post a BLOCK_SHUTDOWN task. This should succeed. | 245 // Post a BLOCK_SHUTDOWN task. This should succeed. |
| 280 EXPECT_TRUE(posted_tasks_.empty()); | 246 std::unique_ptr<Task> task(CreateTask(GetParam())); |
| 281 PostTaskViaTracker(CreateTask(GetParam())); | 247 EXPECT_TRUE(tracker_.WillPostTask(task.get())); |
| 282 EXPECT_EQ(1U, posted_tasks_.size()); | |
| 283 | 248 |
| 284 // Run the BLOCK_SHUTDOWN task. This should succeed. | 249 // Run the BLOCK_SHUTDOWN task. This should succeed. |
| 285 EXPECT_EQ(0U, num_tasks_executed_); | 250 EXPECT_EQ(0U, num_tasks_executed_); |
| 286 RunNextPostedTaskViaTracker(); | 251 tracker_.RunTask(task.get()); |
| 287 EXPECT_EQ(1U, num_tasks_executed_); | 252 EXPECT_EQ(1U, num_tasks_executed_); |
| 288 } else { | 253 } else { |
| 289 // It shouldn't be possible to post a non BLOCK_SHUTDOWN task. | 254 // It shouldn't be possible to post a non BLOCK_SHUTDOWN task. |
| 290 PostTaskViaTracker(CreateTask(GetParam())); | 255 std::unique_ptr<Task> task(CreateTask(GetParam())); |
| 291 EXPECT_TRUE(posted_tasks_.empty()); | 256 EXPECT_FALSE(tracker_.WillPostTask(task.get())); |
| 292 | 257 |
| 293 // Don't try to run the task, because it hasn't been posted successfully. | 258 // Don't try to run the task, because it hasn't been posted successfully. |
| 294 } | 259 } |
| 295 | 260 |
| 296 // Unblock shutdown by running the BLOCK_SHUTDOWN task posted at the beginning | 261 // Unblock shutdown by running the BLOCK_SHUTDOWN task posted at the beginning |
| 297 // of the test. | 262 // of the test. |
| 298 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); | 263 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); |
| 299 tracker_.RunTask(block_shutdown_task.get()); | 264 tracker_.RunTask(block_shutdown_task.get()); |
| 300 EXPECT_EQ(GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN ? 2U : 1U, | 265 EXPECT_EQ(GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN ? 2U : 1U, |
| 301 num_tasks_executed_); | 266 num_tasks_executed_); |
| 302 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); | 267 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); |
| 303 } | 268 } |
| 304 | 269 |
| 305 TEST_P(TaskSchedulerTaskTrackerTest, PostAfterShutdown) { | 270 TEST_P(TaskSchedulerTaskTrackerTest, PostAfterShutdown) { |
| 306 // It is not possible to post a task after shutdown. | 271 // It is not possible to post a task after shutdown. |
| 307 tracker_.Shutdown(); | 272 tracker_.Shutdown(); |
| 308 EXPECT_TRUE(posted_tasks_.empty()); | 273 |
| 274 std::unique_ptr<Task> task(CreateTask(GetParam())); | |
| 309 | 275 |
| 310 if (GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN) { | 276 if (GetParam() == TaskShutdownBehavior::BLOCK_SHUTDOWN) { |
| 311 EXPECT_DCHECK_DEATH({ PostTaskViaTracker(CreateTask(GetParam())); }, ""); | 277 EXPECT_DCHECK_DEATH({ tracker_.WillPostTask(task.get()); }, ""); |
| 312 } else { | 278 } else { |
| 313 PostTaskViaTracker(CreateTask(GetParam())); | 279 EXPECT_FALSE(tracker_.WillPostTask(task.get())); |
| 314 } | 280 } |
| 315 | |
| 316 EXPECT_TRUE(posted_tasks_.empty()); | |
| 317 } | 281 } |
| 318 | 282 |
| 319 INSTANTIATE_TEST_CASE_P( | 283 INSTANTIATE_TEST_CASE_P( |
| 320 ContinueOnShutdown, | 284 ContinueOnShutdown, |
| 321 TaskSchedulerTaskTrackerTest, | 285 TaskSchedulerTaskTrackerTest, |
| 322 ::testing::Values(TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN)); | 286 ::testing::Values(TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN)); |
| 323 INSTANTIATE_TEST_CASE_P( | 287 INSTANTIATE_TEST_CASE_P( |
| 324 SkipOnShutdown, | 288 SkipOnShutdown, |
| 325 TaskSchedulerTaskTrackerTest, | 289 TaskSchedulerTaskTrackerTest, |
| 326 ::testing::Values(TaskShutdownBehavior::SKIP_ON_SHUTDOWN)); | 290 ::testing::Values(TaskShutdownBehavior::SKIP_ON_SHUTDOWN)); |
| 327 INSTANTIATE_TEST_CASE_P( | 291 INSTANTIATE_TEST_CASE_P( |
| 328 BlockShutdown, | 292 BlockShutdown, |
| 329 TaskSchedulerTaskTrackerTest, | 293 TaskSchedulerTaskTrackerTest, |
| 330 ::testing::Values(TaskShutdownBehavior::BLOCK_SHUTDOWN)); | 294 ::testing::Values(TaskShutdownBehavior::BLOCK_SHUTDOWN)); |
| 331 | 295 |
| 332 } // namespace internal | 296 } // namespace internal |
| 333 } // namespace base | 297 } // namespace base |
| OLD | NEW |