testing: add with_deferred_starts to model async start_workflow - #3
Draft
jelinson-figma wants to merge 2 commits into
Draft
testing: add with_deferred_starts to model async start_workflow#3jelinson-figma wants to merge 2 commits into
jelinson-figma wants to merge 2 commits into
Conversation
In local testing mode start_workflow runs the workflow inline and synchronously, which collapses the start/run split of a real Temporal server. That deadlocks a caller which starts a fire-and-forget workflow while holding a resource (e.g. a lock) that the workflow then re-acquires: the workflow runs inline mid-call instead of after the caller releases. with_deferred_starts queues fire-and-forget start_workflow calls and runs them when the block exits, after the caller's stack has unwound -- matching the async ordering of a real worker. Awaited workflows can't be deferred (await needs the result synchronously), so only fire-and-forget starts belong inside the block. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…ndence Address review: - raise unless local! and raise on nested calls; the flat queue can't support nesting, so forbid it rather than silently drop workflows. - clear the defer flag (not restore-to-previous) before draining. - document/verify that the drain runs each workflow independently -- one failing is recorded FAILED and does not abort the others. Adds specs for nesting, non-local, and drain-after-failure. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
In local testing mode (
Temporal::Testing.local!),start_workflowruns the workflow inline and synchronously, collapsing the start/run split that a real Temporal server has. A caller that starts a fire-and-forget workflow while holding a resource the workflow re-acquires (e.g. a lock) then deadlocks -- the workflow runs inline mid-call instead of after the caller releases.Temporal::Testing.with_deferred_starts { ... }queues fire-and-forgetstart_workflowcalls made inside the block and runs them when the block exits, after the caller's stack has unwound -- matching the async ordering of a real worker.schedule_workflow->ScheduledWorkflows), reusing the same executor-lambda + drain pattern.FAILED(not propagated), so one failing workflow does not abort the others, mirroring separate executions on a real worker.NOTE: awaited workflows can't be deferred --
await_workflow_resultneeds the result synchronously and local mode does not capture a workflow's return value. Only fire-and-forget starts belong inside the block.NOTE: the block requires
local!mode and cannot be nested -- the queue is a single flat array, so a nested block would drain/clear the outer's queued starts. Both are guarded with a raise rather than failing silently.NOTE: style follows the existing
ScheduledWorkflowsmodule -- a public API delegating to aPrivate::Storedefined withclass << self-- rather than introducing a new convention.Example
Tests
rspec spec/unit/lib/temporal/testing/temporal_override_spec.rb-- 30 examples, 0 failures. New cases: defers until block exit, runs inline outside the block, does not run deferred workflows when the block raises, restores inline execution after the block, runs every deferred workflow even if an earlier one fails, raises when nested, raises when not in local mode.