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

Unified Diff: base/task_scheduler/scheduler_worker_pool_impl_unittest.cc

Issue 2430633003: TaskScheduler: Record TaskScheduler.NumTasksBeforeDetach.* from OnDetach(). (Closed)
Patch Set: self-review 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 fdfe41cd100e9b1d25109537dac2f3852f7c217f..660e174a7f903bd799b5e9aff0184aad963c87ce 100644
--- a/base/task_scheduler/scheduler_worker_pool_impl_unittest.cc
+++ b/base/task_scheduler/scheduler_worker_pool_impl_unittest.cc
@@ -594,19 +594,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);
};
@@ -720,15 +715,32 @@ 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();
@@ -737,22 +749,15 @@ TEST_F(TaskSchedulerWorkerPoolHistogramTest, NumTasksBeforeDetach) {
// - Make sure it isn't on top of the idle stack by waking up another
// SchedulerWorker.
gab 2016/10/19 18:12:23 How is this condition guaranteed to be met (and ho
fdoray 2016/10/20 13:45:02 Done.
// - Release |task_runner|.
- other_task_runner->PostTask(FROM_HERE, Bind(&DoNothing));
+ other_task_runner->PostTask(FROM_HERE, Bind(&VerifyThreadIdIsNot, thread_id));
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));

Powered by Google App Engine
This is Rietveld 408576698