|
| 1 | +"""Tests for gRPC configuration in WorkflowApp.""" |
| 2 | +import pytest |
| 3 | +from unittest.mock import MagicMock, patch, call |
| 4 | +import types |
| 5 | +from dapr_agents.workflow.base import WorkflowApp |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def mock_workflow_dependencies(): |
| 10 | + """Mock all the dependencies needed for WorkflowApp initialization.""" |
| 11 | + with patch("dapr_agents.workflow.base.WorkflowRuntime") as mock_runtime, patch( |
| 12 | + "dapr_agents.workflow.base.DaprWorkflowClient" |
| 13 | + ) as mock_client, patch( |
| 14 | + "dapr_agents.workflow.base.get_default_llm" |
| 15 | + ) as mock_llm, patch.object( |
| 16 | + WorkflowApp, "start_runtime" |
| 17 | + ) as mock_start, patch.object( |
| 18 | + WorkflowApp, "setup_signal_handlers" |
| 19 | + ) as mock_handlers: |
| 20 | + mock_runtime_instance = MagicMock() |
| 21 | + mock_runtime.return_value = mock_runtime_instance |
| 22 | + |
| 23 | + mock_client_instance = MagicMock() |
| 24 | + mock_client.return_value = mock_client_instance |
| 25 | + |
| 26 | + mock_llm_instance = MagicMock() |
| 27 | + mock_llm.return_value = mock_llm_instance |
| 28 | + |
| 29 | + yield { |
| 30 | + "runtime": mock_runtime, |
| 31 | + "runtime_instance": mock_runtime_instance, |
| 32 | + "client": mock_client, |
| 33 | + "client_instance": mock_client_instance, |
| 34 | + "llm": mock_llm, |
| 35 | + "llm_instance": mock_llm_instance, |
| 36 | + "start_runtime": mock_start, |
| 37 | + "signal_handlers": mock_handlers, |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | +def test_workflow_app_without_grpc_config(mock_workflow_dependencies): |
| 42 | + """Test that WorkflowApp initializes without gRPC configuration.""" |
| 43 | + # Create WorkflowApp without gRPC config |
| 44 | + app = WorkflowApp() |
| 45 | + |
| 46 | + # Verify the app was created |
| 47 | + assert app is not None |
| 48 | + assert app.grpc_max_send_message_length is None |
| 49 | + assert app.grpc_max_receive_message_length is None |
| 50 | + |
| 51 | + # Verify runtime and client were initialized |
| 52 | + assert app.wf_runtime is not None |
| 53 | + assert app.wf_client is not None |
| 54 | + |
| 55 | + |
| 56 | +def test_workflow_app_with_grpc_config(mock_workflow_dependencies): |
| 57 | + """Test that WorkflowApp initializes with gRPC configuration.""" |
| 58 | + # Mock the grpc module and durabletask shared module |
| 59 | + mock_grpc = MagicMock() |
| 60 | + mock_shared = MagicMock() |
| 61 | + mock_channel = MagicMock() |
| 62 | + |
| 63 | + # Set up the mock channel |
| 64 | + mock_grpc.insecure_channel.return_value = mock_channel |
| 65 | + mock_shared.get_grpc_channel = MagicMock() |
| 66 | + |
| 67 | + with patch.dict( |
| 68 | + "sys.modules", |
| 69 | + { |
| 70 | + "grpc": mock_grpc, |
| 71 | + "durabletask.internal.shared": mock_shared, |
| 72 | + }, |
| 73 | + ): |
| 74 | + # Create WorkflowApp with gRPC config (16MB) |
| 75 | + app = WorkflowApp( |
| 76 | + grpc_max_send_message_length=16 * 1024 * 1024, # 16MB |
| 77 | + grpc_max_receive_message_length=16 * 1024 * 1024, # 16MB |
| 78 | + ) |
| 79 | + |
| 80 | + # Verify the configuration was set |
| 81 | + assert app.grpc_max_send_message_length == 16 * 1024 * 1024 |
| 82 | + assert app.grpc_max_receive_message_length == 16 * 1024 * 1024 |
| 83 | + |
| 84 | + # Verify runtime and client were initialized |
| 85 | + assert app.wf_runtime is not None |
| 86 | + assert app.wf_client is not None |
| 87 | + |
| 88 | + |
| 89 | +def test_configure_grpc_channel_options_is_called(mock_workflow_dependencies): |
| 90 | + """Test that _configure_grpc_channel_options is called when gRPC config is provided.""" |
| 91 | + with patch.object(WorkflowApp, "_configure_grpc_channel_options") as mock_configure: |
| 92 | + # Create WorkflowApp with gRPC config |
| 93 | + WorkflowApp( |
| 94 | + grpc_max_send_message_length=8 * 1024 * 1024, # 8MB |
| 95 | + ) |
| 96 | + |
| 97 | + # Verify the configuration method was called |
| 98 | + mock_configure.assert_called_once() |
| 99 | + |
| 100 | + |
| 101 | +def test_configure_grpc_channel_options_not_called_without_config( |
| 102 | + mock_workflow_dependencies, |
| 103 | +): |
| 104 | + """Test that _configure_grpc_channel_options is not called without gRPC config.""" |
| 105 | + with patch.object(WorkflowApp, "_configure_grpc_channel_options") as mock_configure: |
| 106 | + # Create WorkflowApp without gRPC config |
| 107 | + WorkflowApp() |
| 108 | + |
| 109 | + # Verify the configuration method was NOT called |
| 110 | + mock_configure.assert_not_called() |
| 111 | + |
| 112 | + |
| 113 | +def test_grpc_channel_patching(): |
| 114 | + """Test that the gRPC channel factory is properly patched with custom options.""" |
| 115 | + # Mock the grpc module and durabletask shared module |
| 116 | + mock_grpc = MagicMock() |
| 117 | + mock_shared = MagicMock() |
| 118 | + mock_channel = MagicMock() |
| 119 | + |
| 120 | + # Set up the mock channel |
| 121 | + mock_grpc.insecure_channel.return_value = mock_channel |
| 122 | + |
| 123 | + # Keep original reference |
| 124 | + def original_get_grpc_channel(*_, **__): |
| 125 | + return "original" |
| 126 | + |
| 127 | + mock_shared.get_grpc_channel = original_get_grpc_channel |
| 128 | + |
| 129 | + # Create dummy package/module structure so 'from durabletask.internal import shared' works |
| 130 | + durabletask_module = types.ModuleType("durabletask") |
| 131 | + internal_module = types.ModuleType("durabletask.internal") |
| 132 | + setattr(durabletask_module, "internal", internal_module) |
| 133 | + setattr(internal_module, "shared", mock_shared) |
| 134 | + |
| 135 | + with patch.dict( |
| 136 | + "sys.modules", |
| 137 | + { |
| 138 | + "grpc": mock_grpc, |
| 139 | + "durabletask": durabletask_module, |
| 140 | + "durabletask.internal": internal_module, |
| 141 | + "durabletask.internal.shared": mock_shared, |
| 142 | + }, |
| 143 | + ), patch("dapr_agents.workflow.base.WorkflowRuntime"), patch( |
| 144 | + "dapr_agents.workflow.base.DaprWorkflowClient" |
| 145 | + ), patch("dapr_agents.workflow.base.get_default_llm"), patch.object( |
| 146 | + WorkflowApp, "start_runtime" |
| 147 | + ), patch.object(WorkflowApp, "setup_signal_handlers"): |
| 148 | + # Create WorkflowApp with gRPC config |
| 149 | + max_send = 10 * 1024 * 1024 # 10MB |
| 150 | + max_recv = 12 * 1024 * 1024 # 12MB |
| 151 | + |
| 152 | + WorkflowApp( |
| 153 | + grpc_max_send_message_length=max_send, |
| 154 | + grpc_max_receive_message_length=max_recv, |
| 155 | + ) |
| 156 | + |
| 157 | + # Confirm get_grpc_channel was overridden |
| 158 | + assert callable(mock_shared.get_grpc_channel) |
| 159 | + assert mock_shared.get_grpc_channel is not original_get_grpc_channel |
| 160 | + assert ( |
| 161 | + getattr(mock_shared.get_grpc_channel, "__name__", "") |
| 162 | + == "get_grpc_channel_with_options" |
| 163 | + ) |
| 164 | + |
| 165 | + # Call the patched function |
| 166 | + test_address = "localhost:50001" |
| 167 | + mock_shared.get_grpc_channel(test_address) |
| 168 | + |
| 169 | + # Verify insecure_channel was called with correct options |
| 170 | + mock_grpc.insecure_channel.assert_called_once() |
| 171 | + call_args = mock_grpc.insecure_channel.call_args |
| 172 | + |
| 173 | + # Check that the address was passed |
| 174 | + assert call_args[0][0] == test_address |
| 175 | + |
| 176 | + # Check that options were passed |
| 177 | + assert "options" in call_args.kwargs |
| 178 | + options = call_args.kwargs["options"] |
| 179 | + |
| 180 | + # Verify options contain our custom message size limits |
| 181 | + assert ("grpc.max_send_message_length", max_send) in options |
| 182 | + assert ("grpc.max_receive_message_length", max_recv) in options |
| 183 | + |
| 184 | + |
| 185 | +def test_grpc_config_with_only_send_limit(mock_workflow_dependencies): |
| 186 | + """Test gRPC configuration with only send limit set.""" |
| 187 | + with patch.object(WorkflowApp, "_configure_grpc_channel_options") as mock_configure: |
| 188 | + app = WorkflowApp( |
| 189 | + grpc_max_send_message_length=20 * 1024 * 1024, # 20MB |
| 190 | + ) |
| 191 | + |
| 192 | + # Verify configuration was called |
| 193 | + mock_configure.assert_called_once() |
| 194 | + |
| 195 | + # Verify only send limit was set |
| 196 | + assert app.grpc_max_send_message_length == 20 * 1024 * 1024 |
| 197 | + assert app.grpc_max_receive_message_length is None |
| 198 | + |
| 199 | + |
| 200 | +def test_grpc_config_with_only_receive_limit(mock_workflow_dependencies): |
| 201 | + """Test gRPC configuration with only receive limit set.""" |
| 202 | + with patch.object(WorkflowApp, "_configure_grpc_channel_options") as mock_configure: |
| 203 | + app = WorkflowApp( |
| 204 | + grpc_max_receive_message_length=24 * 1024 * 1024, # 24MB |
| 205 | + ) |
| 206 | + |
| 207 | + # Verify configuration was called |
| 208 | + mock_configure.assert_called_once() |
| 209 | + |
| 210 | + # Verify only receive limit was set |
| 211 | + assert app.grpc_max_send_message_length is None |
| 212 | + assert app.grpc_max_receive_message_length == 24 * 1024 * 1024 |
0 commit comments