|
|
@@ -0,0 +1,206 @@
|
|
|
+import unittest
|
|
|
+import time
|
|
|
+from unittest.mock import MagicMock, patch
|
|
|
+from vs_types import (
|
|
|
+ BizException,
|
|
|
+ RateLimiteddError,
|
|
|
+ RateLimitedError,
|
|
|
+ SessionExpiredOrInvalidError,
|
|
|
+ SessionExpiredError,
|
|
|
+ PermissionDeniedError,
|
|
|
+ NotFoundError,
|
|
|
+ BizLogicError,
|
|
|
+ NetworkTimeoutError,
|
|
|
+ VSPluginError,
|
|
|
+ BookerStandaloneConfig,
|
|
|
+ VSPlgConfig,
|
|
|
+)
|
|
|
+from standalone import StandaloneGCO
|
|
|
+
|
|
|
+
|
|
|
+class TestMetadataExceptions(unittest.TestCase):
|
|
|
+ def test_exception_pure_facts(self):
|
|
|
+ """验证异常仅表达领域事实(状态码、HTTP状态、信息),不携带调度器执行策略"""
|
|
|
+ # 1. RateLimiteddError
|
|
|
+ err_rate = RateLimiteddError("Too fast")
|
|
|
+ self.assertEqual(err_rate.code, 42901)
|
|
|
+ self.assertEqual(err_rate.http_status, 429)
|
|
|
+ self.assertEqual(err_rate.message, "Too fast")
|
|
|
+ self.assertFalse(hasattr(err_rate, "lock_account"))
|
|
|
+ self.assertFalse(hasattr(err_rate, "destroy_session"))
|
|
|
+ self.assertFalse(hasattr(err_rate, "cooldown_sec"))
|
|
|
+ self.assertFalse(hasattr(err_rate, "rotate_proxy"))
|
|
|
+
|
|
|
+ # 别名验证
|
|
|
+ self.assertIs(RateLimitedError, RateLimiteddError)
|
|
|
+ self.assertIs(SessionExpiredError, SessionExpiredOrInvalidError)
|
|
|
+ self.assertIs(VSPluginError, BizException)
|
|
|
+
|
|
|
+ # 2. SessionExpiredOrInvalidError
|
|
|
+ err_session = SessionExpiredOrInvalidError("Kicked out")
|
|
|
+ self.assertEqual(err_session.code, 40101)
|
|
|
+ self.assertEqual(err_session.http_status, 401)
|
|
|
+
|
|
|
+ # 3. NetworkTimeoutError
|
|
|
+ err_net = NetworkTimeoutError("Gateway timeout")
|
|
|
+ self.assertEqual(err_net.code, 50401)
|
|
|
+ self.assertEqual(err_net.http_status, 504)
|
|
|
+
|
|
|
+ # 4. PermissionDeniedError
|
|
|
+ err_perm = PermissionDeniedError("403 Forbidden")
|
|
|
+ self.assertEqual(err_perm.code, 40301)
|
|
|
+ self.assertEqual(err_perm.http_status, 403)
|
|
|
+
|
|
|
+ def test_standalone_normalize_error(self):
|
|
|
+ cfg = BookerStandaloneConfig(identifier="test_group")
|
|
|
+ gco = StandaloneGCO(cfg=cfg, redis_conf={})
|
|
|
+
|
|
|
+ # 已是规范异常
|
|
|
+ rate_err = RateLimiteddError()
|
|
|
+ self.assertIs(gco._normalize_error(rate_err), rate_err)
|
|
|
+
|
|
|
+ # 历史遗留字符串 "42901" 兼容识别
|
|
|
+ raw_429 = Exception("Request failed with 42901 code")
|
|
|
+ normalized_429 = gco._normalize_error(raw_429)
|
|
|
+ self.assertIsInstance(normalized_429, RateLimiteddError)
|
|
|
+
|
|
|
+ # 历史遗留字符串 "Session expired" 兼容识别
|
|
|
+ raw_sess = Exception("Error: Session expired on server")
|
|
|
+ normalized_sess = gco._normalize_error(raw_sess)
|
|
|
+ self.assertIsInstance(normalized_sess, SessionExpiredOrInvalidError)
|
|
|
+
|
|
|
+ # 未知异常保底
|
|
|
+ raw_unknown = KeyError("element_not_found")
|
|
|
+ normalized_unknown = gco._normalize_error(raw_unknown)
|
|
|
+ self.assertIsInstance(normalized_unknown, BizException)
|
|
|
+ self.assertEqual(normalized_unknown.code, 50000)
|
|
|
+
|
|
|
+ @patch("toolkit.vs_cloud_api.VSCloudApi.Instance")
|
|
|
+ def test_standalone_action_matrix_policy(self, mock_cloud_instance):
|
|
|
+ mock_cloud = MagicMock()
|
|
|
+ mock_cloud_instance.return_value = mock_cloud
|
|
|
+
|
|
|
+ cfg = BookerStandaloneConfig(identifier="test_group")
|
|
|
+ cfg.account.id = 12345
|
|
|
+ cfg.account.username = "test_user"
|
|
|
+ cfg.login_interval = 1200
|
|
|
+ gco = StandaloneGCO(cfg=cfg, redis_conf={})
|
|
|
+
|
|
|
+ mock_inst = MagicMock()
|
|
|
+ gco.m_instance = mock_inst
|
|
|
+
|
|
|
+ # 场景 A: 登录/建连时 429 -> 账号受限,锁账号
|
|
|
+ err = RateLimiteddError()
|
|
|
+ gco._dispatch_error_policy("create_session", err)
|
|
|
+ mock_cloud.lock_account.assert_called_once_with(12345, 1200)
|
|
|
+ mock_inst.cleanup.assert_called_once()
|
|
|
+ self.assertIsNone(gco.m_instance)
|
|
|
+
|
|
|
+ # 场景 B: 查询时 429 -> 会话已登录,属出口 IP 频率超限,绝不锁账号,换 IP 即可!
|
|
|
+ mock_cloud.reset_mock()
|
|
|
+ mock_inst2 = MagicMock()
|
|
|
+ gco.m_instance = mock_inst2
|
|
|
+ gco._dispatch_error_policy("query", err)
|
|
|
+ mock_cloud.lock_account.assert_not_called()
|
|
|
+ mock_inst2.cleanup.assert_called_once()
|
|
|
+ self.assertIsNone(gco.m_instance)
|
|
|
+ self.assertGreater(gco.m_next_query_time, time.time())
|
|
|
+
|
|
|
+ def test_standalone_dispatch_session_expired_policy(self):
|
|
|
+ cfg = BookerStandaloneConfig(identifier="test_group")
|
|
|
+ gco = StandaloneGCO(cfg=cfg, redis_conf={})
|
|
|
+
|
|
|
+ mock_inst = MagicMock()
|
|
|
+ gco.m_instance = mock_inst
|
|
|
+
|
|
|
+ # 触发 SessionExpiredOrInvalidError
|
|
|
+ err = SessionExpiredOrInvalidError()
|
|
|
+ gco._dispatch_error_policy("query", err)
|
|
|
+
|
|
|
+ # 验证会话已销毁,并排期 5s 后重连
|
|
|
+ mock_inst.cleanup.assert_called_once()
|
|
|
+ self.assertIsNone(gco.m_instance)
|
|
|
+ self.assertGreater(gco.m_next_query_time, time.time())
|
|
|
+
|
|
|
+ @patch("toolkit.vs_cloud_api.VSCloudApi.Instance")
|
|
|
+ def test_sentinel_error_dispatch(self, mock_cloud_instance):
|
|
|
+ from sentinel import SentinelGCO
|
|
|
+ from vs_types import GroupConfig, Task
|
|
|
+
|
|
|
+ mock_cloud = MagicMock()
|
|
|
+ mock_cloud_instance.return_value = mock_cloud
|
|
|
+
|
|
|
+ cfg = GroupConfig(identifier="test_sentinel")
|
|
|
+ cfg.login_backoff = 1800
|
|
|
+ cfg.proxy_cd = 300
|
|
|
+ gco = SentinelGCO(cfg=cfg, redis_conf={})
|
|
|
+
|
|
|
+ # 场景 1: spawn 时 429 -> 账号被限,锁账号
|
|
|
+ plg_cfg = VSPlgConfig()
|
|
|
+ plg_cfg.account.id = 999
|
|
|
+ plg_cfg.account.username = "sentinel_user"
|
|
|
+ gco._dispatch_error_policy("spawn", RateLimiteddError(), plg_cfg=plg_cfg)
|
|
|
+ mock_cloud.lock_account.assert_called_once_with(999, 1800)
|
|
|
+ self.assertGreater(gco.m_spawn_cooldown_until, time.time())
|
|
|
+
|
|
|
+ # 场景 2: query 时 429 -> IP 被限,不锁账号,移除 task 换代理
|
|
|
+ mock_cloud.reset_mock()
|
|
|
+ mock_inst = MagicMock()
|
|
|
+ mock_inst.config = plg_cfg
|
|
|
+ task = Task(instance=mock_inst)
|
|
|
+ gco.m_tasks.append(task)
|
|
|
+
|
|
|
+ gco._dispatch_error_policy("query", RateLimiteddError(), task=task)
|
|
|
+ mock_cloud.lock_account.assert_not_called()
|
|
|
+ self.assertNotIn(task, gco.m_tasks)
|
|
|
+ mock_inst.cleanup.assert_called_once()
|
|
|
+
|
|
|
+ # 场景 3: spawn 触发 SessionExpiredOrInvalidError
|
|
|
+ gco._dispatch_error_policy("spawn", SessionExpiredOrInvalidError(), plg_cfg=plg_cfg)
|
|
|
+ self.assertGreater(gco.m_spawn_cooldown_until, 0)
|
|
|
+
|
|
|
+ # 场景 4: 模拟 PermissionDeniedError
|
|
|
+ mock_inst2 = MagicMock()
|
|
|
+ task2 = Task(instance=mock_inst2)
|
|
|
+ gco.m_tasks.append(task2)
|
|
|
+ gco._dispatch_error_policy("query", PermissionDeniedError(), task=task2)
|
|
|
+ self.assertNotIn(task2, gco.m_tasks)
|
|
|
+
|
|
|
+ @patch("toolkit.vs_cloud_api.VSCloudApi.Instance")
|
|
|
+ def test_booker_error_dispatch(self, mock_cloud_instance):
|
|
|
+ from booker import BuiltinBookerGCO
|
|
|
+ from vs_types import GroupConfig, Task
|
|
|
+
|
|
|
+ mock_cloud = MagicMock()
|
|
|
+ mock_cloud_instance.return_value = mock_cloud
|
|
|
+
|
|
|
+ cfg = GroupConfig(identifier="test_booker")
|
|
|
+ cfg.login_backoff = 900
|
|
|
+ cfg.proxy_cd = 120
|
|
|
+ gco = BuiltinBookerGCO(cfg=cfg, redis_conf={})
|
|
|
+
|
|
|
+ mock_inst = MagicMock()
|
|
|
+ mock_inst.config = None
|
|
|
+ task = Task(instance=mock_inst)
|
|
|
+ gco.m_tasks.append(task)
|
|
|
+
|
|
|
+ # 场景 1: 模拟 booking 触发 RateLimiteddError -> 接口/IP频控,不锁账号,移除 task
|
|
|
+ err = RateLimiteddError()
|
|
|
+ gco._dispatch_error_policy("book", err, task=task)
|
|
|
+
|
|
|
+ mock_cloud.lock_account.assert_not_called()
|
|
|
+ self.assertNotIn(task, gco.m_tasks)
|
|
|
+ mock_inst.cleanup.assert_called_once()
|
|
|
+
|
|
|
+ # 场景 2: 模拟 spawn 触发 RateLimiteddError -> 账号受限,锁账号
|
|
|
+ mock_cloud.reset_mock()
|
|
|
+ plg_cfg = VSPlgConfig()
|
|
|
+ plg_cfg.account.id = 777
|
|
|
+ plg_cfg.account.username = "booker_user"
|
|
|
+ gco._dispatch_error_policy("spawn", RateLimiteddError(), plg_cfg=plg_cfg)
|
|
|
+ mock_cloud.lock_account.assert_called_once_with(777, 900)
|
|
|
+ self.assertGreater(gco.m_spawn_cooldown_until, time.time())
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ unittest.main()
|