mirror of
https://github.com/langgenius/dify.git
synced 2026-10-04 13:47:55 +08:00
test: centralize app and account config overrides (#40860)
This commit is contained in:
1 parent
bc845ea748
commit
f3bb73b671
11 files changed
+273
-256
No files matched your search
@@ -115,8 +115,10 @@ def _unexpected_timer(interval: float, function: Callable[[], bool]) -> _FakeTim
|
||||
|
||||
|
||||
class TestBuildStreamingTaskOnSubscribe:
|
||||
def test_streams_starts_only_when_hook_is_invoked_without_creating_timer(self, monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.setattr(ags_module.dify_config, "PUBSUB_REDIS_CHANNEL_TYPE", "streams")
|
||||
def test_streams_starts_only_when_hook_is_invoked_without_creating_timer(
|
||||
self, monkeypatch: pytest.MonkeyPatch, config_overrides: Callable[..., None]
|
||||
):
|
||||
config_overrides(PUBSUB_REDIS_CHANNEL_TYPE="streams")
|
||||
|
||||
monkeypatch.setattr(ags_module.threading, "Timer", _unexpected_timer)
|
||||
called: list[int] = []
|
||||
@@ -133,8 +135,9 @@ class TestBuildStreamingTaskOnSubscribe:
|
||||
self,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
channel_type: str,
|
||||
config_overrides: Callable[..., None],
|
||||
):
|
||||
monkeypatch.setattr(ags_module.dify_config, "PUBSUB_REDIS_CHANNEL_TYPE", channel_type)
|
||||
config_overrides(PUBSUB_REDIS_CHANNEL_TYPE=channel_type)
|
||||
timers: list[_FakeTimer] = []
|
||||
|
||||
def build_timer(interval: float, function: Callable[[], bool]) -> _FakeTimer:
|
||||
@@ -157,8 +160,10 @@ class TestBuildStreamingTaskOnSubscribe:
|
||||
assert called == [1]
|
||||
assert timers[0].cancelled is True
|
||||
|
||||
def test_pubsub_fallback_starts_task_if_hook_is_never_invoked(self, monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.setattr(ags_module.dify_config, "PUBSUB_REDIS_CHANNEL_TYPE", "pubsub")
|
||||
def test_pubsub_fallback_starts_task_if_hook_is_never_invoked(
|
||||
self, monkeypatch: pytest.MonkeyPatch, config_overrides: Callable[..., None]
|
||||
):
|
||||
config_overrides(PUBSUB_REDIS_CHANNEL_TYPE="pubsub")
|
||||
timers: list[_FakeTimer] = []
|
||||
|
||||
def build_timer(interval: float, function: Callable[[], bool]) -> _FakeTimer:
|
||||
@@ -174,8 +179,10 @@ class TestBuildStreamingTaskOnSubscribe:
|
||||
on_subscribe()
|
||||
assert called == [1]
|
||||
|
||||
def test_streams_retries_after_enqueue_failure(self, monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.setattr(ags_module.dify_config, "PUBSUB_REDIS_CHANNEL_TYPE", "streams")
|
||||
def test_streams_retries_after_enqueue_failure(
|
||||
self, monkeypatch: pytest.MonkeyPatch, config_overrides: Callable[..., None]
|
||||
):
|
||||
config_overrides(PUBSUB_REDIS_CHANNEL_TYPE="streams")
|
||||
monkeypatch.setattr(ags_module.threading, "Timer", _unexpected_timer)
|
||||
call_count = 0
|
||||
|
||||
@@ -191,8 +198,10 @@ class TestBuildStreamingTaskOnSubscribe:
|
||||
on_subscribe()
|
||||
assert call_count == 2
|
||||
|
||||
def test_concurrent_subscribe_only_starts_once(self, monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.setattr(ags_module.dify_config, "PUBSUB_REDIS_CHANNEL_TYPE", "streams")
|
||||
def test_concurrent_subscribe_only_starts_once(
|
||||
self, monkeypatch: pytest.MonkeyPatch, config_overrides: Callable[..., None]
|
||||
):
|
||||
config_overrides(PUBSUB_REDIS_CHANNEL_TYPE="streams")
|
||||
monkeypatch.setattr(ags_module.threading, "Timer", _unexpected_timer)
|
||||
call_count = 0
|
||||
|
||||
@@ -213,33 +222,28 @@ class TestBuildStreamingTaskOnSubscribe:
|
||||
# _get_max_active_requests
|
||||
# ---------------------------------------------------------------------------
|
||||
class TestGetMaxActiveRequests:
|
||||
def test_both_zero_returns_zero(self, monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.setattr(ags_module.dify_config, "APP_MAX_ACTIVE_REQUESTS", 0)
|
||||
monkeypatch.setattr(ags_module.dify_config, "APP_DEFAULT_ACTIVE_REQUESTS", 0)
|
||||
def test_both_zero_returns_zero(self, config_overrides: Callable[..., None]):
|
||||
config_overrides(APP_MAX_ACTIVE_REQUESTS=0, APP_DEFAULT_ACTIVE_REQUESTS=0)
|
||||
app = _make_app(AppMode.CHAT, max_active_requests=0)
|
||||
assert AppGenerateService._get_max_active_requests(app) == 0
|
||||
|
||||
def test_app_limit_only(self, monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.setattr(ags_module.dify_config, "APP_MAX_ACTIVE_REQUESTS", 0)
|
||||
monkeypatch.setattr(ags_module.dify_config, "APP_DEFAULT_ACTIVE_REQUESTS", 0)
|
||||
def test_app_limit_only(self, config_overrides: Callable[..., None]):
|
||||
config_overrides(APP_MAX_ACTIVE_REQUESTS=0, APP_DEFAULT_ACTIVE_REQUESTS=0)
|
||||
app = _make_app(AppMode.CHAT, max_active_requests=5)
|
||||
assert AppGenerateService._get_max_active_requests(app) == 5
|
||||
|
||||
def test_config_limit_only(self, monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.setattr(ags_module.dify_config, "APP_MAX_ACTIVE_REQUESTS", 10)
|
||||
monkeypatch.setattr(ags_module.dify_config, "APP_DEFAULT_ACTIVE_REQUESTS", 0)
|
||||
def test_config_limit_only(self, config_overrides: Callable[..., None]):
|
||||
config_overrides(APP_MAX_ACTIVE_REQUESTS=10, APP_DEFAULT_ACTIVE_REQUESTS=0)
|
||||
app = _make_app(AppMode.CHAT, max_active_requests=0)
|
||||
assert AppGenerateService._get_max_active_requests(app) == 10
|
||||
|
||||
def test_both_non_zero_returns_min(self, monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.setattr(ags_module.dify_config, "APP_MAX_ACTIVE_REQUESTS", 20)
|
||||
monkeypatch.setattr(ags_module.dify_config, "APP_DEFAULT_ACTIVE_REQUESTS", 0)
|
||||
def test_both_non_zero_returns_min(self, config_overrides: Callable[..., None]):
|
||||
config_overrides(APP_MAX_ACTIVE_REQUESTS=20, APP_DEFAULT_ACTIVE_REQUESTS=0)
|
||||
app = _make_app(AppMode.CHAT, max_active_requests=5)
|
||||
assert AppGenerateService._get_max_active_requests(app) == 5
|
||||
|
||||
def test_default_active_requests_used_when_app_has_none(self, monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.setattr(ags_module.dify_config, "APP_MAX_ACTIVE_REQUESTS", 0)
|
||||
monkeypatch.setattr(ags_module.dify_config, "APP_DEFAULT_ACTIVE_REQUESTS", 15)
|
||||
def test_default_active_requests_used_when_app_has_none(self, config_overrides: Callable[..., None]):
|
||||
config_overrides(APP_MAX_ACTIVE_REQUESTS=0, APP_DEFAULT_ACTIVE_REQUESTS=15)
|
||||
app = _make_app(AppMode.CHAT, max_active_requests=0)
|
||||
assert AppGenerateService._get_max_active_requests(app) == 15
|
||||
|
||||
@@ -251,8 +255,8 @@ class TestGenerate:
|
||||
"""Tests for AppGenerateService.generate covering each mode."""
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _common(self, mocker: MockerFixture, monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.setattr(ags_module.dify_config, "DEPLOYMENT_EDITION", DeploymentEdition.COMMUNITY)
|
||||
def _common(self, mocker: MockerFixture, config_overrides: Callable[..., None]):
|
||||
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.COMMUNITY)
|
||||
mocker.patch("services.app_generate_service.RateLimit", _DummyRateLimit)
|
||||
# Prevent AppExecutionParams.new from touching real models via isinstance
|
||||
mocker.patch(
|
||||
@@ -403,7 +407,8 @@ class TestGenerate:
|
||||
retrieve_spy.assert_not_called()
|
||||
|
||||
# -- ADVANCED_CHAT streaming --------------------------------------------
|
||||
def test_advanced_chat_streaming(self, mocker: MockerFixture):
|
||||
def test_advanced_chat_streaming(self, mocker: MockerFixture, config_overrides: Callable[..., None]):
|
||||
config_overrides(PUBSUB_REDIS_CHANNEL_TYPE="streams")
|
||||
workflow = _make_workflow()
|
||||
mocker.patch.object(AppGenerateService, "_get_workflow", return_value=workflow)
|
||||
mocker.patch(
|
||||
@@ -463,7 +468,8 @@ class TestGenerate:
|
||||
assert call_kwargs["pause_state_config"].state_owner_user_id == "owner-id"
|
||||
|
||||
# -- WORKFLOW streaming -------------------------------------------------
|
||||
def test_workflow_streaming(self, mocker: MockerFixture):
|
||||
def test_workflow_streaming(self, mocker: MockerFixture, config_overrides: Callable[..., None]):
|
||||
config_overrides(PUBSUB_REDIS_CHANNEL_TYPE="streams")
|
||||
workflow = _make_workflow()
|
||||
mocker.patch.object(AppGenerateService, "_get_workflow", return_value=workflow)
|
||||
mocker.patch(
|
||||
@@ -514,15 +520,15 @@ class TestGenerate:
|
||||
# ---------------------------------------------------------------------------
|
||||
class TestGenerateBilling:
|
||||
@pytest.fixture(autouse=True)
|
||||
def _common(self, mocker: MockerFixture, monkeypatch: pytest.MonkeyPatch):
|
||||
def _common(self, mocker: MockerFixture):
|
||||
mocker.patch("services.app_generate_service.RateLimit", _DummyRateLimit)
|
||||
mocker.patch(
|
||||
"services.app_generate_service.rate_limit_context",
|
||||
_noop_rate_limit_context,
|
||||
)
|
||||
|
||||
def test_cloud_edition_consumes_quota(self, mocker: MockerFixture, monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.setattr(ags_module.dify_config, "DEPLOYMENT_EDITION", DeploymentEdition.CLOUD)
|
||||
def test_cloud_edition_consumes_quota(self, mocker: MockerFixture, config_overrides: Callable[..., None]):
|
||||
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.CLOUD)
|
||||
quota_charge = MagicMock()
|
||||
reserve_mock = mocker.patch(
|
||||
"services.app_generate_service.QuotaService.reserve",
|
||||
@@ -549,12 +555,12 @@ class TestGenerateBilling:
|
||||
quota_charge.commit.assert_called_once()
|
||||
|
||||
def test_billing_quota_exceeded_raises_rate_limit_error(
|
||||
self, mocker: MockerFixture, monkeypatch: pytest.MonkeyPatch
|
||||
self, mocker: MockerFixture, config_overrides: Callable[..., None]
|
||||
):
|
||||
from services.errors.app import QuotaExceededError
|
||||
from services.errors.llm import InvokeRateLimitError
|
||||
|
||||
monkeypatch.setattr(ags_module.dify_config, "DEPLOYMENT_EDITION", DeploymentEdition.CLOUD)
|
||||
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.CLOUD)
|
||||
mocker.patch(
|
||||
"services.app_generate_service.QuotaService.reserve",
|
||||
side_effect=QuotaExceededError(feature="workflow", tenant_id="t", required=1),
|
||||
@@ -570,8 +576,10 @@ class TestGenerateBilling:
|
||||
session=MagicMock(),
|
||||
)
|
||||
|
||||
def test_exception_refunds_quota_and_exits_rate_limit(self, mocker: MockerFixture, monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.setattr(ags_module.dify_config, "DEPLOYMENT_EDITION", DeploymentEdition.CLOUD)
|
||||
def test_exception_refunds_quota_and_exits_rate_limit(
|
||||
self, mocker: MockerFixture, config_overrides: Callable[..., None]
|
||||
):
|
||||
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.CLOUD)
|
||||
quota_charge = MagicMock()
|
||||
mocker.patch(
|
||||
"services.app_generate_service.QuotaService.reserve",
|
||||
@@ -598,10 +606,10 @@ class TestGenerateBilling:
|
||||
quota_charge.refund.assert_called_once()
|
||||
|
||||
def test_rate_limit_exit_called_in_finally_for_blocking(
|
||||
self, mocker: MockerFixture, monkeypatch: pytest.MonkeyPatch
|
||||
self, mocker: MockerFixture, config_overrides: Callable[..., None]
|
||||
):
|
||||
"""For non-streaming (blocking) calls, rate_limit.exit should be called in finally."""
|
||||
monkeypatch.setattr(ags_module.dify_config, "DEPLOYMENT_EDITION", DeploymentEdition.COMMUNITY)
|
||||
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.COMMUNITY)
|
||||
|
||||
exit_calls: list[str] = []
|
||||
|
||||
@@ -630,8 +638,8 @@ class TestGenerateBilling:
|
||||
# exit is called in finally block for non-streaming
|
||||
assert exit_calls == ["dummy-request-id"]
|
||||
|
||||
def test_blocking_failure_exits_rate_limit_once(self, mocker: MockerFixture, monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.setattr(ags_module.dify_config, "DEPLOYMENT_EDITION", DeploymentEdition.CLOUD)
|
||||
def test_blocking_failure_exits_rate_limit_once(self, mocker: MockerFixture, config_overrides: Callable[..., None]):
|
||||
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.CLOUD)
|
||||
quota_charge = MagicMock()
|
||||
mocker.patch(
|
||||
"services.app_generate_service.QuotaService.reserve",
|
||||
@@ -662,8 +670,10 @@ class TestGenerateBilling:
|
||||
quota_charge.refund.assert_called_once()
|
||||
assert exit_calls == ["dummy-request-id"]
|
||||
|
||||
def test_streaming_failure_exits_rate_limit_once(self, mocker: MockerFixture, monkeypatch: pytest.MonkeyPatch):
|
||||
monkeypatch.setattr(ags_module.dify_config, "DEPLOYMENT_EDITION", DeploymentEdition.CLOUD)
|
||||
def test_streaming_failure_exits_rate_limit_once(
|
||||
self, mocker: MockerFixture, config_overrides: Callable[..., None]
|
||||
):
|
||||
config_overrides(DEPLOYMENT_EDITION=DeploymentEdition.CLOUD)
|
||||
quota_charge = MagicMock()
|
||||
mocker.patch(
|
||||
"services.app_generate_service.QuotaService.reserve",
|
||||
|
||||
Reference in new issue
Block a user