|
| 1 | +""" |
| 2 | +Copyright 2025 The Dapr Authors |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +""" |
| 13 | + |
| 14 | +from unittest.mock import MagicMock, Mock, patch |
| 15 | + |
| 16 | +import grpc |
| 17 | + |
| 18 | +from durabletask import worker |
| 19 | + |
| 20 | + |
| 21 | +def test_execute_orchestrator_grpc_error_benign_cancelled(): |
| 22 | + """Test that benign gRPC errors in orchestrator execution are handled gracefully.""" |
| 23 | + w = worker.TaskHubGrpcWorker() |
| 24 | + |
| 25 | + # Add a dummy orchestrator |
| 26 | + def test_orchestrator(ctx, input): |
| 27 | + return "result" |
| 28 | + |
| 29 | + w.add_orchestrator(test_orchestrator) |
| 30 | + |
| 31 | + # Mock the stub to raise a benign error |
| 32 | + mock_stub = MagicMock() |
| 33 | + mock_error = grpc.RpcError() |
| 34 | + mock_error.code = Mock(return_value=grpc.StatusCode.CANCELLED) |
| 35 | + mock_stub.CompleteOrchestratorTask.side_effect = mock_error |
| 36 | + |
| 37 | + # Create a mock request with proper structure |
| 38 | + mock_req = MagicMock() |
| 39 | + mock_req.instanceId = "test-id" |
| 40 | + mock_req.pastEvents = [] |
| 41 | + mock_req.newEvents = [MagicMock()] |
| 42 | + mock_req.newEvents[0].HasField = lambda x: x == "executionStarted" |
| 43 | + mock_req.newEvents[0].executionStarted.name = "test_orchestrator" |
| 44 | + mock_req.newEvents[0].executionStarted.input = None |
| 45 | + mock_req.newEvents[0].router.targetAppID = None |
| 46 | + mock_req.newEvents[0].router.sourceAppID = None |
| 47 | + mock_req.newEvents[0].timestamp.ToDatetime = Mock(return_value=None) |
| 48 | + |
| 49 | + # Should not raise exception (benign error) |
| 50 | + w._execute_orchestrator(mock_req, mock_stub, "token") |
| 51 | + |
| 52 | + |
| 53 | +def test_execute_orchestrator_grpc_error_non_benign(): |
| 54 | + """Test that non-benign gRPC errors in orchestrator execution are logged.""" |
| 55 | + w = worker.TaskHubGrpcWorker() |
| 56 | + |
| 57 | + # Add a dummy orchestrator |
| 58 | + def test_orchestrator(ctx, input): |
| 59 | + return "result" |
| 60 | + |
| 61 | + w.add_orchestrator(test_orchestrator) |
| 62 | + |
| 63 | + # Mock the stub to raise a non-benign error |
| 64 | + mock_stub = MagicMock() |
| 65 | + mock_error = grpc.RpcError() |
| 66 | + mock_error.code = Mock(return_value=grpc.StatusCode.INTERNAL) |
| 67 | + mock_stub.CompleteOrchestratorTask.side_effect = mock_error |
| 68 | + |
| 69 | + # Create a mock request with proper structure |
| 70 | + mock_req = MagicMock() |
| 71 | + mock_req.instanceId = "test-id" |
| 72 | + mock_req.pastEvents = [] |
| 73 | + mock_req.newEvents = [MagicMock()] |
| 74 | + mock_req.newEvents[0].HasField = lambda x: x == "executionStarted" |
| 75 | + mock_req.newEvents[0].executionStarted.name = "test_orchestrator" |
| 76 | + mock_req.newEvents[0].executionStarted.input = None |
| 77 | + mock_req.newEvents[0].router.targetAppID = None |
| 78 | + mock_req.newEvents[0].router.sourceAppID = None |
| 79 | + mock_req.newEvents[0].timestamp.ToDatetime = Mock(return_value=None) |
| 80 | + |
| 81 | + # Should not raise exception (error is logged but handled) |
| 82 | + with patch.object(w._logger, "exception") as mock_log: |
| 83 | + w._execute_orchestrator(mock_req, mock_stub, "token") |
| 84 | + # Verify error was logged |
| 85 | + assert mock_log.called |
| 86 | + |
| 87 | + |
| 88 | +def test_execute_activity_grpc_error_benign(): |
| 89 | + """Test that benign gRPC errors in activity execution are handled gracefully.""" |
| 90 | + w = worker.TaskHubGrpcWorker() |
| 91 | + |
| 92 | + # Add a dummy activity |
| 93 | + def test_activity(ctx, input): |
| 94 | + return "result" |
| 95 | + |
| 96 | + w.add_activity(test_activity) |
| 97 | + |
| 98 | + # Mock the stub to raise a benign error |
| 99 | + mock_stub = MagicMock() |
| 100 | + mock_error = grpc.RpcError() |
| 101 | + mock_error.code = Mock(return_value=grpc.StatusCode.CANCELLED) |
| 102 | + str_return = "unknown instance ID/task ID combo" |
| 103 | + mock_error.__str__ = Mock(return_value=str_return) |
| 104 | + mock_stub.CompleteActivityTask.side_effect = mock_error |
| 105 | + |
| 106 | + # Create a mock request |
| 107 | + mock_req = MagicMock() |
| 108 | + mock_req.orchestrationInstance.instanceId = "test-id" |
| 109 | + mock_req.name = "test_activity" |
| 110 | + mock_req.taskId = 1 |
| 111 | + mock_req.input.value = '""' |
| 112 | + |
| 113 | + # Should not raise exception (benign error) |
| 114 | + w._execute_activity(mock_req, mock_stub, "token") |
0 commit comments