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

Unified Diff: base/task_scheduler/scheduler_worker_pool_impl_unittest.cc

Issue 2430633003: TaskScheduler: Record TaskScheduler.NumTasksBeforeDetach.* from OnDetach(). (Closed)
Patch Set: CR gab #3 rebase 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 side-by-side diff with in-line comments
Download patch
Index: base/task_scheduler/scheduler_worker_pool_impl_unittest.cc
diff --git a/base/task_scheduler/scheduler_worker_pool_impl_unittest.cc b/base/task_scheduler/scheduler_worker_pool_impl_unittest.cc
index 0d044ecfa7b0ed5fe434892379c6fa278b3b5816..412d75e4544e1193585584fe8e193c93f03fe36c 100644
--- a/base/task_scheduler/scheduler_worker_pool_impl_unittest.cc
+++ b/base/task_scheduler/scheduler_worker_pool_impl_unittest.cc
@@ -583,19 +583,14 @@ class TaskSchedulerWorkerPoolHistogramTest
TaskSchedulerWorkerPoolHistogramTest() = default;
protected:
+ // Override SetUp() to allow every test case to initialize a worker pool with
+ // its own arguments.
void SetUp() override {}
- void TearDown() override {
- // |worker_pool_| initialization is done in test body.
- if (worker_pool_)
- worker_pool_->JoinForTesting();
- }
-
private:
std::unique_ptr<StatisticsRecorder> statistics_recorder_ =
StatisticsRecorder::CreateTemporaryForTesting();
- private:
DISALLOW_COPY_AND_ASSIGN(TaskSchedulerWorkerPoolHistogramTest);
};
@@ -709,39 +704,50 @@ TEST_F(TaskSchedulerWorkerPoolHistogramTest, NumTasksBetweenWaitsWithDetach) {
worker_pool_->DisallowWorkerDetachmentForTesting();
}
+namespace {
+
+void CaptureThreadId(PlatformThreadId* thread_id) {
+ ASSERT_TRUE(thread_id);
+ *thread_id = PlatformThread::CurrentId();
+}
+
+void VerifyThreadIdIsNot(PlatformThreadId thread_id) {
+ EXPECT_NE(thread_id, PlatformThread::CurrentId());
+}
+
+} // namespace
+
TEST_F(TaskSchedulerWorkerPoolHistogramTest, NumTasksBeforeDetach) {
InitializeWorkerPool(kReclaimTimeForDetachTests, kNumWorkersInWorkerPool);
+
+ // This test assumes that the TaskRunners aren't assigned to the same worker.
auto task_runner = worker_pool_->CreateTaskRunnerWithTraits(
TaskTraits(), ExecutionMode::SINGLE_THREADED);
auto other_task_runner = worker_pool_->CreateTaskRunnerWithTraits(
TaskTraits(), ExecutionMode::SINGLE_THREADED);
// Post 3 tasks and wait until they run.
- task_runner->PostTask(FROM_HERE, Bind(&DoNothing));
+ PlatformThreadId thread_id;
+ task_runner->PostTask(FROM_HERE,
+ Bind(&CaptureThreadId, Unretained(&thread_id)));
task_runner->PostTask(FROM_HERE, Bind(&DoNothing));
task_runner->PostTask(FROM_HERE, Bind(&DoNothing));
worker_pool_->WaitForAllWorkersIdleForTesting();
// To allow the SchedulerWorker associated with |task_runner| to detach:
// - Make sure it isn't on top of the idle stack by waking up another
- // SchedulerWorker.
+ // SchedulerWorker and waiting until it goes back to sleep.
// - Release |task_runner|.
- other_task_runner->PostTask(FROM_HERE, Bind(&DoNothing));
+ other_task_runner->PostTask(FROM_HERE, Bind(&VerifyThreadIdIsNot, thread_id));
+ worker_pool_->WaitForAllWorkersIdleForTesting();
task_runner = nullptr;
- // Allow the SchedulerWorker to detach.
+ // Allow the SchedulerWorker that was associated with |task_runner| to detach.
PlatformThread::Sleep(kReclaimTimeForDetachTests + kExtraTimeToWaitForDetach);
-
- // Join the SchedulerWorkerPool. This forces SchedulerWorkers that detached
- // during the test to record to the histogram.
- worker_pool_->WaitForAllWorkersIdleForTesting();
worker_pool_->DisallowWorkerDetachmentForTesting();
- worker_pool_->JoinForTesting();
- const auto* histogram = worker_pool_->num_tasks_before_detach_histogram();
- other_task_runner = nullptr;
- worker_pool_.reset();
// Verify that counts were recorded to the histogram as expected.
+ const auto* histogram = worker_pool_->num_tasks_before_detach_histogram();
EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(0));
EXPECT_EQ(1, histogram->SnapshotSamples()->GetCount(3));
EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(10));
« no previous file with comments | « base/task_scheduler/scheduler_worker_pool_impl.cc ('k') | base/task_scheduler/scheduler_worker_stack_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698