@@ -75,8 +75,6 @@ def test_initialization(self, mock_client, mock_registry):
7575 assert handler ._identity == "test_identity"
7676 assert handler ._registry == mock_registry
7777 assert handler ._options == {"option1" : "value1" }
78- assert isinstance (handler ._workflow_engines , dict )
79- assert len (handler ._workflow_engines ) == 0
8078
8179 @pytest .mark .asyncio
8280 async def test_handle_task_implementation_success (self , handler , sample_decision_task , mock_registry ):
@@ -139,8 +137,8 @@ async def test_handle_task_implementation_workflow_not_found(self, handler, samp
139137 await handler ._handle_task_implementation (sample_decision_task )
140138
141139 @pytest .mark .asyncio
142- async def test_handle_task_implementation_reuses_existing_engine (self , handler , sample_decision_task , mock_registry ):
143- """Test that decision task handler reuses existing workflow engine."""
140+ async def test_handle_task_implementation_creates_new_engine (self , handler , sample_decision_task , mock_registry ):
141+ """Test that decision task handler creates new workflow engine for each task ."""
144142 # Mock workflow function
145143 mock_workflow_func = Mock ()
146144 mock_registry .get_workflow .return_value = mock_workflow_func
@@ -153,23 +151,19 @@ async def test_handle_task_implementation_reuses_existing_engine(self, handler,
153151 mock_decision_result .query_results = {}
154152 mock_engine .process_decision = AsyncMock (return_value = mock_decision_result )
155153
156- with patch ('cadence.worker._decision_task_handler.WorkflowEngine' , return_value = mock_engine ):
154+ with patch ('cadence.worker._decision_task_handler.WorkflowEngine' , return_value = mock_engine ) as mock_engine_class :
157155 # First call - should create new engine
158156 await handler ._handle_task_implementation (sample_decision_task )
159157
160- # Second call - should reuse existing engine
158+ # Second call - should create another new engine
161159 await handler ._handle_task_implementation (sample_decision_task )
162160
163- # Registry should only be called once
164- mock_registry .get_workflow .assert_called_once_with ( "TestWorkflow" )
161+ # Registry should be called for each task
162+ assert mock_registry .get_workflow .call_count == 2
165163
166- # Engine should be called twice
164+ # Engine should be created twice and called twice
165+ assert mock_engine_class .call_count == 2
167166 assert mock_engine .process_decision .call_count == 2
168-
169- # Should have one engine in the cache
170- assert len (handler ._workflow_engines ) == 1
171- engine_key = "test_workflow_id:test_run_id"
172- assert engine_key in handler ._workflow_engines
173167
174168 @pytest .mark .asyncio
175169 async def test_handle_task_failure_keyerror (self , handler , sample_decision_task ):
@@ -237,8 +231,6 @@ async def test_respond_decision_task_completed_success(self, handler, sample_dec
237231 """Test successful decision task completion response."""
238232 decision_result = Mock (spec = DecisionResult )
239233 decision_result .decisions = [Decision (), Decision ()]
240- decision_result .force_create_new_decision_task = True
241- decision_result .query_results = None # Test without query results first
242234
243235 await handler ._respond_decision_task_completed (sample_decision_task , decision_result )
244236
@@ -248,62 +240,33 @@ async def test_respond_decision_task_completed_success(self, handler, sample_dec
248240 assert call_args .task_token == sample_decision_task .task_token
249241 assert call_args .identity == handler ._identity
250242 assert call_args .return_new_decision_task
251- assert call_args .force_create_new_decision_task
243+ assert not call_args .force_create_new_decision_task
252244 assert len (call_args .decisions ) == 2
253- # query_results should not be set when None
254- assert not hasattr (call_args , 'query_results' ) or len (call_args .query_results ) == 0
255245
256246 @pytest .mark .asyncio
257247 async def test_respond_decision_task_completed_no_query_results (self , handler , sample_decision_task ):
258248 """Test decision task completion response without query results."""
259249 decision_result = Mock (spec = DecisionResult )
260250 decision_result .decisions = []
261- decision_result .force_create_new_decision_task = False
262- decision_result .query_results = None
263251
264252 await handler ._respond_decision_task_completed (sample_decision_task , decision_result )
265253
266254 call_args = handler ._client .worker_stub .RespondDecisionTaskCompleted .call_args [0 ][0 ]
267- assert not call_args .return_new_decision_task
255+ assert call_args .return_new_decision_task
268256 assert not call_args .force_create_new_decision_task
269257 assert len (call_args .decisions ) == 0
270- # query_results should not be set when None
271- assert not hasattr (call_args , 'query_results' ) or len (call_args .query_results ) == 0
272258
273259 @pytest .mark .asyncio
274260 async def test_respond_decision_task_completed_error (self , handler , sample_decision_task ):
275261 """Test decision task completion response error handling."""
276262 decision_result = Mock (spec = DecisionResult )
277263 decision_result .decisions = []
278- decision_result .force_create_new_decision_task = False
279- decision_result .query_results = {}
280264
281265 handler ._client .worker_stub .RespondDecisionTaskCompleted .side_effect = Exception ("Respond failed" )
282266
283267 with pytest .raises (Exception , match = "Respond failed" ):
284268 await handler ._respond_decision_task_completed (sample_decision_task , decision_result )
285269
286- def test_cleanup_workflow_engine (self , handler ):
287- """Test workflow engine cleanup."""
288- # Add some mock engines
289- handler ._workflow_engines ["workflow1:run1" ] = Mock ()
290- handler ._workflow_engines ["workflow2:run2" ] = Mock ()
291-
292- # Clean up one engine
293- handler .cleanup_workflow_engine ("workflow1" , "run1" )
294-
295- # Verify only one engine was removed
296- assert len (handler ._workflow_engines ) == 1
297- assert "workflow1:run1" not in handler ._workflow_engines
298- assert "workflow2:run2" in handler ._workflow_engines
299-
300- def test_cleanup_workflow_engine_not_found (self , handler ):
301- """Test cleanup of non-existent workflow engine."""
302- # Should not raise error
303- handler .cleanup_workflow_engine ("nonexistent" , "run" )
304-
305- # Should not affect existing engines
306- assert len (handler ._workflow_engines ) == 0
307270
308271 @pytest .mark .asyncio
309272 async def test_workflow_engine_creation_with_workflow_info (self , handler , sample_decision_task , mock_registry ):
0 commit comments