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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
#include <mbgl/test/util.hpp>
#include <mbgl/util/chrono.hpp>
#include <mbgl/util/timer.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/chrono.hpp>
#include <memory>
using namespace mbgl::util;
TEST(Timer, TEST_REQUIRES_ACCURATE_TIMING(Basic)) {
RunLoop loop;
Timer timer;
auto callback = [&loop] { loop.stop(); };
auto interval = mbgl::Milliseconds(300);
auto expectedTotalTime = interval;
auto first = mbgl::Clock::now();
timer.start(interval, mbgl::Duration::zero(), callback);
loop.run();
auto totalTime = std::chrono::duration_cast<mbgl::Milliseconds>(mbgl::Clock::now() - first);
// These are not high precision timers. Especially libuv uses
// cached time from the beginning of of the main loop iteration
// and it is very prone to fire earlier, which is, odd.
EXPECT_GE(totalTime, expectedTotalTime * 0.8);
EXPECT_LE(totalTime, expectedTotalTime * 1.2);
}
TEST(Timer, TEST_REQUIRES_ACCURATE_TIMING(Repeat)) {
RunLoop loop;
Timer timer;
unsigned count = 10;
auto callback = [&] {
if (!--count) {
loop.stop();
}
};
auto interval = mbgl::Milliseconds(50);
auto expectedTotalTime = interval * count;
auto first = mbgl::Clock::now();
timer.start(interval, interval, callback);
loop.run();
auto totalTime = std::chrono::duration_cast<mbgl::Milliseconds>(mbgl::Clock::now() - first);
EXPECT_GE(totalTime, expectedTotalTime * 0.8);
EXPECT_LE(totalTime, expectedTotalTime * 1.2);
}
TEST(Timer, TEST_REQUIRES_ACCURATE_TIMING(Stop)) {
RunLoop loop;
Timer timer1;
Timer timer2;
auto interval1 = mbgl::Milliseconds(50);
auto interval2 = mbgl::Milliseconds(250);
auto expectedTotalTime = interval2;
int count = 0;
auto callback1 = [&] {
++count;
timer1.stop();
};
auto callback2 = [&] {
++count;
loop.stop();
};
auto first = mbgl::Clock::now();
timer1.start(interval1, interval1, callback1);
timer2.start(interval2, mbgl::Duration::zero(), callback2);
loop.run();
auto totalTime = std::chrono::duration_cast<mbgl::Milliseconds>(mbgl::Clock::now() - first);
EXPECT_EQ(count, 2);
EXPECT_GE(totalTime, expectedTotalTime * 0.8);
EXPECT_LE(totalTime, expectedTotalTime * 1.2);
}
TEST(Timer, TEST_REQUIRES_ACCURATE_TIMING(DestroyShouldStop)) {
RunLoop loop;
auto timer1 = std::make_unique<Timer>();
Timer timer2;
auto interval1 = mbgl::Milliseconds(50);
auto interval2 = mbgl::Milliseconds(250);
auto expectedTotalTime = interval2;
int count = 0;
auto callback1 = [&] {
++count;
timer1.reset();
};
auto callback2 = [&] {
++count;
loop.stop();
};
auto first = mbgl::Clock::now();
timer1->start(interval1, interval1, callback1);
timer2.start(interval2, mbgl::Duration::zero(), callback2);
loop.run();
auto totalTime = std::chrono::duration_cast<mbgl::Milliseconds>(mbgl::Clock::now() - first);
EXPECT_EQ(count, 2);
EXPECT_GE(totalTime, expectedTotalTime * 0.8);
EXPECT_LE(totalTime, expectedTotalTime * 1.2);
}
TEST(Timer, TEST_REQUIRES_ACCURATE_TIMING(StartOverrides)) {
RunLoop loop;
Timer timer;
auto interval1 = mbgl::Milliseconds(50);
auto interval2 = mbgl::Milliseconds(250);
auto expectedTotalTime = interval1 + interval2;
int count = 0;
auto callback2 = [&] {
++count;
loop.stop();
};
auto callback1 = [&] {
++count;
timer.start(interval2, mbgl::Duration::zero(), callback2);
};
auto first = mbgl::Clock::now();
timer.start(interval1, mbgl::Duration::zero(), callback1);
loop.run();
auto totalTime = std::chrono::duration_cast<mbgl::Milliseconds>(mbgl::Clock::now() - first);
EXPECT_EQ(count, 2);
EXPECT_GE(totalTime, expectedTotalTime * 0.8);
EXPECT_LE(totalTime, expectedTotalTime * 1.2);
}
TEST(Timer, TEST_REQUIRES_ACCURATE_TIMING(CanStopNonStartedTimer)) {
RunLoop loop;
Timer timer;
timer.stop();
}
|