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: use concrete parent context boundaries (#43372)

Asuka Minato64f028564e96
diff --git a/api/tests/unit_tests/core/ops/unified_trace/test_parent_context.py b/api/tests/unit_tests/core/ops/unified_trace/test_parent_context.py
index bd95b3636d5..e41b56e2dd6 100644
--- a/api/tests/unit_tests/core/ops/unified_trace/test_parent_context.py
+++ b/api/tests/unit_tests/core/ops/unified_trace/test_parent_context.py
@@ -1,6 +1,5 @@
import json
-from types import SimpleNamespace
-from unittest.mock import MagicMock
+from collections.abc import Callable
import pytest
@@ -15,11 +14,34 @@ from core.ops.unified_trace.parent_context import (
ParentDestination,
ParentResolutionKind,
ProviderParentContext,
+ RedisParentContextStore,
destination_scope,
parent_destination_from_config,
)
+class InMemoryParentContextStore(RedisParentContextStore):
+ def __init__(self, value: bytes | str | None = None) -> None:
+ self.value = value
+ self.get_calls: list[str] = []
+ self.setex_calls: list[tuple[str, int, str]] = []
+ self.read_error: Exception | None = None
+ self.write_error: Exception | None = None
+
+ def get(self, name: str) -> bytes | str | None:
+ self.get_calls.append(name)
+ if self.read_error is not None:
+ raise self.read_error
+ return self.value
+
+ def setex(self, name: str, time: int, value: str) -> object:
+ self.setex_calls.append((name, time, value))
+ if self.write_error is not None:
+ raise self.write_error
+ self.value = value
+ return True
+
+
def parent() -> ParentTraceContext:
return ParentTraceContext(parent_workflow_run_id="outer-run", parent_node_execution_id="outer-tool")
@@ -36,7 +58,7 @@ def context(**overrides: object) -> ProviderParentContext:
return ProviderParentContext.model_validate(values)
-def coordinator(redis: MagicMock, destination: ParentDestination | None) -> ParentContextCoordinator:
+def coordinator(redis: InMemoryParentContextStore, destination: ParentDestination | None) -> ParentContextCoordinator:
return ParentContextCoordinator(redis, lambda _workflow_run_id: destination)
@@ -55,25 +77,21 @@ def test_parent_destination_uses_non_secret_provider_scope() -> None:
assert "secret" not in destination.scope
-def test_publish_uses_unified_namespace_and_configured_ttl(monkeypatch: pytest.MonkeyPatch) -> None:
- redis = MagicMock()
+def test_publish_uses_unified_namespace_and_configured_ttl(config_overrides: Callable[..., None]) -> None:
+ redis = InMemoryParentContextStore()
value = context()
- monkeypatch.setattr(
- "core.ops.unified_trace.parent_context.dify_config",
- SimpleNamespace(OPS_TRACE_PARENT_CONTEXT_TTL_SECONDS=1_800),
- )
+ config_overrides(OPS_TRACE_PARENT_CONTEXT_TTL_SECONDS=1_800)
coordinator(redis, None).publish("outer-tool", value)
- key, ttl, payload = redis.setex.call_args.args
+ key, ttl, payload = redis.setex_calls[-1]
assert key == "trace:unified:parent:outer-tool"
assert ttl == 1_800
assert json.loads(payload)["provider"] == "langsmith"
def test_resolve_returns_compatible_context() -> None:
- redis = MagicMock()
- redis.get.return_value = context().model_dump_json().encode()
+ redis = InMemoryParentContextStore(context().model_dump_json().encode())
subject = coordinator(redis, ParentDestination(provider="langsmith", scope="scope-a", unified=True))
result = subject.resolve(parent(), expected_provider="langsmith", expected_scope="scope-a")
@@ -84,9 +102,13 @@ def test_resolve_returns_compatible_context() -> None:
def test_resolve_required_restores_message_context_without_destination_lookup() -> None:
- redis = MagicMock()
- redis.get.return_value = context().model_dump_json().encode()
- resolve_destination = MagicMock()
+ redis = InMemoryParentContextStore(context().model_dump_json().encode())
+ destination_calls: list[str] = []
+
+ def resolve_destination(workflow_run_id: str) -> ParentDestination | None:
+ destination_calls.append(workflow_run_id)
+ return None
+
subject = ParentContextCoordinator(redis, resolve_destination)
result = subject.resolve_required(
@@ -97,13 +119,12 @@ def test_resolve_required_restores_message_context_without_destination_lookup()
assert result.kind is ParentResolutionKind.RESTORED
assert result.context == context()
- redis.get.assert_called_once_with("trace:unified:parent:message-1")
- resolve_destination.assert_not_called()
+ assert redis.get_calls == ["trace:unified:parent:message-1"]
+ assert destination_calls == []
def test_missing_required_message_context_is_retryable() -> None:
- redis = MagicMock()
- redis.get.return_value = None
+ redis = InMemoryParentContextStore()
subject = coordinator(redis, None)
with pytest.raises(PendingTraceParentContextError):
@@ -115,8 +136,7 @@ def test_missing_required_message_context_is_retryable() -> None:
def test_missing_compatible_context_is_retryable() -> None:
- redis = MagicMock()
- redis.get.return_value = None
+ redis = InMemoryParentContextStore()
subject = coordinator(redis, ParentDestination(provider="langsmith", scope="scope-a", unified=True))
with pytest.raises(PendingTraceParentContextError):
@@ -124,7 +144,7 @@ def test_missing_compatible_context_is_retryable() -> None:
def test_non_unified_or_incompatible_parent_becomes_linked_root() -> None:
- redis = MagicMock()
+ redis = InMemoryParentContextStore()
destinations = [
None,
ParentDestination(provider="langsmith", scope="scope-a", unified=False),
@@ -139,23 +159,23 @@ def test_non_unified_or_incompatible_parent_becomes_linked_root() -> None:
assert result.kind is ParentResolutionKind.LINKED_ROOT
assert result.linked_parent == parent()
- redis.get.assert_not_called()
+ assert redis.get_calls == []
def test_malformed_or_stale_context_is_terminal() -> None:
- redis = MagicMock()
+ redis = InMemoryParentContextStore()
subject = coordinator(redis, ParentDestination(provider="langsmith", scope="scope-a", unified=True))
for payload in (b"not-json", b'{"version": 2}', context(scope="scope-b").model_dump_json().encode()):
- redis.get.return_value = payload
+ redis.value = payload
with pytest.raises(InvalidTraceParentContextError):
subject.resolve(parent(), expected_provider="langsmith", expected_scope="scope-a")
def test_redis_read_and_write_failures_are_retryable() -> None:
- redis = MagicMock()
- redis.get.side_effect = ConnectionError("down")
- redis.setex.side_effect = ConnectionError("down")
+ redis = InMemoryParentContextStore()
+ redis.read_error = ConnectionError("down")
+ redis.write_error = ConnectionError("down")
subject = coordinator(redis, ParentDestination(provider="langsmith", scope="scope-a", unified=True))
with pytest.raises(TraceParentContextAccessError):