// Copyright 2015 The Chromium OS 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 LIBBRILLO_BRILLO_MESSAGE_LOOPS_FAKE_MESSAGE_LOOP_H_ #define LIBBRILLO_BRILLO_MESSAGE_LOOPS_FAKE_MESSAGE_LOOP_H_ #include #include #include #include #include #include #include #include #include #include #include namespace brillo { // The FakeMessageLoop implements a message loop that doesn't block or wait for // time based tasks to be ready. The tasks are executed in the order they should // be executed in a real message loop implementation, but the time is advanced // to the time when the first task should be executed instead of blocking. // To keep a consistent notion of time for other classes, FakeMessageLoop // optionally updates a SimpleTestClock instance when it needs to advance the // clock. // This message loop implementation is useful for unittests. class BRILLO_EXPORT FakeMessageLoop : public MessageLoop { public: // Create a FakeMessageLoop optionally using a SimpleTestClock to update the // time when Run() or RunOnce(true) are called and should block. explicit FakeMessageLoop(base::SimpleTestClock* clock); ~FakeMessageLoop() override = default; TaskId PostDelayedTask(const base::Location& from_here, base::OnceClosure task, base::TimeDelta delay) override; using MessageLoop::PostDelayedTask; bool CancelTask(TaskId task_id) override; bool RunOnce(bool may_block) override; // FakeMessageLoop methods: // Return whether there are peding tasks. Useful to check that no // callbacks were leaked. bool PendingTasks(); private: struct ScheduledTask { base::Location location; base::OnceClosure callback; }; // The sparse list of scheduled pending callbacks. std::map tasks_; // Using std::greater<> for the priority_queue means that the top() of the // queue is the lowest (earliest) time, and for the same time, the smallest // TaskId. This determines the order in which the tasks will be fired. std::priority_queue< std::pair, std::vector>, std::greater>> fire_order_; base::SimpleTestClock* test_clock_ = nullptr; base::Time current_time_ = base::Time::FromDoubleT(1246996800.); MessageLoop::TaskId last_id_ = kTaskIdNull; DISALLOW_COPY_AND_ASSIGN(FakeMessageLoop); }; } // namespace brillo #endif // LIBBRILLO_BRILLO_MESSAGE_LOOPS_FAKE_MESSAGE_LOOP_H_