Annotate

spec/util_throttle_spec.lua @ 13742:47e537e340c4 default tip

Merge 13.0->trunk
author Matthew Wild <mwild1@gmail.com>
date Mon, 17 Feb 2025 23:06:26 +0000
parent 13399:f4415f76727b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1
13399
f4415f76727b util.throttle: Silence some strict luacheck warnings
Kim Alvefur <zash@zash.se>
parents: 9993
diff changeset
2 -- luacheck: ignore 411/a
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 -- Mock util.time
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 local now = 0; -- wibbly-wobbly... timey-wimey... stuff
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 local function later(n)
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 now = now + n; -- time passes at a different rate
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8 end
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 package.loaded["util.time"] = {
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 now = function() return now; end
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 }
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 local throttle = require "util.throttle";
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15
8245
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
16 describe("util.throttle", function()
8246
ea2667fc6781 util_throttle_spec: Slight stylistic update for function test group titles
Waqas Hussain <waqas20@gmail.com>
parents: 8245
diff changeset
17 describe("#create()", function()
8245
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
18 it("should be created with correct values", function()
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
19 now = 5;
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 local a = throttle.create(3, 10);
8245
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
21 assert.same(a, { balance = 3, max = 3, rate = 0.3, t = 5 });
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
22
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
23 local a = throttle.create(3, 5);
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
24 assert.same(a, { balance = 3, max = 3, rate = 0.6, t = 5 });
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
25
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
26 local a = throttle.create(1, 1);
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
27 assert.same(a, { balance = 1, max = 1, rate = 1, t = 5 });
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
28
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
29 local a = throttle.create(10, 10);
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
30 assert.same(a, { balance = 10, max = 10, rate = 1, t = 5 });
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
31
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
32 local a = throttle.create(10, 1);
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
33 assert.same(a, { balance = 10, max = 10, rate = 10, t = 5 });
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
34 end);
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
35 end);
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
36
8246
ea2667fc6781 util_throttle_spec: Slight stylistic update for function test group titles
Waqas Hussain <waqas20@gmail.com>
parents: 8245
diff changeset
37 describe("#update()", function()
8473
f024cb5acc25 util_throttle_spec: Fix minor typo in test title
Waqas Hussain <waqas20@gmail.com>
parents: 8246
diff changeset
38 it("does nothing when no time has passed, even if balance is not full", function()
8245
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
39 now = 5;
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
40 local a = throttle.create(10, 10);
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
41 for i=1,5 do
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
42 a:update();
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
43 assert.same(a, { balance = 10, max = 10, rate = 1, t = 5 });
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
44 end
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
45 a.balance = 0;
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
46 for i=1,5 do
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
47 a:update();
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
48 assert.same(a, { balance = 0, max = 10, rate = 1, t = 5 });
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
49 end
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
50 end);
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
51 it("updates only time when time passes but balance is full", function()
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
52 now = 5;
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
53 local a = throttle.create(10, 10);
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
54 for i=1,5 do
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
55 later(5);
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
56 a:update();
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
57 assert.same(a, { balance = 10, max = 10, rate = 1, t = 5 + i*5 });
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
58 end
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
59 end);
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
60 it("updates balance when balance has room to grow as time passes", function()
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
61 now = 5;
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
62 local a = throttle.create(10, 10);
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
63 a.balance = 0;
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
64 assert.same(a, { balance = 0, max = 10, rate = 1, t = 5 });
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
65
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
66 later(1);
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
67 a:update();
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
68 assert.same(a, { balance = 1, max = 10, rate = 1, t = 6 });
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
69
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
70 later(3);
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
71 a:update();
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
72 assert.same(a, { balance = 4, max = 10, rate = 1, t = 9 });
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
73
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
74 later(10);
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
75 a:update();
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
76 assert.same(a, { balance = 10, max = 10, rate = 1, t = 19 });
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
77 end);
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
78 it("handles 10 x 0.1s updates the same as 1 x 1s update ", function()
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
79 now = 5;
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
80 local a = throttle.create(1, 1);
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81
8245
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
82 a.balance = 0;
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
83 later(1);
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
84 a:update();
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
85 assert.same(a, { balance = 1, max = 1, rate = 1, t = now });
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
86
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
87 a.balance = 0;
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
88 for i=1,10 do
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
89 later(0.1);
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
90 a:update();
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
91 end
9993
02a41315d275 Fix various spelling mistakes [codespell]
Kim Alvefur <zash@zash.se>
parents: 8473
diff changeset
92 assert(math.abs(a.balance - 1) < 0.0001); -- incremental updates cause rounding errors
8245
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
93 end);
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
94 end);
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
95
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
96 -- describe("po")
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
97
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
98 describe("#poll()", function()
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
99 it("should only allow successful polls until cost is hit", function()
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
100 now = 5;
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
101
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
102 local a = throttle.create(3, 10);
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
103 assert.same(a, { balance = 3, max = 3, rate = 0.3, t = 5 });
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
104
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
105 assert.is_true(a:poll(1)); -- 3 -> 2
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
106 assert.same(a, { balance = 2, max = 3, rate = 0.3, t = 5 });
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
107
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
108 assert.is_true(a:poll(2)); -- 2 -> 1
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
109 assert.same(a, { balance = 0, max = 3, rate = 0.3, t = 5 });
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
110
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
111 assert.is_false(a:poll(1)); -- MEEP, out of credits!
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
112 assert.is_false(a:poll(1)); -- MEEP, out of credits!
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
113 assert.same(a, { balance = 0, max = 3, rate = 0.3, t = 5 });
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
114 end);
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
115
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
116 it("should not allow polls more than the cost", function()
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
117 now = 0;
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
118
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
119 local a = throttle.create(10, 10);
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
120 assert.same(a, { balance = 10, max = 10, rate = 1, t = 0 });
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
121
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
122 assert.is_false(a:poll(11));
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
123 assert.same(a, { balance = 10, max = 10, rate = 1, t = 0 });
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
124
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
125 assert.is_true(a:poll(6));
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
126 assert.same(a, { balance = 4, max = 10, rate = 1, t = 0 });
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
127
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
128 assert.is_false(a:poll(5));
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
129 assert.same(a, { balance = 4, max = 10, rate = 1, t = 0 });
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
130
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
131 -- fractional
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
132 assert.is_true(a:poll(3.5));
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
133 assert.same(a, { balance = 0.5, max = 10, rate = 1, t = 0 });
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
134
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
135 assert.is_true(a:poll(0.25));
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
136 assert.same(a, { balance = 0.25, max = 10, rate = 1, t = 0 });
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
137
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
138 assert.is_false(a:poll(0.3));
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
139 assert.same(a, { balance = 0.25, max = 10, rate = 1, t = 0 });
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
140
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
141 assert.is_true(a:poll(0.25));
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
142 assert.same(a, { balance = 0, max = 10, rate = 1, t = 0 });
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
143
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
144 assert.is_false(a:poll(0.1));
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
145 assert.same(a, { balance = 0, max = 10, rate = 1, t = 0 });
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
146
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
147 assert.is_true(a:poll(0));
9499db96c032 util.throttle: Fix initial time setting (double accounting the first time) and fractional balance updates (0.1*10 was not the same as 1*1)
Waqas Hussain <waqas20@gmail.com>
parents: 8236
diff changeset
148 assert.same(a, { balance = 0, max = 10, rate = 1, t = 0 });
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
149 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
150 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
151 end);