Alex Deymo | ef3955a | 2015-06-09 10:14:01 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Vakulenko | fed60b0 | 2015-10-27 09:53:05 -0700 | [diff] [blame] | 5 | #ifndef LIBBRILLO_BRILLO_MESSAGE_LOOPS_FAKE_MESSAGE_LOOP_H_ |
| 6 | #define LIBBRILLO_BRILLO_MESSAGE_LOOPS_FAKE_MESSAGE_LOOP_H_ |
Alex Deymo | ef3955a | 2015-06-09 10:14:01 -0700 | [diff] [blame] | 7 | |
| 8 | #include <functional> |
| 9 | #include <map> |
| 10 | #include <queue> |
Alex Deymo | 3845c6d | 2015-06-25 11:08:30 -0700 | [diff] [blame] | 11 | #include <set> |
Alex Deymo | ef3955a | 2015-06-09 10:14:01 -0700 | [diff] [blame] | 12 | #include <utility> |
| 13 | #include <vector> |
| 14 | |
Alex Deymo | 106edd1 | 2015-06-18 20:15:11 -0700 | [diff] [blame] | 15 | #include <base/location.h> |
Alex Deymo | ef3955a | 2015-06-09 10:14:01 -0700 | [diff] [blame] | 16 | #include <base/test/simple_test_clock.h> |
| 17 | #include <base/time/time.h> |
| 18 | |
Alex Vakulenko | 9ed0cab | 2015-10-12 15:21:28 -0700 | [diff] [blame] | 19 | #include <brillo/brillo_export.h> |
| 20 | #include <brillo/message_loops/message_loop.h> |
Alex Deymo | ef3955a | 2015-06-09 10:14:01 -0700 | [diff] [blame] | 21 | |
Alex Vakulenko | 9ed0cab | 2015-10-12 15:21:28 -0700 | [diff] [blame] | 22 | namespace brillo { |
Alex Deymo | ef3955a | 2015-06-09 10:14:01 -0700 | [diff] [blame] | 23 | |
| 24 | // The FakeMessageLoop implements a message loop that doesn't block or wait for |
| 25 | // time based tasks to be ready. The tasks are executed in the order they should |
| 26 | // be executed in a real message loop implementation, but the time is advanced |
| 27 | // to the time when the first task should be executed instead of blocking. |
| 28 | // To keep a consistent notion of time for other classes, FakeMessageLoop |
| 29 | // optionally updates a SimpleTestClock instance when it needs to advance the |
| 30 | // clock. |
| 31 | // This message loop implementation is useful for unittests. |
Alex Vakulenko | 9ed0cab | 2015-10-12 15:21:28 -0700 | [diff] [blame] | 32 | class BRILLO_EXPORT FakeMessageLoop : public MessageLoop { |
Alex Deymo | ef3955a | 2015-06-09 10:14:01 -0700 | [diff] [blame] | 33 | public: |
| 34 | // Create a FakeMessageLoop optionally using a SimpleTestClock to update the |
| 35 | // time when Run() or RunOnce(true) are called and should block. |
| 36 | explicit FakeMessageLoop(base::SimpleTestClock* clock); |
| 37 | ~FakeMessageLoop() override = default; |
| 38 | |
Alex Deymo | 3845c6d | 2015-06-25 11:08:30 -0700 | [diff] [blame] | 39 | TaskId PostDelayedTask(const tracked_objects::Location& from_here, |
Alex Deymo | a8632e4 | 2015-07-14 08:38:28 -0700 | [diff] [blame] | 40 | const base::Closure& task, |
Alex Deymo | 3845c6d | 2015-06-25 11:08:30 -0700 | [diff] [blame] | 41 | base::TimeDelta delay) override; |
Alex Deymo | 106edd1 | 2015-06-18 20:15:11 -0700 | [diff] [blame] | 42 | using MessageLoop::PostDelayedTask; |
Alex Deymo | 3845c6d | 2015-06-25 11:08:30 -0700 | [diff] [blame] | 43 | TaskId WatchFileDescriptor(const tracked_objects::Location& from_here, |
| 44 | int fd, |
| 45 | WatchMode mode, |
| 46 | bool persistent, |
Alex Deymo | a8632e4 | 2015-07-14 08:38:28 -0700 | [diff] [blame] | 47 | const base::Closure& task) override; |
Alex Deymo | 3845c6d | 2015-06-25 11:08:30 -0700 | [diff] [blame] | 48 | using MessageLoop::WatchFileDescriptor; |
Alex Deymo | ef3955a | 2015-06-09 10:14:01 -0700 | [diff] [blame] | 49 | bool CancelTask(TaskId task_id) override; |
| 50 | bool RunOnce(bool may_block) override; |
| 51 | |
| 52 | // FakeMessageLoop methods: |
| 53 | |
Alex Deymo | 3845c6d | 2015-06-25 11:08:30 -0700 | [diff] [blame] | 54 | // Pretend, for the purpose of the FakeMessageLoop watching for file |
| 55 | // descriptors, that the file descriptor |fd| readiness to perform the |
| 56 | // operation described by |mode| is |ready|. Initially, no file descriptor |
| 57 | // is ready for any operation. |
| 58 | void SetFileDescriptorReadiness(int fd, WatchMode mode, bool ready); |
| 59 | |
Alex Deymo | ef3955a | 2015-06-09 10:14:01 -0700 | [diff] [blame] | 60 | // Return whether there are peding tasks. Useful to check that no |
| 61 | // callbacks were leaked. |
| 62 | bool PendingTasks(); |
| 63 | |
| 64 | private: |
Alex Deymo | 3845c6d | 2015-06-25 11:08:30 -0700 | [diff] [blame] | 65 | struct ScheduledTask { |
| 66 | tracked_objects::Location location; |
| 67 | bool persistent; |
| 68 | base::Closure callback; |
| 69 | }; |
| 70 | |
| 71 | // The sparse list of scheduled pending callbacks. |
| 72 | std::map<MessageLoop::TaskId, ScheduledTask> tasks_; |
| 73 | |
Alex Deymo | ef3955a | 2015-06-09 10:14:01 -0700 | [diff] [blame] | 74 | // Using std::greater<> for the priority_queue means that the top() of the |
| 75 | // queue is the lowest (earliest) time, and for the same time, the smallest |
| 76 | // TaskId. This determines the order in which the tasks will be fired. |
| 77 | std::priority_queue< |
| 78 | std::pair<base::Time, MessageLoop::TaskId>, |
| 79 | std::vector<std::pair<base::Time, MessageLoop::TaskId>>, |
| 80 | std::greater<std::pair<base::Time, MessageLoop::TaskId>>> fire_order_; |
Alex Deymo | 3845c6d | 2015-06-25 11:08:30 -0700 | [diff] [blame] | 81 | |
| 82 | // The bag of watched (fd, mode) pair associated with the TaskId that's |
| 83 | // watching them. |
| 84 | std::multimap<std::pair<int, WatchMode>, MessageLoop::TaskId> fds_watched_; |
| 85 | |
| 86 | // The set of (fd, mode) pairs that are faked as ready. |
| 87 | std::set<std::pair<int, WatchMode>> fds_ready_; |
Alex Deymo | ef3955a | 2015-06-09 10:14:01 -0700 | [diff] [blame] | 88 | |
| 89 | base::SimpleTestClock* test_clock_ = nullptr; |
| 90 | base::Time current_time_ = base::Time::FromDoubleT(1246996800.); |
| 91 | |
| 92 | MessageLoop::TaskId last_id_ = kTaskIdNull; |
| 93 | |
| 94 | DISALLOW_COPY_AND_ASSIGN(FakeMessageLoop); |
| 95 | }; |
| 96 | |
Alex Vakulenko | 9ed0cab | 2015-10-12 15:21:28 -0700 | [diff] [blame] | 97 | } // namespace brillo |
Alex Deymo | ef3955a | 2015-06-09 10:14:01 -0700 | [diff] [blame] | 98 | |
Alex Vakulenko | fed60b0 | 2015-10-27 09:53:05 -0700 | [diff] [blame] | 99 | #endif // LIBBRILLO_BRILLO_MESSAGE_LOOPS_FAKE_MESSAGE_LOOP_H_ |