Comparison

tests/test_util_throttle.lua @ 7024:8ce592e376ff

tests: Add small test for util.throttle
author Kim Alvefur <zash@zash.se>
date Wed, 23 Dec 2015 08:49:38 +0100
child 8262:e6f3e440c843
comparison
equal deleted inserted replaced
7023:c2ccbfe30113 7024:8ce592e376ff
1
2 local now = 0; -- wibbly-wobbly... timey-wimey... stuff
3 local function predictable_gettime()
4 return now;
5 end
6 local function later(n)
7 now = now + n; -- time passes at a different rate
8 end
9
10 local function override_gettime(throttle)
11 local i = 0;
12 repeat
13 i = i + 1;
14 local name = debug.getupvalue(throttle.update, i);
15 if name then
16 debug.setupvalue(throttle.update, i, predictable_gettime);
17 return throttle;
18 end
19 until not name;
20 end
21
22 function create(create)
23 local a = override_gettime( create(3, 10) );
24
25 assert_equal(a:poll(1), true); -- 3 -> 2
26 assert_equal(a:poll(1), true); -- 2 -> 1
27 assert_equal(a:poll(1), true); -- 1 -> 0
28 assert_equal(a:poll(1), false); -- MEEP, out of credits!
29 later(1); -- ... what about
30 assert_equal(a:poll(1), false); -- now? - Still no!
31 later(9); -- Later that day
32 assert_equal(a:poll(1), true); -- Should be back at 3 credits ... 2
33 end
34