Files
Byparr/tests/utils_test.py
T
ThePhaselessandClaude Opus 5.5 70d6ced2cb fix: resolve the browser timezone once per process instead of per request
Every request builds a new InvisiblePlaywright, and its __aenter__ calls
prepare_session_geo(): with the default timezone="" and no proxy that
discovers the egress IP through api.ipify.org, icanhazip.com and
checkip.amazonaws.com, then calls ensure_geoip_mmdb(), which HEADs GitHub
for a newer GeoIP release, three times per session. On a network where
those hosts are slow or blocked every request paid for it before the
browser even started. Measured in the app image: ipify black-holed adds
~5s per request, all three echo hosts black-holed ~12.5s, GitHub
black-holed ~75s. Setting BROWSER_LOCALE alone did not help, because
the timezone was still "auto".

Without a proxy the egress is the host's own, so the answer does not
change between requests. Resolve it on the first request, off the event
loop and under a lock so concurrent first requests share one lookup,
then hand the zone and the locale's primary tag to InvisiblePlaywright
explicitly; an explicit zone and tag make prepare_session_geo touch no
network. The resulting browser is the same as with "auto": passing the
primary tag back reproduces the decided language list for every Firefox
build the locale table can pick, and without a proxy the egress IP,
coordinates and srflx are not used by InvisiblePlaywright anyway.

When the lookup fails upstream falls back to the host timezone by
passing "", but passing "" again would repeat the lookup on every
request, so the host zone is named explicitly (TZ, else the
/etc/localtime link, else UTC) and the lookup is retried after ten
minutes.

Sessions behind a proxy (PROXY_SERVER or X-Proxy-Server) still resolve
per request through the proxy: each proxy has its own egress, and
upstream deliberately fails the launch when it cannot be resolved.

BROWSER_TIMEZONE mirrors BROWSER_LOCALE and wins also behind a proxy;
with both set the lookup is skipped entirely.

Fixes #416

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 00:05:13 +02:00

127 lines
4.6 KiB
Python

import asyncio
import math
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from invisible_core import SessionGeo, SessionLocale
from src import utils
BERLIN = SessionGeo(
"Europe/Berlin",
"203.0.113.7",
locale=SessionLocale(languages=("de", "en-US", "en"), region="DE"),
)
FAILED = SessionGeo(
"", None, locale=SessionLocale(languages=("en-US", "en"), region="US")
)
@pytest.fixture(autouse=True)
def fresh_geo_cache(monkeypatch: pytest.MonkeyPatch):
"""Every test starts with nothing resolved."""
monkeypatch.setattr(utils, "_geo", None)
monkeypatch.setattr(utils, "_geo_lock", asyncio.Lock())
def test_host_timezone_prefers_tz_env(monkeypatch: pytest.MonkeyPatch):
"""TZ wins over /etc/localtime, with or without the leading colon."""
monkeypatch.setenv("TZ", ":Asia/Shanghai")
assert utils.host_timezone() == "Asia/Shanghai"
def test_host_timezone_reads_localtime_symlink(monkeypatch: pytest.MonkeyPatch):
"""Without TZ the zone comes from where /etc/localtime points."""
monkeypatch.delenv("TZ", raising=False)
with patch(
"src.utils.os.path.realpath", return_value="/usr/share/zoneinfo/Europe/Warsaw"
):
assert utils.host_timezone() == "Europe/Warsaw"
def test_host_timezone_defaults_to_utc(monkeypatch: pytest.MonkeyPatch):
"""No TZ and no zoneinfo link is what the Docker image has: UTC."""
monkeypatch.delenv("TZ", raising=False)
with patch("src.utils.os.path.realpath", return_value="/etc/localtime"):
assert utils.host_timezone() == "UTC"
@pytest.mark.asyncio
async def test_egress_lookup_runs_once_per_process():
"""Concurrent first requests and every later one share a single lookup."""
with patch("src.utils.prepare_session_geo", return_value=BERLIN) as lookup:
results = await asyncio.gather(*(utils.get_browser_geo() for _ in range(3)))
results.append(await utils.get_browser_geo())
lookup.assert_called_once_with("", None, "auto")
assert set(results) == {utils.BrowserGeo("Europe/Berlin", "de", math.inf)}
@pytest.mark.asyncio
async def test_failed_lookup_uses_host_timezone_then_retries(
monkeypatch: pytest.MonkeyPatch,
):
"""A failed lookup is not repaid per request, but is retried later."""
monkeypatch.setenv("TZ", "Asia/Shanghai")
with patch("src.utils.prepare_session_geo", return_value=FAILED) as lookup:
geo = await utils.get_browser_geo()
assert (geo.timezone, geo.locale) == ("Asia/Shanghai", "en-US")
await utils.get_browser_geo()
assert lookup.call_count == 1
monkeypatch.setattr(utils, "_geo", geo._replace(expires_at=0))
await utils.get_browser_geo()
assert lookup.call_count == 2 # noqa: PLR2004
@pytest.mark.asyncio
async def test_explicit_timezone_and_locale_touch_no_network():
"""BROWSER_TIMEZONE with BROWSER_LOCALE needs neither egress IP nor geoip."""
with (
patch("src.utils.BROWSER_TIMEZONE", "Asia/Shanghai"),
patch("src.utils.BROWSER_LOCALE", "zh-CN"),
patch("invisible_core._geo.discover_egress_ip") as discover,
patch("invisible_core.download.ensure_geoip_mmdb") as geoip,
):
geo = await utils.get_browser_geo()
assert (geo.timezone, geo.locale) == ("Asia/Shanghai", "zh-CN")
discover.assert_not_called()
geoip.assert_not_called()
def fake_invisible_playwright() -> MagicMock:
"""Stand-in for InvisiblePlaywright that records how it was built."""
browser = MagicMock()
browser.new_context = AsyncMock(return_value=AsyncMock())
session = MagicMock()
session.__aenter__ = AsyncMock(return_value=browser)
session.__aexit__ = AsyncMock(return_value=False)
return MagicMock(return_value=session)
@pytest.mark.asyncio
@pytest.mark.parametrize(
("proxy_header", "expected"),
[
(None, {"timezone": "Europe/Berlin", "locale": "de"}),
("socks5://proxy.test:1080", {"timezone": "", "locale": "auto"}),
],
)
async def test_proxy_sessions_resolve_their_own_egress(
proxy_header: str | None, expected: dict[str, str]
):
"""The cache only covers direct sessions; a proxy's egress is its own."""
launcher = fake_invisible_playwright()
with (
patch("src.utils.PROXY_SERVER", None),
patch("src.utils.InvisiblePlaywright", launcher),
patch("src.utils.prepare_session_geo", return_value=BERLIN) as lookup,
):
async for _ in utils.get_browser(x_proxy_server=proxy_header):
pass
kwargs = launcher.call_args.kwargs
assert {"timezone": kwargs["timezone"], "locale": kwargs["locale"]} == expected
assert lookup.called is (proxy_header is None)