-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from alisaifee/dev
Switch to py.test
- Loading branch information
Showing
10 changed files
with
143 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[pytest] | ||
norecursedirs = google google_appengine build *.egg | ||
markers = | ||
unit: mark a test as a unit test. | ||
integration: mark a test as an integration test. | ||
addopts = | ||
--verbose | ||
--tb=short | ||
--capture=no | ||
-rfEsxX | ||
--cov=limits | ||
-m unit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
-r main.txt | ||
nose | ||
mock | ||
coverage | ||
nose-cov | ||
pytest | ||
pytest-cov | ||
coverage<5 | ||
pymemcache | ||
redis<3.1.0 | ||
redis-py-cluster>=1.1.0 | ||
PyYAML | ||
hiro>0.1.6 | ||
nose-timer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,17 @@ | ||
import platform | ||
import sys | ||
from functools import wraps | ||
|
||
from nose.plugins.skip import SkipTest | ||
import unittest | ||
|
||
RUN_GAE = ( | ||
sys.version_info[:2] == (2, 7) | ||
and platform.python_implementation() == 'CPython' | ||
sys.version_info[:2] == (2, 7) | ||
and platform.python_implementation() == 'CPython' | ||
) | ||
|
||
|
||
def test_import(): | ||
import limits | ||
|
||
|
||
def test_module_version(): | ||
import limits | ||
assert limits.__version__ is not None | ||
|
||
|
||
def skip_if(cond): | ||
def _inner(fn): | ||
@wraps(fn) | ||
def __inner(*a, **k): | ||
if cond() if callable(cond) else cond: | ||
raise SkipTest | ||
return fn(*a, **k) | ||
|
||
return __inner | ||
|
||
return _inner | ||
def skip_if_pypy(fn): | ||
return unittest.skipIf( | ||
platform.python_implementation().lower() == 'pypy', | ||
reason='Skipped for pypy' | ||
)(fn) | ||
|
||
|
||
def skip_if_pypy(fn): | ||
return skip_if(platform.python_implementation().lower() == 'pypy')(fn) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import threading | ||
import time | ||
import unittest | ||
from uuid import uuid4 | ||
|
||
import pytest | ||
|
||
from limits.limits import RateLimitItemPerSecond | ||
from limits.storage import MemoryStorage | ||
from limits.strategies import FixedWindowRateLimiter, MovingWindowRateLimiter | ||
|
||
|
||
@pytest.mark.integration | ||
class ConcurrencyTests(unittest.TestCase): | ||
|
||
def test_memory_storage_fixed_window(self): | ||
storage = MemoryStorage() | ||
limiter = FixedWindowRateLimiter(storage) | ||
per_second = RateLimitItemPerSecond(100) | ||
|
||
[limiter.hit(per_second, uuid4().hex) for _ in range(1000)] | ||
|
||
key = uuid4().hex | ||
hits = [] | ||
|
||
def hit(): | ||
if limiter.hit(per_second, key): | ||
hits.append(None) | ||
|
||
start = time.time() | ||
|
||
threads = [threading.Thread(target=hit) for _ in range(1000)] | ||
[t.start() for t in threads] | ||
[t.join() for t in threads] | ||
|
||
self.assertTrue(time.time() - start < 1) | ||
self.assertEqual(len(hits), 100) | ||
|
||
def test_memory_storage_moving_window(self): | ||
storage = MemoryStorage() | ||
limiter = MovingWindowRateLimiter(storage) | ||
per_second = RateLimitItemPerSecond(100) | ||
|
||
[limiter.hit(per_second, uuid4().hex) for _ in range(100)] | ||
|
||
key = uuid4().hex | ||
hits = [] | ||
|
||
def hit(): | ||
if limiter.hit(per_second, key): | ||
hits.append(None) | ||
|
||
start = time.time() | ||
|
||
threads = [threading.Thread(target=hit) for _ in range(1000)] | ||
[t.start() for t in threads] | ||
[t.join() for t in threads] | ||
|
||
self.assertTrue(time.time() - start < 1) | ||
self.assertEqual(len(hits), 100) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters