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

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

Issue 2426813004: TaskScheduler: Nits in service thread and delayed tasks code. (Closed)
Patch Set: Created 4 years, 2 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
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/scheduler_worker_pool_impl.h" 5 #include "base/task_scheduler/scheduler_worker_pool_impl.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <unordered_set> 10 #include <unordered_set>
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 } 295 }
296 296
297 // Verify that a Task can't be posted after shutdown. 297 // Verify that a Task can't be posted after shutdown.
298 TEST_P(TaskSchedulerWorkerPoolImplTest, PostTaskAfterShutdown) { 298 TEST_P(TaskSchedulerWorkerPoolImplTest, PostTaskAfterShutdown) {
299 auto task_runner = 299 auto task_runner =
300 worker_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()); 300 worker_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam());
301 task_tracker_.Shutdown(); 301 task_tracker_.Shutdown();
302 EXPECT_FALSE(task_runner->PostTask(FROM_HERE, Bind(&ShouldNotRunCallback))); 302 EXPECT_FALSE(task_runner->PostTask(FROM_HERE, Bind(&ShouldNotRunCallback)));
303 } 303 }
304 304
305 // Verify that a Task doesn't run before its delay expires.
306 TEST_P(TaskSchedulerWorkerPoolImplTest, PostDelayedTaskNeverRuns) {
307 // Post a task with a very long delay.
308 EXPECT_TRUE(worker_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam())
309 ->PostDelayedTask(FROM_HERE, Bind([]() {
310 ADD_FAILURE()
311 << "The delayed task should not run.";
312 }),
313 TimeDelta::FromDays(30)));
314
315 // Wait a few milliseconds. The test will fail if the delayed task runs.
316 PlatformThread::Sleep(TestTimeouts::tiny_timeout());
317 }
318
319 // Verify that a Task runs shortly after its delay expires. 305 // Verify that a Task runs shortly after its delay expires.
320 TEST_P(TaskSchedulerWorkerPoolImplTest, PostDelayedTask) { 306 TEST_P(TaskSchedulerWorkerPoolImplTest, PostDelayedTask) {
321 TimeTicks start_time = TimeTicks::Now(); 307 TimeTicks start_time = TimeTicks::Now();
322 308
323 // Post a task with a short delay. 309 // Post a task with a short delay.
324 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, 310 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL,
325 WaitableEvent::InitialState::NOT_SIGNALED); 311 WaitableEvent::InitialState::NOT_SIGNALED);
326 EXPECT_TRUE(worker_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()) 312 EXPECT_TRUE(worker_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam())
327 ->PostDelayedTask(FROM_HERE, Bind(&WaitableEvent::Signal, 313 ->PostDelayedTask(FROM_HERE, Bind(&WaitableEvent::Signal,
328 Unretained(&task_ran)), 314 Unretained(&task_ran)),
329 TestTimeouts::tiny_timeout())); 315 TestTimeouts::tiny_timeout()));
330 316
331 // Wait until the task runs. 317 // Wait until the task runs.
332 task_ran.Wait(); 318 task_ran.Wait();
333 319
334 // Expect the task to run less than 250 ms after its delay expires. 320 // Expect the task to run after its delay expires, but not more than 250 ms
335 EXPECT_LT(TimeTicks::Now() - start_time, 321 // after that.
322 const TimeDelta actual_delay = TimeTicks::Now() - start_time;
323 EXPECT_GE(actual_delay, TestTimeouts::tiny_timeout());
324 EXPECT_LT(actual_delay,
336 TimeDelta::FromMilliseconds(250) + TestTimeouts::tiny_timeout()); 325 TimeDelta::FromMilliseconds(250) + TestTimeouts::tiny_timeout());
337 } 326 }
338 327
339 // Verify that the RunsTasksOnCurrentThread() method of a SEQUENCED TaskRunner 328 // Verify that the RunsTasksOnCurrentThread() method of a SEQUENCED TaskRunner
340 // returns false when called from a task that isn't part of the sequence. 329 // returns false when called from a task that isn't part of the sequence.
341 TEST_P(TaskSchedulerWorkerPoolImplTest, SequencedRunsTasksOnCurrentThread) { 330 TEST_P(TaskSchedulerWorkerPoolImplTest, SequencedRunsTasksOnCurrentThread) {
342 scoped_refptr<TaskRunner> task_runner( 331 scoped_refptr<TaskRunner> task_runner(
343 worker_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam())); 332 worker_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()));
344 scoped_refptr<TaskRunner> sequenced_task_runner( 333 scoped_refptr<TaskRunner> sequenced_task_runner(
345 worker_pool_->CreateTaskRunnerWithTraits(TaskTraits(), 334 worker_pool_->CreateTaskRunnerWithTraits(TaskTraits(),
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after
753 worker_pool_.reset(); 742 worker_pool_.reset();
754 743
755 // Verify that counts were recorded to the histogram as expected. 744 // Verify that counts were recorded to the histogram as expected.
756 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(0)); 745 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(0));
757 EXPECT_EQ(1, histogram->SnapshotSamples()->GetCount(3)); 746 EXPECT_EQ(1, histogram->SnapshotSamples()->GetCount(3));
758 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(10)); 747 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(10));
759 } 748 }
760 749
761 } // namespace internal 750 } // namespace internal
762 } // namespace base 751 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698