The naive tunnel check gives up after 5 s (was 25 s) when another line can be tried, the failed line backs off 1/5/10 min, at most 3 lines per connect. Real keys on win10: connected on the working line in 9.2-9.5 s, without it the same click failed after 26.6 s and stayed disconnected. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
134 lines
5.8 KiB
Python
134 lines
5.8 KiB
Python
"""E2E: a connect on a line that answers nothing goes on with another line (owner decision 2026-11-29).
|
|
|
|
Real AppController, real ``sing-box.exe`` front, the stock ``naive.exe`` and two REAL naive keys: a line whose server closes
|
|
every tunnel and a working line of another entry. Key files come from the environment (`KVN_START_FALLBACK_BAD_KEY`,
|
|
`KVN_START_FALLBACK_GOOD_KEY`; one key line each, never printed); without them, or off Windows, the test skips, which is an
|
|
honest "unproven" and not a synthetic stand-in. Measured on win10 on 2026-11-29: the connect ended on the working line in
|
|
9.2-9.5 s; with the fallback off the same click failed after 26.6 s and stayed disconnected.
|
|
|
|
Keep the ``test_app_*`` prefix: the module needs a QApplication.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import shutil
|
|
import tempfile
|
|
import time
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
|
|
|
from PyQt6.QtCore import QEventLoop, QTimer
|
|
from PyQt6.QtWidgets import QApplication
|
|
|
|
_existing = QApplication.instance()
|
|
app = _existing if isinstance(_existing, QApplication) else (_existing or QApplication([]))
|
|
|
|
from xray_fluent.constants import NAIVE_PATH_DEFAULT, SINGBOX_PATH_DEFAULT, SINGBOX_TEMPLATES_DIR, XRAY_PATH_DEFAULT
|
|
|
|
_BAD = os.environ.get("KVN_START_FALLBACK_BAD_KEY", "")
|
|
_GOOD = os.environ.get("KVN_START_FALLBACK_GOOD_KEY", "")
|
|
_RUNNABLE = (
|
|
os.name == "nt" and NAIVE_PATH_DEFAULT.is_file() and SINGBOX_PATH_DEFAULT.is_file()
|
|
and Path(_BAD).is_file() and Path(_GOOD).is_file()
|
|
)
|
|
|
|
|
|
def _spin_until(condition, timeout_s: float) -> bool:
|
|
deadline = time.monotonic() + timeout_s
|
|
while time.monotonic() < deadline:
|
|
if condition():
|
|
return True
|
|
loop = QEventLoop()
|
|
QTimer.singleShot(30, loop.quit)
|
|
loop.exec()
|
|
return condition()
|
|
|
|
|
|
@unittest.skipUnless(_RUNNABLE, "needs Windows, the bundled naive.exe and sing-box.exe and two real naive key files")
|
|
class StartFallbackE2ETests(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
from tests.test_app_transition_gui_stalls import _FakeWinProxy
|
|
from xray_fluent.application import runtime_services
|
|
from xray_fluent.application.controller import AppController
|
|
from xray_fluent.importer.link_parser import parse_single
|
|
from xray_fluent.profiles.models import AppState
|
|
|
|
self.tmp = Path(tempfile.mkdtemp(prefix="zkvn-e2e-startfb-"))
|
|
configs = self.tmp / "configs"
|
|
configs.mkdir()
|
|
shutil.copyfile(SINGBOX_TEMPLATES_DIR / "default.json", configs / "default.json")
|
|
self.patchers = [
|
|
patch("xray_fluent.application.controller.SINGBOX_CONFIGS_DIR", configs),
|
|
patch.object(runtime_services, "start_metrics_worker", lambda c: None),
|
|
patch("xray_fluent.application.server_bypass.ServerBypass.learn_pool_addresses", lambda self: False),
|
|
patch("xray_fluent.application.controller.DATA_DIR", self.tmp),
|
|
]
|
|
for patcher in self.patchers:
|
|
patcher.start()
|
|
self.c = AppController()
|
|
self.c.schedule_save = lambda: None
|
|
self.c.save = lambda: None
|
|
self.c._start_metrics_worker = lambda: None
|
|
self.c.proxy = _FakeWinProxy()
|
|
self.bad = parse_single(Path(_BAD).read_text(encoding="utf-8").strip())
|
|
self.good = parse_single(Path(_GOOD).read_text(encoding="utf-8").strip())
|
|
self.bad.sort_order, self.good.sort_order = 0, 1
|
|
state = AppState()
|
|
state.nodes = [self.bad, self.good]
|
|
state.selected_node_id = self.bad.id
|
|
settings = state.settings
|
|
settings.enable_system_proxy = False
|
|
settings.tun_mode = False
|
|
settings.auto_switch_enabled = True
|
|
settings.auto_reconnect_enabled = True
|
|
settings.singbox_path = str(SINGBOX_PATH_DEFAULT)
|
|
settings.xray_path = str(XRAY_PATH_DEFAULT)
|
|
settings.zapret_target.tcp_proxy_enabled = False
|
|
settings.zapret_target.quic_proxy_enabled = False
|
|
settings.zapret_target.wireguard_enabled = False
|
|
self.c.state = state
|
|
self.c._invalidate_xray_outbound_pool_cache()
|
|
self.logs: list[str] = []
|
|
self.c.log_line.connect(self.logs.append)
|
|
|
|
def tearDown(self) -> None:
|
|
c = self.c
|
|
c._desired_connected = False
|
|
c._request_transition("test over")
|
|
_spin_until(lambda: not c._transition_active and not c.connected, 60)
|
|
for manager in (c.singbox, c.xray, c.hysteria, c.amnezia, c.naive):
|
|
try:
|
|
manager.stop()
|
|
except Exception:
|
|
pass
|
|
for patcher in reversed(self.patchers):
|
|
patcher.stop()
|
|
shutil.rmtree(self.tmp, ignore_errors=True)
|
|
|
|
def _connect_and_wait(self) -> float:
|
|
started = time.monotonic()
|
|
self.c.toggle_connection()
|
|
_spin_until(lambda: self.c.connected and not self.c._transition_active, 80)
|
|
return time.monotonic() - started
|
|
|
|
def test_a_closing_line_is_left_for_the_working_line_within_seconds(self) -> None:
|
|
elapsed = self._connect_and_wait()
|
|
self.assertTrue(self.c.connected, "the connect did not end connected")
|
|
self.assertEqual(self.good.id, self.c.selected_node.id, "the connect ended on the closing line")
|
|
self.assertLess(elapsed, 40, f"the fallback took {elapsed:.0f} s")
|
|
self.assertTrue(any("[start-fallback]" in line for line in self.logs))
|
|
|
|
def test_without_the_fallback_the_same_click_fails_and_stays_disconnected(self) -> None:
|
|
with patch("xray_fluent.application.auto_switch_service.START_FALLBACK_LINES", 1):
|
|
self._connect_and_wait()
|
|
self.assertFalse(self.c.connected)
|
|
self.assertEqual(self.bad.id, self.c.selected_node.id)
|
|
self.assertFalse(any("[start-fallback]" in line for line in self.logs))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|