Skip to content

Commit c67c7af

Browse files
GWealecopybara-github
authored andcommitted
test: deflake test_parallel_function_execution_timing
Co-authored-by: George Weale <gweale@google.com> PiperOrigin-RevId: 930722042
1 parent 4cb27fd commit c67c7af

1 file changed

Lines changed: 10 additions & 35 deletions

File tree

tests/unittests/flows/llm_flows/test_functions_simple.py

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -685,26 +685,17 @@ def test_shallow_vs_deep_copy_demonstration():
685685
@pytest.mark.asyncio
686686
async def test_parallel_function_execution_timing():
687687
"""Test that multiple function calls are executed in parallel, not sequentially."""
688-
import time
689-
690688
execution_order = []
691-
execution_times = {}
692689

693690
async def slow_function_1(delay: float = 0.1) -> dict:
694-
start_time = time.time()
695691
execution_order.append('start_1')
696692
await asyncio.sleep(delay)
697-
end_time = time.time()
698-
execution_times['func_1'] = (start_time, end_time)
699693
execution_order.append('end_1')
700694
return {'result': 'function_1_result'}
701695

702696
async def slow_function_2(delay: float = 0.1) -> dict:
703-
start_time = time.time()
704697
execution_order.append('start_2')
705698
await asyncio.sleep(delay)
706-
end_time = time.time()
707-
execution_times['func_2'] = (start_time, end_time)
708699
execution_order.append('end_2')
709700
return {'result': 'function_2_result'}
710701

@@ -740,35 +731,19 @@ async def slow_function_2(delay: float = 0.1) -> dict:
740731
)
741732
runner = testing_utils.TestInMemoryRunner(agent)
742733

743-
# Measure total execution time
744-
start_time = time.time()
745734
events = await runner.run_async_with_new_session('test')
746-
total_time = time.time() - start_time
747-
748-
# Verify parallel execution by checking execution order
749-
# In parallel execution, both functions should start before either finishes
750-
assert 'start_1' in execution_order
751-
assert 'start_2' in execution_order
752-
assert 'end_1' in execution_order
753-
assert 'end_2' in execution_order
754735

755-
# Verify both functions started within a reasonable time window
756-
func_1_start, func_1_end = execution_times['func_1']
757-
func_2_start, func_2_end = execution_times['func_2']
758-
759-
# Functions should start at approximately the same time (within 10ms)
760-
start_time_diff = abs(func_1_start - func_2_start)
761-
assert (
762-
start_time_diff < 0.01
763-
), f'Functions started too far apart: {start_time_diff}s'
764-
765-
# Total execution time should be less than the sum of all parallel function delays (0.2s)
766-
# This proves parallel execution rather than sequential execution
767-
sequential_time = 0.2 # 0.1s + 0.1s if functions ran sequentially
768-
assert total_time < sequential_time, (
769-
f'Execution took too long: {total_time}s, expected < {sequential_time}s'
770-
' (sequential time)'
736+
# Parallel execution means both functions start before either one finishes.
737+
assert set(execution_order) == {'start_1', 'start_2', 'end_1', 'end_2'}
738+
last_start = max(
739+
execution_order.index('start_1'), execution_order.index('start_2')
740+
)
741+
first_end = min(
742+
execution_order.index('end_1'), execution_order.index('end_2')
771743
)
744+
assert (
745+
last_start < first_end
746+
), f'Functions did not overlap; execution was sequential: {execution_order}'
772747

773748
# Verify the results are correct
774749
assert testing_utils.simplify_events(events) == [

0 commit comments

Comments
 (0)