Software /
code /
prosody
Annotate
spec/util_throttle_spec.lua @ 12642:9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
We began moving away from simple "is this user an admin?" permission checks
before 0.12, with the introduction of mod_authz_internal and the ability to
dynamically change the roles of individual users.
The approach in 0.12 still had various limitations however, and apart from
the introduction of roles other than "admin" and the ability to pull that info
from storage, not much actually changed.
This new framework shakes things up a lot, though aims to maintain the same
functionality and behaviour on the surface for a default Prosody
configuration. That is, if you don't take advantage of any of the new
features, you shouldn't notice any change.
The biggest change visible to developers is that usermanager.is_admin() (and
the auth provider is_admin() method) have been removed. Gone. Completely.
Permission checks should now be performed using a new module API method:
module:may(action_name, context)
This method accepts an action name, followed by either a JID (string) or
(preferably) a table containing 'origin'/'session' and 'stanza' fields (e.g.
the standard object passed to most events). It will return true if the action
should be permitted, or false/nil otherwise.
Modules should no longer perform permission checks based on the role name.
E.g. a lot of code previously checked if the user's role was prosody:admin
before permitting some action. Since many roles might now exist with similar
permissions, and the permissions of prosody:admin may be redefined
dynamically, it is no longer suitable to use this method for permission
checks. Use module:may().
If you start an action name with ':' (recommended) then the current module's
name will automatically be used as a prefix.
To define a new permission, use the new module API:
module:default_permission(role_name, action_name)
module:default_permissions(role_name, { action_name[, action_name...] })
This grants the specified role permission to execute the named action(s) by
default. This may be overridden via other mechanisms external to your module.
The built-in roles that developers should use are:
- prosody:user (normal user)
- prosody:admin (host admin)
- prosody:operator (global admin)
The new prosody:operator role is intended for server-wide actions (such as
shutting down Prosody).
Finally, all usage of is_admin() in modules has been fixed by this commit.
Some of these changes were trickier than others, but no change is expected to
break existing deployments.
EXCEPT: mod_auth_ldap no longer supports the ldap_admin_filter option. It's
very possible nobody is using this, but if someone is then we can later update
it to pull roles from LDAP somehow.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 15 Jun 2022 12:15:01 +0100 |
parent | 9993:02a41315d275 |
child | 13399:f4415f76727b |
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); |