Annotate

spec/util_throttle_spec.lua @ 13248:db433ed3135c

Merge 0.12->trunk
author Kim Alvefur <zash@zash.se>
date Tue, 25 Jul 2023 12:35:11 +0200
parent 9993:02a41315d275
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
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 -- Mock util.time
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 local now = 0; -- wibbly-wobbly... timey-wimey... stuff
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 local function later(n)
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 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
7 end
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8 package.loaded["util.time"] = {
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 now = function() return now; end
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 }
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 local throttle = require "util.throttle";
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14
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
15 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
16 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
17 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
18 now = 5;
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 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
20 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
21
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 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
23 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
24
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 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
26 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
27
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 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
29 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
30
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 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
32 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
33 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
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
8246
ea2667fc6781 util_throttle_spec: Slight stylistic update for function test group titles
Waqas Hussain <waqas20@gmail.com>
parents: 8245
diff changeset
36 describe("#update()", function()
8473
f024cb5acc25 util_throttle_spec: Fix minor typo in test title
Waqas Hussain <waqas20@gmail.com>
parents: 8246
diff changeset
37 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
38 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
39 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
40 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
41 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
42 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
43 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
44 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
45 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
46 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
47 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
48 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
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 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
51 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
52 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
53 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
54 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
55 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
56 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
57 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
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 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
60 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
61 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
62 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
63 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
64
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 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
66 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
67 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
68
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 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
70 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
71 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
72
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 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
74 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
75 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
76 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
77 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
78 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
79 local a = throttle.create(1, 1);
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80
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
81 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
82 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
83 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
84 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
85
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 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
87 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
88 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
89 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
90 end
9993
02a41315d275 Fix various spelling mistakes [codespell]
Kim Alvefur <zash@zash.se>
parents: 8473
diff changeset
91 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
92 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
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
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 -- 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
96
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 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
98 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
99 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
100
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 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
102 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
103
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 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
105 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
106
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 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
108 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
109
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 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
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.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
113 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
114
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 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
116 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
117
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 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
119 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
120
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 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
122 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
123
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 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
125 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
126
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 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
128 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
129
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 -- 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
131 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
132 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
133
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 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
135 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
136
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 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
138 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
139
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 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
141 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
142
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 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
144 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
145
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 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
147 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
148 end);
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);