Skip to content

Commit 1fa3ba7

Browse files
committed
allow for elapsed time in tests (instead of fudge factor)
1 parent 158324f commit 1fa3ba7

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

tests/test_limiter.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,35 +31,29 @@ def test_general_functionality(rate, capacity):
3131
consume_one()
3232
time.sleep(float(capacity) / rate)
3333

34-
# NOTE(kgriffs): This works because we can consume at a much
35-
# higher rate relative to the replenishment rate, such that we
36-
# easily consume the total capacity before a single token can
37-
# be replenished.
3834
def consume_all():
39-
for i in range(capacity + 3):
35+
start = time.time()
36+
i = 0
37+
while True:
4038
conforming = consume_one()
41-
42-
# NOTE(kgriffs): One past the end should be non-conforming,
43-
# but sometimes an extra token or two can be generated, so
44-
# only check a couple past the end for non-conforming.
39+
elapsed = time.time() - start
4540
if i < capacity:
4641
assert conforming
47-
elif i > capacity + 1:
42+
elif i > capacity + (rate * elapsed):
4843
assert not conforming
44+
break
45+
i += 1
4946

5047
# Check non-conforming after consuming all of the tokens
5148
consume_all()
49+
start = time.time()
5250

5351
# Let the bucket replenish 1 token
5452
time.sleep(1.0 / rate)
5553
assert consume_one()
5654

57-
# NOTE(kgriffs): Occasionally enough time will have elapsed to
58-
# cause an additional token to be generated. Clear that one
59-
# out if it is there.
60-
consume_one()
61-
62-
assert storage.get_token_count(key) < 1.0
55+
elapsed = time.time() - start
56+
assert storage.get_token_count(key) < (rate * elapsed)
6357

6458
# NOTE(kgriffs): Let the bucket replenish all the tokens; do this
6559
# twice to verify that the bucket is limited to capacity.

0 commit comments

Comments
 (0)