util.x509: Nameprep commonName once
[prosody.git] / spec / util_throttle_spec.lua
blob985afae82a06ebc19ddc698f17df2c5dfae9d13d
3 -- Mock util.time
4 local now = 0; -- wibbly-wobbly... timey-wimey... stuff
5 local function later(n)
6 now = now + n; -- time passes at a different rate
7 end
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()
18 now = 5;
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 });
33 end);
34 end);
36 describe("#update()", function()
37 it("does nothing when no time has passed, even if balance is not full", function()
38 now = 5;
39 local a = throttle.create(10, 10);
40 for i=1,5 do
41 a:update();
42 assert.same(a, { balance = 10, max = 10, rate = 1, t = 5 });
43 end
44 a.balance = 0;
45 for i=1,5 do
46 a:update();
47 assert.same(a, { balance = 0, max = 10, rate = 1, t = 5 });
48 end
49 end);
50 it("updates only time when time passes but balance is full", function()
51 now = 5;
52 local a = throttle.create(10, 10);
53 for i=1,5 do
54 later(5);
55 a:update();
56 assert.same(a, { balance = 10, max = 10, rate = 1, t = 5 + i*5 });
57 end
58 end);
59 it("updates balance when balance has room to grow as time passes", function()
60 now = 5;
61 local a = throttle.create(10, 10);
62 a.balance = 0;
63 assert.same(a, { balance = 0, max = 10, rate = 1, t = 5 });
65 later(1);
66 a:update();
67 assert.same(a, { balance = 1, max = 10, rate = 1, t = 6 });
69 later(3);
70 a:update();
71 assert.same(a, { balance = 4, max = 10, rate = 1, t = 9 });
73 later(10);
74 a:update();
75 assert.same(a, { balance = 10, max = 10, rate = 1, t = 19 });
76 end);
77 it("handles 10 x 0.1s updates the same as 1 x 1s update ", function()
78 now = 5;
79 local a = throttle.create(1, 1);
81 a.balance = 0;
82 later(1);
83 a:update();
84 assert.same(a, { balance = 1, max = 1, rate = 1, t = now });
86 a.balance = 0;
87 for i=1,10 do
88 later(0.1);
89 a:update();
90 end
91 assert(math.abs(a.balance - 1) < 0.0001); -- incremental updates cause rounding errors
92 end);
93 end);
95 -- describe("po")
97 describe("#poll()", function()
98 it("should only allow successful polls until cost is hit", function()
99 now = 5;
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 });
113 end);
115 it("should not allow polls more than the cost", function()
116 now = 0;
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 });
130 -- fractional
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 });
148 end);
149 end);
150 end);