Skip to content

Commit 4545913

Browse files
committed
Add unit tests for tokenbucket.py
1 parent 5ab6a77 commit 4545913

2 files changed

Lines changed: 68 additions & 5 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import time
2+
3+
from cloudbot.util.tokenbucket import TokenBucket
4+
5+
6+
# noinspection PyProtectedMember
7+
def test_bucket_consume():
8+
bucket = TokenBucket(10, 5)
9+
# larger then capacity
10+
assert bucket.consume(15) is False
11+
# success
12+
assert bucket.consume(10) is True
13+
# check if bucket has no tokens
14+
assert bucket._tokens == 0
15+
# bucket is empty from above, should fail
16+
assert bucket.consume(10) is False
17+
18+
19+
# noinspection PyProtectedMember
20+
def test_bucket_advanced():
21+
bucket = TokenBucket(10, 1)
22+
# tokens start at 10
23+
assert bucket._tokens == 10
24+
# empty tokens
25+
assert bucket.empty() is True
26+
# check tokens is 0
27+
assert bucket._tokens == 0
28+
# refill tokens
29+
assert bucket.refill() is True
30+
# check tokens is 10
31+
assert bucket._tokens == 10
32+
33+
34+
def test_bucket_regen():
35+
bucket = TokenBucket(10, 10)
36+
# success
37+
assert bucket.consume(10) is True
38+
# sleep
39+
time.sleep(1)
40+
# bucket should be full again and this should succeed
41+
assert bucket.tokens == 10
42+
assert bucket.consume(10) is True

cloudbot/util/tokenbucket.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,51 @@ class TokenBucket(object):
1111
False
1212
"""
1313

14-
def __init__(self, tokens, fill_rate):
14+
def __init__(self, _capacity, fill_rate):
15+
"""
16+
:param _capacity: The total amount of token the bucket can contain
17+
:param fill_rate: The rate at which tokens regenerate. (fill_rate per second)
18+
"""
1519
"""tokens is the total tokens in the bucket. fill_rate is the
1620
rate in tokens/second that the bucket will be refilled."""
17-
self.capacity = float(tokens)
18-
self._tokens = float(tokens)
21+
self.capacity = float(_capacity)
22+
self._tokens = float(_capacity)
1923
self.fill_rate = float(fill_rate)
2024
self.timestamp = time()
2125

2226
def consume(self, tokens):
23-
"""Consume tokens from the bucket. Returns True if there were
24-
sufficient tokens otherwise False."""
27+
"""
28+
Consume tokens from the bucket.
29+
:param tokens: The number of tokens to consume
30+
:return true if there were sufficient tokens otherwise false
31+
"""
2532
if tokens <= self.tokens:
2633
self._tokens -= tokens
2734
else:
2835
return False
2936
return True
3037

3138
def refill(self):
39+
"""
40+
Sets the current token count to the max capacity
41+
"""
3242
self._tokens = self.capacity
43+
return True
3344

3445
def empty(self):
46+
"""
47+
Sets the current token count to zero
48+
"""
3549
self._tokens = float(0)
50+
return True
3651

3752
def get_tokens(self):
53+
"""
54+
Calculates and returns the current amount of tokens the bucker contains
55+
56+
:return Amount of tokens the bucket contains
57+
:rtype Float
58+
"""
3859
now = time()
3960
if self._tokens < self.capacity:
4061
delta = self.fill_rate * (now - self.timestamp)

0 commit comments

Comments
 (0)