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

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

Issue 2427963002: Support FileDescriptorWatcher in TaskScheduler. (Closed)
Patch Set: nacl 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>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/atomicops.h" 13 #include "base/atomicops.h"
14 #include "base/bind.h" 14 #include "base/bind.h"
15 #include "base/bind_helpers.h" 15 #include "base/bind_helpers.h"
16 #include "base/callback.h" 16 #include "base/callback.h"
17 #include "base/macros.h" 17 #include "base/macros.h"
18 #include "base/memory/ptr_util.h" 18 #include "base/memory/ptr_util.h"
19 #include "base/memory/ref_counted.h" 19 #include "base/memory/ref_counted.h"
20 #include "base/message_loop/message_loop.h"
20 #include "base/metrics/histogram.h" 21 #include "base/metrics/histogram.h"
21 #include "base/metrics/histogram_samples.h" 22 #include "base/metrics/histogram_samples.h"
22 #include "base/metrics/statistics_recorder.h" 23 #include "base/metrics/statistics_recorder.h"
23 #include "base/synchronization/condition_variable.h" 24 #include "base/synchronization/condition_variable.h"
24 #include "base/synchronization/lock.h" 25 #include "base/synchronization/lock.h"
25 #include "base/synchronization/waitable_event.h" 26 #include "base/synchronization/waitable_event.h"
26 #include "base/task_runner.h" 27 #include "base/task_runner.h"
27 #include "base/task_scheduler/delayed_task_manager.h" 28 #include "base/task_scheduler/delayed_task_manager.h"
28 #include "base/task_scheduler/scheduler_worker_pool_params.h" 29 #include "base/task_scheduler/scheduler_worker_pool_params.h"
29 #include "base/task_scheduler/sequence.h" 30 #include "base/task_scheduler/sequence.h"
30 #include "base/task_scheduler/sequence_sort_key.h" 31 #include "base/task_scheduler/sequence_sort_key.h"
31 #include "base/task_scheduler/task_tracker.h" 32 #include "base/task_scheduler/task_tracker.h"
32 #include "base/task_scheduler/test_task_factory.h" 33 #include "base/task_scheduler/test_task_factory.h"
33 #include "base/test/gtest_util.h" 34 #include "base/test/gtest_util.h"
34 #include "base/test/test_simple_task_runner.h" 35 #include "base/test/test_simple_task_runner.h"
35 #include "base/test/test_timeouts.h" 36 #include "base/test/test_timeouts.h"
36 #include "base/threading/platform_thread.h" 37 #include "base/threading/platform_thread.h"
37 #include "base/threading/simple_thread.h" 38 #include "base/threading/simple_thread.h"
38 #include "base/threading/thread.h" 39 #include "base/threading/thread.h"
39 #include "base/threading/thread_checker_impl.h" 40 #include "base/threading/thread_checker_impl.h"
40 #include "base/threading/thread_local_storage.h" 41 #include "base/threading/thread_local_storage.h"
41 #include "base/threading/thread_restrictions.h" 42 #include "base/threading/thread_restrictions.h"
42 #include "base/time/time.h" 43 #include "base/time/time.h"
44 #include "build/build_config.h"
43 #include "testing/gtest/include/gtest/gtest.h" 45 #include "testing/gtest/include/gtest/gtest.h"
44 46
45 namespace base { 47 namespace base {
46 namespace internal { 48 namespace internal {
47 namespace { 49 namespace {
48 50
49 constexpr size_t kNumWorkersInWorkerPool = 4; 51 constexpr size_t kNumWorkersInWorkerPool = 4;
50 constexpr size_t kNumThreadsPostingTasks = 4; 52 constexpr size_t kNumThreadsPostingTasks = 4;
51 constexpr size_t kNumTasksPostedPerThread = 150; 53 constexpr size_t kNumTasksPostedPerThread = 150;
52 // This can't be lower because Windows' WaitableEvent wakes up too early when a 54 // This can't be lower because Windows' WaitableEvent wakes up too early when a
(...skipping 19 matching lines...) Expand all
72 void TearDown() override { 74 void TearDown() override {
73 service_thread_.Stop(); 75 service_thread_.Stop();
74 worker_pool_->WaitForAllWorkersIdleForTesting(); 76 worker_pool_->WaitForAllWorkersIdleForTesting();
75 worker_pool_->JoinForTesting(); 77 worker_pool_->JoinForTesting();
76 } 78 }
77 79
78 void InitializeWorkerPool(const TimeDelta& suggested_reclaim_time, 80 void InitializeWorkerPool(const TimeDelta& suggested_reclaim_time,
79 size_t num_workers) { 81 size_t num_workers) {
80 ASSERT_FALSE(worker_pool_); 82 ASSERT_FALSE(worker_pool_);
81 ASSERT_FALSE(delayed_task_manager_); 83 ASSERT_FALSE(delayed_task_manager_);
82 service_thread_.Start(); 84 constexpr MessageLoop::Type kServiceThreadMessageLoopType =
85 #if defined(OS_POSIX) && !defined(OS_NACL_SFI)
86 MessageLoop::TYPE_IO;
87 #else
88 MessageLoop::TYPE_DEFAULT;
89 #endif
90 constexpr size_t kDefaultStackSize = 0;
91 ASSERT_TRUE(service_thread_.StartWithOptions(
92 Thread::Options(kServiceThreadMessageLoopType, kDefaultStackSize)));
93 task_tracker_ = MakeUnique<TaskTracker>(
94 #if defined(OS_POSIX) && !defined(OS_NACL_SFI)
95 static_cast<MessageLoopForIO*>(service_thread_.message_loop())
96 #endif
97 );
83 delayed_task_manager_ = 98 delayed_task_manager_ =
84 base::MakeUnique<DelayedTaskManager>(service_thread_.task_runner()); 99 MakeUnique<DelayedTaskManager>(service_thread_.task_runner());
85 worker_pool_ = SchedulerWorkerPoolImpl::Create( 100 worker_pool_ = SchedulerWorkerPoolImpl::Create(
86 SchedulerWorkerPoolParams("TestWorkerPool", ThreadPriority::NORMAL, 101 SchedulerWorkerPoolParams("TestWorkerPool", ThreadPriority::NORMAL,
87 IORestriction::ALLOWED, num_workers, 102 IORestriction::ALLOWED, num_workers,
88 suggested_reclaim_time), 103 suggested_reclaim_time),
89 Bind(&TaskSchedulerWorkerPoolImplTest::ReEnqueueSequenceCallback, 104 Bind(&TaskSchedulerWorkerPoolImplTest::ReEnqueueSequenceCallback,
90 Unretained(this)), 105 Unretained(this)),
91 &task_tracker_, delayed_task_manager_.get()); 106 task_tracker_.get(), delayed_task_manager_.get());
92 ASSERT_TRUE(worker_pool_); 107 ASSERT_TRUE(worker_pool_);
93 } 108 }
94 109
95 std::unique_ptr<SchedulerWorkerPoolImpl> worker_pool_; 110 std::unique_ptr<SchedulerWorkerPoolImpl> worker_pool_;
96 111
97 TaskTracker task_tracker_;
98 Thread service_thread_; 112 Thread service_thread_;
113 std::unique_ptr<TaskTracker> task_tracker_;
99 std::unique_ptr<DelayedTaskManager> delayed_task_manager_; 114 std::unique_ptr<DelayedTaskManager> delayed_task_manager_;
100 115
101 private: 116 private:
102 void ReEnqueueSequenceCallback(scoped_refptr<Sequence> sequence) { 117 void ReEnqueueSequenceCallback(scoped_refptr<Sequence> sequence) {
103 // In production code, this callback would be implemented by the 118 // In production code, this callback would be implemented by the
104 // TaskScheduler which would first determine which PriorityQueue the 119 // TaskScheduler which would first determine which PriorityQueue the
105 // sequence must be re-enqueued. 120 // sequence must be re-enqueued.
106 const SequenceSortKey sort_key(sequence->GetSortKey()); 121 const SequenceSortKey sort_key(sequence->GetSortKey());
107 worker_pool_->ReEnqueueSequence(std::move(sequence), sort_key); 122 worker_pool_->ReEnqueueSequence(std::move(sequence), sort_key);
108 } 123 }
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 306
292 // Wait until all workers are idle to be sure that no task accesses 307 // Wait until all workers are idle to be sure that no task accesses
293 // its TestTaskFactory after it is destroyed. 308 // its TestTaskFactory after it is destroyed.
294 worker_pool_->WaitForAllWorkersIdleForTesting(); 309 worker_pool_->WaitForAllWorkersIdleForTesting();
295 } 310 }
296 311
297 // Verify that a Task can't be posted after shutdown. 312 // Verify that a Task can't be posted after shutdown.
298 TEST_P(TaskSchedulerWorkerPoolImplTest, PostTaskAfterShutdown) { 313 TEST_P(TaskSchedulerWorkerPoolImplTest, PostTaskAfterShutdown) {
299 auto task_runner = 314 auto task_runner =
300 worker_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()); 315 worker_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam());
301 task_tracker_.Shutdown(); 316 task_tracker_->Shutdown();
302 EXPECT_FALSE(task_runner->PostTask(FROM_HERE, Bind(&ShouldNotRunCallback))); 317 EXPECT_FALSE(task_runner->PostTask(FROM_HERE, Bind(&ShouldNotRunCallback)));
303 } 318 }
304 319
305 // Verify that a Task doesn't run before its delay expires. 320 // Verify that a Task doesn't run before its delay expires.
306 TEST_P(TaskSchedulerWorkerPoolImplTest, PostDelayedTaskNeverRuns) { 321 TEST_P(TaskSchedulerWorkerPoolImplTest, PostDelayedTaskNeverRuns) {
307 // Post a task with a very long delay. 322 // Post a task with a very long delay.
308 EXPECT_TRUE(worker_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam()) 323 EXPECT_TRUE(worker_pool_->CreateTaskRunnerWithTraits(TaskTraits(), GetParam())
309 ->PostDelayedTask(FROM_HERE, Bind([]() { 324 ->PostDelayedTask(FROM_HERE, Bind([]() {
310 ADD_FAILURE() 325 ADD_FAILURE()
311 << "The delayed task should not run."; 326 << "The delayed task should not run.";
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 public: 412 public:
398 TaskSchedulerWorkerPoolImplIORestrictionTest() = default; 413 TaskSchedulerWorkerPoolImplIORestrictionTest() = default;
399 414
400 private: 415 private:
401 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerWorkerPoolImplIORestrictionTest); 416 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerWorkerPoolImplIORestrictionTest);
402 }; 417 };
403 418
404 } // namespace 419 } // namespace
405 420
406 TEST_P(TaskSchedulerWorkerPoolImplIORestrictionTest, IORestriction) { 421 TEST_P(TaskSchedulerWorkerPoolImplIORestrictionTest, IORestriction) {
407 TaskTracker task_tracker; 422 MessageLoopForIO service_message_loop;
408 DelayedTaskManager delayed_task_manager( 423 TaskTracker task_tracker {
409 make_scoped_refptr(new TestSimpleTaskRunner)); 424 #if defined(OS_POSIX) && !defined(OS_NACL_SFI)
425 &service_message_loop
426 #endif
427 };
428 DelayedTaskManager delayed_task_manager(service_message_loop.task_runner());
410 429
411 auto worker_pool = SchedulerWorkerPoolImpl::Create( 430 auto worker_pool = SchedulerWorkerPoolImpl::Create(
412 SchedulerWorkerPoolParams("TestWorkerPoolWithParam", 431 SchedulerWorkerPoolParams("TestWorkerPoolWithParam",
413 ThreadPriority::NORMAL, GetParam(), 1U, 432 ThreadPriority::NORMAL, GetParam(), 1U,
414 TimeDelta::Max()), 433 TimeDelta::Max()),
415 Bind(&NotReachedReEnqueueSequenceCallback), &task_tracker, 434 Bind(&NotReachedReEnqueueSequenceCallback), &task_tracker,
416 &delayed_task_manager); 435 &delayed_task_manager);
417 ASSERT_TRUE(worker_pool); 436 ASSERT_TRUE(worker_pool);
418 437
419 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL, 438 WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL,
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after
753 worker_pool_.reset(); 772 worker_pool_.reset();
754 773
755 // Verify that counts were recorded to the histogram as expected. 774 // Verify that counts were recorded to the histogram as expected.
756 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(0)); 775 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(0));
757 EXPECT_EQ(1, histogram->SnapshotSamples()->GetCount(3)); 776 EXPECT_EQ(1, histogram->SnapshotSamples()->GetCount(3));
758 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(10)); 777 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(10));
759 } 778 }
760 779
761 } // namespace internal 780 } // namespace internal
762 } // namespace base 781 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | base/task_scheduler/scheduler_worker_stack_unittest.cc » ('j') | base/task_scheduler/task_tracker.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698