Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

test(api): avoid real waits in retry and collection tests (#42585)

Stephen Zhou6f61b91b6bed
diff --git a/api/providers/vdb/vdb-couchbase/tests/unit_tests/test_couchbase_vector.py b/api/providers/vdb/vdb-couchbase/tests/unit_tests/test_couchbase_vector.py
index 0390b75c128..f05cabc129f 100644
--- a/api/providers/vdb/vdb-couchbase/tests/unit_tests/test_couchbase_vector.py
+++ b/api/providers/vdb/vdb-couchbase/tests/unit_tests/test_couchbase_vector.py
@@ -3,7 +3,7 @@ import json
import sys
import types
from types import SimpleNamespace
-from unittest.mock import MagicMock, patch
+from unittest.mock import MagicMock, call, patch
import pytest
from pydantic import ValidationError
@@ -197,6 +197,8 @@ def test_init_sets_cluster_handles(couchbase_module):
def test_create_and_create_collection_branches(couchbase_module, monkeypatch: pytest.MonkeyPatch):
+ sleep = MagicMock()
+ monkeypatch.setattr(couchbase_module, "time", SimpleNamespace(sleep=sleep))
vector = couchbase_module.CouchbaseVector.__new__(couchbase_module.CouchbaseVector)
vector._collection_name = "collection_1"
vector._client_config = _config(couchbase_module)
@@ -243,6 +245,7 @@ def test_create_and_create_collection_branches(couchbase_module, monkeypatch: py
== 3
)
couchbase_module.redis_client.set.assert_called_once()
+ assert sleep.call_args_list == [call(2), call(1)]
def test_collection_exists_get_type_and_add_texts(couchbase_module):
diff --git a/api/providers/vdb/vdb-lindorm/tests/unit_tests/test_lindorm_vector.py b/api/providers/vdb/vdb-lindorm/tests/unit_tests/test_lindorm_vector.py
index a7e4ca76a10..3ec045036da 100644
--- a/api/providers/vdb/vdb-lindorm/tests/unit_tests/test_lindorm_vector.py
+++ b/api/providers/vdb/vdb-lindorm/tests/unit_tests/test_lindorm_vector.py
@@ -2,8 +2,9 @@ import importlib
import json
import sys
import types
+from functools import partial
from types import SimpleNamespace
-from unittest.mock import MagicMock, patch
+from unittest.mock import MagicMock, call, patch
import pytest
from pydantic import ValidationError
@@ -136,7 +137,10 @@ def test_create_refresh_and_add_texts_success(lindorm_module, monkeypatch: pytes
vector._client.indices.refresh.assert_called_once_with(index="collection")
-def test_add_texts_error_paths(lindorm_module):
+def test_add_texts_error_paths(lindorm_module, monkeypatch: pytest.MonkeyPatch):
+ sleep = MagicMock()
+ # Keep the production retry/stop/backoff rules, but do not wait in real time.
+ monkeypatch.setattr(lindorm_module, "retry", partial(lindorm_module.retry, sleep=sleep))
vector = lindorm_module.LindormVectorStore("collection", _config(lindorm_module), using_ugc=False)
vector._client.bulk.return_value = {"errors": True, "items": [{"index": {"error": "boom"}}]}
@@ -144,10 +148,18 @@ def test_add_texts_error_paths(lindorm_module):
with pytest.raises(Exception, match="RetryError"):
vector.add_texts(docs, [[0.1]], batch_size=1)
+ assert vector._client.bulk.call_count == 3
+ assert sleep.call_args_list == [call(4), call(4)]
+ vector._client.bulk.reset_mock()
+ sleep.reset_mock()
+
vector._client.bulk.side_effect = RuntimeError("bulk failed")
with pytest.raises(Exception, match="RetryError"):
vector.add_texts(docs, [[0.1]], batch_size=1)
+ assert vector._client.bulk.call_count == 3
+ assert sleep.call_args_list == [call(4), call(4)]
+
def test_metadata_lookup_and_delete_by_metadata(lindorm_module):
vector = lindorm_module.LindormVectorStore(
diff --git a/api/tests/unit_tests/core/helper/test_ssrf_proxy.py b/api/tests/unit_tests/core/helper/test_ssrf_proxy.py
index 824db04c56d..617fffd618c 100644
--- a/api/tests/unit_tests/core/helper/test_ssrf_proxy.py
+++ b/api/tests/unit_tests/core/helper/test_ssrf_proxy.py
@@ -1,5 +1,6 @@
import gzip
from collections.abc import Callable
+from types import SimpleNamespace
from typing import override
from unittest.mock import ANY, MagicMock, call, patch
@@ -7,6 +8,7 @@ import httpx
import pytest
from core.helper.ssrf_proxy import (
+ BACKOFF_FACTOR,
SSRF_DEFAULT_MAX_RETRIES,
ResponseTooLargeError,
SSRFProxy,
@@ -116,7 +118,9 @@ def test_request_can_return_an_open_stream_the_caller_closes() -> None:
@patch("core.helper.ssrf_proxy._get_ssrf_client", autospec=True)
-def test_retry_exceed_max_retries(mock_get_client):
+def test_retry_exceed_max_retries(mock_get_client, monkeypatch: pytest.MonkeyPatch):
+ sleep = MagicMock()
+ monkeypatch.setattr("core.helper.ssrf_proxy.time", SimpleNamespace(sleep=sleep))
mock_client = MagicMock()
mock_response = MagicMock()
mock_response.status_code = 500
@@ -126,6 +130,10 @@ def test_retry_exceed_max_retries(mock_get_client):
with pytest.raises(Exception) as e:
make_request("GET", "http://example.com", max_retries=SSRF_DEFAULT_MAX_RETRIES - 1)
assert str(e.value) == f"Reached maximum retries ({SSRF_DEFAULT_MAX_RETRIES - 1}) for URL http://example.com"
+ assert mock_client.send.call_count == SSRF_DEFAULT_MAX_RETRIES
+ assert sleep.call_args_list == [
+ call(BACKOFF_FACTOR * 2**attempt) for attempt in range(SSRF_DEFAULT_MAX_RETRIES - 1)
+ ]
@patch("core.helper.ssrf_proxy._get_ssrf_client", autospec=True)
diff --git a/api/tests/unit_tests/services/test_billing_service.py b/api/tests/unit_tests/services/test_billing_service.py
index 7eb979756db..eaf116bf0f2 100644
--- a/api/tests/unit_tests/services/test_billing_service.py
+++ b/api/tests/unit_tests/services/test_billing_service.py
@@ -15,10 +15,12 @@ Tests follow the Arrange-Act-Assert pattern for clarity.
import json
import logging
-from unittest.mock import MagicMock, patch
+from types import SimpleNamespace
+from unittest.mock import MagicMock, call, patch
import httpx
import pytest
+import tenacity
from werkzeug.exceptions import InternalServerError
from enums import CloudPlan
@@ -286,7 +288,24 @@ class TestBillingServiceSendRequest:
assert "Unable to process delete request" in str(exc_info.value)
assert "DELETE response" in caplog.text
- def test_retry_on_request_error(self, mock_httpx_request, mock_billing_config):
+ @pytest.fixture
+ def retry_sleep(self, monkeypatch: pytest.MonkeyPatch) -> MagicMock:
+ """Advance a virtual clock while retaining the production retry policy."""
+ elapsed = 0.0
+
+ def advance(seconds: float) -> None:
+ nonlocal elapsed
+ elapsed += seconds
+
+ sleep = MagicMock(side_effect=advance)
+ # Replace tenacity's module reference, not the process-wide time module.
+ monkeypatch.setattr(tenacity, "time", SimpleNamespace(monotonic=lambda: elapsed))
+ monkeypatch.setattr("services.billing_service.BillingService._send_request.retry.sleep", sleep)
+ return sleep
+
+ def test_retry_on_request_error(
+ self, mock_httpx_request: MagicMock, mock_billing_config: None, retry_sleep: MagicMock
+ ) -> None:
"""Test that _send_request retries on httpx.RequestError."""
# Arrange
expected_response = {"result": "success"}
@@ -306,8 +325,11 @@ class TestBillingServiceSendRequest:
# Assert
assert result == expected_response
assert mock_httpx_request.call_count == 2
+ retry_sleep.assert_called_once_with(2)
- def test_retry_exhausted_raises_exception(self, mock_httpx_request, mock_billing_config):
+ def test_retry_exhausted_raises_exception(
+ self, mock_httpx_request: MagicMock, mock_billing_config: None, retry_sleep: MagicMock
+ ) -> None:
"""Test that _send_request raises exception after retries are exhausted."""
# Arrange
mock_httpx_request.side_effect = httpx.RequestError("Network error")
@@ -316,8 +338,9 @@ class TestBillingServiceSendRequest:
with pytest.raises(httpx.RequestError):
BillingService._send_request("GET", "/test")
- # Should retry multiple times (wait=2, stop_before_delay=10 means ~5 attempts)
- assert mock_httpx_request.call_count > 1
+ # The next two-second wait would reach the ten-second stop boundary.
+ assert mock_httpx_request.call_count == 5
+ assert retry_sleep.call_args_list == [call(2)] * 4
class TestBillingServicePortalRequest: