summaryrefslogtreecommitdiffstats
path: root/base/test/task_runner_test_template.h
diff options
context:
space:
mode:
authormichaeln@google.com <michaeln@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2013-03-07 01:35:37 +0000
committermichaeln@google.com <michaeln@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2013-03-07 01:35:37 +0000
commit13ecb5e99e5fad75162cad08f1082eff259ff0d9 (patch)
tree988e3a5dc1b8fc33e4e28dd2826e8c0176350cd4 /base/test/task_runner_test_template.h
parent8c2acc21b17cdcc88edc0bea2623a637c97fdeab (diff)
downloadchromium_src-13ecb5e99e5fad75162cad08f1082eff259ff0d9.zip
chromium_src-13ecb5e99e5fad75162cad08f1082eff259ff0d9.tar.gz
chromium_src-13ecb5e99e5fad75162cad08f1082eff259ff0d9.tar.bz2
Flush SequenceWorkerPool tasks after each test. Applies to unit_test, interactive_ui test, browser_tests... pretty much all gtest based content library test harnesses are affected.
The CL changes semantics and implementation of the existing FlushForTesting method (which wasn't suitable for this usage). * The old method would wait for delayed tasks prior to continuing, the new method will not run them but will delete them prior to continuing. * The old method would deadlock if called after Shutdown had been called, the new method returns immediately in that case. A few SWP unittests relied on the waiting for delayed task completion behavior. Those have been modified to explicitly wait thru other means. BUG=168415,166470 Review URL: https://codereview.chromium.org/11649032 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@186578 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/test/task_runner_test_template.h')
-rw-r--r--base/test/task_runner_test_template.h14
1 files changed, 12 insertions, 2 deletions
diff --git a/base/test/task_runner_test_template.h b/base/test/task_runner_test_template.h
index 437a7d9..2dd8814 100644
--- a/base/test/task_runner_test_template.h
+++ b/base/test/task_runner_test_template.h
@@ -25,7 +25,8 @@
// }
//
// // Stop the task runner and make sure all tasks posted before
-// // this is called are run.
+// // this is called are run. Caveat: delayed tasks are not run,
+ // they're simply deleted.
// void StopTaskRunner() {
// ...
// }
@@ -58,6 +59,7 @@
#include "base/bind.h"
#include "base/callback.h"
#include "base/memory/ref_counted.h"
+#include "base/synchronization/condition_variable.h"
#include "base/synchronization/lock.h"
#include "base/task_runner.h"
#include "base/threading/thread.h"
@@ -81,6 +83,9 @@ class TaskTracker : public RefCountedThreadSafe<TaskTracker> {
std::map<int, int> GetTaskRunCounts() const;
+ // Returns after the tracker observes a total of |count| task completions.
+ void WaitForCompletedTasks(int count);
+
private:
friend class RefCountedThreadSafe<TaskTracker>;
@@ -88,8 +93,10 @@ class TaskTracker : public RefCountedThreadSafe<TaskTracker> {
void RunTask(const Closure& task, int i);
- mutable Lock task_run_counts_lock_;
+ mutable Lock lock_;
std::map<int, int> task_run_counts_;
+ int task_runs_;
+ ConditionVariable task_runs_cv_;
DISALLOW_COPY_AND_ASSIGN(TaskTracker);
};
@@ -140,6 +147,7 @@ TYPED_TEST_P(TaskRunnerTest, Delayed) {
}
std::map<int, int> expected_task_run_counts;
+ int expected_total_tasks = 0;
this->delegate_.StartTaskRunner();
scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner();
@@ -150,8 +158,10 @@ TYPED_TEST_P(TaskRunnerTest, Delayed) {
task_runner->PostDelayedTask(
FROM_HERE, ith_task, base::TimeDelta::FromMilliseconds(j));
++expected_task_run_counts[i];
+ ++expected_total_tasks;
}
}
+ this->task_tracker_->WaitForCompletedTasks(expected_total_tasks);
this->delegate_.StopTaskRunner();
EXPECT_EQ(expected_task_run_counts,