|
| 1 | +"""Tests for per-state concurrency limits in batch execution.""" |
| 2 | + |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +pytestmark = pytest.mark.v2 |
| 8 | + |
| 9 | + |
| 10 | +class TestConcurrencyConfig: |
| 11 | + """Test ConcurrencyConfig dataclass.""" |
| 12 | + |
| 13 | + def test_defaults(self) -> None: |
| 14 | + from codeframe.core.conductor import ConcurrencyConfig |
| 15 | + |
| 16 | + cfg = ConcurrencyConfig() |
| 17 | + assert cfg.max_parallel == 4 |
| 18 | + assert cfg.by_status == {} |
| 19 | + |
| 20 | + def test_custom_values(self) -> None: |
| 21 | + from codeframe.core.conductor import ConcurrencyConfig |
| 22 | + |
| 23 | + cfg = ConcurrencyConfig(max_parallel=8, by_status={"READY": 3, "IN_PROGRESS": 2}) |
| 24 | + assert cfg.max_parallel == 8 |
| 25 | + assert cfg.by_status["READY"] == 3 |
| 26 | + |
| 27 | + def test_get_limit_for_status_configured(self) -> None: |
| 28 | + from codeframe.core.conductor import ConcurrencyConfig |
| 29 | + |
| 30 | + cfg = ConcurrencyConfig(max_parallel=4, by_status={"READY": 2}) |
| 31 | + assert cfg.get_limit_for_status("READY") == 2 |
| 32 | + |
| 33 | + def test_get_limit_for_status_fallback_to_global(self) -> None: |
| 34 | + from codeframe.core.conductor import ConcurrencyConfig |
| 35 | + |
| 36 | + cfg = ConcurrencyConfig(max_parallel=4, by_status={"READY": 2}) |
| 37 | + assert cfg.get_limit_for_status("IN_PROGRESS") == 4 |
| 38 | + |
| 39 | + def test_get_limit_for_status_empty_by_status(self) -> None: |
| 40 | + from codeframe.core.conductor import ConcurrencyConfig |
| 41 | + |
| 42 | + cfg = ConcurrencyConfig(max_parallel=6) |
| 43 | + assert cfg.get_limit_for_status("READY") == 6 |
| 44 | + |
| 45 | + def test_effective_workers_global_limit(self) -> None: |
| 46 | + from codeframe.core.conductor import ConcurrencyConfig |
| 47 | + |
| 48 | + cfg = ConcurrencyConfig(max_parallel=2, by_status={"READY": 5}) |
| 49 | + # Global limit (2) is less than per-status limit (5) |
| 50 | + workers = cfg.effective_workers(statuses=["READY"], group_size=10, global_running=0) |
| 51 | + assert workers == 2 |
| 52 | + |
| 53 | + def test_effective_workers_per_status_limit(self) -> None: |
| 54 | + from codeframe.core.conductor import ConcurrencyConfig |
| 55 | + |
| 56 | + cfg = ConcurrencyConfig(max_parallel=10, by_status={"READY": 3}) |
| 57 | + # Per-status limit (3) is less than global (10) |
| 58 | + workers = cfg.effective_workers(statuses=["READY"], group_size=10, global_running=0) |
| 59 | + assert workers == 3 |
| 60 | + |
| 61 | + def test_effective_workers_group_size_limit(self) -> None: |
| 62 | + from codeframe.core.conductor import ConcurrencyConfig |
| 63 | + |
| 64 | + cfg = ConcurrencyConfig(max_parallel=10) |
| 65 | + workers = cfg.effective_workers(statuses=["READY"], group_size=2, global_running=0) |
| 66 | + assert workers == 2 |
| 67 | + |
| 68 | + def test_effective_workers_accounts_for_running(self) -> None: |
| 69 | + from codeframe.core.conductor import ConcurrencyConfig |
| 70 | + |
| 71 | + cfg = ConcurrencyConfig(max_parallel=4) |
| 72 | + workers = cfg.effective_workers(statuses=["READY"], group_size=10, global_running=3) |
| 73 | + assert workers == 1 # Only 1 global slot left |
| 74 | + |
| 75 | + def test_effective_workers_mixed_statuses(self) -> None: |
| 76 | + from codeframe.core.conductor import ConcurrencyConfig |
| 77 | + |
| 78 | + cfg = ConcurrencyConfig(max_parallel=10, by_status={"READY": 3, "IN_PROGRESS": 1}) |
| 79 | + # Mixed group: bottleneck is IN_PROGRESS (1) |
| 80 | + workers = cfg.effective_workers(statuses=["READY", "IN_PROGRESS"], group_size=5, global_running=0) |
| 81 | + assert workers == 1 |
| 82 | + |
| 83 | + def test_effective_workers_never_negative(self) -> None: |
| 84 | + from codeframe.core.conductor import ConcurrencyConfig |
| 85 | + |
| 86 | + cfg = ConcurrencyConfig(max_parallel=2) |
| 87 | + workers = cfg.effective_workers(statuses=["READY"], group_size=5, global_running=10) |
| 88 | + assert workers >= 1 # At least 1 worker |
| 89 | + |
| 90 | + |
| 91 | +class TestBatchConfig: |
| 92 | + """Test BatchConfig in EnvironmentConfig.""" |
| 93 | + |
| 94 | + def test_defaults(self) -> None: |
| 95 | + from codeframe.core.config import EnvironmentConfig |
| 96 | + |
| 97 | + cfg = EnvironmentConfig() |
| 98 | + assert cfg.batch.max_parallel == 4 |
| 99 | + assert cfg.batch.max_parallel_by_status == {} |
| 100 | + |
| 101 | + def test_from_dict(self) -> None: |
| 102 | + from codeframe.core.config import EnvironmentConfig |
| 103 | + |
| 104 | + cfg = EnvironmentConfig.from_dict({ |
| 105 | + "batch": { |
| 106 | + "max_parallel": 8, |
| 107 | + "max_parallel_by_status": {"READY": 3, "IN_PROGRESS": 2}, |
| 108 | + } |
| 109 | + }) |
| 110 | + assert cfg.batch.max_parallel == 8 |
| 111 | + assert cfg.batch.max_parallel_by_status["READY"] == 3 |
| 112 | + |
| 113 | + def test_roundtrip(self) -> None: |
| 114 | + from codeframe.core.config import BatchConfig, EnvironmentConfig |
| 115 | + |
| 116 | + orig = EnvironmentConfig(batch=BatchConfig(max_parallel=6, max_parallel_by_status={"READY": 2})) |
| 117 | + d = orig.to_dict() |
| 118 | + restored = EnvironmentConfig.from_dict(d) |
| 119 | + assert restored.batch.max_parallel == 6 |
| 120 | + assert restored.batch.max_parallel_by_status["READY"] == 2 |
| 121 | + |
| 122 | + |
| 123 | +class TestStartBatchConcurrency: |
| 124 | + """Test start_batch with concurrency_by_status.""" |
| 125 | + |
| 126 | + def test_start_batch_accepts_concurrency_by_status(self) -> None: |
| 127 | + from codeframe.core.conductor import start_batch |
| 128 | + |
| 129 | + workspace = MagicMock() |
| 130 | + workspace.id = "w1" |
| 131 | + |
| 132 | + mock_task = MagicMock() |
| 133 | + mock_task.id = "t1" |
| 134 | + mock_task.title = "Test" |
| 135 | + |
| 136 | + with patch("codeframe.core.conductor.tasks.get", return_value=mock_task): |
| 137 | + with patch("codeframe.core.conductor._save_batch"): |
| 138 | + with patch("codeframe.core.conductor.events.emit_for_workspace"): |
| 139 | + with patch("codeframe.core.conductor._execute_serial"): |
| 140 | + batch = start_batch( |
| 141 | + workspace, ["t1"], |
| 142 | + concurrency_by_status={"READY": 2}, |
| 143 | + ) |
| 144 | + |
| 145 | + assert batch.concurrency.by_status == {"READY": 2} |
| 146 | + assert batch.concurrency.max_parallel == 4 # default |
| 147 | + |
| 148 | + |
| 149 | +class TestParseConcurrencyString: |
| 150 | + """Test parsing of --max-parallel-by-status CLI flag.""" |
| 151 | + |
| 152 | + def test_parse_valid_string(self) -> None: |
| 153 | + from codeframe.core.conductor import parse_concurrency_by_status |
| 154 | + |
| 155 | + result = parse_concurrency_by_status("READY=3,IN_PROGRESS=2") |
| 156 | + assert result == {"READY": 3, "IN_PROGRESS": 2} |
| 157 | + |
| 158 | + def test_parse_single_value(self) -> None: |
| 159 | + from codeframe.core.conductor import parse_concurrency_by_status |
| 160 | + |
| 161 | + result = parse_concurrency_by_status("READY=5") |
| 162 | + assert result == {"READY": 5} |
| 163 | + |
| 164 | + def test_parse_none_returns_empty(self) -> None: |
| 165 | + from codeframe.core.conductor import parse_concurrency_by_status |
| 166 | + |
| 167 | + result = parse_concurrency_by_status(None) |
| 168 | + assert result == {} |
| 169 | + |
| 170 | + def test_parse_empty_returns_empty(self) -> None: |
| 171 | + from codeframe.core.conductor import parse_concurrency_by_status |
| 172 | + |
| 173 | + result = parse_concurrency_by_status("") |
| 174 | + assert result == {} |
| 175 | + |
| 176 | + def test_parse_invalid_status_raises(self) -> None: |
| 177 | + from codeframe.core.conductor import parse_concurrency_by_status |
| 178 | + |
| 179 | + with pytest.raises(ValueError, match="Invalid status"): |
| 180 | + parse_concurrency_by_status("INVALID=3") |
| 181 | + |
| 182 | + def test_parse_invalid_format_raises(self) -> None: |
| 183 | + from codeframe.core.conductor import parse_concurrency_by_status |
| 184 | + |
| 185 | + with pytest.raises(ValueError): |
| 186 | + parse_concurrency_by_status("READY:3") |
0 commit comments