blob: 783af1b393cf3cd41f4b82f342d6c16173816d83 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
// 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 <functional>
#include <map>
#include <queue>
#include <set>
#include <utility>
#include <vector>
#include <base/location.h>
#include <base/test/simple_test_clock.h>
#include <base/time/time.h>
#include <brillo/brillo_export.h>
#include <brillo/message_loops/message_loop.h>
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<MessageLoop::TaskId, ScheduledTask> 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<base::Time, MessageLoop::TaskId>,
std::vector<std::pair<base::Time, MessageLoop::TaskId>>,
std::greater<std::pair<base::Time, MessageLoop::TaskId>>> 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_
|