Skip to content

Commit 8021fb5

Browse files
committed
test(workflows): add regression coverage for async probe edge cases (#9642)
1 parent 4935175 commit 8021fb5

1 file changed

Lines changed: 175 additions & 0 deletions

File tree

‎python/packages/core/tests/workflow/test_edge.py‎

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,4 +1866,179 @@ async def async_selector(data: Any, available_targets: list[str]) -> list[str]:
18661866
assert mock_send.call_args[0][0] == target1.id
18671867

18681868

1869+
async def test_switch_case_edge_group_async_condition_future_and_custom_awaitable() -> None:
1870+
"""Async condition results from asyncio.Future and custom awaitable objects are properly awaited."""
1871+
source = MockExecutor(id="source_executor")
1872+
target1 = MockExecutor(id="target_executor_1")
1873+
target2 = MockExecutor(id="target_executor_2")
1874+
target3 = MockExecutor(id="target_executor_3")
1875+
1876+
class CustomAwaitableBool:
1877+
def __init__(self, value: bool) -> None:
1878+
self.value = value
1879+
1880+
def __await__(self):
1881+
async def _coro() -> bool:
1882+
await asyncio.sleep(0)
1883+
return self.value
1884+
1885+
return _coro().__await__()
1886+
1887+
def future_condition(message: MockMessage) -> asyncio.Future[bool]:
1888+
loop = asyncio.get_running_loop()
1889+
fut: asyncio.Future[bool] = loop.create_future()
1890+
fut.set_result(message.data == 100)
1891+
return fut
1892+
1893+
def custom_awaitable_condition(message: MockMessage) -> CustomAwaitableBool:
1894+
return CustomAwaitableBool(message.data == 200)
1895+
1896+
edge_group = SwitchCaseEdgeGroup(
1897+
source_id=source.id,
1898+
cases=[
1899+
SwitchCaseEdgeGroupCase(condition=future_condition, target_id=target1.id),
1900+
SwitchCaseEdgeGroupCase(condition=custom_awaitable_condition, target_id=target2.id),
1901+
SwitchCaseEdgeGroupDefault(target_id=target3.id),
1902+
],
1903+
)
1904+
executors: dict[str, Executor] = {
1905+
source.id: source,
1906+
target1.id: target1,
1907+
target2.id: target2,
1908+
target3.id: target3,
1909+
}
1910+
edge_runner = create_edge_runner(edge_group, executors)
1911+
state = State()
1912+
ctx = InProcRunnerContext()
1913+
1914+
# Matches Future condition
1915+
with patch("agent_framework._workflows._edge_runner.EdgeRunner._execute_on_target") as mock_send:
1916+
await edge_runner.send_message(WorkflowMessage(data=MockMessage(data=100), source_id=source.id), state, ctx)
1917+
assert mock_send.call_args[0][0] == target1.id
1918+
1919+
# Matches Custom awaitable condition
1920+
with patch("agent_framework._workflows._edge_runner.EdgeRunner._execute_on_target") as mock_send:
1921+
await edge_runner.send_message(WorkflowMessage(data=MockMessage(data=200), source_id=source.id), state, ctx)
1922+
assert mock_send.call_args[0][0] == target2.id
1923+
1924+
# Falls through to default when neither matches
1925+
with patch("agent_framework._workflows._edge_runner.EdgeRunner._execute_on_target") as mock_send:
1926+
await edge_runner.send_message(WorkflowMessage(data=MockMessage(data=0), source_id=source.id), state, ctx)
1927+
assert mock_send.call_args[0][0] == target3.id
1928+
1929+
1930+
async def test_switch_case_edge_group_sync_match_skips_later_async_predicate() -> None:
1931+
"""A synchronous match on an earlier case skips evaluating later async predicates."""
1932+
source = MockExecutor(id="source_executor")
1933+
target1 = MockExecutor(id="target_executor_1")
1934+
target2 = MockExecutor(id="target_executor_2")
1935+
target3 = MockExecutor(id="target_executor_3")
1936+
1937+
async_called = False
1938+
1939+
async def bomb_condition(message: MockMessage) -> bool:
1940+
nonlocal async_called
1941+
async_called = True
1942+
raise RuntimeError("Should never be called")
1943+
1944+
edge_group = SwitchCaseEdgeGroup(
1945+
source_id=source.id,
1946+
cases=[
1947+
SwitchCaseEdgeGroupCase(condition=lambda msg: msg.data == 1, target_id=target1.id),
1948+
SwitchCaseEdgeGroupCase(condition=bomb_condition, target_id=target2.id),
1949+
SwitchCaseEdgeGroupDefault(target_id=target3.id),
1950+
],
1951+
)
1952+
executors: dict[str, Executor] = {
1953+
source.id: source,
1954+
target1.id: target1,
1955+
target2.id: target2,
1956+
target3.id: target3,
1957+
}
1958+
edge_runner = create_edge_runner(edge_group, executors)
1959+
1960+
state = State()
1961+
ctx = InProcRunnerContext()
1962+
1963+
with patch("agent_framework._workflows._edge_runner.EdgeRunner._execute_on_target") as mock_send:
1964+
success = await edge_runner.send_message(
1965+
WorkflowMessage(data=MockMessage(data=1), source_id=source.id), state, ctx
1966+
)
1967+
assert success is True
1968+
assert mock_send.call_args[0][0] == target1.id
1969+
assert async_called is False
1970+
1971+
1972+
async def test_switch_case_edge_group_async_condition_cancellation() -> None:
1973+
"""Cancellation during async condition evaluation propagates CancelledError and skips next cases."""
1974+
source = MockExecutor(id="source_executor")
1975+
target1 = MockExecutor(id="target_executor_1")
1976+
target2 = MockExecutor(id="target_executor_2")
1977+
target3 = MockExecutor(id="target_executor_3")
1978+
1979+
case2_called = False
1980+
1981+
async def cancelling_condition(message: MockMessage) -> bool:
1982+
task = asyncio.current_task()
1983+
if task:
1984+
task.cancel()
1985+
await asyncio.sleep(0.01)
1986+
return False
1987+
1988+
def case2_condition(message: MockMessage) -> bool:
1989+
nonlocal case2_called
1990+
case2_called = True
1991+
return True
1992+
1993+
edge_group = SwitchCaseEdgeGroup(
1994+
source_id=source.id,
1995+
cases=[
1996+
SwitchCaseEdgeGroupCase(condition=cancelling_condition, target_id=target1.id),
1997+
SwitchCaseEdgeGroupCase(condition=case2_condition, target_id=target2.id),
1998+
SwitchCaseEdgeGroupDefault(target_id=target3.id),
1999+
],
2000+
)
2001+
executors: dict[str, Executor] = {
2002+
source.id: source,
2003+
target1.id: target1,
2004+
target2.id: target2,
2005+
target3.id: target3,
2006+
}
2007+
edge_runner = create_edge_runner(edge_group, executors)
2008+
2009+
with patch("agent_framework._workflows._edge_runner.EdgeRunner._execute_on_target") as mock_send:
2010+
with pytest.raises(asyncio.CancelledError):
2011+
await edge_runner.send_message(
2012+
WorkflowMessage(data=MockMessage(data=1), source_id=source.id), State(), InProcRunnerContext()
2013+
)
2014+
assert mock_send.call_count == 0
2015+
assert case2_called is False
2016+
2017+
2018+
async def test_fan_out_edge_group_with_async_selection_func_invalid_result_raises() -> None:
2019+
"""FanOutEdgeRunner raises RuntimeError when an async selection_func returns invalid target IDs."""
2020+
source = MockExecutor(id="source_executor")
2021+
target1 = MockExecutor(id="target_executor_1")
2022+
target2 = MockExecutor(id="target_executor_2")
2023+
2024+
async def async_invalid_selector(data: Any, available_targets: list[str]) -> list[str]:
2025+
await asyncio.sleep(0)
2026+
return [target1.id, "invalid_target_id"]
2027+
2028+
edge_group = FanOutEdgeGroup(
2029+
source_id=source.id,
2030+
target_ids=[target1.id, target2.id],
2031+
selection_func=async_invalid_selector,
2032+
)
2033+
executors: dict[str, Executor] = {source.id: source, target1.id: target1, target2.id: target2}
2034+
edge_runner = create_edge_runner(edge_group, executors)
2035+
2036+
state = State()
2037+
ctx = InProcRunnerContext()
2038+
2039+
message = WorkflowMessage(data=MockMessage(data=1), source_id=source.id)
2040+
with pytest.raises(RuntimeError):
2041+
await edge_runner.send_message(message, state, ctx)
2042+
2043+
18692044
# endregion SwitchCaseEdgeGroup

0 commit comments

Comments
 (0)