diff options
author | erg <erg@chromium.org> | 2015-01-12 13:38:57 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-01-12 21:39:57 +0000 |
commit | 42f058496033336073d38ba0a1e36052d5b0475c (patch) | |
tree | 25c98584d9c8448913a974057cc06856cff29dc2 /base | |
parent | 2f67cfeab4e2f157fc8da9b32a376ef587d483aa (diff) | |
download | chromium_src-42f058496033336073d38ba0a1e36052d5b0475c.zip chromium_src-42f058496033336073d38ba0a1e36052d5b0475c.tar.gz chromium_src-42f058496033336073d38ba0a1e36052d5b0475c.tar.bz2 |
Revert of Add TestMockTimeTaskRunner in base/test. (patchset #5 id:100001 of https://codereview.chromium.org/823143004/)
Reason for revert:
Suspect that this patch broke the chromeos asan bots. It looks like all the AutomaticRebootManagerTestInstance/AutomaticRebootManagerTest tests now leak memory:
https://build.chromium.org/p/chromium.memory/builders/Linux%20Chromium%20OS%20ASan%20LSan%20Tests%20%281%29/builds/5799
Original issue's description:
> Add TestMockTimeTaskRunner in base/test.
>
> Multiple implementations of this functionality exist with slight modifications. This CL consolidates them.
>
> BUG=329911
> TBR=thestig@chromium.org # For base/base.gyp changes.
>
> Committed: https://crrev.com/8d897fb17470c526155b2a9e86752be9a7f33c03
> Cr-Commit-Position: refs/heads/master@{#311091}
TBR=bartfab@chromium.org,phajdan.jr@chromium.org,stevenjb@chromium.org,thestig@chromium.org,engedy@chromium.org
NOTREECHECKS=true
NOTRY=true
BUG=329911
Review URL: https://codereview.chromium.org/844353002
Cr-Commit-Position: refs/heads/master@{#311114}
Diffstat (limited to 'base')
-rw-r--r-- | base/base.gyp | 2 | ||||
-rw-r--r-- | base/test/BUILD.gn | 2 | ||||
-rw-r--r-- | base/test/test_mock_time_task_runner.cc | 157 | ||||
-rw-r--r-- | base/test/test_mock_time_task_runner.h | 121 |
4 files changed, 0 insertions, 282 deletions
diff --git a/base/base.gyp b/base/base.gyp index 5d26fc4..2a4ee9f 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -976,8 +976,6 @@ 'test/test_io_thread.h', 'test/test_listener_ios.h', 'test/test_listener_ios.mm', - 'test/test_mock_time_task_runner.cc', - 'test/test_mock_time_task_runner.h', 'test/test_pending_task.cc', 'test/test_pending_task.h', 'test/test_reg_util_win.cc', diff --git a/base/test/BUILD.gn b/base/test/BUILD.gn index b4987d9..3aab89e 100644 --- a/base/test/BUILD.gn +++ b/base/test/BUILD.gn @@ -78,8 +78,6 @@ source_set("test_support") { "test_io_thread.h", "test_listener_ios.h", "test_listener_ios.mm", - "test_mock_time_task_runner.cc", - "test_mock_time_task_runner.h", "test_pending_task.cc", "test_pending_task.h", "test_reg_util_win.cc", diff --git a/base/test/test_mock_time_task_runner.cc b/base/test/test_mock_time_task_runner.cc deleted file mode 100644 index 1698391..0000000 --- a/base/test/test_mock_time_task_runner.cc +++ /dev/null @@ -1,157 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/test/test_mock_time_task_runner.h" - -#include "base/logging.h" -#include "base/memory/ref_counted.h" - -namespace base { - -namespace { - -// TickClock that always returns the then-current mock time of |task_runner| as -// the current time. -class MockTickClock : public TickClock { - public: - explicit MockTickClock( - scoped_refptr<const TestMockTimeTaskRunner> task_runner); - ~MockTickClock() override; - - // TickClock: - TimeTicks NowTicks() override; - - private: - scoped_refptr<const TestMockTimeTaskRunner> task_runner_; - - DISALLOW_COPY_AND_ASSIGN(MockTickClock); -}; - -MockTickClock::MockTickClock( - scoped_refptr<const TestMockTimeTaskRunner> task_runner) - : task_runner_(task_runner) { -} - -MockTickClock::~MockTickClock() { -} - -TimeTicks MockTickClock::NowTicks() { - return task_runner_->GetCurrentMockTime(); -} - -} // namespace - -bool TestMockTimeTaskRunner::TemporalOrder::operator()( - const TestPendingTask& first_task, - const TestPendingTask& second_task) const { - return first_task.GetTimeToRun() > second_task.GetTimeToRun(); -} - -TestMockTimeTaskRunner::TestMockTimeTaskRunner() { -} - -TestMockTimeTaskRunner::~TestMockTimeTaskRunner() { -} - -void TestMockTimeTaskRunner::FastForwardBy(TimeDelta delta) { - DCHECK(thread_checker_.CalledOnValidThread()); - - OnBeforeSelectingTask(); - - const base::TimeTicks original_now = now_; - TestPendingTask task_info; - while (DequeueNextTask(original_now, delta, &task_info)) { - if (task_info.GetTimeToRun() - now_ > base::TimeDelta()) { - now_ = task_info.GetTimeToRun(); - OnAfterTimePassed(); - } - - task_info.task.Run(); - - OnAfterTaskRun(); - OnBeforeSelectingTask(); - } - - if (!delta.is_max() && now_ - original_now < delta) { - now_ = original_now + delta; - OnAfterTimePassed(); - } -} - -void TestMockTimeTaskRunner::RunUntilIdle() { - FastForwardBy(TimeDelta()); -} - -void TestMockTimeTaskRunner::FastForwardUntilNoTasksRemain() { - FastForwardBy(TimeDelta::Max()); -} - -TimeTicks TestMockTimeTaskRunner::GetCurrentMockTime() const { - DCHECK(thread_checker_.CalledOnValidThread()); - return now_; -} - -scoped_ptr<TickClock> TestMockTimeTaskRunner::GetMockTickClock() const { - DCHECK(thread_checker_.CalledOnValidThread()); - return make_scoped_ptr(new MockTickClock(this)); -} - -bool TestMockTimeTaskRunner::HasPendingTask() const { - DCHECK(thread_checker_.CalledOnValidThread()); - return !tasks_.empty(); -} - -TimeDelta TestMockTimeTaskRunner::NextPendingTaskDelay() const { - DCHECK(thread_checker_.CalledOnValidThread()); - return tasks_.empty() ? TimeDelta::Max() : tasks_.top().GetTimeToRun() - now_; -} - -bool TestMockTimeTaskRunner::RunsTasksOnCurrentThread() const { - return thread_checker_.CalledOnValidThread(); -} - -bool TestMockTimeTaskRunner::PostDelayedTask( - const tracked_objects::Location& from_here, - const Closure& task, - TimeDelta delay) { - base::AutoLock scoped_lock(tasks_lock_); - tasks_.push( - TestPendingTask(from_here, task, now_, delay, TestPendingTask::NESTABLE)); - return true; -} - -bool TestMockTimeTaskRunner::PostNonNestableDelayedTask( - const tracked_objects::Location& from_here, - const Closure& task, - TimeDelta delay) { - NOTREACHED(); - return false; -} - -void TestMockTimeTaskRunner::OnBeforeSelectingTask() { - // Empty default implementation. -} - -void TestMockTimeTaskRunner::OnAfterTimePassed() { - // Empty default implementation. -} - -void TestMockTimeTaskRunner::OnAfterTaskRun() { - // Empty default implementation. -} - -bool TestMockTimeTaskRunner::DequeueNextTask(const base::TimeTicks& reference, - const base::TimeDelta& max_delta, - TestPendingTask* next_task) { - base::AutoLock scoped_lock(tasks_lock_); - if (!tasks_.empty() && - (tasks_.top().GetTimeToRun() - reference) <= max_delta) { - *next_task = tasks_.top(); - tasks_.pop(); - return true; - } - return false; -} - -} // namespace base diff --git a/base/test/test_mock_time_task_runner.h b/base/test/test_mock_time_task_runner.h deleted file mode 100644 index 12c426d..0000000 --- a/base/test/test_mock_time_task_runner.h +++ /dev/null @@ -1,121 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_ -#define BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_ - -#include <queue> -#include <vector> - -#include "base/macros.h" -#include "base/memory/scoped_ptr.h" -#include "base/single_thread_task_runner.h" -#include "base/synchronization/lock.h" -#include "base/test/test_pending_task.h" -#include "base/threading/thread_checker.h" -#include "base/time/tick_clock.h" -#include "base/time/time.h" - -namespace base { - -// Runs pending tasks in the order of the tasks' post time + delay, and keeps -// track of a mock (virtual) tick clock time that can be fast-forwarded. -// -// TestMockTimeTaskRunner has the following properties: -// -// - Methods RunsTasksOnCurrentThread() and Post[Delayed]Task() can be called -// from any thread, but the rest of the methods must be called on the same -// thread the TaskRunner was created on. -// - It allows for reentrancy, in that it handles the running of tasks that in -// turn call back into it (e.g., to post more tasks). -// - Tasks are stored in a priority queue, and executed in the increasing -// order of post time + delay. -// - Non-nestable tasks are not supported. -// - Tasks aren't guaranteed to be destroyed immediately after they're run. -// -// This is a slightly more sophisticated version of TestSimpleTaskRunner, in -// that it supports running delayed tasks in the correct temporal order. -class TestMockTimeTaskRunner : public base::SingleThreadTaskRunner { - public: - TestMockTimeTaskRunner(); - - // Fast-forwards virtual time by |delta|, causing all tasks with a remaining - // delay less than or equal to |delta| to be executed. - void FastForwardBy(base::TimeDelta delta); - - // Fast-forwards virtual time just until all tasks are executed. - void FastForwardUntilNoTasksRemain(); - - // Executes all tasks that have no remaining delay. Tasks with a remaining - // delay greater than zero will remain enqueued, and no virtual time will - // elapse. - void RunUntilIdle(); - - // Returns the current virtual time. - TimeTicks GetCurrentMockTime() const; - - // Returns a TickClock that uses the mock time of |this| as its time source. - scoped_ptr<TickClock> GetMockTickClock() const; - - bool HasPendingTask() const; - TimeDelta NextPendingTaskDelay() const; - - // SingleThreadTaskRunner: - bool RunsTasksOnCurrentThread() const override; - bool PostDelayedTask(const tracked_objects::Location& from_here, - const base::Closure& task, - TimeDelta delay) override; - bool PostNonNestableDelayedTask( - const tracked_objects::Location& from_here, - const base::Closure& task, - TimeDelta delay) override; - - protected: - ~TestMockTimeTaskRunner() override; - - // Called before the next task to run is selected, so that subclasses have a - // last chance to make sure all tasks are posted. - virtual void OnBeforeSelectingTask(); - - // Called after the current mock time has been incremented so that subclasses - // can react to the passing of time. - virtual void OnAfterTimePassed(); - - // Called after each task is run so that subclasses may perform additional - // activities, e.g., pump additional task runners. - virtual void OnAfterTaskRun(); - - private: - // Predicate that defines a strict weak temporal ordering of tasks. - class TemporalOrder { - public: - bool operator()(const TestPendingTask& first_task, - const TestPendingTask& second_task) const; - }; - - typedef std::priority_queue<TestPendingTask, - std::vector<TestPendingTask>, - TemporalOrder> TaskPriorityQueue; - - // Returns the |next_task| to run if there is any with a running time that is - // at most |reference| + |max_delta|. This additional complexity is required - // so that |max_delta| == TimeDelta::Max() can be supported. - bool DequeueNextTask(const base::TimeTicks& reference, - const base::TimeDelta& max_delta, - TestPendingTask* next_task); - - base::ThreadChecker thread_checker_; - base::TimeTicks now_; - - // Temporally ordered heap of pending tasks. Must only be accessed while the - // |tasks_lock_| is held. - TaskPriorityQueue tasks_; - base::Lock tasks_lock_; - - DISALLOW_COPY_AND_ASSIGN(TestMockTimeTaskRunner); -}; - -} // namespace base - -#endif // BASE_TEST_TEST_MOCK_TIME_TASK_RUNNER_H_ |