Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix allowance calculation which always return 0 in some circumstances. #6

Merged
merged 1 commit into from
Dec 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ratelimitingfilter/ratelimitingfilter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import division

from collections import defaultdict
import difflib
from functools import partial
Expand Down
17 changes: 15 additions & 2 deletions tests/ratelimitingfilter_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import division

import logging
import os
from unittest.case import TestCase
Expand Down Expand Up @@ -62,15 +64,26 @@ def test_limit_to_no_records_per_second(self):

self.assertEqual(result.count(True), 0)

def test_limit_to_two_record_per_ten_second(self):
f = RateLimitingFilter(rate=2, per=10)

result = self._filter_r_records_over_s_seconds(f, 20, 10)

self.assertEqual(result.count(True), 2)

def _filter_twenty_records_over_two_seconds(self, f):
return self._filter_r_records_over_s_seconds(f, 20, 2)


def _filter_r_records_over_s_seconds(self, f, nb_records, nb_seconds):
mock_record = Mock()
mock_record.msg = 'test message'

result = []

for _ in range(20):
for _ in range(nb_records):
result.append(f.filter(mock_record))
self.mock_time.return_value += 0.1
self.mock_time.return_value += nb_seconds / (nb_records or 1)

return result

Expand Down