Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /tests /unit /test_golden_chaos.py
| """Golden Test 1: Chaos Monkey - Swarm Resilience.""" | |
| import asyncio | |
| import pytest | |
| from agents.agent_base import AgentRole, BaseAgent | |
| from core.cache import MemoryCache | |
| class MockAgent(BaseAgent): | |
| """Mock agent for testing.""" | |
| async def execute_task(self, task): | |
| """Execute task.""" | |
| return {"status": "completed", "agent_id": self.id} | |
| class TestChaosMonkeySwarmResilience: | |
| """One test to rule them all.""" | |
| async def cache(self): | |
| """Create fresh cache.""" | |
| c = MemoryCache(max_size=1000) | |
| yield c | |
| await c.clear() | |
| async def swarm(self): | |
| """Create 10 test agents.""" | |
| agents = [] | |
| for i in range(10): | |
| role = AgentRole.RESEARCHER if i % 2 == 0 else AgentRole.ANALYST | |
| agent = MockAgent(name=f"chaos_test_{i}", role=role) | |
| agents.append(agent) | |
| return agents | |
| async def test_chaos_monkey_swarm_resilience(self, cache, swarm): | |
| """DeepSeek-Style: Maximum impact, single test.""" | |
| # 1. Start all agents | |
| start_tasks = [agent.start() for agent in swarm] | |
| await asyncio.gather(*start_tasks) | |
| await asyncio.sleep(0.1) | |
| # Verify all running | |
| for agent in swarm: | |
| assert agent.running is True | |
| await cache.set(f"agent:{agent.id}:state", "running") | |
| # 2. CHAOS: Crash half of them | |
| crashed_agents = swarm[::2] | |
| for agent in crashed_agents: | |
| if agent.task: | |
| agent.task.cancel() | |
| try: | |
| await agent.task | |
| except asyncio.CancelledError: | |
| pass | |
| agent.running = False | |
| await cache.set(f"agent:{agent.id}:state", "crashed") | |
| # 3. Recovery/Consistency Check | |
| await asyncio.sleep(0.1) | |
| for agent in swarm: | |
| cached_state = await cache.get(f"agent:{agent.id}:state") | |
| if agent in crashed_agents: | |
| assert cached_state == "crashed" | |
| assert agent.running is False | |
| else: | |
| assert agent.running is True | |
| assert cached_state == "running" | |
| # 4. Cleanup (Critical: No resource leaks) | |
| for agent in swarm: | |
| if agent.running: | |
| await agent.stop() | |
| await cache.delete(f"agent:{agent.id}:state") | |
Xet Storage Details
- Size:
- 2.45 kB
- Xet hash:
- f8b0aa1bb1e005bfcf8138ba5f0589fda37bebfc6f6e6d7449653adbfd43a232
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.