|
6 | 6 | import psycogreen.gevent |
7 | 7 | import pytest |
8 | 8 | import sqlalchemy |
| 9 | +from unittest.mock import patch |
9 | 10 |
|
10 | 11 | from ..app import create_simple_app, GeventTimeoutMiddleware, db |
11 | 12 | from ..config import Configuration |
|
14 | 15 | @pytest.mark.parametrize("use_middleware", [True, False]) |
15 | 16 | def test_use_middleware(use_middleware): |
16 | 17 | """Test using middleware""" |
17 | | - Configuration.GEVENT_WORKER = use_middleware |
18 | | - Configuration.GEVENT_REQUEST_TIMEOUT = 1 |
19 | | - application = create_simple_app() |
| 18 | + with patch.object( |
| 19 | + Configuration, |
| 20 | + "GEVENT_WORKER", |
| 21 | + use_middleware, |
| 22 | + ), patch.object( |
| 23 | + Configuration, |
| 24 | + "GEVENT_REQUEST_TIMEOUT", |
| 25 | + 1, |
| 26 | + ): |
| 27 | + application = create_simple_app() |
20 | 28 |
|
21 | | - def ping(): |
22 | | - gevent.sleep(Configuration.GEVENT_REQUEST_TIMEOUT + 1) |
23 | | - return "pong" |
| 29 | + def ping(): |
| 30 | + gevent.sleep(Configuration.GEVENT_REQUEST_TIMEOUT + 1) |
| 31 | + return "pong" |
24 | 32 |
|
25 | | - application.add_url_rule("/test", "ping", ping) |
26 | | - app_context = application.app_context() |
27 | | - app_context.push() |
| 33 | + application.add_url_rule("/test", "ping", ping) |
| 34 | + app_context = application.app_context() |
| 35 | + app_context.push() |
28 | 36 |
|
29 | | - assert isinstance(application.wsgi_app, GeventTimeoutMiddleware) == use_middleware |
30 | | - # in case of gevent, dummy endpoint it set to time out |
31 | | - assert application.test_client().get("/test").status_code == ( |
32 | | - 504 if use_middleware else 200 |
33 | | - ) |
| 37 | + assert ( |
| 38 | + isinstance(application.wsgi_app, GeventTimeoutMiddleware) == use_middleware |
| 39 | + ) |
| 40 | + # in case of gevent, dummy endpoint it set to time out |
| 41 | + assert application.test_client().get("/test").status_code == ( |
| 42 | + 504 if use_middleware else 200 |
| 43 | + ) |
34 | 44 |
|
35 | 45 |
|
36 | 46 | def test_catch_timeout(): |
37 | 47 | """Test proper handling of gevent timeout with db.session.rollback""" |
38 | 48 | psycogreen.gevent.patch_psycopg() |
39 | | - Configuration.GEVENT_WORKER = True |
40 | | - Configuration.GEVENT_REQUEST_TIMEOUT = 1 |
41 | | - application = create_simple_app() |
| 49 | + with patch.object( |
| 50 | + Configuration, |
| 51 | + "GEVENT_WORKER", |
| 52 | + True, |
| 53 | + ), patch.object( |
| 54 | + Configuration, |
| 55 | + "GEVENT_REQUEST_TIMEOUT", |
| 56 | + 1, |
| 57 | + ): |
| 58 | + application = create_simple_app() |
42 | 59 |
|
43 | | - def unhandled(): |
44 | | - try: |
45 | | - db.session.execute("SELECT pg_sleep(1.1);") |
46 | | - finally: |
47 | | - db.session.execute("SELECT 1;") |
48 | | - return "" |
| 60 | + def unhandled(): |
| 61 | + try: |
| 62 | + db.session.execute("SELECT pg_sleep(1.1);") |
| 63 | + finally: |
| 64 | + db.session.execute("SELECT 1;") |
| 65 | + return "" |
49 | 66 |
|
50 | | - def timeout(): |
51 | | - try: |
52 | | - db.session.execute("SELECT pg_sleep(1.1);") |
53 | | - except gevent.timeout.Timeout: |
54 | | - db.session.rollback() |
55 | | - raise |
56 | | - finally: |
57 | | - db.session.execute("SELECT 1;") |
58 | | - return "" |
| 67 | + def timeout(): |
| 68 | + try: |
| 69 | + db.session.execute("SELECT pg_sleep(1.1);") |
| 70 | + except gevent.timeout.Timeout: |
| 71 | + db.session.rollback() |
| 72 | + raise |
| 73 | + finally: |
| 74 | + db.session.execute("SELECT 1;") |
| 75 | + return "" |
59 | 76 |
|
60 | | - application.add_url_rule("/unhandled", "unhandled", unhandled) |
61 | | - application.add_url_rule("/timeout", "timeout", timeout) |
62 | | - app_context = application.app_context() |
63 | | - app_context.push() |
| 77 | + application.add_url_rule("/unhandled", "unhandled", unhandled) |
| 78 | + application.add_url_rule("/timeout", "timeout", timeout) |
| 79 | + app_context = application.app_context() |
| 80 | + app_context.push() |
64 | 81 |
|
65 | | - assert application.test_client().get("/timeout").status_code == 504 |
| 82 | + assert application.test_client().get("/timeout").status_code == 504 |
66 | 83 |
|
67 | | - # in case of missing rollback sqlalchemy would raise error |
68 | | - with pytest.raises(sqlalchemy.exc.PendingRollbackError): |
69 | | - application.test_client().get("/unhandled") |
| 84 | + # in case of missing rollback sqlalchemy would raise error |
| 85 | + with pytest.raises(sqlalchemy.exc.PendingRollbackError): |
| 86 | + application.test_client().get("/unhandled") |
70 | 87 |
|
71 | | - db.session.rollback() |
| 88 | + db.session.rollback() |
0 commit comments