Skip to content
Myr

test_regression.py

This file

Path

tests/test_regression.py

102 lines · 2.4 KiB · Python

Last changed by

Always run gc before leak testArmin Ronacherbefore adoption883cb7c

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        gc.collect()43        new_objects = len(gc.get_objects())44        if new_objects > self.old_objects:45            pytest.fail('Example code leaked')46        _gc_lock.release()47        gc.enable()484950def test_memory_consumption():51    app = flask.Flask(__name__)5253    @app.route('/')54    def index():55        return flask.render_template('simple_template.html', whiskey=42)5657    def fire():58        with app.test_client() as c:59            rv = c.get('/')60            assert rv.status_code == 20061            assert rv.data == b'<h1>42</h1>'6263    # Trigger caches64    fire()6566    # This test only works on CPython 2.7.67    if sys.version_info >= (2, 7) and \68            not hasattr(sys, 'pypy_translation_info'):69        with assert_no_leak():70            for x in range(10):71                fire()727374def test_safe_join_toplevel_pardir():75    from flask.helpers import safe_join76    with pytest.raises(NotFound):77        safe_join('/foo', '..')787980def test_aborting():81    class Foo(Exception):82        whatever = 4283    app = flask.Flask(__name__)84    app.testing = True8586    @app.errorhandler(Foo)87    def handle_foo(e):88        return str(e.whatever)8990    @app.route('/')91    def index():92        raise flask.abort(flask.redirect(flask.url_for('test')))9394    @app.route('/test')95    def test():96        raise Foo()9798    with app.test_client() as c:99        rv = c.get('/')100        assert rv.headers['Location'] == 'http://localhost/test'101        rv = c.get('/test')102        assert rv.data == b'42'