forked from orbit-oss/flask
Kill classes in test_regression
This commit is contained in:
parent
f8a778deae
commit
71dae37733
1 changed files with 51 additions and 61 deletions
|
|
@ -19,14 +19,10 @@ import threading
|
||||||
from werkzeug.exceptions import NotFound
|
from werkzeug.exceptions import NotFound
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
_gc_lock = threading.Lock()
|
_gc_lock = threading.Lock()
|
||||||
|
|
||||||
|
|
||||||
class _NoLeakAsserter(object):
|
class assert_no_leak(object):
|
||||||
|
|
||||||
def __init__(self, testcase):
|
|
||||||
self.testcase = testcase
|
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
gc.disable()
|
gc.disable()
|
||||||
|
|
@ -47,7 +43,7 @@ class _NoLeakAsserter(object):
|
||||||
gc.collect()
|
gc.collect()
|
||||||
new_objects = len(gc.get_objects())
|
new_objects = len(gc.get_objects())
|
||||||
if new_objects > self.old_objects:
|
if new_objects > self.old_objects:
|
||||||
self.testcase.fail('Example code leaked')
|
pytest.fail('Example code leaked')
|
||||||
_gc_lock.release()
|
_gc_lock.release()
|
||||||
gc.enable()
|
gc.enable()
|
||||||
|
|
||||||
|
|
@ -56,62 +52,56 @@ class _NoLeakAsserter(object):
|
||||||
# ported Flask to Python 3.
|
# ported Flask to Python 3.
|
||||||
@pytest.mark.skipif(os.environ.get('RUN_FLASK_MEMORY_TESTS') != '1',
|
@pytest.mark.skipif(os.environ.get('RUN_FLASK_MEMORY_TESTS') != '1',
|
||||||
reason='Turned off due to envvar.')
|
reason='Turned off due to envvar.')
|
||||||
class TestMemory(object):
|
def test_memory_consumption():
|
||||||
|
app = flask.Flask(__name__)
|
||||||
|
|
||||||
def assert_no_leak(self):
|
@app.route('/')
|
||||||
return _NoLeakAsserter(self)
|
def index():
|
||||||
|
return flask.render_template('simple_template.html', whiskey=42)
|
||||||
def test_memory_consumption(self):
|
|
||||||
app = flask.Flask(__name__)
|
|
||||||
|
|
||||||
@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>'
|
|
||||||
|
|
||||||
# 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()
|
|
||||||
|
|
||||||
def test_safe_join_toplevel_pardir(self):
|
|
||||||
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
|
|
||||||
|
|
||||||
@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('/test')
|
|
||||||
def test():
|
|
||||||
raise Foo()
|
|
||||||
|
|
||||||
|
def fire():
|
||||||
with app.test_client() as c:
|
with app.test_client() as c:
|
||||||
rv = c.get('/')
|
rv = c.get('/')
|
||||||
assert rv.headers['Location'] == 'http://localhost/test'
|
assert rv.status_code == 200
|
||||||
rv = c.get('/test')
|
assert rv.data == b'<h1>42</h1>'
|
||||||
assert rv.data == b'42'
|
|
||||||
|
# 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 assert_no_leak():
|
||||||
|
for x in range(10):
|
||||||
|
fire()
|
||||||
|
|
||||||
|
|
||||||
|
def test_safe_join_toplevel_pardir():
|
||||||
|
from flask.helpers import safe_join
|
||||||
|
with pytest.raises(NotFound):
|
||||||
|
safe_join('/foo', '..')
|
||||||
|
|
||||||
|
|
||||||
|
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.route('/')
|
||||||
|
def index():
|
||||||
|
raise flask.abort(flask.redirect(flask.url_for('test')))
|
||||||
|
|
||||||
|
@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'
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue