|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pytest |
| 4 | +from django.http import HttpRequest |
| 5 | +from django.http import JsonResponse |
| 6 | + |
| 7 | +from django_github_app.github import SyncGitHubAPI |
| 8 | +from django_github_app.routing import GitHubRouter |
| 9 | +from django_github_app.views import BaseWebhookView |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture(autouse=True) |
| 13 | +def test_router(): |
| 14 | + import django_github_app.views |
| 15 | + from django_github_app.routing import GitHubRouter |
| 16 | + |
| 17 | + old_routers = GitHubRouter._routers.copy() |
| 18 | + GitHubRouter._routers = [] |
| 19 | + |
| 20 | + old_router = django_github_app.views._router |
| 21 | + |
| 22 | + test_router = GitHubRouter() |
| 23 | + django_github_app.views._router = test_router |
| 24 | + |
| 25 | + yield test_router |
| 26 | + |
| 27 | + GitHubRouter._routers = old_routers |
| 28 | + django_github_app.views._router = old_router |
| 29 | + |
| 30 | + |
| 31 | +class View(BaseWebhookView[SyncGitHubAPI]): |
| 32 | + github_api_class = SyncGitHubAPI |
| 33 | + |
| 34 | + def post(self, request: HttpRequest) -> JsonResponse: |
| 35 | + return JsonResponse({}) |
| 36 | + |
| 37 | + |
| 38 | +class LegacyView(BaseWebhookView[SyncGitHubAPI]): |
| 39 | + github_api_class = SyncGitHubAPI |
| 40 | + |
| 41 | + @property |
| 42 | + def router(self) -> GitHubRouter: |
| 43 | + # Always create a new router (simulating issue #73) |
| 44 | + return GitHubRouter(*GitHubRouter.routers) |
| 45 | + |
| 46 | + def post(self, request: HttpRequest) -> JsonResponse: |
| 47 | + return JsonResponse({}) |
| 48 | + |
| 49 | + |
| 50 | +class TestGitHubRouter: |
| 51 | + def test_router_single_instance(self): |
| 52 | + view1 = View() |
| 53 | + view2 = View() |
| 54 | + |
| 55 | + router1 = view1.router |
| 56 | + router2 = view2.router |
| 57 | + |
| 58 | + assert router1 is router2 |
| 59 | + assert view1.router is router1 |
| 60 | + assert view2.router is router2 |
| 61 | + |
| 62 | + def test_no_duplicate_routers(self): |
| 63 | + router_ids = set() |
| 64 | + |
| 65 | + for _ in range(1000): |
| 66 | + view = View() |
| 67 | + router_ids.add(id(view.router)) |
| 68 | + |
| 69 | + assert len(router_ids) == 1 |
| 70 | + |
| 71 | + def test_duplicate_routers_without_module_level_router(self): |
| 72 | + router_ids = set() |
| 73 | + |
| 74 | + for _ in range(5): |
| 75 | + view = LegacyView() |
| 76 | + router_ids.add(id(view.router)) |
| 77 | + |
| 78 | + assert len(router_ids) == 5 |
| 79 | + |
| 80 | + @pytest.mark.limit_memory("2.5MB") |
| 81 | + def test_router_memory_stress_test(self): |
| 82 | + view_count = 50000 |
| 83 | + views = [] |
| 84 | + |
| 85 | + for _ in range(view_count): |
| 86 | + view = View() |
| 87 | + views.append(view) |
| 88 | + |
| 89 | + assert len(views) == view_count |
| 90 | + assert all(view.router is views[0].router for view in views) |
| 91 | + |
| 92 | + @pytest.mark.limit_memory("4MB") |
| 93 | + def test_router_memory_stress_test_legacy(self): |
| 94 | + view_count = 50000 |
| 95 | + views = [] |
| 96 | + |
| 97 | + for _ in range(view_count): |
| 98 | + view = LegacyView() |
| 99 | + views.append(view) |
| 100 | + |
| 101 | + assert len(views) == view_count |
| 102 | + assert not all(view.router is views[0].router for view in views) |
0 commit comments