Software /
code /
prosody
Annotate
tests/test_util_throttle.lua @ 8792:c2b99fa134b3
MUC: Import revised, more comprehensive patch for 8da11142fabf (#345)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 16 May 2018 12:14:21 +0100 |
parent | 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 |
8262
e6f3e440c843
tests: util.throttle: Fix time override to simply override util.time. Recent change bypasses current override method.
Matthew Wild <mwild1@gmail.com>
parents:
7024
diff
changeset
|
10 package.loaded["util.time"] = { |
e6f3e440c843
tests: util.throttle: Fix time override to simply override util.time. Recent change bypasses current override method.
Matthew Wild <mwild1@gmail.com>
parents:
7024
diff
changeset
|
11 now = predictable_gettime; |
e6f3e440c843
tests: util.throttle: Fix time override to simply override util.time. Recent change bypasses current override method.
Matthew Wild <mwild1@gmail.com>
parents:
7024
diff
changeset
|
12 } |
7024
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 function create(create) |
8262
e6f3e440c843
tests: util.throttle: Fix time override to simply override util.time. Recent change bypasses current override method.
Matthew Wild <mwild1@gmail.com>
parents:
7024
diff
changeset
|
15 local a = create(3, 10); |
7024
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 assert_equal(a:poll(1), true); -- 3 -> 2 |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 assert_equal(a:poll(1), true); -- 2 -> 1 |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 assert_equal(a:poll(1), true); -- 1 -> 0 |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 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
|
21 later(1); -- ... what about |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 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
|
23 later(9); -- Later that day |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 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
|
25 end |
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 |