all repos — rastro @ 6533c8851b4a2970689b2081579d5f63f5136a58

BitTorrent tracker!

tests/unit/test_http_scheduler.py (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
"""HTTP size limit and scheduler lock behaviour."""

from __future__ import annotations

from unittest.mock import AsyncMock, patch

import httpx
import pytest
import respx
from app.probes.http import probe_http
from app.scheduler import _locks, job_import_sources


@pytest.mark.asyncio
@respx.mock
async def test_http_response_size_limit():
    url = "http://tracker.example:80/announce"
    big = b"x" * 2000

    def _handler(request: httpx.Request) -> httpx.Response:
        return httpx.Response(200, content=big)

    respx.route(method="GET", host="tracker.example").mock(side_effect=_handler)
    async with httpx.AsyncClient() as client:
        outcome = await probe_http(
            url,
            client=client,
            max_bytes=1000,
            resolved_ips=["1.1.1.1"],
        )
    assert outcome.response_valid is False
    assert outcome.error_kind == "too_large"


@pytest.mark.asyncio
async def test_scheduler_skips_overlapping_import():
    # Hold the import lock and ensure job returns immediately without work
    assert not _locks["import"].locked()
    await _locks["import"].acquire()
    try:
        with patch("app.scheduler.import_all_sources", new_callable=AsyncMock) as mocked:
            await job_import_sources()
            mocked.assert_not_called()
    finally:
        _locks["import"].release()