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

Unified Diff: base/threading/sequenced_worker_pool_unittest.cc

Issue 11649032: Flush SequenceWorkerPool tasks after each unit test. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 10 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/threading/sequenced_worker_pool_unittest.cc
===================================================================
--- base/threading/sequenced_worker_pool_unittest.cc (revision 183651)
+++ base/threading/sequenced_worker_pool_unittest.cc (working copy)
@@ -97,16 +97,18 @@
SignalWorkerDone(id);
}
- void PostAdditionalTasks(int id, SequencedWorkerPool* pool) {
+ void PostAdditionalTasks(
+ int id, SequencedWorkerPool* pool,
+ bool expected_return_value) {
Closure fast_task = base::Bind(&TestTracker::FastTask, this, 100);
- EXPECT_FALSE(
- pool->PostWorkerTaskWithShutdownBehavior(
- FROM_HERE, fast_task,
- SequencedWorkerPool::CONTINUE_ON_SHUTDOWN));
- EXPECT_FALSE(
- pool->PostWorkerTaskWithShutdownBehavior(
- FROM_HERE, fast_task,
- SequencedWorkerPool::SKIP_ON_SHUTDOWN));
+ EXPECT_EQ(expected_return_value,
+ pool->PostWorkerTaskWithShutdownBehavior(
+ FROM_HERE, fast_task,
+ SequencedWorkerPool::CONTINUE_ON_SHUTDOWN));
+ EXPECT_EQ(expected_return_value,
+ pool->PostWorkerTaskWithShutdownBehavior(
+ FROM_HERE, fast_task,
+ SequencedWorkerPool::SKIP_ON_SHUTDOWN));
pool->PostWorkerTaskWithShutdownBehavior(
FROM_HERE, fast_task,
SequencedWorkerPool::BLOCK_SHUTDOWN);
@@ -137,6 +139,11 @@
return ret;
}
+ size_t GetTasksCompletedCount() {
+ base::AutoLock lock(lock_);
+ return complete_sequence_.size();
+ }
+
void ClearCompleteSequence() {
base::AutoLock lock(lock_);
complete_sequence_.clear();
@@ -454,7 +461,8 @@
for (int i = 0; i < kNumQueuedTasks; ++i) {
EXPECT_TRUE(pool()->PostWorkerTaskWithShutdownBehavior(
FROM_HERE,
- base::Bind(&TestTracker::PostAdditionalTasks, tracker(), i, pool()),
+ base::Bind(&TestTracker::PostAdditionalTasks, tracker(), i, pool(),
+ false),
SequencedWorkerPool::BLOCK_SHUTDOWN));
}
@@ -686,6 +694,47 @@
unused_pool->Shutdown();
}
+// Verify that FlushForTesting works as intended.
+TEST_F(SequencedWorkerPoolTest, FlushForTesting) {
+ // Should be fine to call on a new instance.
+ pool()->FlushForTesting();
+
+ // Queue up a bunch of work, including a long delayed task and
+ // a task that produces additional tasks as an artifact.
+ pool()->PostDelayedWorkerTask(
+ FROM_HERE,
+ base::Bind(&TestTracker::FastTask, tracker(), 0),
+ TimeDelta::FromMinutes(5));
+ pool()->PostWorkerTask(FROM_HERE,
+ base::Bind(&TestTracker::SlowTask, tracker(), 0));
+ const size_t kNumFastTasks = 20;
+ for (size_t i = 0; i < kNumFastTasks; i++) {
+ pool()->PostWorkerTask(FROM_HERE,
+ base::Bind(&TestTracker::FastTask, tracker(), 0));
+ }
+ pool()->PostWorkerTask(
+ FROM_HERE,
+ base::Bind(&TestTracker::PostAdditionalTasks, tracker(), 0, pool(),
+ true));
+
+ // We expect all except the delayed task to have been run. We verify all
+ // closures have been deleted deleted by looking at the refcount of the
+ // tracker.
+ EXPECT_FALSE(tracker()->HasOneRef());
+ pool()->FlushForTesting();
+ EXPECT_TRUE(tracker()->HasOneRef());
+ EXPECT_EQ(1 + kNumFastTasks + 1 + 3, tracker()->GetTasksCompletedCount());
+
+ // Should be fine to call on an idle instance with all threads created, and
+ // spamming the method shouldn't deadlock or confuse the class.
+ pool()->FlushForTesting();
+ pool()->FlushForTesting();
+
+ // Should be fine to call after shutdown too.
+ pool()->Shutdown();
+ pool()->FlushForTesting();
+}
+
class SequencedWorkerPoolTaskRunnerTestDelegate {
public:
SequencedWorkerPoolTaskRunnerTestDelegate() {}
@@ -702,8 +751,8 @@
}
void StopTaskRunner() {
- // Make sure all tasks (including delayed ones) are run before shutting
- // down.
+ // Make sure all tasks are run before shutting down. Delayed tasks are
+ // not run, they're simply deleted.
pool_owner_->pool()->FlushForTesting();
pool_owner_->pool()->Shutdown();
// Don't reset |pool_owner_| here, as the test may still hold a
@@ -742,8 +791,8 @@
}
void StopTaskRunner() {
- // Make sure all tasks (including delayed ones) are run before shutting
- // down.
+ // Make sure all tasks are run before shutting down. Delayed tasks are
+ // not run, they're simply deleted.
pool_owner_->pool()->FlushForTesting();
pool_owner_->pool()->Shutdown();
// Don't reset |pool_owner_| here, as the test may still hold a
@@ -783,8 +832,8 @@
}
void StopTaskRunner() {
- // Make sure all tasks (including delayed ones) are run before shutting
- // down.
+ // Make sure all tasks are run before shutting down. Delayed tasks are
+ // not run, they're simply deleted.
pool_owner_->pool()->FlushForTesting();
pool_owner_->pool()->Shutdown();
// Don't reset |pool_owner_| here, as the test may still hold a

Powered by Google App Engine
This is Rietveld 408576698