feat: fpw plugins, validation/crawl perf, WS stats, test DB isolation
- Add Free_Proxy_Website-style fpw_* plugins and register them - Per-plugin crawl timeout (crawl_timeout_seconds=120); remove global crawl_timeout setting - Validator: fix connect vs total timeout on save; SOCKS session LRU cache; drop redundant semaphore - Validation handler uses single DB connection; batch upsert after crawl; WorkerPool put_nowait - Remove unused max_retries from settings API/UI; settings maintenance SQL + init_db cleanup of deprecated keys - WebSocket dashboard stats; ProxyList pool_filter and API alignment - POST /api/proxies/delete-one for IPv6-safe deletes; task poll stops on 404 - pytest uses PROXYPOOL_DB_PATH=db/proxies.test.sqlite so tests do not wipe production DB - .gitignore: explicit proxies.test.sqlite patterns; fix plugin_service ValidationException import Made-with: Cursor
This commit is contained in:
@@ -1,5 +1,15 @@
|
||||
"""pytest 配置文件和 fixtures"""
|
||||
# 必须在任何 app.* 导入之前:下方 app fixture 会清空表,不可与生产共用 db/proxies.sqlite
|
||||
import os
|
||||
|
||||
os.environ["PROXYPOOL_DB_PATH"] = "db/proxies.test.sqlite"
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
|
||||
if sys.platform == "win32":
|
||||
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
||||
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from typing import AsyncGenerator
|
||||
@@ -17,22 +27,28 @@ from app.plugins import (
|
||||
SpeedXPlugin,
|
||||
YunDaiLiPlugin,
|
||||
ProxyScrapePlugin,
|
||||
FpwProxyListDownloadPlugin,
|
||||
FpwSocksSslProxyPlugin,
|
||||
FpwSpysOnePlugin,
|
||||
FpwProxynovaPlugin,
|
||||
FpwHidemyPlugin,
|
||||
FpwPremproxyPlugin,
|
||||
FpwFreeproxylistsPlugin,
|
||||
FpwGatherproxyPlugin,
|
||||
FpwCheckerproxyPlugin,
|
||||
)
|
||||
from app.repositories.proxy_repo import ProxyRepository
|
||||
from app.models.domain import ProxyRaw
|
||||
|
||||
|
||||
@pytest_asyncio.fixture(scope="function")
|
||||
async def app():
|
||||
"""创建应用实例"""
|
||||
# 初始化测试数据库并清空历史数据
|
||||
await init_db()
|
||||
async with get_db() as db:
|
||||
await db.execute("DELETE FROM proxies")
|
||||
await db.execute("DELETE FROM settings")
|
||||
await db.commit()
|
||||
|
||||
# 清理并重新注册插件,防止跨测试污染
|
||||
|
||||
registry.clear()
|
||||
for plugin_cls in [
|
||||
Fate0Plugin,
|
||||
@@ -43,14 +59,22 @@ async def app():
|
||||
SpeedXPlugin,
|
||||
YunDaiLiPlugin,
|
||||
ProxyScrapePlugin,
|
||||
FpwProxyListDownloadPlugin,
|
||||
FpwSocksSslProxyPlugin,
|
||||
FpwSpysOnePlugin,
|
||||
FpwProxynovaPlugin,
|
||||
FpwHidemyPlugin,
|
||||
FpwPremproxyPlugin,
|
||||
FpwFreeproxylistsPlugin,
|
||||
FpwGatherproxyPlugin,
|
||||
FpwCheckerproxyPlugin,
|
||||
]:
|
||||
registry.register(plugin_cls)
|
||||
|
||||
|
||||
test_app = create_app()
|
||||
async with test_app.router.lifespan_context(test_app):
|
||||
yield test_app
|
||||
|
||||
# 给 aiosqlite / aiohttp 后台线程留出收尾时间
|
||||
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
|
||||
@@ -80,32 +104,4 @@ async def sample_proxy(db, proxy_repo):
|
||||
"""创建一个测试代理"""
|
||||
await proxy_repo.insert_or_update(db, "192.168.1.1", 8080, "http", 50)
|
||||
yield {"ip": "192.168.1.1", "port": 8080, "protocol": "http", "score": 50}
|
||||
# 清理
|
||||
await proxy_repo.delete(db, "192.168.1.1", 8080)
|
||||
|
||||
|
||||
@pytest_asyncio.fixture(autouse=True)
|
||||
async def mock_external_requests(monkeypatch, request):
|
||||
"""
|
||||
自动在集成/E2E 测试中 mock 外部网络请求:
|
||||
1. 插件爬取返回固定测试代理,避免真实 HTTP 请求
|
||||
2. 代理验证瞬间成功,避免连接超时等待
|
||||
"""
|
||||
if "/unit/" in request.node.nodeid:
|
||||
return
|
||||
from app.services.plugin_runner import PluginRunner
|
||||
from app.services.validator_service import ValidatorService
|
||||
|
||||
async def _mock_run(self, plugin):
|
||||
from app.models.domain import CrawlResult
|
||||
return CrawlResult(
|
||||
plugin_name=plugin.name,
|
||||
proxies=[ProxyRaw("192.168.100.10", 8080, "http")],
|
||||
success_count=1,
|
||||
)
|
||||
|
||||
async def _mock_validate(self, ip: str, port: int, protocol: str = "http"):
|
||||
return True, 1.23
|
||||
|
||||
monkeypatch.setattr(PluginRunner, "run", _mock_run)
|
||||
monkeypatch.setattr(ValidatorService, "validate", _mock_validate)
|
||||
|
||||
Reference in New Issue
Block a user