Skip to content

Commit 2b872e5

Browse files
committed
Fix failing tests with random 504
Do not update global config variable for gevent mode. Make sure we do not use gevent env for tests apart of dedicated tests. In those tests mock configuration rather than modifing global variable.
1 parent a9986b1 commit 2b872e5

File tree

2 files changed

+59
-41
lines changed

2 files changed

+59
-41
lines changed

server/.test.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ SECURITY_BEARER_SALT='bearer'
2424
SECURITY_EMAIL_SALT='email'
2525
SECURITY_PASSWORD_SALT='password'
2626
DIAGNOSTIC_LOGS_DIR=/tmp/diagnostic_logs
27+
GEVENT_WORKER=0

server/mergin/tests/test_middleware.py

Lines changed: 58 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import psycogreen.gevent
77
import pytest
88
import sqlalchemy
9+
from unittest.mock import patch
910

1011
from ..app import create_simple_app, GeventTimeoutMiddleware, db
1112
from ..config import Configuration
@@ -14,58 +15,74 @@
1415
@pytest.mark.parametrize("use_middleware", [True, False])
1516
def test_use_middleware(use_middleware):
1617
"""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()
2028

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"
2432

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()
2836

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+
)
3444

3545

3646
def test_catch_timeout():
3747
"""Test proper handling of gevent timeout with db.session.rollback"""
3848
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()
4259

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 ""
4966

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 ""
5976

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()
6481

65-
assert application.test_client().get("/timeout").status_code == 504
82+
assert application.test_client().get("/timeout").status_code == 504
6683

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")
7087

71-
db.session.rollback()
88+
db.session.rollback()

0 commit comments

Comments
 (0)