|
|
|
@ -19,14 +19,10 @@ import threading
|
|
|
|
|
from werkzeug.exceptions import NotFound |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_gc_lock = threading.Lock() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class _NoLeakAsserter(object): |
|
|
|
|
|
|
|
|
|
def __init__(self, testcase): |
|
|
|
|
self.testcase = testcase |
|
|
|
|
class assert_no_leak(object): |
|
|
|
|
|
|
|
|
|
def __enter__(self): |
|
|
|
|
gc.disable() |
|
|
|
@ -47,7 +43,7 @@ class _NoLeakAsserter(object):
|
|
|
|
|
gc.collect() |
|
|
|
|
new_objects = len(gc.get_objects()) |
|
|
|
|
if new_objects > self.old_objects: |
|
|
|
|
self.testcase.fail('Example code leaked') |
|
|
|
|
pytest.fail('Example code leaked') |
|
|
|
|
_gc_lock.release() |
|
|
|
|
gc.enable() |
|
|
|
|
|
|
|
|
@ -56,62 +52,56 @@ class _NoLeakAsserter(object):
|
|
|
|
|
# ported Flask to Python 3. |
|
|
|
|
@pytest.mark.skipif(os.environ.get('RUN_FLASK_MEMORY_TESTS') != '1', |
|
|
|
|
reason='Turned off due to envvar.') |
|
|
|
|
class TestMemory(object): |
|
|
|
|
|
|
|
|
|
def assert_no_leak(self): |
|
|
|
|
return _NoLeakAsserter(self) |
|
|
|
|
|
|
|
|
|
def test_memory_consumption(self): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
def test_memory_consumption(): |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
|
|
|
|
|
@app.route('/') |
|
|
|
|
def index(): |
|
|
|
|
return flask.render_template('simple_template.html', whiskey=42) |
|
|
|
|
@app.route('/') |
|
|
|
|
def index(): |
|
|
|
|
return flask.render_template('simple_template.html', whiskey=42) |
|
|
|
|
|
|
|
|
|
def fire(): |
|
|
|
|
with app.test_client() as c: |
|
|
|
|
rv = c.get('/') |
|
|
|
|
assert rv.status_code == 200 |
|
|
|
|
assert rv.data == b'<h1>42</h1>' |
|
|
|
|
def fire(): |
|
|
|
|
with app.test_client() as c: |
|
|
|
|
rv = c.get('/') |
|
|
|
|
assert rv.status_code == 200 |
|
|
|
|
assert rv.data == b'<h1>42</h1>' |
|
|
|
|
|
|
|
|
|
# Trigger caches |
|
|
|
|
fire() |
|
|
|
|
# Trigger caches |
|
|
|
|
fire() |
|
|
|
|
|
|
|
|
|
# This test only works on CPython 2.7. |
|
|
|
|
if sys.version_info >= (2, 7) and \ |
|
|
|
|
not hasattr(sys, 'pypy_translation_info'): |
|
|
|
|
with self.assert_no_leak(): |
|
|
|
|
for x in range(10): |
|
|
|
|
fire() |
|
|
|
|
# This test only works on CPython 2.7. |
|
|
|
|
if sys.version_info >= (2, 7) and \ |
|
|
|
|
not hasattr(sys, 'pypy_translation_info'): |
|
|
|
|
with assert_no_leak(): |
|
|
|
|
for x in range(10): |
|
|
|
|
fire() |
|
|
|
|
|
|
|
|
|
def test_safe_join_toplevel_pardir(self): |
|
|
|
|
from flask.helpers import safe_join |
|
|
|
|
with pytest.raises(NotFound): |
|
|
|
|
safe_join('/foo', '..') |
|
|
|
|
|
|
|
|
|
def test_safe_join_toplevel_pardir(): |
|
|
|
|
from flask.helpers import safe_join |
|
|
|
|
with pytest.raises(NotFound): |
|
|
|
|
safe_join('/foo', '..') |
|
|
|
|
|
|
|
|
|
class TestException(object): |
|
|
|
|
|
|
|
|
|
def test_aborting(self): |
|
|
|
|
class Foo(Exception): |
|
|
|
|
whatever = 42 |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
app.testing = True |
|
|
|
|
def test_aborting(): |
|
|
|
|
class Foo(Exception): |
|
|
|
|
whatever = 42 |
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
app.testing = True |
|
|
|
|
|
|
|
|
|
@app.errorhandler(Foo) |
|
|
|
|
def handle_foo(e): |
|
|
|
|
return str(e.whatever) |
|
|
|
|
@app.errorhandler(Foo) |
|
|
|
|
def handle_foo(e): |
|
|
|
|
return str(e.whatever) |
|
|
|
|
|
|
|
|
|
@app.route('/') |
|
|
|
|
def index(): |
|
|
|
|
raise flask.abort(flask.redirect(flask.url_for('test'))) |
|
|
|
|
@app.route('/') |
|
|
|
|
def index(): |
|
|
|
|
raise flask.abort(flask.redirect(flask.url_for('test'))) |
|
|
|
|
|
|
|
|
|
@app.route('/test') |
|
|
|
|
def test(): |
|
|
|
|
raise Foo() |
|
|
|
|
@app.route('/test') |
|
|
|
|
def test(): |
|
|
|
|
raise Foo() |
|
|
|
|
|
|
|
|
|
with app.test_client() as c: |
|
|
|
|
rv = c.get('/') |
|
|
|
|
assert rv.headers['Location'] == 'http://localhost/test' |
|
|
|
|
rv = c.get('/test') |
|
|
|
|
assert rv.data == b'42' |
|
|
|
|
with app.test_client() as c: |
|
|
|
|
rv = c.get('/') |
|
|
|
|
assert rv.headers['Location'] == 'http://localhost/test' |
|
|
|
|
rv = c.get('/test') |
|
|
|
|
assert rv.data == b'42' |
|
|
|
|