feat: 自动代理每批同时领取 SOCKS5、HTTP 和 HTTPS
This commit is contained in:
@@ -9,7 +9,7 @@ from euniv.proxy_pool import (
|
||||
BATCH_SIZE,
|
||||
MAX_FETCHES,
|
||||
PROBE_URL,
|
||||
SOURCE_PROTOCOL,
|
||||
SOURCE_PROTOCOLS,
|
||||
_fetch_batch,
|
||||
_fetches,
|
||||
_holds,
|
||||
@@ -26,19 +26,20 @@ def _clear() -> None:
|
||||
_fetches.clear()
|
||||
|
||||
|
||||
def test_fetch_batch_asks_for_eight_chinese_https_proxies(monkeypatch) -> None:
|
||||
seen: dict = {}
|
||||
def test_fetch_batch_asks_for_socks5_then_http_then_https(monkeypatch) -> None:
|
||||
calls: list[dict] = []
|
||||
|
||||
class FakeResponse:
|
||||
def __init__(self, protocol: str) -> None:
|
||||
self.protocol = protocol
|
||||
|
||||
def json(self) -> dict:
|
||||
return {
|
||||
"code": 200,
|
||||
"message": "success",
|
||||
"data": {
|
||||
"proxies": ["1.2.3.4:8080", "10.0.0.1:3128", "bad", "8.8.8.8:9"],
|
||||
"count": 4,
|
||||
},
|
||||
}
|
||||
proxies = {
|
||||
"socks5": ["1.2.3.4:1080"],
|
||||
"http": ["9.9.9.9:8080"],
|
||||
"https": ["1.2.3.4:1080", "8.8.8.8:8080", "10.0.0.1:1"],
|
||||
}[self.protocol]
|
||||
return {"code": 200, "message": "success", "data": {"proxies": proxies, "count": len(proxies)}}
|
||||
|
||||
class FakeClient:
|
||||
def __init__(self, *_args, **_kwargs) -> None:
|
||||
@@ -51,31 +52,35 @@ def test_fetch_batch_asks_for_eight_chinese_https_proxies(monkeypatch) -> None:
|
||||
return False
|
||||
|
||||
def get(self, url: str, params: dict | None = None):
|
||||
seen["url"] = url
|
||||
seen["params"] = params
|
||||
return FakeResponse()
|
||||
calls.append({"url": url, "params": params or {}})
|
||||
return FakeResponse(str((params or {}).get("protocol")))
|
||||
|
||||
monkeypatch.setattr("euniv.proxy_pool.httpx.Client", FakeClient)
|
||||
assert _fetch_batch() == [("1.2.3.4", 8080), ("8.8.8.8", 9)]
|
||||
assert seen["url"].endswith("/api/get_proxy.php")
|
||||
assert seen["params"] == {"protocol": SOURCE_PROTOCOL, "count": BATCH_SIZE, "country_code": "CN"}
|
||||
assert _fetch_batch() == [
|
||||
("socks5", "1.2.3.4", 1080),
|
||||
("http", "9.9.9.9", 8080),
|
||||
("http", "8.8.8.8", 8080),
|
||||
]
|
||||
assert [item["params"]["protocol"] for item in calls] == [protocol for protocol, _scheme in SOURCE_PROTOCOLS]
|
||||
assert all(item["url"].endswith("/api/get_proxy.php") for item in calls)
|
||||
assert all(item["params"]["count"] == BATCH_SIZE and item["params"]["country_code"] == "CN" for item in calls)
|
||||
|
||||
|
||||
def test_acquire_stops_at_the_first_usable_proxy(monkeypatch) -> None:
|
||||
_clear()
|
||||
batches = [[("1.1.1.1", 1), ("2.2.2.2", 2), ("3.3.3.3", 3)]]
|
||||
batches = [[("socks5", "1.1.1.1", 1), ("socks5", "2.2.2.2", 2), ("http", "3.3.3.3", 3)]]
|
||||
probed: list[str] = []
|
||||
|
||||
monkeypatch.setattr("euniv.proxy_pool._fetch_batch", lambda: list(batches.pop(0)))
|
||||
|
||||
def fake_probe(_scheme: str, host: str, _port: int):
|
||||
probed.append(host)
|
||||
def fake_probe(scheme: str, host: str, _port: int):
|
||||
probed.append(f"{scheme}:{host}")
|
||||
return host == "2.2.2.2", 10, ""
|
||||
|
||||
monkeypatch.setattr("euniv.proxy_pool.probe", fake_probe)
|
||||
assert acquire("u1") == "http://2.2.2.2:2"
|
||||
assert probed == ["1.1.1.1", "2.2.2.2"]
|
||||
assert acquire("u1") == "http://2.2.2.2:2"
|
||||
assert acquire("u1") == "socks5://2.2.2.2:2"
|
||||
assert probed == ["socks5:1.1.1.1", "socks5:2.2.2.2"]
|
||||
assert acquire("u1") == "socks5://2.2.2.2:2"
|
||||
assert batches == []
|
||||
|
||||
|
||||
@@ -85,7 +90,7 @@ def test_acquire_uses_eight_batches_then_direct(monkeypatch) -> None:
|
||||
|
||||
def fetch() -> list[tuple[str, int]]:
|
||||
calls["n"] += 1
|
||||
return [(f"1.2.3.{calls['n']}", 8000)]
|
||||
return [("http", f"1.2.3.{calls['n']}", 8000)]
|
||||
|
||||
monkeypatch.setattr("euniv.proxy_pool._fetch_batch", fetch)
|
||||
monkeypatch.setattr("euniv.proxy_pool.probe", lambda *_args: (False, 8, "无法连接"))
|
||||
@@ -101,7 +106,7 @@ def test_failed_proxy_fetches_the_next_batch_until_budget(monkeypatch) -> None:
|
||||
|
||||
def fetch() -> list[tuple[str, int]]:
|
||||
calls["n"] += 1
|
||||
return [(f"1.2.3.{calls['n']}", 8000)]
|
||||
return [("http", f"1.2.3.{calls['n']}", 8000)]
|
||||
|
||||
monkeypatch.setattr("euniv.proxy_pool._fetch_batch", fetch)
|
||||
monkeypatch.setattr("euniv.proxy_pool.probe", lambda *_args: (True, 20, ""))
|
||||
@@ -158,7 +163,7 @@ def test_direct_recheck_keeps_valid_credential_without_login(monkeypatch) -> Non
|
||||
|
||||
def test_release_lets_the_next_task_fetch_again(monkeypatch) -> None:
|
||||
_clear()
|
||||
monkeypatch.setattr("euniv.proxy_pool._fetch_batch", lambda: [("8.8.4.4", 8080)])
|
||||
monkeypatch.setattr("euniv.proxy_pool._fetch_batch", lambda: [("http", "8.8.4.4", 8080)])
|
||||
monkeypatch.setattr("euniv.proxy_pool.probe", lambda *_args: (True, 15, ""))
|
||||
assert acquire("u1") == "http://8.8.4.4:8080"
|
||||
release("u1")
|
||||
|
||||
Reference in New Issue
Block a user