优化浏览器context复用

This commit is contained in:
2026-06-09 16:12:55 +08:00
parent 3b8bdf09f7
commit 28fcc619a5
2 changed files with 51 additions and 12 deletions
+17
View File
@@ -111,6 +111,23 @@ async def test_warm_context_rebuilds_and_closes_on_failure():
assert sess2.session_id != sess1.session_id # 换了新会话
@pytest.mark.asyncio
async def test_expired_warm_context_is_closed_on_rebuild():
pool = _FakeBrowserPool()
# 存活时长设为 0:任何已存在的保活上下文都立即判定过期
settings = Settings(session_ttl_seconds=0, cloudflare_reuse_wait_timeout_seconds=8.0)
mgr = CloudflareSessionManager(settings, pool, SessionStore(settings))
await mgr.fetch_html(_DETAIL_URL.format(1))
assert pool.create_calls == 1
# 第二次请求:旧上下文过期 -> 退役关闭 -> 重建,旧上下文不应泄漏
await mgr.fetch_html(_DETAIL_URL.format(2))
assert pool.create_calls == 2
assert pool.contexts[0].closed is True
assert len(mgr._warm_contexts) == 1 # 注册表中只保留新的那个
@pytest.mark.asyncio
async def test_close_releases_all_warm_contexts():
pool = _FakeBrowserPool()