gabriel / musehub public
test_opengraph_browser_idle_329.py python
83 lines 2.7 KB
Raw
sha256:0fa8ccb2724554e6bb2f98afeabd24d84009764c28824f982a84ef1fd5f1430e fix(opengraph): close the per-process Chromium after an idl… Opus 5.5 minor ⚠ breaking 1 day ago
1 """musehub#329: the OpenGraph Chromium is one per process and is closed after an idle period (it relaunches on demand).
2
3 Before, each uvicorn worker launched Chromium on its first card and kept it for the life of the process (~170 MB each,
4 ~500 MB per container in production). These tests drive the module with fake Playwright objects; no browser runs.
5 """
6 from __future__ import annotations
7
8 import sys
9 import types
10
11 import pytest
12
13 from musehub.services import opengraph_renderer as og
14
15
16 class _FakeBrowser:
17 def __init__(self) -> None:
18 self.closed = False
19
20 def close(self) -> None:
21 self.closed = True
22
23
24 class _FakePlaywright:
25 launches = 0
26
27 def __init__(self) -> None:
28 self.stopped = False
29 self.chromium = types.SimpleNamespace(launch=self._launch)
30
31 def _launch(self, headless: bool = True) -> _FakeBrowser:
32 type(self).launches += 1
33 return _FakeBrowser()
34
35 def stop(self) -> None:
36 self.stopped = True
37
38
39 @pytest.fixture(autouse=True)
40 def _clean(monkeypatch: pytest.MonkeyPatch) -> None:
41 monkeypatch.setattr(og, "_browser", None)
42 monkeypatch.setattr(og, "_playwright_instance", None)
43 monkeypatch.setattr(og, "_last_render", 0.0)
44 _FakePlaywright.launches = 0
45 fake_api = types.ModuleType("playwright.sync_api")
46 fake_api.sync_playwright = lambda: types.SimpleNamespace(start=_FakePlaywright) # type: ignore[attr-defined]
47 monkeypatch.setitem(sys.modules, "playwright", types.ModuleType("playwright"))
48 monkeypatch.setitem(sys.modules, "playwright.sync_api", fake_api)
49
50
51 def test_at_most_one_browser_per_process() -> None:
52 first = og._get_sync_browser()
53 second = og._get_sync_browser()
54 assert first is second and _FakePlaywright.launches == 1
55
56
57 def test_an_idle_browser_is_closed_and_playwright_stopped() -> None:
58 browser = og._get_sync_browser()
59 pw = og._playwright_instance
60 og._last_render = 1_000.0
61 assert og._close_if_idle_sync(now=1_000.0 + 121, idle_seconds=120) is True
62 assert browser.closed and pw.stopped and og._browser is None and og._playwright_instance is None
63
64
65 def test_a_recently_used_browser_is_kept() -> None:
66 browser = og._get_sync_browser()
67 og._last_render = 1_000.0
68 assert og._close_if_idle_sync(now=1_000.0 + 30, idle_seconds=120) is False
69 assert not browser.closed and og._browser is browser
70
71
72 def test_the_next_render_after_a_close_relaunches() -> None:
73 og._get_sync_browser()
74 og._last_render = 0.0
75 og._close_if_idle_sync(now=10_000.0, idle_seconds=120)
76 og._get_sync_browser()
77 assert _FakePlaywright.launches == 2
78
79
80 def test_the_idle_timeout_is_configurable() -> None:
81 from musehub.config import settings
82
83 assert settings.opengraph_browser_idle_seconds == 120.0
File History 1 commit
sha256:0fa8ccb2724554e6bb2f98afeabd24d84009764c28824f982a84ef1fd5f1430e fix(opengraph): close the per-process Chromium after an idl… Opus 5.5 minor ⚠ 1 day ago