zapret-kvn/tests/test_start_fallback.py
loop-uh 67ca37f257 Start fallback: a connect on a line that answers nothing is left in about 8 s and goes on with another entry
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>
2026-11-29 14:49:02 -05:00

81 lines
3.5 KiB
Python

"""Start fallback (owner decision 2026-11-29): a connect on a line that answered nothing goes on with another line.
The rules are plain functions of the auto-switch service; the real-data proof (a real closing line and a real working
line through the real controller and the stock naive.exe) is `tests/test_app_e2e_start_fallback.py`.
Host names follow the real scheme of the rows (`<id>.<entry>.<zone>`), the zone is a placeholder.
"""
from __future__ import annotations
import unittest
from test_auto_switch_dead_link import FakeController
from xray_fluent.application import auto_switch_service
from xray_fluent.application.auto_switch_service import (
START_FALLBACK_LINES,
START_READY_SEC,
line_in_backoff,
start_fallback_after_failure,
start_fallback_possible,
start_ready_timeout,
)
from xray_fluent.application.selection_source import SelectionSource
from xray_fluent.profiles.models import Node
ZONE = "zone.example"
def _controller() -> FakeController:
controller = FakeController(3)
controller.connected = False
controller.state.nodes = [
Node(id="n0", name="first", server=f"x964.rufristal.{ZONE}", port=443, scheme="naive"),
Node(id="n1", name="same-entry", server=f"y111.rufristal.{ZONE}", port=443, scheme="naive"),
Node(id="n2", name="other-entry", server=f"q573.rurugogo.{ZONE}", port=443, scheme="naive"),
]
controller.state.selected_node_id = "n0"
controller._desired_connected = False
return controller
class StartFallbackTests(unittest.TestCase):
def test_the_tunnel_check_is_short_only_when_another_line_can_be_tried(self) -> None:
controller = _controller()
self.assertEqual(START_READY_SEC, start_ready_timeout(controller))
controller.state.nodes = controller.state.nodes[:1]
self.assertIsNone(start_ready_timeout(controller), "one line: nothing to fall back to, the full check stays")
def test_the_switch_off_keeps_the_full_check_and_never_falls_back(self) -> None:
controller = _controller()
controller.state.settings.auto_switch_enabled = False
self.assertFalse(start_fallback_possible(controller))
self.assertIsNone(start_ready_timeout(controller))
self.assertFalse(start_fallback_after_failure(controller))
self.assertEqual([], controller.selected)
def test_the_failed_line_is_left_for_another_entry_and_backs_off(self) -> None:
controller = _controller()
self.assertTrue(start_fallback_after_failure(controller))
self.assertEqual([("n2", SelectionSource.AUTO_SWITCH)], controller.selected)
self.assertTrue(controller._desired_connected, "the connect is wanted again")
self.assertTrue(line_in_backoff(controller, "n0", auto_switch_service.time.monotonic()))
self.assertTrue(any("[start-fallback]" in line for line in controller.logs))
def test_three_lines_at_most_for_one_connect(self) -> None:
controller = _controller()
picked = []
for _ in range(5):
if not start_fallback_after_failure(controller):
break
node_id = controller.selected[-1][0]
picked.append(node_id)
controller.state.selected_node_id = node_id # the selection followed, the next connect failed too
self.assertEqual(START_FALLBACK_LINES - 1, len(picked))
self.assertEqual(len(set(picked)), len(picked), "no line is tried twice in a row")
self.assertEqual(0, controller._start_fallback_tries, "the chain is over: the next click starts fresh")
if __name__ == "__main__":
unittest.main()