aboutsummaryrefslogtreecommitdiffstats
path: root/spec/util_throttle_spec.lua
blob: 077f3d40517647c39a16eaed82cef1d69ca750c0 (plain)
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

-- luacheck: ignore 411/a

-- Mock util.time
local now = 0; -- wibbly-wobbly... timey-wimey... stuff
local function later(n)
	now = now + n; -- time passes at a different rate
end
package.loaded["util.time"] = {
	now = function() return now; end
}


local throttle = require "util.throttle";

describe("util.throttle", function()
	describe("#create()", function()
		it("should be created with correct values", function()
			now = 5;
			local a = throttle.create(3, 10);
			assert.same(a, { balance = 3, max = 3, rate = 0.3, t = 5 });

			local a = throttle.create(3, 5);
			assert.same(a, { balance = 3, max = 3, rate = 0.6, t = 5 });

			local a = throttle.create(1, 1);
			assert.same(a, { balance = 1, max = 1, rate = 1, t = 5 });

			local a = throttle.create(10, 10);
			assert.same(a, { balance = 10, max = 10, rate = 1, t = 5 });

			local a = throttle.create(10, 1);
			assert.same(a, { balance = 10, max = 10, rate = 10, t = 5 });
		end);
	end);

	describe("#update()", function()
		it("does nothing when no time has passed, even if balance is not full", function()
			now = 5;
			local a = throttle.create(10, 10);
			for i=1,5 do
				a:update();
				assert.same(a, { balance = 10, max = 10, rate = 1, t = 5 });
			end
			a.balance = 0;
			for i=1,5 do
				a:update();
				assert.same(a, { balance = 0, max = 10, rate = 1, t = 5 });
			end
		end);
		it("updates only time when time passes but balance is full", function()
			now = 5;
			local a = throttle.create(10, 10);
			for i=1,5 do
				later(5);
				a:update();
				assert.same(a, { balance = 10, max = 10, rate = 1, t = 5 + i*5 });
			end
		end);
		it("updates balance when balance has room to grow as time passes", function()
			now = 5;
			local a = throttle.create(10, 10);
			a.balance = 0;
			assert.same(a, { balance = 0, max = 10, rate = 1, t = 5 });

			later(1);
			a:update();
			assert.same(a, { balance = 1, max = 10, rate = 1, t = 6 });

			later(3);
			a:update();
			assert.same(a, { balance = 4, max = 10, rate = 1, t = 9 });

			later(10);
			a:update();
			assert.same(a, { balance = 10, max = 10, rate = 1, t = 19 });
		end);
		it("handles 10 x 0.1s updates the same as 1 x 1s update ", function()
			now = 5;
			local a = throttle.create(1, 1);

			a.balance = 0;
			later(1);
			a:update();
			assert.same(a, { balance = 1, max = 1, rate = 1, t = now });

			a.balance = 0;
			for i=1,10 do
				later(0.1);
				a:update();
			end
			assert(math.abs(a.balance - 1) < 0.0001); -- incremental updates cause rounding errors
		end);
	end);

	-- describe("po")

	describe("#poll()", function()
		it("should only allow successful polls until cost is hit", function()
			now = 5;

			local a = throttle.create(3, 10);
			assert.same(a, { balance = 3, max = 3, rate = 0.3, t = 5 });

			assert.is_true(a:poll(1));  -- 3 -> 2
			assert.same(a, { balance = 2, max = 3, rate = 0.3, t = 5 });

			assert.is_true(a:poll(2));  -- 2 -> 1
			assert.same(a, { balance = 0, max = 3, rate = 0.3, t = 5 });

			assert.is_false(a:poll(1)); -- MEEP, out of credits!
			assert.is_false(a:poll(1)); -- MEEP, out of credits!
			assert.same(a, { balance = 0, max = 3, rate = 0.3, t = 5 });
		end);

		it("should not allow polls more than the cost", function()
			now = 0;

			local a = throttle.create(10, 10);
			assert.same(a, { balance = 10, max = 10, rate = 1, t = 0 });

			assert.is_false(a:poll(11));
			assert.same(a, { balance = 10, max = 10, rate = 1, t = 0 });

			assert.is_true(a:poll(6));
			assert.same(a, { balance = 4, max = 10, rate = 1, t = 0 });

			assert.is_false(a:poll(5));
			assert.same(a, { balance = 4, max = 10, rate = 1, t = 0 });

			-- fractional
			assert.is_true(a:poll(3.5));
			assert.same(a, { balance = 0.5, max = 10, rate = 1, t = 0 });

			assert.is_true(a:poll(0.25));
			assert.same(a, { balance = 0.25, max = 10, rate = 1, t = 0 });

			assert.is_false(a:poll(0.3));
			assert.same(a, { balance = 0.25, max = 10, rate = 1, t = 0 });

			assert.is_true(a:poll(0.25));
			assert.same(a, { balance = 0, max = 10, rate = 1, t = 0 });

			assert.is_false(a:poll(0.1));
			assert.same(a, { balance = 0, max = 10, rate = 1, t = 0 });

			assert.is_true(a:poll(0));
			assert.same(a, { balance = 0, max = 10, rate = 1, t = 0 });
		end);
	end);
end);