File size: 2,394 Bytes
a9e27c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import asyncio
import threading
from unittest.mock import AsyncMock, patch

import httpx
import pytest
from anyio import to_thread

from free_claude_code.config.loader import ManagedConfigStore
from tests.api.support import create_test_app


@pytest.mark.asyncio
@pytest.mark.parametrize(
    "path", ["config", "status", "providers/lmstudio/local-status", "config/apply"]
)
async def test_admin_storage_wait_allows_health_and_authenticated_requests(path):
    app = create_test_app()
    entered, release = threading.Event(), threading.Event()
    read = ManagedConfigStore.read
    worker_limiter = to_thread.current_default_thread_limiter()
    original_tokens = worker_limiter.total_tokens

    def blocked_read(store, *args):
        entered.set()
        assert release.wait(5), "Admin blocked the event loop"
        return read(store, *args)

    async with httpx.AsyncClient(
        transport=httpx.ASGITransport(app=app, client=("127.0.0.1", 50000)),
        base_url="http://127.0.0.1",
    ) as client:

        async def health_during_read():
            while not entered.is_set():
                await asyncio.sleep(0)
            try:
                assert (await client.get("/health")).status_code == 200
                response = await asyncio.wait_for(client.get("/"), timeout=1)
                assert response.status_code == 200
            finally:
                release.set()

        observer = asyncio.create_task(health_during_read())
        try:
            # Model the last shared worker being occupied by configuration I/O.
            worker_limiter.total_tokens = 1
            with (
                patch.object(ManagedConfigStore, "read", blocked_read),
                patch(
                    "free_claude_code.api.admin_routes._check_local_provider",
                    AsyncMock(return_value={}),
                ),
            ):
                if path == "config/apply":
                    response = await client.post(
                        f"/admin/api/{path}", json={"values": {"PORT": None}}
                    )
                else:
                    response = await client.get(f"/admin/api/{path}")
                assert response.status_code == 200
            await asyncio.wait_for(observer, 5)
        finally:
            release.set()
            observer.cancel()
            worker_limiter.total_tokens = original_tokens