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

Unified Diff: media/cast/test/fake_single_thread_task_runner.cc

Issue 289083002: Cast: Add tests for clocks being slow/fast (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: merge Created 6 years, 7 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: media/cast/test/fake_single_thread_task_runner.cc
diff --git a/media/cast/test/fake_single_thread_task_runner.cc b/media/cast/test/fake_single_thread_task_runner.cc
index d2950fd9fb6df2efe7fa9799709e4ead88ce927c..b60a1b12eafdfdf7b12952b567ec9b4ed2232a76 100644
--- a/media/cast/test/fake_single_thread_task_runner.cc
+++ b/media/cast/test/fake_single_thread_task_runner.cc
@@ -14,7 +14,8 @@ namespace test {
FakeSingleThreadTaskRunner::FakeSingleThreadTaskRunner(
base::SimpleTestTickClock* clock)
- : clock_(clock) {}
+ : clock_(clock),
+ fail_on_next_task_(false) {}
FakeSingleThreadTaskRunner::~FakeSingleThreadTaskRunner() {}
@@ -22,6 +23,9 @@ bool FakeSingleThreadTaskRunner::PostDelayedTask(
const tracked_objects::Location& from_here,
const base::Closure& task,
base::TimeDelta delay) {
+ if (fail_on_next_task_) {
+ LOG(FATAL) << "Infinite task-add loop detected.";
+ }
EXPECT_GE(delay, base::TimeDelta());
PostedTask posed_task(from_here,
task,
@@ -38,7 +42,7 @@ bool FakeSingleThreadTaskRunner::RunsTasksOnCurrentThread() const {
}
void FakeSingleThreadTaskRunner::RunTasks() {
- while(true) {
+ while (true) {
// Run all tasks equal or older than current time.
std::multimap<base::TimeTicks, PostedTask>::iterator it = tasks_.begin();
if (it == tasks_.end())
@@ -53,6 +57,38 @@ void FakeSingleThreadTaskRunner::RunTasks() {
}
}
+void FakeSingleThreadTaskRunner::Sleep(base::TimeDelta t) {
+ base::TimeTicks run_until = clock_->NowTicks() + t;
+ while (1) {
+ // If we run more than 100000 iterations, we've probably
+ // hit some sort of case where a new task is posted every
+ // time that we invoke a task, and we can't make progress
+ // anymore. If that happens, set fail_on_next_task_ to true
+ // and throw an error when the next task is posted.
+ for (int i = 0; i < 100000; i++) {
+ // Run all tasks equal or older than current time.
+ std::multimap<base::TimeTicks, PostedTask>::iterator it = tasks_.begin();
+ if (it == tasks_.end()) {
+ clock_->Advance(run_until - clock_->NowTicks());
+ return;
+ }
+
+ PostedTask task = it->second;
+ if (run_until < task.GetTimeToRun()) {
+ clock_->Advance(run_until - clock_->NowTicks());
+ return;
+ }
+
+ clock_->Advance(task.GetTimeToRun() - clock_->NowTicks());
+ tasks_.erase(it);
+ task.task.Run();
+ }
+ // Instead of failing immediately, we fail when the next task is
+ // added so that the backtrace will include the task that was added.
+ fail_on_next_task_ = true;
+ }
+}
+
bool FakeSingleThreadTaskRunner::PostNonNestableDelayedTask(
const tracked_objects::Location& from_here,
const base::Closure& task,
« no previous file with comments | « media/cast/test/fake_single_thread_task_runner.h ('k') | media/cast/test/skewed_single_thread_task_runner.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698