- Postgres в compose; пакет db (SQLAlchemy async): clients, windows, requests. - Все эндпоинты, кроме /health, — только с ключом в X-API-Key, иначе 401; без выпущенных ключей доступа нет. В базе — только sha256 ключа. - Задача и окно сессии привязаны к клиенту: чужой ключ их не видит. - Воркер пишет журнал: окно (открыто, последнее обращение, закрыто) и задачу (в очереди, начало, конец, итог, данные без секретов). Сбой записи не роняет задачу. - python -m db.cli create|list|revoke|stats; make key-create/key-list/key-revoke/stats. - smoke и loadtest с ключом. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
75 lines
2.5 KiB
Python
75 lines
2.5 KiB
Python
import fakeredis
|
|
import pytest
|
|
|
|
from tasks import store as task_store
|
|
from tasks.store import Status, TaskStore
|
|
|
|
pytestmark = pytest.mark.anyio
|
|
|
|
|
|
@pytest.fixture
|
|
def store():
|
|
return TaskStore(fakeredis.aioredis.FakeRedis(decode_responses=True))
|
|
|
|
|
|
async def test_claim_once(store):
|
|
await store.create("t", "s", "https://example.com")
|
|
await store.heartbeat("w1", {})
|
|
assert (await store.get("t"))["status"] == Status.QUEUED
|
|
assert await store.claim("t", "w1")
|
|
assert not await store.claim("t", "w2")
|
|
assert not await store.cancel("t")
|
|
|
|
|
|
async def test_cancelled_task_is_not_claimed(store):
|
|
await store.create("t", "s", None)
|
|
assert await store.cancel("t")
|
|
assert not await store.claim("t", "w1")
|
|
assert (await store.get("t"))["status"] == Status.CANCELLED
|
|
|
|
|
|
async def test_missing_task_is_not_claimed(store):
|
|
assert not await store.claim("nope", "w1")
|
|
|
|
|
|
async def test_redelivery_while_running_here_is_skipped(store):
|
|
await store.create("t", "s", None)
|
|
await store.heartbeat("w1", {})
|
|
assert await store.claim("t", "w1")
|
|
assert not await store.claim("t", "w1", running_here=True)
|
|
|
|
|
|
async def test_task_of_dead_worker_is_reclaimed_limited_times(store):
|
|
await store.create("t", "s", None)
|
|
await store.heartbeat("w2", {})
|
|
assert await store.claim("t", "w1") # w1 умер — heartbeat нет
|
|
assert await store.claim("t", "w2") # attempts = 2
|
|
await store.remove_worker("w2")
|
|
assert not await store.claim("t", "w3")
|
|
task = await store.get("t")
|
|
assert task["status"] == Status.FAILED and "worker died" in task["error"]
|
|
|
|
|
|
async def test_task_of_live_worker_is_not_reclaimed(store):
|
|
await store.create("t", "s", None)
|
|
await store.heartbeat("w1", {})
|
|
assert await store.claim("t", "w1")
|
|
assert not await store.claim("t", "w2")
|
|
|
|
|
|
async def test_finish_sets_result_and_ttl(store):
|
|
await store.create("t", "s", None)
|
|
assert await store.redis.ttl("robotex:task:t") == task_store.PENDING_TTL
|
|
await store.finish("t", {"status": "ok", "actions": []})
|
|
task = await store.get("t")
|
|
assert task["status"] == Status.DONE and task["result"] == {"status": "ok", "actions": []}
|
|
assert await store.redis.ttl("robotex:task:t") == task_store.RESULT_TTL
|
|
|
|
|
|
async def test_live_window_requires_live_worker(store):
|
|
await store.set_session("s", "w1", 5, 300, client_id=7)
|
|
assert await store.live_window("s") is None
|
|
await store.heartbeat("w1", {"browser_slots": 1, "free_slots": 0})
|
|
assert await store.live_window("s") == {"worker_id": "w1", "idle_timeout": "5", "client_id": "7"}
|
|
assert await store.workers() == [{"worker_id": "w1", "browser_slots": 1, "free_slots": 0}]
|