This commit is contained in:
Your Name
2026-04-22 14:13:21 +08:00
parent e0406b5d0e
commit 7cbde2aa78
145 changed files with 23086 additions and 2243 deletions

View File

@@ -2,7 +2,10 @@ import unittest
from unittest.mock import patch
from app.services.sync_push_service import (
_build_task_pull_backlog_limits,
_extract_detect_result_projection_events,
_resolve_detect_result_target_job_id,
_should_throttle_task_pull,
ingest_runtime_projection,
)
@@ -46,6 +49,61 @@ class _FakeConnection:
class SyncPushServiceTests(unittest.TestCase):
def test_build_task_pull_backlog_limits_scales_with_thread_configuration(self) -> None:
limits = _build_task_pull_backlog_limits(
5000,
settings_payload={
"thread_count": 100,
"node_thread_counts": {
"mainland-controller-01": 2000,
"mainland-worker-01": 1200,
},
},
)
self.assertEqual(3200, limits["estimated_total_threads"])
self.assertEqual(6400, limits["max_pending_total"])
self.assertEqual(3200, limits["max_register_pending"])
self.assertEqual(800, limits["max_downstream_pending"])
def test_should_throttle_task_pull_when_register_backlog_overwhelms_downstream(self) -> None:
should_throttle, reason = _should_throttle_task_pull(
{
"pending_total": 9438,
"claimed_total": 410,
"running_total": 15,
"register_pending": 8487,
"downstream_pending": 951,
},
{
"max_pending_total": 12000,
"max_register_pending": 3200,
"max_downstream_pending": 800,
},
)
self.assertTrue(should_throttle)
self.assertEqual("register_pending", reason)
def test_should_not_throttle_task_pull_when_backlog_is_within_limits(self) -> None:
should_throttle, reason = _should_throttle_task_pull(
{
"pending_total": 1800,
"claimed_total": 100,
"running_total": 300,
"register_pending": 600,
"downstream_pending": 120,
},
{
"max_pending_total": 6400,
"max_register_pending": 3200,
"max_downstream_pending": 800,
},
)
self.assertFalse(should_throttle)
self.assertEqual("", reason)
def test_extract_detect_result_projection_events_adds_import_metadata(self) -> None:
projection = {
"job": {
@@ -79,6 +137,43 @@ class SyncPushServiceTests(unittest.TestCase):
self.assertTrue(event["payload"]["imported_from_projection"])
self.assertTrue(event["payload"]["import_fingerprint"])
@patch("app.services.sync_push_service.get_db")
def test_resolve_detect_result_target_job_id_prefers_matching_job_code(self, mock_get_db) -> None:
fake_conn = _FakeConnection(rows=[(456,)])
mock_get_db.return_value = fake_conn
target_job_id = _resolve_detect_result_target_job_id(
projection={
"job": {
"job_code": "sync-mainland-123",
}
}
)
self.assertEqual(456, target_job_id)
self.assertIn("WHERE job_code = %s", fake_conn.cursor_obj.executed[0][0])
@patch("app.services.detect_job_service.get_active_detect_job_summary")
@patch("app.services.sync_push_service.get_db")
def test_resolve_detect_result_target_job_id_falls_back_to_active_job(
self,
mock_get_db,
mock_get_active_detect_job_summary,
) -> None:
fake_conn = _FakeConnection(rows=[None])
mock_get_db.return_value = fake_conn
mock_get_active_detect_job_summary.return_value = {"job_id": 789}
target_job_id = _resolve_detect_result_target_job_id(
projection={
"job": {
"job_code": "sync-mainland-999",
}
}
)
self.assertEqual(789, target_job_id)
@patch("app.services.sync_push_service._import_detect_result_projection_events")
@patch("app.services.sync_push_service.get_db")
def test_ingest_runtime_projection_imports_detect_result_events_on_new_record(