Files
privateGPT/tests/components/streaming/test_chat_scheduler.py
Javier Martinez cd8ca2214a feat: resumable chat worker + tool worker + async tokenizer (#2298)
* fix: avoid to block the loop

* fix: blocks in expansion

* fix: remove maximum concurrent users

...

* fix: multiplexer

* fix: readers

* fix: more fixes

...

* fix: impl

* feat: tool scheduler

* feat: add adaptative

* feat: add chat worker

* fix: max

* feat: add chat/tools workers

* fix: mypy

* feat: add generic scheduler

* fix: get result

* feat: do serializable the tool executor

* fix: tools

* fix: config

* fix: config

* fix: args

* fix: config

* fix: serializer

* Revert "fix: blocks in expansion"

This reverts commit a2110f94a8.

* fix: unify all logic

* feat: add ingestion scheduler

* fix: settings

* fix: config

* feat: add arq worker to chat

* fix: arq worker

* fix: add nest

* fix: mypy

* fix: await

* fix: script stress

* fix: tokenizer

* fix: chat scheduler

* fix: mypy

* fix: add async tokenizer

* fix: improve condense

* fix: tool scheduler

* feat: add initial real async chat worker

* fix: mypy

* fix: do resumable local executor

...

...

...

fix: revert usleess changes

fix: remove parent chat job

fix: refactor

fix: loop

ref: rename models

fix: chat engine

fix: mypy

...

...

...

fix: fix deps

* fix: tests

* fix: tests

* ...

* fix: stream

* fix: config

* fix: scheduler

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Handle PGPT_WORKER_MODE=celery in health check worker status

* fix: cancel

* fix: arch

* fix: test ingestion

* fix: deserialization of chat messages

* fix: broken results

* fix: mypy

* fix: test

* fix: config

* fix: remove arq tool worker

* fix: output cls

* fix: preserve early resumable tool callbacks

* fix: preserve async tool result order

* refactor: address worker PR review comments

* fix: mypy

* test: colocate ARQ chat enqueue coverage

* fix: remove redis from tests

* test: isolate chat mocks and cancellation timing

* fix: tests

(cherry picked from commit 218b599c66)

# Conflicts:
#	tests/server/chat/anthropic/test_anthropic_client.py
#	tests/server/chat/anthropic/test_langchain_anthropic.py
#	tests/server/chat/test_chat_knowledge_revamp.py
#	tests/server/chat/test_chat_routes.py
#	tests/server/chat/test_chat_routes_skills_integration.py

* fix: tests

(cherry picked from commit fc5ec0f72a)

* fix: ruff

* fix: test

* fix: worker config

(cherry picked from commit 1371c275a1)

* fix: principal

* test: remove flaky chat cancellation assertion

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
2026-07-15 12:12:45 +02:00

88 lines
2.4 KiB
Python

import asyncio
from types import SimpleNamespace
from unittest.mock import AsyncMock
import pytest
from injector import Injector
from private_gpt.components.streaming.tasks.chat_scheduler import (
ArqChatScheduler,
ChatSchedulerFactory,
LocalChatScheduler,
)
@pytest.fixture
def injector() -> Injector:
return Injector()
@pytest.mark.anyio
async def test_arq_chat_scheduler_cancel_aborts_job(
monkeypatch: pytest.MonkeyPatch,
) -> None:
abort_chat_job = AsyncMock(return_value=True)
monkeypatch.setattr(
"private_gpt.components.streaming.tasks.chat_scheduler.abort_chat_job",
abort_chat_job,
)
scheduler = ArqChatScheduler()
cancelled = await scheduler.cancel("msg-arq-3")
assert cancelled is True
abort_chat_job.assert_awaited_once_with(correlation_id="msg-arq-3")
@pytest.mark.anyio
async def test_local_chat_scheduler_cancel_cancels_task() -> None:
async def _work() -> None:
await asyncio.sleep(100)
task = asyncio.create_task(_work(), name="chat_msg-local-2")
scheduler = LocalChatScheduler()
cancelled = await scheduler.cancel("msg-local-2")
assert cancelled is True
task.cancel()
with pytest.raises(asyncio.CancelledError):
await task
@pytest.mark.anyio
async def test_local_chat_scheduler_cancel_returns_false_when_no_task() -> None:
scheduler = LocalChatScheduler()
cancelled = await scheduler.cancel("nonexistent-msg")
assert cancelled is False
def test_chat_scheduler_factory_selects_local_mode(injector: Injector) -> None:
factory = ChatSchedulerFactory(
settings=SimpleNamespace(
scheduler=SimpleNamespace(chat=SimpleNamespace(mode="local"))
),
injector=injector,
)
assert isinstance(factory.get(), LocalChatScheduler)
def test_chat_scheduler_factory_selects_arq_mode(injector: Injector) -> None:
factory = ChatSchedulerFactory(
settings=SimpleNamespace(
scheduler=SimpleNamespace(chat=SimpleNamespace(mode="arq"))
),
injector=injector,
)
assert isinstance(factory.get(), ArqChatScheduler)
def test_chat_scheduler_factory_raises_on_unknown_mode(injector: Injector) -> None:
with pytest.raises(ValueError, match=r"Unknown scheduler\.chat\.mode"):
ChatSchedulerFactory(
settings=SimpleNamespace(
scheduler=SimpleNamespace(chat=SimpleNamespace(mode="missing"))
),
injector=injector,
)