Skip to content

Commit

Permalink
Added tests and Travis CI integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanYohansson committed Jul 25, 2016
1 parent 5d3753b commit 7c2c36d
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 2 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
*.pyx
*.pyc

*.swp
*.orig

.idea/

# virtualenv
venv/
env/

build/
dist/

*.egg**

11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: python
python:
- "2.7"
- "3.5"
script: "make test"
branches:
only:
- master
services:
- redis-server

5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
init:
python setup.py install
test:
python tests/rate_limit_test.py

12 changes: 10 additions & 2 deletions src/rate_limit/__init__.py → redis_rate_limit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
local current
current = tonumber(redis.call("incr", KEYS[1]))
if current == 1 then
redis.call("expire", KEYS[1], ARGS[1])
redis.call("expire", KEYS[1], ARGV[1])
end
return current
"""
Expand Down Expand Up @@ -84,7 +84,7 @@ def has_been_exceeded(self):
:return: bool: True if limit has been exceeded or False otherwise
"""
return self._get_usage() > self._max_requests
return self.get_usage() >= self._max_requests

def increment_usage(self):
"""
Expand Down Expand Up @@ -117,3 +117,11 @@ def _is_rate_limit_supported(self):
redis_version = self._redis.info()['redis_version']
is_supported = StrictVersion(redis_version) >= StrictVersion('2.6.0')
return bool(is_supported)

def _reset(self):
"""
Deletes all keys that start with ‘rate_limit:’.
"""
for rate_limit_key in self._redis.keys('rate_limit:*'):
self._redis.delete(rate_limit_key)

2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[bdist_wheel]
universal=1
Empty file added tests/__init__.py
Empty file.
70 changes: 70 additions & 0 deletions tests/rate_limit_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
import time
from redis_rate_limit import RateLimit, TooManyRequests


class TestRedisRateLimit(unittest.TestCase):
def setUp(self):
"""
Initialises Rate Limit class and delete all keys from Redis.
"""
self.rate_limit = RateLimit(resource='test', client='localhost',
max_requests=10)
self.rate_limit._reset()

def _make_10_requests(self):
"""
Increments usage ten times.
"""
for x in range(0, 10):
with self.rate_limit:
pass

def test_limit_10_max_request(self):
"""
Should raise TooManyRequests Exception when trying to increment for the
eleventh time.
"""
self.assertEquals(self.rate_limit.get_usage(), 0)
self.assertEquals(self.rate_limit.has_been_exceeded(), False)

self._make_10_requests()
self.assertEquals(self.rate_limit.get_usage(), 10)
self.assertEquals(self.rate_limit.has_been_exceeded(), True)

with self.assertRaises(TooManyRequests):
with self.rate_limit:
pass

self.assertEquals(self.rate_limit.get_usage(), 11)
self.assertEquals(self.rate_limit.has_been_exceeded(), True)

def test_expire(self):
"""
Should not raise TooManyRequests Exception when trying to increment for
the eleventh time after the expire time.
"""
self._make_10_requests()
time.sleep(1)
with self.rate_limit:
pass

def test_not_expired(self):
"""
Should raise TooManyRequests Exception when the expire time has not
been reached yet.
"""
self.rate_limit = RateLimit(resource='test', client='localhost',
max_requests=10, expire=2)
self._make_10_requests()
time.sleep(1)
with self.assertRaises(TooManyRequests):
with self.rate_limit:
pass


if __name__ == '__main__':
unittest.main()

0 comments on commit 7c2c36d

Please sign in to comment.