Skip to content
Myr

test_regression.py

This file

Path

tests/test_regression.py

107 lines · 2.6 KiB · Python

Last changed by

Merge pull request #1303 from Parkayun/masterMarkus Unterwaditzerbefore adoption5b98266

History of this file

Take it

1# -*- coding: utf-8 -*-2"""3    tests.regression4    ~~~~~~~~~~~~~~~~~~~~~~~~~~56    Tests regressions.78    :copyright: (c) 2015 by Armin Ronacher.9    :license: BSD, see LICENSE for more details.10"""1112import pytest1314import os15import gc16import sys17import flask18import threading19from werkzeug.exceptions import NotFound202122_gc_lock = threading.Lock()232425class assert_no_leak(object):2627    def __enter__(self):28        gc.disable()29        _gc_lock.acquire()30        loc = flask._request_ctx_stack._local3132        # Force Python to track this dictionary at all times.33        # This is necessary since Python only starts tracking34        # dicts if they contain mutable objects.  It's a horrible,35        # horrible hack but makes this kinda testable.36        loc.__storage__['FOOO'] = [1, 2, 3]3738        gc.collect()39        self.old_objects = len(gc.get_objects())4041    def __exit__(self, exc_type, exc_value, tb):42        if not hasattr(sys, 'getrefcount'):43            gc.collect()44        new_objects = len(gc.get_objects())45        if new_objects > self.old_objects:46            pytest.fail('Example code leaked')47        _gc_lock.release()48        gc.enable()495051# XXX: untitaker: These tests need to be revised. They broke around the time we52# ported Flask to Python 3.53@pytest.mark.skipif(os.environ.get('RUN_FLASK_MEMORY_TESTS') != '1',54                    reason='Turned off due to envvar.')55def test_memory_consumption():56    app = flask.Flask(__name__)5758    @app.route('/')59    def index():60        return flask.render_template('simple_template.html', whiskey=42)6162    def fire():63        with app.test_client() as c:64            rv = c.get('/')65            assert rv.status_code == 20066            assert rv.data == b'<h1>42</h1>'6768    # Trigger caches69    fire()7071    # This test only works on CPython 2.7.72    if sys.version_info >= (2, 7) and \73            not hasattr(sys, 'pypy_translation_info'):74        with assert_no_leak():75            for x in range(10):76                fire()777879def test_safe_join_toplevel_pardir():80    from flask.helpers import safe_join81    with pytest.raises(NotFound):82        safe_join('/foo', '..')838485def test_aborting():86    class Foo(Exception):87        whatever = 4288    app = flask.Flask(__name__)89    app.testing = True9091    @app.errorhandler(Foo)92    def handle_foo(e):93        return str(e.whatever)9495    @app.route('/')96    def index():97        raise flask.abort(flask.redirect(flask.url_for('test')))9899    @app.route('/test')100    def test():101        raise Foo()102103    with app.test_client() as c:104        rv = c.get('/')105        assert rv.headers['Location'] == 'http://localhost/test'106        rv = c.get('/test')107        assert rv.data == b'42'