Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /terrene-foundation_kailash-py /tests /integration /runtime /test_parallel.py
| """ | |
| Unit tests for kailash.runtime.parallel module. | |
| Tests the ParallelRuntime class which provides concurrent execution of workflow nodes. | |
| Tests cover: | |
| - Basic runtime functionality | |
| - Concurrent node execution | |
| - Dependency management | |
| - Error handling | |
| - Performance tracking | |
| NO MOCKING - Tests verify actual parallel runtime behavior with real components. | |
| """ | |
| import asyncio | |
| import time | |
| from unittest.mock import AsyncMock, MagicMock, patch | |
| import pytest | |
| from kailash.nodes.base import Node, NodeParameter | |
| from kailash.nodes.base_async import AsyncNode | |
| from kailash.runtime.parallel import ParallelRuntime | |
| from kailash.sdk_exceptions import ( | |
| RuntimeExecutionError, | |
| WorkflowExecutionError, | |
| WorkflowValidationError, | |
| ) | |
| from kailash.tracking import TaskManager, TaskStatus | |
| from kailash.workflow import Workflow | |
| from kailash.workflow.builder import WorkflowBuilder | |
| class MockAsyncNode(AsyncNode): | |
| """Mock async node for testing purposes.""" | |
| def __init__(self, name: str, result: str = "async_result", delay: float = 0.1): | |
| super().__init__(name=name) | |
| self.result = result | |
| self.delay = delay | |
| self.execution_count = 0 | |
| def get_parameters(self): | |
| """Define parameters for this test node.""" | |
| return { | |
| "input_data": NodeParameter( | |
| name="input_data", | |
| type=dict, | |
| required=False, | |
| description="Input data for the node", | |
| ) | |
| } | |
| async def async_run(self, **inputs): | |
| """Run async with optional delay.""" | |
| self.execution_count += 1 | |
| await asyncio.sleep(self.delay) | |
| return {"result": self.result, "inputs": inputs, "count": self.execution_count} | |
| class MockSyncNode(Node): | |
| """Mock sync node for testing purposes.""" | |
| def __init__(self, name: str, result: str = "sync_result", delay: float = 0.0): | |
| super().__init__(name=name) | |
| self.result = result | |
| self.delay = delay | |
| self.execution_count = 0 | |
| def get_parameters(self): | |
| """Define parameters for this test node.""" | |
| return { | |
| "input_data": NodeParameter( | |
| name="input_data", | |
| type=dict, | |
| required=False, | |
| description="Input data for the node", | |
| ) | |
| } | |
| def run(self, **inputs): | |
| """Run synchronously with optional delay.""" | |
| self.execution_count += 1 | |
| if self.delay > 0: | |
| time.sleep(self.delay) | |
| return {"result": self.result, "inputs": inputs, "count": self.execution_count} | |
| class MockErrorNode(Node): | |
| """Mock node that always raises an error.""" | |
| def __init__(self, name: str, error_message: str = "Test error"): | |
| super().__init__(name=name) | |
| self.error_message = error_message | |
| def get_parameters(self): | |
| """Define parameters for this test node.""" | |
| return {} | |
| def run(self, **inputs): | |
| """Always raise an error.""" | |
| raise RuntimeError(self.error_message) | |
| class TestParallelRuntime: | |
| """Test ParallelRuntime class.""" | |
| def test_init_default(self): | |
| """Test default initialization.""" | |
| runtime = ParallelRuntime() | |
| assert runtime.max_workers == 8 | |
| assert runtime.debug is False | |
| assert runtime.semaphore is None | |
| def test_init_with_options(self): | |
| """Test initialization with custom options.""" | |
| runtime = ParallelRuntime(max_workers=4, debug=True) | |
| assert runtime.max_workers == 4 | |
| assert runtime.debug is True | |
| assert runtime.semaphore is None | |
| async def test_execute_simple_workflow(self): | |
| """Test executing a simple workflow.""" | |
| runtime = ParallelRuntime() | |
| # Create simple workflow | |
| builder = WorkflowBuilder() | |
| builder.add_node("PythonCodeNode", "node1", {"code": "result = {'value': 42}"}) | |
| workflow = builder.build() | |
| # Execute workflow | |
| results, run_id = await runtime.execute(workflow) | |
| assert results is not None | |
| assert isinstance(results, dict) | |
| assert "node1" in results | |
| # Note: PythonCodeNode returns nested structure | |
| assert results["node1"]["result"]["value"] == 42 | |
| async def test_execute_with_parameters(self): | |
| """Test executing workflow with parameters.""" | |
| runtime = ParallelRuntime() | |
| # Create workflow with parameters | |
| builder = WorkflowBuilder() | |
| builder.add_node( | |
| "PythonCodeNode", | |
| "node1", | |
| { | |
| "code": "result = {'doubled': input_value * 2}", | |
| "parameters": {"input_value": 10}, | |
| }, | |
| ) | |
| workflow = builder.build() | |
| # Execute with different parameters | |
| parameters = {"node1": {"input_value": 15}} | |
| results, run_id = await runtime.execute(workflow, parameters=parameters) | |
| assert results is not None | |
| assert "node1" in results | |
| assert results["node1"]["result"]["doubled"] == 30 | |
| async def test_execute_with_task_manager(self): | |
| """Test executing workflow with task manager.""" | |
| runtime = ParallelRuntime() | |
| # Create simple workflow | |
| builder = WorkflowBuilder() | |
| builder.add_node("PythonCodeNode", "node1", {"code": "result = {'value': 1}"}) | |
| workflow = builder.build() | |
| # Mock task manager | |
| task_manager = MagicMock() | |
| task_manager.create_run.return_value = "test_run_id" | |
| # Execute workflow | |
| results, run_id = await runtime.execute(workflow, task_manager=task_manager) | |
| assert results is not None | |
| assert run_id == "test_run_id" | |
| # Verify task manager interactions | |
| task_manager.create_run.assert_called_once() | |
| task_manager.update_run_status.assert_called() | |
| async def test_execute_no_workflow_error(self): | |
| """Test executing with no workflow raises error.""" | |
| runtime = ParallelRuntime() | |
| with pytest.raises(RuntimeExecutionError, match="No workflow provided"): | |
| await runtime.execute(None) | |
| async def test_execute_workflow_validation_error(self): | |
| """Test workflow validation error handling.""" | |
| runtime = ParallelRuntime() | |
| # Create invalid workflow (mock validation failure) | |
| workflow = MagicMock(spec=Workflow) | |
| workflow.name = "test_workflow" | |
| workflow.validate.side_effect = WorkflowValidationError("Invalid workflow") | |
| with pytest.raises(WorkflowValidationError, match="Invalid workflow"): | |
| await runtime.execute(workflow) | |
| async def test_execute_parallel_nodes(self): | |
| """Test parallel execution of independent nodes.""" | |
| runtime = ParallelRuntime(max_workers=2) | |
| # Create workflow with parallel nodes | |
| builder = WorkflowBuilder() | |
| builder.add_node("PythonCodeNode", "node1", {"code": "result = {'node': 1}"}) | |
| builder.add_node("PythonCodeNode", "node2", {"code": "result = {'node': 2}"}) | |
| builder.add_node("PythonCodeNode", "node3", {"code": "result = {'node': 3}"}) | |
| workflow = builder.build() | |
| # Execute workflow | |
| start_time = time.time() | |
| results, run_id = await runtime.execute(workflow) | |
| execution_time = time.time() - start_time | |
| assert results is not None | |
| assert all(f"node{i}" in results for i in range(1, 4)) | |
| # With max_workers=2, three nodes should execute with some parallelism | |
| # Total time should be less than sequential execution | |
| async def test_execute_with_dependencies(self): | |
| """Test execution respecting node dependencies.""" | |
| runtime = ParallelRuntime() | |
| # Create workflow with dependencies | |
| builder = WorkflowBuilder() | |
| builder.add_node("PythonCodeNode", "node1", {"code": "result = {'value': 1}"}) | |
| builder.add_node( | |
| "PythonCodeNode", | |
| "node2", | |
| {"code": "result = {'value': input_data['value'] * 2}"}, | |
| ) | |
| builder.add_node( | |
| "PythonCodeNode", | |
| "node3", | |
| {"code": "result = {'value': input_data['value'] * 3}"}, | |
| ) | |
| # Add dependencies: node2 and node3 depend on node1 | |
| builder.add_connection("node1", "result", "node2", "input_data") | |
| builder.add_connection("node1", "result", "node3", "input_data") | |
| workflow = builder.build() | |
| # Execute workflow | |
| results, run_id = await runtime.execute(workflow) | |
| assert results is not None | |
| assert "node1" in results | |
| assert "node2" in results | |
| assert "node3" in results | |
| # Verify dependency execution | |
| assert results["node2"]["result"]["value"] == 2 # 1 * 2 | |
| assert results["node3"]["result"]["value"] == 3 # 1 * 3 | |
| async def test_execute_with_node_failure(self): | |
| """Test handling of node execution failures.""" | |
| runtime = ParallelRuntime() | |
| # Create workflow with failing node that has dependents | |
| builder = WorkflowBuilder() | |
| builder.add_node("PythonCodeNode", "node1", {"code": "result = {'value': 1}"}) | |
| builder.add_node( | |
| "PythonCodeNode", "node2", {"code": "raise RuntimeError('Node failed')"} | |
| ) | |
| builder.add_node( | |
| "PythonCodeNode", | |
| "node3", | |
| {"code": "result = {'value': input_data['value'] * 2}"}, | |
| ) | |
| # Add dependency so that failure will stop execution | |
| builder.add_connection("node2", "result", "node3", "input_data") | |
| workflow = builder.build() | |
| # Execute workflow - should raise error because node2 has dependents | |
| with pytest.raises((RuntimeExecutionError, WorkflowExecutionError)): | |
| await runtime.execute(workflow) | |
| async def test_execute_with_async_nodes(self): | |
| """Test execution with async nodes.""" | |
| runtime = ParallelRuntime() | |
| # Create workflow with mock async nodes | |
| workflow = Workflow(workflow_id="test_async", name="Test Async") | |
| # Create async nodes | |
| async_node1 = MockAsyncNode("async_node1", "result1", delay=0.1) | |
| async_node2 = MockAsyncNode("async_node2", "result2", delay=0.1) | |
| workflow._node_instances = { | |
| "async_node1": async_node1, | |
| "async_node2": async_node2, | |
| } | |
| # Add nodes to graph | |
| workflow.graph.add_node("async_node1") | |
| workflow.graph.add_node("async_node2") | |
| # Execute workflow | |
| start_time = time.time() | |
| results, run_id = await runtime.execute(workflow) | |
| execution_time = time.time() - start_time | |
| assert results is not None | |
| assert "async_node1" in results | |
| assert "async_node2" in results | |
| assert results["async_node1"]["result"] == "result1" | |
| assert results["async_node2"]["result"] == "result2" | |
| # Both async nodes should execute in parallel | |
| # Total time should be around 0.1s, not 0.2s | |
| assert execution_time < 0.15 # Allow some overhead | |
| async def test_execute_mixed_sync_async_nodes(self): | |
| """Test execution with mixed sync and async nodes.""" | |
| runtime = ParallelRuntime() | |
| # Create workflow with mixed nodes | |
| workflow = Workflow(workflow_id="test_mixed", name="Test Mixed") | |
| # Create nodes | |
| sync_node = MockSyncNode("sync_node", "sync_result") | |
| async_node = MockAsyncNode("async_node", "async_result", delay=0.1) | |
| workflow._node_instances = {"sync_node": sync_node, "async_node": async_node} | |
| # Add nodes to graph with dependency to ensure proper execution | |
| workflow.graph.add_node("sync_node") | |
| workflow.graph.add_node("async_node") | |
| # Add dependency to work around parallel runtime bug | |
| # The whole output dict is passed as input_data | |
| workflow.graph.add_edge("sync_node", "async_node", mapping={}) | |
| # Execute workflow | |
| results, run_id = await runtime.execute(workflow) | |
| assert results is not None | |
| assert "sync_node" in results | |
| assert "async_node" in results | |
| assert results["sync_node"]["result"] == "sync_result" | |
| assert results["async_node"]["result"] == "async_result" | |
| async def test_semaphore_usage(self): | |
| """Test that semaphore limits concurrent execution.""" | |
| runtime = ParallelRuntime(max_workers=1) | |
| # Create workflow with multiple nodes | |
| workflow = Workflow(workflow_id="test_semaphore", name="Test Semaphore") | |
| # Create nodes with delays | |
| node1 = MockAsyncNode("node1", "result1", delay=0.1) | |
| node2 = MockAsyncNode("node2", "result2", delay=0.1) | |
| workflow._node_instances = {"node1": node1, "node2": node2} | |
| # Add nodes to graph | |
| workflow.graph.add_node("node1") | |
| workflow.graph.add_node("node2") | |
| # Execute workflow | |
| start_time = time.time() | |
| results, run_id = await runtime.execute(workflow) | |
| execution_time = time.time() - start_time | |
| assert results is not None | |
| # With max_workers=1, nodes should execute sequentially | |
| # Total time should be around 0.1s (2 * 0.05s), not 0.05s | |
| assert execution_time >= 0.08 # Allow some timing variance | |
| async def test_execute_empty_workflow(self): | |
| """Test executing an empty workflow.""" | |
| runtime = ParallelRuntime() | |
| # Create empty workflow | |
| workflow = Workflow(workflow_id="empty", name="Empty Workflow") | |
| # Execute workflow | |
| results, run_id = await runtime.execute(workflow) | |
| assert results == {} | |
| assert run_id is None # No task manager provided | |
| async def test_debug_mode(self): | |
| """Test runtime with debug mode enabled.""" | |
| runtime = ParallelRuntime(debug=True) | |
| assert runtime.debug is True | |
| # Create simple workflow | |
| builder = WorkflowBuilder() | |
| builder.add_node( | |
| "PythonCodeNode", "node1", {"code": "result = {'debug': True}"} | |
| ) | |
| workflow = builder.build() | |
| # Execute workflow | |
| results, run_id = await runtime.execute(workflow) | |
| assert results is not None | |
| assert "node1" in results | |
| async def test_task_manager_error_handling(self): | |
| """Test error handling when task manager operations fail.""" | |
| runtime = ParallelRuntime() | |
| # Create simple workflow | |
| builder = WorkflowBuilder() | |
| builder.add_node("PythonCodeNode", "node1", {"code": "result = {'value': 1}"}) | |
| workflow = builder.build() | |
| # Mock task manager that fails | |
| task_manager = MagicMock() | |
| task_manager.create_run.side_effect = Exception("Task manager error") | |
| # Execute workflow - should continue despite task manager error | |
| results, run_id = await runtime.execute(workflow, task_manager=task_manager) | |
| assert results is not None | |
| assert run_id is None # Failed to create run | |
| async def test_complex_dependency_chain(self): | |
| """Test execution with complex dependency chains.""" | |
| runtime = ParallelRuntime() | |
| # Create workflow with chain: node1 -> node2 -> node3 | |
| builder = WorkflowBuilder() | |
| builder.add_node("PythonCodeNode", "node1", {"code": "result = {'value': 1}"}) | |
| builder.add_node( | |
| "PythonCodeNode", | |
| "node2", | |
| {"code": "result = {'value': input_data['value'] + 1}"}, | |
| ) | |
| builder.add_node( | |
| "PythonCodeNode", | |
| "node3", | |
| {"code": "result = {'value': input_data['value'] + 1}"}, | |
| ) | |
| # Create dependency chain | |
| builder.add_connection("node1", "result", "node2", "input_data") | |
| builder.add_connection("node2", "result", "node3", "input_data") | |
| workflow = builder.build() | |
| # Execute workflow | |
| results, run_id = await runtime.execute(workflow) | |
| assert results is not None | |
| assert results["node1"]["result"]["value"] == 1 | |
| assert results["node2"]["result"]["value"] == 2 | |
| assert results["node3"]["result"]["value"] == 3 | |
| async def test_diamond_dependency_pattern(self): | |
| """Test execution with diamond dependency pattern.""" | |
| runtime = ParallelRuntime() | |
| # Create workflow with diamond pattern: | |
| # node1 | |
| # / \ | |
| # node2 node3 | |
| # \ / | |
| # node4 | |
| builder = WorkflowBuilder() | |
| builder.add_node("PythonCodeNode", "node1", {"code": "result = {'value': 10}"}) | |
| builder.add_node( | |
| "PythonCodeNode", | |
| "node2", | |
| {"code": "result = {'value': input_data['value'] * 2}"}, | |
| ) | |
| builder.add_node( | |
| "PythonCodeNode", | |
| "node3", | |
| {"code": "result = {'value': input_data['value'] * 3}"}, | |
| ) | |
| builder.add_node( | |
| "PythonCodeNode", | |
| "node4", | |
| {"code": "result = {'sum': input1['value'] + input2['value']}"}, | |
| ) | |
| # Create diamond dependencies | |
| builder.add_connection("node1", "result", "node2", "input_data") | |
| builder.add_connection("node1", "result", "node3", "input_data") | |
| builder.add_connection("node2", "result", "node4", "input1") | |
| builder.add_connection("node3", "result", "node4", "input2") | |
| workflow = builder.build() | |
| # Execute workflow | |
| results, run_id = await runtime.execute(workflow) | |
| assert results is not None | |
| assert results["node1"]["result"]["value"] == 10 | |
| assert results["node2"]["result"]["value"] == 20 # 10 * 2 | |
| assert results["node3"]["result"]["value"] == 30 # 10 * 3 | |
| assert results["node4"]["result"]["sum"] == 50 # 20 + 30 | |
| async def test_execution_metrics(self): | |
| """Test that execution metrics are properly tracked.""" | |
| runtime = ParallelRuntime() | |
| # Create workflow with nodes | |
| builder = WorkflowBuilder() | |
| builder.add_node("PythonCodeNode", "node1", {"code": "result = {'value': 1}"}) | |
| builder.add_node("PythonCodeNode", "node2", {"code": "result = {'value': 2}"}) | |
| workflow = builder.build() | |
| # Mock task manager to verify metrics | |
| task_manager = MagicMock() | |
| task_manager.create_run.return_value = "test_run_id" | |
| # Execute workflow | |
| results, run_id = await runtime.execute(workflow, task_manager=task_manager) | |
| assert results is not None | |
| # Verify execution time was tracked | |
| update_calls = task_manager.update_run_status.call_args_list | |
| completed_call = [c for c in update_calls if c[0][1] == "completed"][0] | |
| metadata = completed_call[1]["metadata"] | |
| assert "execution_time" in metadata | |
| assert metadata["execution_time"] > 0 | |
Xet Storage Details
- Size:
- 19.3 kB
- Xet hash:
- f1ce84fc59260acbb81e6f96db6a8053946dce681b1d7cb5db4f9c8cc478cbd1
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.