Skip to content

Commit d445e3d

Browse files
Merge pull request #603 from geoadmin/feat-PB-1788-checker-delay
PB-1788: add way to slow down checker endpoint.
2 parents 4ef0564 + 770ea55 commit d445e3d

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ These settings are read from `settings_dev.py`
544544

545545
| Env | Default | Description |
546546
|-------------|-----------------------|----------------------------------------|
547+
| CHECKER_DELAY | `0` | Delay the response of the checker endpoint by that number of seconds. |
547548
| DEBUG | `False` | Set django DEBUG flag |
548549
| DEBUG_PROPAGATE_API_EXCEPTIONS | `False` | When `True` the API exception are treated as in production, using a JSON response. Otherwise in DEBUG mode the API exception returns an HTML response with backtrace. |
549550
| EXTERNAL_TEST_ASSET_URL | `"https://prod-[...].jpg"` | The URL of an externally hosted jpg file that's used in the external asset tests |

app/config/settings_prod.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,7 @@ def get_logging_config():
378378
SESSION_COOKIE_AGE = env('SESSION_COOKIE_AGE', int, default=60 * 60 * 24 * 7 * 2)
379379
SESSION_COOKIE_SAMESITE = env('SESSION_COOKIE_SAMESITE', str, default='Lax')
380380
SESSION_COOKIE_SECURE = env('SESSION_COOKIE_SECURE', bool, default=False)
381+
382+
# Delay to inject in the checker endpoint, in seconds. This is only meant to be
383+
# used for test purpose.
384+
CHECKER_DELAY = env('CHECKER_DELAY', int, default=0)

app/config/urls.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
1. Import the include() function: from django.urls import include, path
1414
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
1515
"""
16+
import time
17+
1618
from django.conf import settings
1719
from django.contrib import admin
1820
from django.http import JsonResponse
@@ -23,6 +25,9 @@
2325

2426

2527
def checker(request):
28+
if settings.CHECKER_DELAY > 0:
29+
time.sleep(settings.CHECKER_DELAY)
30+
2631
data = {"success": True, "message": "OK"}
2732

2833
return JsonResponse(data)

app/tests/test_checker.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import time
2+
3+
from django.test import Client
4+
from django.test import TestCase
5+
from django.test import override_settings
6+
7+
8+
class CheckerTestCase(TestCase):
9+
10+
def setUp(self):
11+
self.client = Client()
12+
13+
def test_checker(self):
14+
response = self.client.get('/checker')
15+
self.assertEqual(response.status_code, 200)
16+
17+
@override_settings(CHECKER_DELAY=2)
18+
def test_checker_delayed(self):
19+
start = time.time()
20+
response = self.client.get('/checker')
21+
end = time.time()
22+
self.assertEqual(response.status_code, 200)
23+
self.assertGreater(end, start + 2, f'Expected at least 2s, only took {end - start}')

0 commit comments

Comments
 (0)