Skip to content

Commit

Permalink
Merge pull request #253 from alisaifee/docker-for-tests
Browse files Browse the repository at this point in the history
Use docker for setting up backends for tests
  • Loading branch information
alisaifee authored May 19, 2020
2 parents 5c7cb04 + 7339d4d commit 2013e71
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 27 deletions.
11 changes: 2 additions & 9 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,10 @@ jobs:
strategy:
matrix:
python-version: [2.7, pypy2, 3.5, 3.7, 3.8, pypy3]
services:
redis:
image: redis
ports:
- 6379:6379
memcached:
image: memcached
ports:
- 11211:11211
steps:
- uses: actions/checkout@v2
- name: Start backend services
run: docker-compose up -d
- name: Cache dependencies
uses: actions/cache@v1
with:
Expand Down
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3.2'

services:
memcached:
image: memcached
ports:
- 31211:11211
redis:
image: redis
ports:
- 36379:6379
2 changes: 1 addition & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class FlaskLimiterTestCase(unittest.TestCase):
def setUp(self):
redis.Redis().flushall()
redis.from_url("redis://localhost:36379").flushall()

def build_app(self, config={}, **limiter_args):
app = Flask(__name__)
Expand Down
26 changes: 13 additions & 13 deletions tests/test_flask_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ def test_constructor_arguments_over_config(self):
strategy='moving-window', key_func=get_remote_address
)
limiter.init_app(app)
app.config.setdefault(C.STORAGE_URL, "redis://localhost:6379")
app.config.setdefault(C.STORAGE_URL, "redis://localhost:36379")
self.assertEqual(type(limiter._limiter), MovingWindowRateLimiter)
limiter = Limiter(
storage_uri='memcached://localhost:11211',
storage_uri='memcached://localhost:31211',
key_func=get_remote_address
)
limiter.init_app(app)
Expand Down Expand Up @@ -191,7 +191,7 @@ def test_fallback_to_memory_config(self):
_, limiter = self.build_app(
config={C.ENABLED: True},
default_limits=["5/minute"],
storage_uri="redis://localhost:6379",
storage_uri="redis://localhost:36379",
in_memory_fallback=["1/minute"]
)
self.assertEqual(len(limiter._in_memory_fallback), 1)
Expand All @@ -201,30 +201,30 @@ def test_fallback_to_memory_config(self):
config={C.ENABLED: True,
C.IN_MEMORY_FALLBACK: "1/minute"},
default_limits=["5/minute"],
storage_uri="redis://localhost:6379",
storage_uri="redis://localhost:36379",
)
self.assertEqual(len(limiter._in_memory_fallback), 1)
self.assertEqual(limiter._in_memory_fallback_enabled, True)

_, limiter = self.build_app(
config={C.ENABLED: True, C.IN_MEMORY_FALLBACK_ENABLED: True},
global_limits=["5/minute"],
storage_uri="redis://localhost:6379",
storage_uri="redis://localhost:36379",
)
self.assertEqual(limiter._in_memory_fallback_enabled, True)

_, limiter = self.build_app(
config={C.ENABLED: True},
global_limits=["5/minute"],
storage_uri="redis://localhost:6379",
storage_uri="redis://localhost:36379",
in_memory_fallback_enabled=True
)

def test_fallback_to_memory_backoff_check(self):
app, limiter = self.build_app(
config={C.ENABLED: True},
default_limits=["5/minute"],
storage_uri="redis://localhost:6379",
storage_uri="redis://localhost:36379",
in_memory_fallback=["1/minute"]
)

Expand Down Expand Up @@ -276,7 +276,7 @@ def test_fallback_to_memory_with_global_override(self):
app, limiter = self.build_app(
config={C.ENABLED: True},
default_limits=["5/minute"],
storage_uri="redis://localhost:6379",
storage_uri="redis://localhost:36379",
in_memory_fallback=["1/minute"]
)

Expand Down Expand Up @@ -327,7 +327,7 @@ def test_fallback_to_memory(self):
app, limiter = self.build_app(
config={C.ENABLED: True},
global_limits=["2/minute"],
storage_uri="redis://localhost:6379",
storage_uri="redis://localhost:36379",
in_memory_fallback_enabled=True,
headers_enabled=True
)
Expand Down Expand Up @@ -1337,7 +1337,7 @@ def null():
def test_reset_unsupported(self):
app, limiter = self.build_app({
C.GLOBAL_LIMITS: "1 per day",
C.STORAGE_URL: 'memcached://localhost:11211'
C.STORAGE_URL: 'memcached://localhost:31211'
})

@app.route("/")
Expand Down Expand Up @@ -1846,13 +1846,13 @@ def _():

def test_custom_key_prefix(self):
app1, limiter1 = self.build_app(
key_prefix="moo", storage_uri="redis://localhost:6379"
key_prefix="moo", storage_uri="redis://localhost:36379"
)
app2, limiter2 = self.build_app(
{C.KEY_PREFIX: "cow"},
storage_uri="redis://localhost:6379"
storage_uri="redis://localhost:36379"
)
app3, limiter3 = self.build_app(storage_uri="redis://localhost:6379")
app3, limiter3 = self.build_app(storage_uri="redis://localhost:36379")

@app1.route("/test")
@limiter1.limit("1/day")
Expand Down
8 changes: 4 additions & 4 deletions tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_redis_request_slower_than_fixed_window(self):
app, limiter = self.build_app(
{
C.GLOBAL_LIMITS: "5 per second",
C.STORAGE_URL: "redis://localhost:6379",
C.STORAGE_URL: "redis://localhost:36379",
C.STRATEGY: "fixed-window",
C.HEADERS_ENABLED: True
}
Expand All @@ -33,7 +33,7 @@ def test_redis_request_slower_than_moving_window(self):
app, limiter = self.build_app(
{
C.GLOBAL_LIMITS: "5 per second",
C.STORAGE_URL: "redis://localhost:6379",
C.STORAGE_URL: "redis://localhost:36379",
C.STRATEGY: "moving-window",
C.HEADERS_ENABLED: True
}
Expand Down Expand Up @@ -95,12 +95,12 @@ def t1():
def test_custom_key_prefix_with_headers(self):
app1, limiter1 = self.build_app(
key_prefix="moo",
storage_uri="redis://localhost:6379",
storage_uri="redis://localhost:36379",
headers_enabled=True
)
app2, limiter2 = self.build_app(
key_prefix="cow",
storage_uri="redis://localhost:6379",
storage_uri="redis://localhost:36379",
headers_enabled=True
)

Expand Down

0 comments on commit 2013e71

Please sign in to comment.