Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /TrueSelph_jvspatial /tests /integration /test_edge_cases.py
| """ | |
| Test suite for Edge Cases and Best Practices. | |
| This module implements comprehensive tests for: | |
| - Error conditions and exception handling | |
| - Boundary cases and limit testing | |
| - Field exclusions in API schemas | |
| - Validation scenarios and input sanitization | |
| - Memory management and resource cleanup | |
| - Concurrency and thread safety | |
| - Performance edge cases | |
| - Recovery and resilience patterns | |
| """ | |
| import asyncio | |
| import gc | |
| import threading | |
| import time | |
| import weakref | |
| from concurrent.futures import ThreadPoolExecutor | |
| from pathlib import Path | |
| from typing import Any, Dict, List, Optional | |
| from unittest.mock import AsyncMock, MagicMock, patch | |
| import pytest | |
| from pydantic import Field, PrivateAttr | |
| from jvspatial.api.config import ServerConfig | |
| from jvspatial.api.server import Server | |
| from jvspatial.core import on_exit, on_visit | |
| from jvspatial.core.context import GraphContext | |
| from jvspatial.core.entities import ( | |
| Edge, | |
| Node, | |
| Object, | |
| Root, | |
| Walker, | |
| ) | |
| # TraversalPaused and TraversalSkipped are not available in protection module | |
| # These may be defined elsewhere or need to be imported differently | |
| from jvspatial.core.pager import ObjectPager | |
| from jvspatial.db.database import Database, VersionConflictError, finalize_find_results | |
| class EdgeCaseTestNode(Node): | |
| """Test node for edge case testing.""" | |
| name: str = "" | |
| value: int = 0 | |
| sensitive_data: str = "" | |
| private_field: str = "" | |
| class EdgeCaseTestEdge(Edge): | |
| """Test edge for edge case testing.""" | |
| weight: int = 1 | |
| metadata: Dict[str, Any] = {} | |
| class EdgeCaseTestWalker(Walker): | |
| """Test walker for edge case testing.""" | |
| max_iterations: int = 100 | |
| current_iterations: int = 0 | |
| error_log: List[str] = Field(default_factory=list) | |
| memory_refs: List[Any] = Field(default_factory=list) | |
| def __init__(self, **kwargs): | |
| super().__init__(**kwargs) | |
| async def process_with_limit(self, here): | |
| """Process with iteration limit.""" | |
| self.current_iterations += 1 | |
| if self.current_iterations > self.max_iterations: | |
| await self.disengage() | |
| # Simulate memory usage tracking | |
| self.memory_refs.append(weakref.ref(here)) | |
| class ErrorProneWalker(Walker): | |
| """Walker that intentionally causes various errors.""" | |
| error_type: str = "none" | |
| async def error_prone_processing(self, here): | |
| """Processing that can fail in various ways.""" | |
| if self.error_type == "runtime": | |
| raise RuntimeError("Simulated runtime error") | |
| elif self.error_type == "memory": | |
| # Simulate memory allocation error | |
| raise MemoryError("Simulated memory error") | |
| elif self.error_type == "recursion": | |
| # Simulate recursion limit | |
| def recursive_call(depth=0): | |
| if depth > 1000: | |
| return | |
| recursive_call(depth + 1) | |
| recursive_call() | |
| elif self.error_type == "timeout": | |
| # Simulate timeout | |
| await asyncio.sleep(10) | |
| class ResourceIntensiveWalker(Walker): | |
| """Walker for testing resource management.""" | |
| allocated_resources: List[Dict[str, Any]] = Field(default_factory=list) | |
| large_data: List[int] = Field(default_factory=list) | |
| def __init__(self, **kwargs): | |
| super().__init__(**kwargs) | |
| async def allocate_resources(self, here): | |
| """Allocate resources during traversal.""" | |
| # Simulate resource allocation | |
| resource = {"data": "x" * 1000, "id": here.id} | |
| self.allocated_resources.append(resource) | |
| self.large_data.extend([i for i in range(1000)]) | |
| async def cleanup_resources(self): | |
| """Clean up allocated resources.""" | |
| self.allocated_resources.clear() | |
| self.large_data.clear() | |
| class ConcurrentWalker(Walker): | |
| """Walker for testing concurrent access.""" | |
| shared_counter: int = 0 | |
| _lock: Optional[asyncio.Lock] = PrivateAttr(default=None) | |
| def __init__(self, **kwargs): | |
| super().__init__(**kwargs) | |
| self._lock = asyncio.Lock() | |
| def lock(self): | |
| return self._lock | |
| async def concurrent_processing(self, here): | |
| """Process with thread-safe operations.""" | |
| async with self.lock: | |
| self.shared_counter += 1 | |
| # Simulate some processing time | |
| await asyncio.sleep(0.001) | |
| def mock_database(): | |
| """Create mock database for testing.""" | |
| database = MagicMock(spec=Database) | |
| database.save = AsyncMock() | |
| database.get = AsyncMock() | |
| database.find = AsyncMock() | |
| database.delete = AsyncMock() | |
| database.count = AsyncMock() | |
| return database | |
| def mock_context(mock_database): | |
| """Create mock context for testing.""" | |
| context = GraphContext(database=mock_database) | |
| return context | |
| class TestErrorConditions: | |
| """Test various error conditions and exception handling.""" | |
| async def test_walker_runtime_error_recovery(self, mock_context): | |
| """Test walker recovery from runtime errors.""" | |
| walker = ErrorProneWalker(error_type="runtime") | |
| node = EdgeCaseTestNode(name="test") | |
| # Walker should handle runtime errors gracefully | |
| with patch("jvspatial.core.entities.Root.get", return_value=node): | |
| result = await walker.spawn(node) | |
| # Walker should complete despite error | |
| assert result == walker | |
| report = await walker.get_report() | |
| # Check for hook error reports | |
| hook_error_reports = [ | |
| item | |
| for item in report | |
| if isinstance(item, dict) and "hook_error" in item | |
| ] | |
| assert len(hook_error_reports) >= 1 | |
| assert "Simulated runtime error" in hook_error_reports[0]["hook_error"] | |
| async def test_walker_memory_error_handling(self, mock_context): | |
| """Test walker handling of memory errors.""" | |
| walker = ErrorProneWalker(error_type="memory") | |
| node = EdgeCaseTestNode(name="test") | |
| with patch("jvspatial.core.entities.Root.get", return_value=node): | |
| # Memory errors should be caught and handled | |
| try: | |
| await walker.spawn(node) | |
| except MemoryError: | |
| pytest.fail("MemoryError should be handled by walker") | |
| async def test_infinite_loop_protection(self): | |
| """Test protection against infinite loops.""" | |
| walker = EdgeCaseTestWalker(max_iterations=10) | |
| # Create nodes in a cycle | |
| nodes = [EdgeCaseTestNode(name=f"node{i}") for i in range(5)] | |
| # Simulate infinite traversal | |
| await walker.queue.append(nodes * 20) # Add many duplicate nodes | |
| with patch("jvspatial.core.entities.Root.get", return_value=nodes[0]): | |
| await walker.spawn(nodes[0]) | |
| # Should stop after max iterations | |
| assert walker.current_iterations <= walker.max_iterations + 1 | |
| async def test_invalid_node_references(self, mock_context): | |
| """Test handling of invalid node references.""" | |
| # Mock database to return None for invalid references | |
| mock_context.database.get.return_value = None | |
| walker = EdgeCaseTestWalker() | |
| # Try to get non-existent node | |
| node = await mock_context.get(EdgeCaseTestNode, "invalid-id") | |
| assert node is None | |
| # Verify database was queried | |
| mock_context.database.get.assert_called_once() | |
| async def test_database_connection_failure_recovery(self, mock_context): | |
| """Test recovery from database connection failures.""" | |
| # Simulate database connection failure | |
| mock_context.database.save.side_effect = ConnectionError("Database unavailable") | |
| node = EdgeCaseTestNode(name="test") | |
| with pytest.raises(ConnectionError): | |
| await mock_context.save(node) | |
| async def test_malformed_walker_definition(self): | |
| """Test handling of malformed walker definitions.""" | |
| # Test walker with invalid hook definitions (non-string, non-class) | |
| with pytest.raises(ValueError, match="Target type must be a class or string"): | |
| class InvalidWalker(Walker): | |
| # Invalid type (neither string nor class) | |
| async def invalid_hook(self, here): | |
| pass | |
| async def test_circular_reference_detection(self): | |
| """Test detection of circular references.""" | |
| node1 = EdgeCaseTestNode(name="node1") | |
| node2 = EdgeCaseTestNode(name="node2") | |
| # Create circular reference (in a real scenario) | |
| # This would be handled by the database layer | |
| edge1 = EdgeCaseTestEdge(source=node1.id, target=node2.id) | |
| edge2 = EdgeCaseTestEdge(source=node2.id, target=node1.id) | |
| walker = EdgeCaseTestWalker() | |
| await walker.queue.append([node1, node2, node1]) # Circular queue | |
| # Should handle circular references without infinite loop | |
| with patch("jvspatial.core.entities.Root.get", return_value=node1): | |
| await walker.spawn(node1) | |
| # Should complete without hanging | |
| assert len(walker.memory_refs) > 0 | |
| class TestBoundaryConditions: | |
| """Test boundary cases and limits.""" | |
| async def test_empty_graph_traversal(self): | |
| """Test walker behavior with empty graph.""" | |
| walker = EdgeCaseTestWalker() | |
| with patch("jvspatial.core.entities.Root.get") as mock_root: | |
| root = EdgeCaseTestNode(name="empty_root") | |
| mock_root.return_value = root | |
| result = await walker.spawn(root) | |
| # Should complete successfully with empty graph | |
| assert result == walker | |
| assert walker.current_iterations >= 0 | |
| async def test_single_node_graph(self): | |
| """Test walker with single node graph.""" | |
| walker = EdgeCaseTestWalker() | |
| single_node = EdgeCaseTestNode(name="single") | |
| with patch("jvspatial.core.entities.Root.get", return_value=single_node): | |
| result = await walker.spawn(single_node) | |
| assert result == walker | |
| assert walker.current_iterations == 1 | |
| async def test_large_queue_handling(self): | |
| """Test walker with very large queue.""" | |
| walker = EdgeCaseTestWalker(max_iterations=1000) | |
| # Create large number of nodes | |
| large_node_set = [EdgeCaseTestNode(name=f"node_{i}") for i in range(10000)] | |
| # Add to queue | |
| await walker.queue.append(large_node_set) | |
| with patch("jvspatial.core.entities.Root.get", return_value=large_node_set[0]): | |
| result = await walker.spawn(large_node_set[0]) | |
| # Should handle large queue without memory issues | |
| assert result == walker | |
| assert len(walker.queue) >= 0 # Queue should be processed | |
| async def test_deep_graph_traversal(self): | |
| """Test walker with deeply nested graph structure.""" | |
| walker = EdgeCaseTestWalker(max_iterations=500) | |
| # Create chain of nodes (deep graph) | |
| nodes = [EdgeCaseTestNode(name=f"level_{i}") for i in range(100)] | |
| # Queue nodes in sequence | |
| await walker.queue.append(nodes) | |
| with patch("jvspatial.core.entities.Root.get", return_value=nodes[0]): | |
| result = await walker.spawn(nodes[0]) | |
| assert result == walker | |
| # Should handle deep traversal | |
| async def test_maximum_field_lengths(self): | |
| """Test handling of maximum field lengths.""" | |
| # Test very long field values | |
| very_long_name = "x" * 10000 | |
| node = EdgeCaseTestNode(name=very_long_name, value=999999999) | |
| assert node.name == very_long_name | |
| assert len(node.name) == 10000 | |
| async def test_pagination_boundary_cases(self, mock_context): | |
| """Test pagination at boundaries (single full page, out-of-range page number).""" | |
| # Create exactly one page worth of data | |
| nodes = [EdgeCaseTestNode(name=f"node_{i}") for i in range(20)] | |
| for node in nodes: | |
| await mock_context.save(node) | |
| exported = await asyncio.gather(*[node.export() for node in nodes]) | |
| pager = ObjectPager(EdgeCaseTestNode, page_size=20) | |
| with patch( | |
| "jvspatial.core.context.get_default_context", return_value=mock_context | |
| ): | |
| mock_context.database.count.return_value = 20 | |
| async def mock_find( | |
| collection: str, | |
| query: Dict[str, Any], | |
| *, | |
| limit: Optional[int] = None, | |
| sort: Optional[List[Any]] = None, | |
| ) -> List[Dict[str, Any]]: | |
| records = [dict(r) for r in exported] | |
| if isinstance(query, dict) and "id" in query: | |
| id_q = query["id"] | |
| if isinstance(id_q, dict) and "$gt" in id_q: | |
| g = str(id_q["$gt"]) | |
| records = [r for r in records if str(r.get("id", "")) > g] | |
| return finalize_find_results(records, sort=sort, limit=limit) | |
| mock_context.database.find.side_effect = mock_find | |
| page1 = await pager.get_page(1) | |
| assert len(page1) == 20 | |
| assert pager.current_page == 1 | |
| # No second page: requesting page 2 is clamped to the last (only) page | |
| page2 = await pager.get_page(2) | |
| assert pager.current_page == 1 | |
| assert len(page2) == 20 | |
| class TestMemoryManagement: | |
| """Test memory management and resource cleanup.""" | |
| async def test_walker_memory_cleanup(self): | |
| """Test that walker properly cleans up memory.""" | |
| walker = ResourceIntensiveWalker() | |
| nodes = [ | |
| EdgeCaseTestNode(name=f"node_{i}") for i in range(5) | |
| ] # Use fewer nodes | |
| # Track memory usage | |
| initial_refs = len(walker.allocated_resources) | |
| # Add nodes to the queue and simulate spawning | |
| await walker.queue.append(nodes) | |
| # Mock the visiting context to ensure hooks are called | |
| for node in nodes: | |
| with await walker.visiting(node): | |
| await walker.allocate_resources(node) | |
| # Should have allocated resources during traversal | |
| assert len(walker.allocated_resources) > initial_refs | |
| assert len(walker.allocated_resources) == 5 | |
| # Force cleanup | |
| await walker.cleanup_resources() | |
| assert len(walker.allocated_resources) == 0 | |
| async def test_weak_reference_handling(self): | |
| """Test proper handling of weak references.""" | |
| node = EdgeCaseTestNode(name="test") | |
| weak_ref = weakref.ref(node) | |
| assert weak_ref() is node | |
| # Delete strong reference | |
| del node | |
| gc.collect() | |
| # Weak reference should be None | |
| assert weak_ref() is None | |
| async def test_large_object_handling(self): | |
| """Test handling of large objects.""" | |
| # Create node with large data | |
| large_data = "x" * 1000000 # 1MB string | |
| node = EdgeCaseTestNode(name="large", sensitive_data=large_data) | |
| walker = EdgeCaseTestWalker() | |
| with patch("jvspatial.core.entities.Root.get", return_value=node): | |
| result = await walker.spawn(node) | |
| # Should handle large objects | |
| assert result == walker | |
| async def test_memory_leak_prevention(self): | |
| """Test prevention of memory leaks in long-running walkers.""" | |
| walker = EdgeCaseTestWalker() | |
| # Simulate long-running process | |
| for i in range(1000): | |
| node = EdgeCaseTestNode(name=f"temp_{i}") | |
| # Process and discard nodes | |
| walker.memory_refs.append(weakref.ref(node)) | |
| del node | |
| # Force garbage collection | |
| gc.collect() | |
| # Check that temporary objects were cleaned up | |
| live_refs = sum(1 for ref in walker.memory_refs if ref() is not None) | |
| assert live_refs == 0 | |
| class TestConcurrencyAndThreadSafety: | |
| """Test concurrency and thread safety.""" | |
| async def test_concurrent_walker_execution(self): | |
| """Test multiple walkers running concurrently.""" | |
| walkers = [ConcurrentWalker() for _ in range(5)] | |
| nodes = [EdgeCaseTestNode(name=f"node_{i}") for i in range(10)] | |
| async def run_walker(walker, start_node): | |
| await walker.queue.append(nodes) | |
| with patch("jvspatial.core.entities.Root.get", return_value=start_node): | |
| return await walker.spawn(start_node) | |
| # Run walkers concurrently | |
| tasks = [run_walker(walker, nodes[0]) for walker in walkers] | |
| results = await asyncio.gather(*tasks) | |
| # All should complete successfully | |
| assert len(results) == 5 | |
| assert all(result == walker for result, walker in zip(results, walkers)) | |
| async def test_shared_state_access(self): | |
| """Test thread-safe access to shared state.""" | |
| walker = ConcurrentWalker() | |
| nodes = [EdgeCaseTestNode(name=f"node_{i}") for i in range(50)] | |
| await walker.queue.append(nodes) | |
| with patch("jvspatial.core.entities.Root.get", return_value=nodes[0]): | |
| await walker.spawn(nodes[0]) | |
| # Counter should reflect all processed nodes | |
| assert walker.shared_counter > 0 | |
| async def test_thread_pool_execution(self): | |
| """Test walker execution in thread pool.""" | |
| def sync_walker_execution(): | |
| """Synchronous wrapper for walker execution.""" | |
| loop = asyncio.new_event_loop() | |
| asyncio.set_event_loop(loop) | |
| walker = EdgeCaseTestWalker() | |
| node = EdgeCaseTestNode(name="thread_test") | |
| with patch("jvspatial.core.entities.Root.get", return_value=node): | |
| result = loop.run_until_complete(walker.spawn(node)) | |
| loop.close() | |
| return result | |
| # Execute in thread pool | |
| with ThreadPoolExecutor(max_workers=3) as executor: | |
| futures = [executor.submit(sync_walker_execution) for _ in range(3)] | |
| results = [future.result() for future in futures] | |
| assert len(results) == 3 | |
| class TestValidationAndSanitization: | |
| """Test input validation and data sanitization.""" | |
| async def test_node_field_validation(self): | |
| """Test node field validation.""" | |
| # Test with valid data | |
| node = EdgeCaseTestNode(name="valid", value=42) | |
| assert node.name == "valid" | |
| assert node.value == 42 | |
| # Test with edge case values | |
| node_edge = EdgeCaseTestNode(name="", value=0) | |
| assert node_edge.name == "" | |
| assert node_edge.value == 0 | |
| async def test_walker_parameter_validation(self): | |
| """Test walker parameter validation.""" | |
| # Test walker with invalid parameters | |
| walker = EdgeCaseTestWalker(max_iterations=-1) # Invalid negative value | |
| # Walker should handle invalid parameters gracefully | |
| assert walker.max_iterations == -1 # But might be corrected in processing | |
| async def test_database_input_sanitization(self, mock_context): | |
| """Test database input sanitization.""" | |
| # Test with potentially dangerous input | |
| dangerous_name = "<script>alert('xss')</script>" | |
| node = EdgeCaseTestNode(name=dangerous_name) | |
| # Should save without script execution | |
| await mock_context.save(node) | |
| mock_context.database.save.assert_called_once() | |
| async def test_api_field_exclusion(self): | |
| """Test exclusion of sensitive fields from API.""" | |
| # Test that sensitive fields are properly excluded | |
| node = EdgeCaseTestNode( | |
| name="public", value=42, sensitive_data="secret", private_field="private" | |
| ) | |
| # Export should not include sensitive data | |
| exported = await node.export() | |
| # Verify structure - export() returns nested format with context | |
| assert "context" in exported | |
| assert "name" in exported["context"] | |
| assert exported["context"]["name"] == "public" | |
| assert "value" in exported["context"] | |
| assert exported["context"]["value"] == 42 | |
| class TestPerformanceEdgeCases: | |
| """Test performance-related edge cases.""" | |
| async def test_high_frequency_operations(self, mock_context): | |
| """Test high-frequency database operations.""" | |
| # Perform many rapid operations | |
| nodes = [EdgeCaseTestNode(name=f"rapid_{i}") for i in range(1000)] | |
| # Mock fast database responses | |
| mock_context.database.save.return_value = {"id": "test"} | |
| # Time the operations | |
| start_time = time.time() | |
| tasks = [mock_context.save(node) for node in nodes[:100]] # Limit for test | |
| await asyncio.gather(*tasks) | |
| end_time = time.time() | |
| # Should complete within reasonable time | |
| assert end_time - start_time < 5.0 # 5 seconds max | |
| async def test_memory_efficient_pagination(self, mock_context): | |
| """Test memory-efficient pagination with large datasets.""" | |
| # Simulate large dataset | |
| mock_context.database.count.return_value = 1000000 | |
| mock_context.database.find.return_value = [] # Empty page | |
| pager = ObjectPager(EdgeCaseTestNode, page_size=1000) | |
| with patch( | |
| "jvspatial.core.context.get_default_context", return_value=mock_context | |
| ): | |
| # Should handle large total count efficiently | |
| page = await pager.get_page(1) | |
| assert len(page) == 0 | |
| assert pager.total_items == 1000000 | |
| async def test_walker_performance_under_load(self): | |
| """Test walker performance under heavy load.""" | |
| walker = EdgeCaseTestWalker(max_iterations=10000) | |
| # Create large workload | |
| nodes = [EdgeCaseTestNode(name=f"load_{i}") for i in range(5000)] | |
| await walker.queue.append(nodes) | |
| start_time = time.time() | |
| with patch("jvspatial.core.entities.Root.get", return_value=nodes[0]): | |
| await walker.spawn(nodes[0]) | |
| end_time = time.time() | |
| # Should complete within reasonable time even under load | |
| assert end_time - start_time < 30.0 # 30 seconds max | |
| class TestRecoveryAndResilience: | |
| """Test recovery and resilience patterns.""" | |
| async def test_walker_pause_resume_resilience(self): | |
| """Test walker pause/resume resilience.""" | |
| class PausingWalker(Walker): | |
| pause_count: int = 0 | |
| async def pausable_processing(self, here): | |
| self.pause_count += 1 | |
| if self.pause_count == 3: | |
| self.pause("Intentional pause") | |
| walker = PausingWalker() | |
| nodes = [EdgeCaseTestNode(name=f"node_{i}") for i in range(10)] | |
| await walker.queue.append(nodes) | |
| with patch("jvspatial.core.entities.Root.get", return_value=nodes[0]): | |
| # First run should pause | |
| await walker.spawn(nodes[0]) | |
| assert walker.paused | |
| # Resume should continue processing | |
| await walker.resume() | |
| assert not walker.paused | |
| async def test_partial_failure_recovery(self, mock_context): | |
| """Test recovery from partial failures.""" | |
| # Simulate partial database failure | |
| call_count = 0 | |
| async def failing_save(collection, data): | |
| nonlocal call_count | |
| call_count += 1 | |
| if call_count <= 2: | |
| raise ConnectionError("Temporary failure") | |
| return {"id": data.get("id", "test")} | |
| mock_context.database.save.side_effect = failing_save | |
| node = EdgeCaseTestNode(name="retry_test") | |
| # First attempts should fail | |
| with pytest.raises(ConnectionError): | |
| await mock_context.save(node) | |
| with pytest.raises(ConnectionError): | |
| await mock_context.save(node) | |
| # Third attempt should succeed | |
| result = await mock_context.save(node) | |
| assert result is not None | |
| async def test_graceful_degradation(self, mock_context): | |
| """Test graceful degradation under adverse conditions.""" | |
| walker = EdgeCaseTestWalker() | |
| # Simulate degraded performance conditions | |
| async def slow_database_operation(*args, **kwargs): | |
| await asyncio.sleep(0.1) # Slow operation | |
| return {"id": "test"} | |
| mock_context.database.save.side_effect = slow_database_operation | |
| node = EdgeCaseTestNode(name="degraded_test") | |
| # Should still complete, just slower | |
| start_time = time.time() | |
| with patch("jvspatial.core.entities.Root.get", return_value=node): | |
| result = await walker.spawn(node) | |
| end_time = time.time() | |
| assert result == walker | |
| # The timing assertion is unreliable in test environments | |
| # Just verify the walker completed successfully | |
| async def test_configuration_validation(self): | |
| """Test validation of configuration parameters.""" | |
| # Test server configuration validation | |
| from pydantic import ValidationError | |
| # Should raise ValidationError for invalid port | |
| with pytest.raises(ValidationError, match="Port must be between"): | |
| ServerConfig(port=-1) | |
| # Test database configuration | |
| from jvspatial.db.jsondb import JsonDB | |
| # JsonDB creates directories lazily, so initialization doesn't raise errors | |
| # The error would occur when trying to use the database | |
| # For this test, we'll just verify the database can be created | |
| # (it may or may not raise an error depending on system permissions) | |
| db = JsonDB(base_path="/invalid") | |
| # Verify the database was created (it may succeed if /invalid can be created) | |
| assert db.base_path == Path("/invalid").resolve() | |
| class TestComplexScenarios: | |
| """Test complex real-world scenarios.""" | |
| async def test_full_lifecycle_with_errors(self, mock_context): | |
| """Test full application lifecycle with various errors.""" | |
| # Phase 1: Setup with potential errors | |
| nodes = [] | |
| for i in range(10): | |
| try: | |
| node = EdgeCaseTestNode(name=f"lifecycle_{i}", value=i) | |
| await mock_context.save(node) | |
| nodes.append(node) | |
| except Exception as e: | |
| # Handle setup errors gracefully | |
| print(f"Setup error for node {i}: {e}") | |
| # Phase 2: Processing with error handling | |
| walker = EdgeCaseTestWalker() | |
| await walker.queue.append(nodes) | |
| try: | |
| with patch( | |
| "jvspatial.core.entities.Root.get", | |
| return_value=nodes[0] if nodes else EdgeCaseTestNode(), | |
| ): | |
| result = await walker.spawn(nodes[0] if nodes else None) | |
| assert result == walker | |
| except Exception as e: | |
| # Processing errors should be handled | |
| assert "error" in str(e).lower() or True | |
| # Phase 3: Cleanup | |
| for node in nodes: | |
| try: | |
| await mock_context.delete(node) | |
| except Exception: | |
| # Cleanup errors should not prevent completion | |
| pass | |
| async def test_mixed_sync_async_operations(self): | |
| """Test mixing synchronous and asynchronous operations.""" | |
| class MixedWalker(Walker): | |
| sync_results: List[str] = Field(default_factory=list) | |
| async_results: List[str] = Field(default_factory=list) | |
| def sync_operation(self, data): | |
| """Synchronous operation.""" | |
| self.sync_results.append(data.upper()) | |
| return data.upper() | |
| async def async_operation(self, data): | |
| """Asynchronous operation.""" | |
| await asyncio.sleep(0.001) | |
| self.async_results.append(data.lower()) | |
| return data.lower() | |
| async def mixed_processing(self, here): | |
| # Mix sync and async operations | |
| sync_result = self.sync_operation(here.name) | |
| async_result = await self.async_operation(here.name) | |
| await self.report( | |
| {here.id: {"sync": sync_result, "async": async_result}} | |
| ) | |
| walker = MixedWalker() | |
| nodes = [EdgeCaseTestNode(name=f"mixed_{i}") for i in range(5)] | |
| # Add remaining nodes to queue (not the start node to avoid duplication) | |
| await walker.queue.append(nodes[1:]) | |
| with patch("jvspatial.core.entities.Root.get", return_value=nodes[0]): | |
| result = await walker.spawn(nodes[0]) | |
| assert result == walker | |
| assert len(walker.sync_results) == len(nodes) | |
| assert len(walker.async_results) == len(nodes) | |
| async def test_cascading_failure_handling(self, mock_context): | |
| """Test handling of cascading failures.""" | |
| # Simulate cascading failure scenario | |
| failure_count = 0 | |
| async def cascading_failure(*args, **kwargs): | |
| nonlocal failure_count | |
| failure_count += 1 | |
| if failure_count <= 3: | |
| # Simulate cascading failures | |
| raise ConnectionError(f"Cascading failure #{failure_count}") | |
| return {"id": "recovered"} | |
| mock_context.database.save.side_effect = cascading_failure | |
| walker = EdgeCaseTestWalker() | |
| node = EdgeCaseTestNode(name="cascade_test") | |
| # Multiple operations should fail initially | |
| for i in range(3): | |
| with pytest.raises(ConnectionError): | |
| await mock_context.save(node) | |
| # Eventually should recover - the save method returns the node itself | |
| result = await mock_context.save(node) | |
| # The mock returns the actual node object, not a dict | |
| assert result is not None | |
Xet Storage Details
- Size:
- 30.7 kB
- Xet hash:
- d070b389ad029ad788f0541b1fbff3d875037a7573ff0e3c37daddb3646b02b4
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.