@@ -135,8 +135,8 @@ async def test_handle_task_implementation_workflow_not_found(self, handler, samp
135135 await handler ._handle_task_implementation (sample_decision_task )
136136
137137 @pytest .mark .asyncio
138- async def test_handle_task_implementation_creates_new_engines (self , handler , sample_decision_task , mock_registry ):
139- """Test that decision task handler creates new workflow engines for each task ."""
138+ async def test_handle_task_implementation_caches_engines (self , handler , sample_decision_task , mock_registry ):
139+ """Test that decision task handler caches workflow engines for same workflow execution ."""
140140 # Mock workflow function
141141 mock_workflow_func = Mock ()
142142 mock_registry .get_workflow .return_value = mock_workflow_func
@@ -151,14 +151,60 @@ async def test_handle_task_implementation_creates_new_engines(self, handler, sam
151151 # First call - should create new engine
152152 await handler ._handle_task_implementation (sample_decision_task )
153153
154- # Second call - should create another new engine
154+ # Second call with same workflow_id and run_id - should reuse cached engine
155155 await handler ._handle_task_implementation (sample_decision_task )
156156
157+ # Registry should be called for each task (to get workflow function)
158+ assert mock_registry .get_workflow .call_count == 2
159+
160+ # Engine should be created only once (cached for second call)
161+ assert mock_engine_class .call_count == 1
162+
163+ # But process_decision should be called twice
164+ assert mock_engine .process_decision .call_count == 2
165+
166+ @pytest .mark .asyncio
167+ async def test_handle_task_implementation_different_executions_get_separate_engines (self , handler , mock_registry ):
168+ """Test that different workflow executions get separate engines."""
169+ # Mock workflow function
170+ mock_workflow_func = Mock ()
171+ mock_registry .get_workflow .return_value = mock_workflow_func
172+
173+ # Create two different decision tasks
174+ task1 = Mock (spec = PollForDecisionTaskResponse )
175+ task1 .task_token = b"test_task_token_1"
176+ task1 .workflow_execution = Mock ()
177+ task1 .workflow_execution .workflow_id = "workflow_1"
178+ task1 .workflow_execution .run_id = "run_1"
179+ task1 .workflow_type = Mock ()
180+ task1 .workflow_type .name = "TestWorkflow"
181+
182+ task2 = Mock (spec = PollForDecisionTaskResponse )
183+ task2 .task_token = b"test_task_token_2"
184+ task2 .workflow_execution = Mock ()
185+ task2 .workflow_execution .workflow_id = "workflow_2" # Different workflow
186+ task2 .workflow_execution .run_id = "run_2" # Different run
187+ task2 .workflow_type = Mock ()
188+ task2 .workflow_type .name = "TestWorkflow"
189+
190+ # Mock workflow engine
191+ mock_engine = Mock (spec = WorkflowEngine )
192+ mock_decision_result = Mock (spec = DecisionResult )
193+ mock_decision_result .decisions = []
194+ mock_engine .process_decision = AsyncMock (return_value = mock_decision_result )
195+
196+ with patch ('cadence.worker._decision_task_handler.WorkflowEngine' , return_value = mock_engine ) as mock_engine_class :
197+ # Process different workflow executions
198+ await handler ._handle_task_implementation (task1 )
199+ await handler ._handle_task_implementation (task2 )
200+
157201 # Registry should be called for each task
158202 assert mock_registry .get_workflow .call_count == 2
159203
160- # Engine should be created twice and called twice
204+ # Engine should be created twice (different executions)
161205 assert mock_engine_class .call_count == 2
206+
207+ # Process_decision should be called twice
162208 assert mock_engine .process_decision .call_count == 2
163209
164210 @pytest .mark .asyncio
0 commit comments