Software /
code /
prosody
Annotate
tests/test_util_throttle.lua @ 7133:ac142f5209d9
loggingmanager: Write out color code, log level and reset code in one call
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 04 Feb 2016 17:03:04 +0100 |
parent | 7024:8ce592e376ff |
child | 8262:e6f3e440c843 |
rev | line source |
---|---|
7024
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 local now = 0; -- wibbly-wobbly... timey-wimey... stuff |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 local function predictable_gettime() |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 return now; |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 end |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 local function later(n) |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 now = now + n; -- time passes at a different rate |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 end |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 local function override_gettime(throttle) |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 local i = 0; |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 repeat |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 i = i + 1; |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 local name = debug.getupvalue(throttle.update, i); |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 if name then |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 debug.setupvalue(throttle.update, i, predictable_gettime); |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 return throttle; |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 end |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 until not name; |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 end |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 function create(create) |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 local a = override_gettime( create(3, 10) ); |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 assert_equal(a:poll(1), true); -- 3 -> 2 |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 assert_equal(a:poll(1), true); -- 2 -> 1 |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 assert_equal(a:poll(1), true); -- 1 -> 0 |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 assert_equal(a:poll(1), false); -- MEEP, out of credits! |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 later(1); -- ... what about |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 assert_equal(a:poll(1), false); -- now? - Still no! |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 later(9); -- Later that day |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 assert_equal(a:poll(1), true); -- Should be back at 3 credits ... 2 |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 end |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 |