Files
inbraille/tests/test_job_queue.py
T
Lucas RayanandClaude Sonnet 5.5 a9aec5abe4
Release / Plano (reaproveitar a imagem?) (push) Successful in 8s
CI / Trivy (dependências, segredos e configuração) (push) Failing after 8s
Release / ci (push) Failing after 9s
CI / Testes e imagem Docker (linux/arm64) (push) Skipped
Release / Publica a imagem (linux/arm64) (push) Skipped
Release / Cria a release no Gitea (push) Skipped
Recover InBraille from the production image (1.3.1) and move it to Gitea
The GitHub account was suspended and the repository had no backup. The deployed code
(src/, public/, index.html, main.py, requirements.txt) was extracted from the production
image 1.3.1. The tests, Dockerfile, compose files, SECURITY.md, CHANGELOG.md and README were
rebuilt from the records of the work sessions that created them; LICENSE, .gitignore,
robots.txt and sitemap.xml were recreated. CI now runs on Gitea Actions.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-02 00:15:01 -03:00

88 lines
2.8 KiB
Python

import asyncio
import pytest
from src.web.job_queue import JobQueue, QueueFull, QueueTimeout
async def _run(queue, client, order, name, hold):
async with queue.slot(client):
order.append(name)
await asyncio.sleep(hold)
def test_jobs_run_in_arrival_order_and_none_is_lost():
async def scenario():
queue = JobQueue(concurrency=1, max_waiting=10, max_per_client=10)
order = []
tasks = []
for i in range(5):
tasks.append(asyncio.create_task(_run(queue, f"c{i}", order, i, 0.01)))
await asyncio.sleep(0)
await asyncio.gather(*tasks)
return order, queue.waiting
order, waiting = asyncio.run(scenario())
assert order == [0, 1, 2, 3, 4]
assert waiting == 0
def test_concurrency_is_respected():
async def scenario():
queue = JobQueue(concurrency=2, max_waiting=10, max_per_client=10)
running = peak = 0
async def job(i):
nonlocal running, peak
async with queue.slot(f"c{i}"):
running += 1
peak = max(peak, running)
await asyncio.sleep(0.02)
running -= 1
await asyncio.gather(*(job(i) for i in range(6)))
return peak
assert asyncio.run(scenario()) == 2
def test_queue_full_and_per_client_cap():
async def scenario():
queue = JobQueue(concurrency=1, max_waiting=2, max_per_client=1)
order = []
first = asyncio.create_task(_run(queue, "a", order, "a", 0.05))
await asyncio.sleep(0)
second = asyncio.create_task(_run(queue, "b", order, "b", 0.0))
await asyncio.sleep(0)
with pytest.raises(QueueFull): # client "b" already holds a place in line
async with queue.slot("b"):
pass
third = asyncio.create_task(_run(queue, "c", order, "c", 0.0))
await asyncio.sleep(0)
with pytest.raises(QueueFull): # the queue itself is full
async with queue.slot("d"):
pass
await asyncio.gather(first, second, third)
return order
assert asyncio.run(scenario()) == ["a", "b", "c"]
def test_wait_timeout_and_cancellation_free_the_place():
async def scenario():
queue = JobQueue(concurrency=1, max_waiting=5, max_per_client=5, max_wait_seconds=0.05)
holder = asyncio.create_task(_run(queue, "a", [], "a", 0.3))
await asyncio.sleep(0)
with pytest.raises(QueueTimeout):
async with queue.slot("b"):
pass
waiter = asyncio.create_task(_run(queue, "c", [], "c", 0))
await asyncio.sleep(0.01)
waiter.cancel()
with pytest.raises(asyncio.CancelledError):
await waiter
assert queue.waiting == 0
await holder
asyncio.run(scenario())