fix(test): make concurrent invocation tests deterministic #1493
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Fixes flaky concurrent invocation tests that were failing intermittently on macOS Python 3.13.
Root Cause
The tests
test_agent_concurrent_call_raises_exceptionandtest_agent_concurrent_structured_output_raises_exceptionused timing-based synchronization (time.sleep(0.05)) to ensure the second thread started while the first thread held the invocation lock. On faster machines (particularly macOS Python 3.13), thread scheduling could allow the first invocation to complete before the second thread started, causing both threads to succeed instead of one raisingConcurrencyException.Solution
Replace timing-based synchronization with proper thread coordination using
threading.Event:SlowMockedModelto accept an optionalsignal_eventparameterlock_acquired.wait(timeout=2.0)instead oftime.sleep(0.05)Changes
SlowMockedModelnow acceptsdelayandsignal_eventparameterslockvariable toresults_lockfor clarityRelated Issues
This flaky test was blocking PR #1491 and potentially other PRs with spurious CI failures.
Type of Change
Bug fix (test flakiness)
Testing
hatch fmtpassesChecklist