Files
archived-camoufox/python/tests/test_launch_failure_cleanup.py
T
Jake Writer f30f6e8368 Merge pull request #912 from JWriter20/feat/virtual-gpu-display
Add headless="virtual-gpu": render on a named GPU with no monitor attached
2026-10-19 01:51:33 +00:00

72 lines
1.9 KiB
Python

"""headless="virtual" must not strand its Xvfb when the launch fails.
Everything after VirtualDisplay() -- launch_options(), the Playwright launch --
can raise before sync/async_attach_vd wires kill() into close, which used to
leave one Xvfb and its /tmp/.X<n> lock per failed launch.
"""
import asyncio
import pytest
from camoufox import async_api, sync_api, utils
class FakeDisplay:
instances = []
def __init__(self, debug=None):
self.killed = False
FakeDisplay.instances.append(self)
def get(self):
return ":99"
def kill(self):
self.killed = True
class Boom(Exception):
pass
def _raise(*args, **kwargs):
raise Boom()
@pytest.fixture(autouse=True)
def fake_display(monkeypatch):
FakeDisplay.instances.clear()
# NewBrowser and AsyncNewBrowser start the display through
# utils.start_virtual_display.
monkeypatch.setattr(utils, "VirtualDisplay", FakeDisplay)
def test_sync_kills_display_when_launch_options_fails(monkeypatch):
monkeypatch.setattr(sync_api, "launch_options", _raise)
with pytest.raises(Boom):
sync_api.NewBrowser(object(), headless="virtual")
assert FakeDisplay.instances[0].killed
def test_async_kills_display_when_launch_options_fails(monkeypatch):
monkeypatch.setattr(async_api, "launch_options", _raise)
with pytest.raises(Boom):
asyncio.run(async_api.AsyncNewBrowser(object(), headless="virtual"))
assert FakeDisplay.instances[0].killed
def test_sync_kills_display_when_browser_launch_fails(monkeypatch):
monkeypatch.setattr(sync_api, "launch_options", lambda **kw: {})
monkeypatch.setattr(sync_api, "host_identity", lambda *a: None)
class Firefox:
launch = staticmethod(_raise)
class Playwright:
firefox = Firefox()
with pytest.raises(Boom):
sync_api.NewBrowser(Playwright(), headless="virtual")
assert FakeDisplay.instances[0].killed