zapret-kvn/tests/test_fast_failover.py
loop-uh ece3b8863f
Some checks failed
Windows project source guards / test (push) Failing after 3m26s
Fast failover: a silent fresh line is left in about 5 s, failed lines back off 1/5/10 min, replacement prefers another entry
Owner decision 2026-11-29. The metrics worker calls a line that was just connected or switched to silent after 5 s of
sending without an answer (10 s for a settled line); auto-switch then acts at once for a fresh line (no 20 s warm-up,
no 15 s of dead pings); the line that was left is out for 1, 5, then 10 minutes and never banned; the replacement
prefers another entry (second host label), the same entry only when nothing else is left.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-29 12:18:34 -05:00

159 lines
7.3 KiB
Python

"""Fast failover (owner decision 2026-11-29): a line that accepts TCP but never answers is left in about 5 s.
The rule uses only existing lines and the client's own traffic (no new probe, no new wire behaviour):
- the metrics worker calls a FRESH line (just connected or switched to) silent after 5 s of sending without an answer,
instead of 10 s, and the confirmation request through the tunnel stays the same;
- auto-switch then acts at once for a fresh line (no 20 s warm-up, no 15 s of dead pings);
- the failed line is out for 1, then 5, then 10 minutes (never banned), and the replacement prefers another ENTRY;
- the deadline is 5 s now, 3 s when the routes through Russia are gone (one constant).
Host names follow the real scheme of the rows (`<id>.<entry>.<zone>`: entries rufristal, rurugogo, timama), the zone is a
placeholder so that no real address is kept in the repository.
"""
from __future__ import annotations
import unittest
from unittest.mock import patch
from test_auto_switch_dead_link import FakeController, MetricsWorkerPingTargetTests, _tick
from xray_fluent.application import auto_switch_service
from xray_fluent.application.auto_switch_service import (
AUTO_SWITCH_BACKOFF_SEC,
AUTO_SWITCH_DEAD_LINK_SEC,
AUTO_SWITCH_FRESH_SEC,
AUTO_SWITCH_FRESH_WARMUP_SEC,
entry_key,
get_next_node_for_auto_switch,
line_in_backoff,
note_line_failure,
)
from xray_fluent.profiles.models import Node
ZONE = "zone.example"
def _node(node_id: str, host: str) -> Node:
return Node(id=node_id, name=node_id, server=host, port=443, scheme="vless")
class EntryKeyTests(unittest.TestCase):
def test_the_second_label_is_the_entry(self) -> None:
self.assertEqual("rufristal", entry_key(f"x964.rufristal.{ZONE}"))
self.assertEqual("rurugogo", entry_key(f"q573.rurugogo.{ZONE}"))
self.assertEqual("timama", entry_key(f"x482.timama.{ZONE}."))
def test_an_address_or_a_short_name_is_its_own_entry(self) -> None:
self.assertEqual("203.0.113.7", entry_key("203.0.113.7"))
self.assertEqual("[2001:db8::1]", entry_key("[2001:db8::1]"))
self.assertEqual("server.example", entry_key("server.example"))
self.assertIsNone(entry_key(""))
self.assertIsNone(entry_key(None))
class ReplacementTests(unittest.TestCase):
def _controller(self) -> FakeController:
controller = FakeController(3)
controller.state.nodes = [
_node("n0", f"x964.rufristal.{ZONE}"),
_node("n1", f"y111.rufristal.{ZONE}"), # the same entry as the active line, and the faster one
_node("n2", f"q573.rurugogo.{ZONE}"),
]
controller.state.selected_node_id = "n0"
for node, speed in zip(controller.state.nodes, (10.0, 90.0, 40.0)):
node.is_alive = True
node.speed_mbps = speed
node.ping_ms = 50
return controller
def test_another_entry_is_preferred_over_a_faster_line_of_the_failed_entry(self) -> None:
controller = self._controller()
self.assertEqual("n2", get_next_node_for_auto_switch(controller).id)
def test_the_same_entry_is_still_used_when_nothing_else_is_left(self) -> None:
controller = self._controller()
note_line_failure(controller, controller.state.nodes[2], 100.0)
with patch.object(auto_switch_service.time, "monotonic", return_value=101.0):
self.assertEqual("n1", get_next_node_for_auto_switch(controller).id)
def test_a_failed_line_is_out_for_1_then_5_then_10_minutes_and_then_back(self) -> None:
controller = self._controller()
node = controller.state.nodes[2]
self.assertEqual((60.0, 300.0, 600.0), AUTO_SWITCH_BACKOFF_SEC)
at = 1000.0
for expected in (60.0, 300.0, 600.0, 600.0):
self.assertEqual(expected, note_line_failure(controller, node, at))
self.assertTrue(line_in_backoff(controller, node.id, at + expected - 1))
self.assertFalse(line_in_backoff(controller, node.id, at + expected + 1), "never banned for good")
at += expected + 5
class FreshLineTests(unittest.TestCase):
def _fresh(self, controller: FakeController, connected_at: float) -> None:
controller._auto_switch_warmup_until = connected_at + auto_switch_service.AUTO_SWITCH_WARMUP_SEC
controller._auto_switch_fresh_from = connected_at + AUTO_SWITCH_FRESH_WARMUP_SEC
controller._auto_switch_fresh_until = connected_at + AUTO_SWITCH_FRESH_SEC
def test_a_confirmed_silent_fresh_line_is_left_at_once_without_the_warmup(self) -> None:
controller = FakeController()
self._fresh(controller, 100.0)
_tick(controller, 106.0, link_alive=False) # armed: the verdict of the worker arrived 6 s after the connect
self.assertEqual([], controller.selected)
_tick(controller, 107.0, link_alive=False) # the very next tick, no 15 s of dead pings
self.assertEqual(1, len(controller.selected))
def test_before_the_fresh_minimum_nothing_happens(self) -> None:
controller = FakeController()
self._fresh(controller, 100.0)
_tick(controller, 102.0, link_alive=False)
_tick(controller, 103.0, link_alive=False)
self.assertEqual([], controller.selected)
def test_a_settled_line_keeps_the_slow_rule(self) -> None:
controller = FakeController()
_tick(controller, 500.0, link_alive=False)
_tick(controller, 500.0 + AUTO_SWITCH_DEAD_LINK_SEC - 1, link_alive=False)
self.assertEqual([], controller.selected)
_tick(controller, 500.0 + AUTO_SWITCH_DEAD_LINK_SEC + 1, link_alive=False)
self.assertEqual(1, len(controller.selected))
def test_the_switched_away_line_goes_into_back_off(self) -> None:
controller = FakeController()
self._fresh(controller, 100.0)
_tick(controller, 106.0, link_alive=False)
_tick(controller, 107.0, link_alive=False)
self.assertTrue(line_in_backoff(controller, "n0", 108.0))
class FreshWorkerTests(unittest.TestCase):
"""The worker side: 5 s of sending without an answer on a fresh line, 10 s on a settled one."""
def setUp(self) -> None:
# The worker harness of the dead-link tests (a real LiveMetricsWorker with a recording reader and confirmer).
self.harness = MetricsWorkerPingTargetTests("test_set_ping_target_repoints_and_forces_probe")
def _confirms_after(self, seconds: float, *, fresh: bool) -> int:
module, worker, _reads, confirms = self.harness._watching(verdict=False)
worker._fresh_pending = fresh
up, down = module.PING_UNANSWERED_UP_BPS * 2, 0.0
now = 100.0
while now <= 100.0 + seconds:
worker._watch_link(now, down, up)
now += 1.0
self.harness._settle(worker)
return len(confirms)
def test_a_fresh_line_is_confirmed_after_five_seconds(self) -> None:
module = self.harness._watching(verdict=False)[0]
self.assertEqual(5.0, module.LINK_SUSPECT_FRESH_SEC)
self.assertEqual(0, self._confirms_after(4, fresh=True))
self.assertEqual(1, self._confirms_after(6, fresh=True))
def test_a_settled_line_is_confirmed_after_ten_seconds(self) -> None:
self.assertEqual(0, self._confirms_after(8, fresh=False))
self.assertEqual(1, self._confirms_after(11, fresh=False))
if __name__ == "__main__":
unittest.main()