Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Side by Side Diff: base/task_scheduler/task_tracker_unittest.cc

Issue 2798373002: Fix race condition in TaskSchedulerTaskTrackerTest.WillPostAndRunLongTaskBeforeShutdown. (Closed)
Patch Set: self-review Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 245 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 // Run the task. 256 // Run the task.
257 EXPECT_EQ(0U, NumTasksExecuted()); 257 EXPECT_EQ(0U, NumTasksExecuted());
258 EXPECT_TRUE(tracker_.RunTask(std::move(task), SequenceToken::Create())); 258 EXPECT_TRUE(tracker_.RunTask(std::move(task), SequenceToken::Create()));
259 EXPECT_EQ(1U, NumTasksExecuted()); 259 EXPECT_EQ(1U, NumTasksExecuted());
260 260
261 // Shutdown() shouldn't block. 261 // Shutdown() shouldn't block.
262 tracker_.Shutdown(); 262 tracker_.Shutdown();
263 } 263 }
264 264
265 TEST_P(TaskSchedulerTaskTrackerTest, WillPostAndRunLongTaskBeforeShutdown) { 265 TEST_P(TaskSchedulerTaskTrackerTest, WillPostAndRunLongTaskBeforeShutdown) {
266 // Create a task that will block until |event| is signaled. 266 // Create a task that signals |task_scheduled| and blocks until |task_barrier|
267 WaitableEvent event(WaitableEvent::ResetPolicy::AUTOMATIC, 267 // is signaled.
268 WaitableEvent::InitialState::NOT_SIGNALED); 268 WaitableEvent task_scheduled(WaitableEvent::ResetPolicy::AUTOMATIC,
robliao 2017/04/06 18:26:26 This might be clearer as task_running.
fdoray 2017/04/07 12:26:27 Done.
269 WaitableEvent::InitialState::NOT_SIGNALED);
270 WaitableEvent task_barrier(WaitableEvent::ResetPolicy::AUTOMATIC,
271 WaitableEvent::InitialState::NOT_SIGNALED);
269 auto blocked_task = base::MakeUnique<Task>( 272 auto blocked_task = base::MakeUnique<Task>(
270 FROM_HERE, Bind(&WaitableEvent::Wait, Unretained(&event)), 273 FROM_HERE,
274 Bind(
275 [](WaitableEvent* task_scheduled, WaitableEvent* task_barrier) {
276 task_scheduled->Signal();
277 task_barrier->Wait();
278 },
279 Unretained(&task_scheduled), base::Unretained(&task_barrier)),
271 TaskTraits().WithBaseSyncPrimitives().WithShutdownBehavior(GetParam()), 280 TaskTraits().WithBaseSyncPrimitives().WithShutdownBehavior(GetParam()),
272 TimeDelta()); 281 TimeDelta());
273 282
274 // Inform |task_tracker_| that |blocked_task| will be posted. 283 // Inform |task_tracker_| that |blocked_task| will be posted.
275 EXPECT_TRUE(tracker_.WillPostTask(blocked_task.get())); 284 EXPECT_TRUE(tracker_.WillPostTask(blocked_task.get()));
276 285
277 // Run the task asynchronouly. 286 // Create a thread to run the task. Wait until the task starts running.
278 ThreadPostingAndRunningTask thread_running_task( 287 ThreadPostingAndRunningTask thread_running_task(
279 &tracker_, std::move(blocked_task), 288 &tracker_, std::move(blocked_task),
280 ThreadPostingAndRunningTask::Action::RUN, false); 289 ThreadPostingAndRunningTask::Action::RUN, false);
281 thread_running_task.Start(); 290 thread_running_task.Start();
291 task_scheduled.Wait();
282 292
283 // Initiate shutdown while the task is running. 293 // Initiate shutdown after the task has been scheduled.
284 CallShutdownAsync(); 294 CallShutdownAsync();
285 295
286 if (GetParam() == TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) { 296 if (GetParam() == TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) {
287 // Shutdown should complete even with a CONTINUE_ON_SHUTDOWN in progress. 297 // Shutdown should complete even with a CONTINUE_ON_SHUTDOWN in progress.
288 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); 298 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED();
289 } else { 299 } else {
290 // Shutdown should block with any non CONTINUE_ON_SHUTDOWN task in progress. 300 // Shutdown should block with any non CONTINUE_ON_SHUTDOWN task in progress.
291 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS(); 301 VERIFY_ASYNC_SHUTDOWN_IN_PROGRESS();
292 } 302 }
293 303
294 // Unblock the task. 304 // Unblock the task.
295 event.Signal(); 305 task_barrier.Signal();
296 thread_running_task.Join(); 306 thread_running_task.Join();
297 307
298 // Shutdown should now complete for a non CONTINUE_ON_SHUTDOWN task. 308 // Shutdown should now complete for a non CONTINUE_ON_SHUTDOWN task.
299 if (GetParam() != TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) 309 if (GetParam() != TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN)
300 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED(); 310 WAIT_FOR_ASYNC_SHUTDOWN_COMPLETED();
301 } 311 }
302 312
303 TEST_P(TaskSchedulerTaskTrackerTest, WillPostBeforeShutdownRunDuringShutdown) { 313 TEST_P(TaskSchedulerTaskTrackerTest, WillPostBeforeShutdownRunDuringShutdown) {
304 // Inform |task_tracker_| that a task will be posted. 314 // Inform |task_tracker_| that a task will be posted.
305 std::unique_ptr<Task> task(CreateTask(GetParam())); 315 std::unique_ptr<Task> task(CreateTask(GetParam()));
(...skipping 625 matching lines...) Expand 10 before | Expand all | Expand 10 after
931 ASSERT_TRUE(tracker.WillPostTask(task.get())); 941 ASSERT_TRUE(tracker.WillPostTask(task.get()));
932 942
933 HistogramTester tester; 943 HistogramTester tester;
934 EXPECT_TRUE(tracker.RunTask(std::move(task), SequenceToken::Create())); 944 EXPECT_TRUE(tracker.RunTask(std::move(task), SequenceToken::Create()));
935 tester.ExpectTotalCount(test.expected_histogram, 1); 945 tester.ExpectTotalCount(test.expected_histogram, 1);
936 } 946 }
937 } 947 }
938 948
939 } // namespace internal 949 } // namespace internal
940 } // namespace base 950 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698