Files
ai-trade/services/video-render/tests/test_worker.py
gahusb 2ff31b2e76 feat(render-workers): 4 render 워커 heartbeat 배선 + poll_once 카운터
- services/_shared/heartbeat.py (A1) WorkerStats/utc_now_iso/heartbeat_loop 소비
- image-render / video-render / music-render / insta-render 각 worker.py:
  stats = WorkerStats() 모듈 레벨 추가, poll_once에서 dispatch 전 busy=True,
  ack 후 jobs_done+1 / fail 후 jobs_failed+1 + last_job_at + busy=False
- 각 main.py: lifespan에 aioredis(decode_responses=False) + heartbeat_loop 태스크 spawn,
  종료 시 cancel + aclose
- 각 tests/test_worker.py: test_poll_once_increments_jobs_done 추가
  (image:flux / video:sora / music:suno / insta:_process_one mock)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019LV86jBozkNhSFXJA412fq
2026-07-01 00:52:57 +09:00

119 lines
4.1 KiB
Python

"""worker.py — job_type 디스패처 (4 provider)."""
import pytest
from unittest.mock import patch
import worker
def test_dispatch_sora_calls_run_sora_generation():
payload = {"task_id": "t1", "job_type": "sora_generation", "params": {"prompt": "x"}}
with patch("worker.run_sora_generation") as m:
worker._dispatch(payload)
m.assert_called_once_with("t1", {"prompt": "x"})
def test_dispatch_veo_calls_run_veo_generation():
payload = {"task_id": "t2", "job_type": "veo_generation", "params": {"prompt": "x"}}
with patch("worker.run_veo_generation") as m:
worker._dispatch(payload)
m.assert_called_once_with("t2", {"prompt": "x"})
def test_dispatch_kling_calls_run_kling_generation():
payload = {"task_id": "t3", "job_type": "kling_generation", "params": {"prompt": "x"}}
with patch("worker.run_kling_generation") as m:
worker._dispatch(payload)
m.assert_called_once_with("t3", {"prompt": "x"})
def test_dispatch_seedance_calls_run_seedance_generation():
payload = {"task_id": "t4", "job_type": "seedance_generation", "params": {"prompt": "x"}}
with patch("worker.run_seedance_generation") as m:
worker._dispatch(payload)
m.assert_called_once_with("t4", {"prompt": "x"})
def test_dispatch_unknown_job_type_logs_error():
payload = {"task_id": "t5", "job_type": "weird_type", "params": {}}
with patch("worker.webhook_update_task") as m:
worker._dispatch(payload)
m.assert_called_once()
args = m.call_args[0]
assert args[0] == "t5"
assert args[1] == "failed"
# ----- F6: ReliableQueue poll_once -----
import json
from unittest.mock import AsyncMock, MagicMock
@pytest.mark.asyncio
async def test_poll_once_acks_on_success(monkeypatch):
payload = {"task_id": "t1", "job_type": "sora_generation", "params": {}}
raw = json.dumps(payload).encode()
fake_queue = AsyncMock()
fake_queue.dequeue = AsyncMock(return_value=(payload, raw))
fake_queue.ack = AsyncMock()
fake_queue.fail = AsyncMock()
monkeypatch.setattr(worker, "_dispatch", MagicMock())
handled = await worker.poll_once(fake_queue)
assert handled is True
fake_queue.ack.assert_awaited_once_with(raw)
fake_queue.fail.assert_not_awaited()
@pytest.mark.asyncio
async def test_poll_once_calls_fail_on_dispatch_exception(monkeypatch):
payload = {"task_id": "t2", "job_type": "sora_generation", "params": {}}
raw = json.dumps(payload).encode()
fake_queue = AsyncMock()
fake_queue.dequeue = AsyncMock(return_value=(payload, raw))
fake_queue.ack = AsyncMock()
fake_queue.fail = AsyncMock()
def _boom(p):
raise RuntimeError("dispatch crash")
monkeypatch.setattr(worker, "_dispatch", _boom)
handled = await worker.poll_once(fake_queue)
assert handled is True
fake_queue.fail.assert_awaited_once_with(raw, payload)
fake_queue.ack.assert_not_awaited()
@pytest.mark.asyncio
async def test_poll_once_returns_false_on_timeout(monkeypatch):
fake_queue = AsyncMock()
fake_queue.dequeue = AsyncMock(return_value=None)
fake_queue.ack = AsyncMock()
fake_queue.fail = AsyncMock()
monkeypatch.setattr(worker, "_dispatch", MagicMock())
handled = await worker.poll_once(fake_queue)
assert handled is False
fake_queue.ack.assert_not_awaited()
fake_queue.fail.assert_not_awaited()
# ----- heartbeat stats 카운터 -----
class _OneJobQueue:
def __init__(self): self.acked = False
async def dequeue(self, timeout=5):
if self.acked: return None
return ({"job_type": "sora_generation", "task_id": "t1", "params": {}}, b"raw")
async def ack(self, raw): self.acked = True
async def fail(self, raw, payload): pass
@pytest.mark.asyncio
async def test_poll_once_increments_jobs_done(monkeypatch):
worker.stats.jobs_done = 0
monkeypatch.setattr(worker, "run_sora_generation", lambda task_id, params: None)
handled = await worker.poll_once(_OneJobQueue())
assert handled is True
assert worker.stats.jobs_done == 1
assert worker.stats.busy is False
assert worker.stats.last_job_at is not None