4 local now
= 0; -- wibbly-wobbly... timey-wimey... stuff
5 local function later(n
)
6 now
= now
+ n
; -- time passes at a different rate
8 package
.loaded
["util.time"] = {
9 now
= function() return now
; end
13 local throttle
= require
"util.throttle";
15 describe("util.throttle", function()
16 describe("#create()", function()
17 it("should be created with correct values", function()
19 local a
= throttle
.create(3, 10);
20 assert.same(a
, { balance
= 3, max = 3, rate
= 0.3, t
= 5 });
22 local a
= throttle
.create(3, 5);
23 assert.same(a
, { balance
= 3, max = 3, rate
= 0.6, t
= 5 });
25 local a
= throttle
.create(1, 1);
26 assert.same(a
, { balance
= 1, max = 1, rate
= 1, t
= 5 });
28 local a
= throttle
.create(10, 10);
29 assert.same(a
, { balance
= 10, max = 10, rate
= 1, t
= 5 });
31 local a
= throttle
.create(10, 1);
32 assert.same(a
, { balance
= 10, max = 10, rate
= 10, t
= 5 });
36 describe("#update()", function()
37 it("does nothing when no time has passed, even if balance is not full", function()
39 local a
= throttle
.create(10, 10);
42 assert.same(a
, { balance
= 10, max = 10, rate
= 1, t
= 5 });
47 assert.same(a
, { balance
= 0, max = 10, rate
= 1, t
= 5 });
50 it("updates only time when time passes but balance is full", function()
52 local a
= throttle
.create(10, 10);
56 assert.same(a
, { balance
= 10, max = 10, rate
= 1, t
= 5 + i
*5 });
59 it("updates balance when balance has room to grow as time passes", function()
61 local a
= throttle
.create(10, 10);
63 assert.same(a
, { balance
= 0, max = 10, rate
= 1, t
= 5 });
67 assert.same(a
, { balance
= 1, max = 10, rate
= 1, t
= 6 });
71 assert.same(a
, { balance
= 4, max = 10, rate
= 1, t
= 9 });
75 assert.same(a
, { balance
= 10, max = 10, rate
= 1, t
= 19 });
77 it("handles 10 x 0.1s updates the same as 1 x 1s update ", function()
79 local a
= throttle
.create(1, 1);
84 assert.same(a
, { balance
= 1, max = 1, rate
= 1, t
= now
});
91 assert(math
.abs(a
.balance
- 1) < 0.0001); -- incremental updates cause rounding errors
97 describe("#poll()", function()
98 it("should only allow successful polls until cost is hit", function()
101 local a
= throttle
.create(3, 10);
102 assert.same(a
, { balance
= 3, max = 3, rate
= 0.3, t
= 5 });
104 assert.is_true(a
:poll(1)); -- 3 -> 2
105 assert.same(a
, { balance
= 2, max = 3, rate
= 0.3, t
= 5 });
107 assert.is_true(a
:poll(2)); -- 2 -> 1
108 assert.same(a
, { balance
= 0, max = 3, rate
= 0.3, t
= 5 });
110 assert.is_false(a
:poll(1)); -- MEEP, out of credits!
111 assert.is_false(a
:poll(1)); -- MEEP, out of credits!
112 assert.same(a
, { balance
= 0, max = 3, rate
= 0.3, t
= 5 });
115 it("should not allow polls more than the cost", function()
118 local a
= throttle
.create(10, 10);
119 assert.same(a
, { balance
= 10, max = 10, rate
= 1, t
= 0 });
121 assert.is_false(a
:poll(11));
122 assert.same(a
, { balance
= 10, max = 10, rate
= 1, t
= 0 });
124 assert.is_true(a
:poll(6));
125 assert.same(a
, { balance
= 4, max = 10, rate
= 1, t
= 0 });
127 assert.is_false(a
:poll(5));
128 assert.same(a
, { balance
= 4, max = 10, rate
= 1, t
= 0 });
131 assert.is_true(a
:poll(3.5));
132 assert.same(a
, { balance
= 0.5, max = 10, rate
= 1, t
= 0 });
134 assert.is_true(a
:poll(0.25));
135 assert.same(a
, { balance
= 0.25, max = 10, rate
= 1, t
= 0 });
137 assert.is_false(a
:poll(0.3));
138 assert.same(a
, { balance
= 0.25, max = 10, rate
= 1, t
= 0 });
140 assert.is_true(a
:poll(0.25));
141 assert.same(a
, { balance
= 0, max = 10, rate
= 1, t
= 0 });
143 assert.is_false(a
:poll(0.1));
144 assert.same(a
, { balance
= 0, max = 10, rate
= 1, t
= 0 });
146 assert.is_true(a
:poll(0));
147 assert.same(a
, { balance
= 0, max = 10, rate
= 1, t
= 0 });