|
| 1 | +# |
| 2 | +# This file is part of gunicorn released under the MIT license. |
| 3 | +# See the NOTICE for more information. |
| 4 | + |
| 5 | +"""Tests for dirty arbiter TTIN/TTOU signal handling.""" |
| 6 | + |
| 7 | +import signal |
| 8 | +from unittest.mock import Mock |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | + |
| 13 | +class TestDirtyArbiterSignals: |
| 14 | + """Test TTIN/TTOU signal handling in DirtyArbiter.""" |
| 15 | + |
| 16 | + @pytest.fixture |
| 17 | + def arbiter(self, tmp_path): |
| 18 | + """Create a DirtyArbiter for testing.""" |
| 19 | + from gunicorn.dirty.arbiter import DirtyArbiter |
| 20 | + |
| 21 | + cfg = Mock() |
| 22 | + cfg.dirty_workers = 2 |
| 23 | + cfg.dirty_apps = [] |
| 24 | + cfg.dirty_timeout = 30 |
| 25 | + cfg.dirty_graceful_timeout = 30 |
| 26 | + cfg.on_dirty_starting = Mock() |
| 27 | + log = Mock() |
| 28 | + |
| 29 | + arbiter = DirtyArbiter(cfg, log, socket_path=str(tmp_path / "test.sock")) |
| 30 | + return arbiter |
| 31 | + |
| 32 | + def test_initial_num_workers_from_config(self, arbiter): |
| 33 | + """num_workers should be initialized from config.""" |
| 34 | + assert arbiter.num_workers == 2 |
| 35 | + |
| 36 | + def test_ttin_increases_num_workers(self, arbiter): |
| 37 | + """SIGTTIN should increase num_workers by 1.""" |
| 38 | + assert arbiter.num_workers == 2 |
| 39 | + arbiter._signal_handler(signal.SIGTTIN, None) |
| 40 | + assert arbiter.num_workers == 3 |
| 41 | + |
| 42 | + def test_ttin_logs_info(self, arbiter): |
| 43 | + """SIGTTIN should log info about the change.""" |
| 44 | + arbiter._signal_handler(signal.SIGTTIN, None) |
| 45 | + arbiter.log.info.assert_called() |
| 46 | + call_args = arbiter.log.info.call_args[0] |
| 47 | + assert "SIGTTIN" in call_args[0] |
| 48 | + assert "3" in str(call_args) |
| 49 | + |
| 50 | + def test_ttou_decreases_num_workers(self, arbiter): |
| 51 | + """SIGTTOU should decrease num_workers by 1.""" |
| 52 | + arbiter.num_workers = 3 |
| 53 | + arbiter._signal_handler(signal.SIGTTOU, None) |
| 54 | + assert arbiter.num_workers == 2 |
| 55 | + |
| 56 | + def test_ttou_logs_info(self, arbiter): |
| 57 | + """SIGTTOU should log info about the change.""" |
| 58 | + arbiter.num_workers = 3 |
| 59 | + arbiter._signal_handler(signal.SIGTTOU, None) |
| 60 | + arbiter.log.info.assert_called() |
| 61 | + call_args = arbiter.log.info.call_args[0] |
| 62 | + assert "SIGTTOU" in call_args[0] |
| 63 | + assert "2" in str(call_args) |
| 64 | + |
| 65 | + def test_ttou_respects_minimum_one_worker(self, arbiter): |
| 66 | + """SIGTTOU should not go below 1 worker by default.""" |
| 67 | + arbiter.num_workers = 1 |
| 68 | + arbiter._signal_handler(signal.SIGTTOU, None) |
| 69 | + assert arbiter.num_workers == 1 |
| 70 | + |
| 71 | + def test_ttou_logs_warning_at_minimum(self, arbiter): |
| 72 | + """SIGTTOU should log warning when at minimum.""" |
| 73 | + arbiter.num_workers = 1 |
| 74 | + arbiter._signal_handler(signal.SIGTTOU, None) |
| 75 | + arbiter.log.warning.assert_called() |
| 76 | + call_args = arbiter.log.warning.call_args[0] |
| 77 | + assert "Cannot decrease below" in call_args[0] |
| 78 | + |
| 79 | + def test_ttou_respects_app_minimum(self, arbiter): |
| 80 | + """SIGTTOU should not go below app-required minimum.""" |
| 81 | + # App requires 3 workers |
| 82 | + arbiter.app_specs = { |
| 83 | + 'myapp:HeavyTask': { |
| 84 | + 'import_path': 'myapp:HeavyTask', |
| 85 | + 'worker_count': 3, |
| 86 | + 'original_spec': 'myapp:HeavyTask:3', |
| 87 | + } |
| 88 | + } |
| 89 | + arbiter.num_workers = 3 |
| 90 | + |
| 91 | + # Should not decrease below 3 |
| 92 | + arbiter._signal_handler(signal.SIGTTOU, None) |
| 93 | + assert arbiter.num_workers == 3 |
| 94 | + arbiter.log.warning.assert_called() |
| 95 | + |
| 96 | + def test_ttou_with_unlimited_app(self, arbiter): |
| 97 | + """Apps with worker_count=None should not impose minimum.""" |
| 98 | + arbiter.app_specs = { |
| 99 | + 'myapp:UnlimitedTask': { |
| 100 | + 'import_path': 'myapp:UnlimitedTask', |
| 101 | + 'worker_count': None, |
| 102 | + 'original_spec': 'myapp:UnlimitedTask', |
| 103 | + } |
| 104 | + } |
| 105 | + arbiter.num_workers = 2 |
| 106 | + |
| 107 | + # Should decrease to 1 (default minimum) |
| 108 | + arbiter._signal_handler(signal.SIGTTOU, None) |
| 109 | + assert arbiter.num_workers == 1 |
| 110 | + |
| 111 | + def test_multiple_ttin_signals(self, arbiter): |
| 112 | + """Multiple TTIN signals should keep incrementing.""" |
| 113 | + assert arbiter.num_workers == 2 |
| 114 | + arbiter._signal_handler(signal.SIGTTIN, None) |
| 115 | + arbiter._signal_handler(signal.SIGTTIN, None) |
| 116 | + arbiter._signal_handler(signal.SIGTTIN, None) |
| 117 | + assert arbiter.num_workers == 5 |
| 118 | + |
| 119 | + def test_multiple_ttou_signals(self, arbiter): |
| 120 | + """Multiple TTOU signals should decrement until minimum.""" |
| 121 | + arbiter.num_workers = 5 |
| 122 | + arbiter._signal_handler(signal.SIGTTOU, None) |
| 123 | + arbiter._signal_handler(signal.SIGTTOU, None) |
| 124 | + arbiter._signal_handler(signal.SIGTTOU, None) |
| 125 | + arbiter._signal_handler(signal.SIGTTOU, None) |
| 126 | + # Should stop at 1 |
| 127 | + assert arbiter.num_workers == 1 |
| 128 | + |
| 129 | + |
| 130 | +class TestGetMinimumWorkers: |
| 131 | + """Test _get_minimum_workers calculation.""" |
| 132 | + |
| 133 | + @pytest.fixture |
| 134 | + def arbiter(self, tmp_path): |
| 135 | + """Create a DirtyArbiter for testing.""" |
| 136 | + from gunicorn.dirty.arbiter import DirtyArbiter |
| 137 | + |
| 138 | + cfg = Mock() |
| 139 | + cfg.dirty_workers = 2 |
| 140 | + cfg.dirty_apps = [] |
| 141 | + cfg.dirty_timeout = 30 |
| 142 | + cfg.dirty_graceful_timeout = 30 |
| 143 | + cfg.on_dirty_starting = Mock() |
| 144 | + log = Mock() |
| 145 | + |
| 146 | + arbiter = DirtyArbiter(cfg, log, socket_path=str(tmp_path / "test.sock")) |
| 147 | + return arbiter |
| 148 | + |
| 149 | + def test_minimum_workers_no_apps(self, arbiter): |
| 150 | + """With no apps, minimum should be 1.""" |
| 151 | + arbiter.app_specs = {} |
| 152 | + assert arbiter._get_minimum_workers() == 1 |
| 153 | + |
| 154 | + def test_minimum_workers_single_app_with_limit(self, arbiter): |
| 155 | + """Single app with worker_count should set minimum.""" |
| 156 | + arbiter.app_specs = { |
| 157 | + 'app:Task': { |
| 158 | + 'import_path': 'app:Task', |
| 159 | + 'worker_count': 3, |
| 160 | + 'original_spec': 'app:Task:3', |
| 161 | + } |
| 162 | + } |
| 163 | + assert arbiter._get_minimum_workers() == 3 |
| 164 | + |
| 165 | + def test_minimum_workers_single_app_unlimited(self, arbiter): |
| 166 | + """Single app with worker_count=None should use default minimum.""" |
| 167 | + arbiter.app_specs = { |
| 168 | + 'app:Task': { |
| 169 | + 'import_path': 'app:Task', |
| 170 | + 'worker_count': None, |
| 171 | + 'original_spec': 'app:Task', |
| 172 | + } |
| 173 | + } |
| 174 | + assert arbiter._get_minimum_workers() == 1 |
| 175 | + |
| 176 | + def test_minimum_workers_multiple_apps_with_limits(self, arbiter): |
| 177 | + """Multiple apps should use the maximum worker_count.""" |
| 178 | + arbiter.app_specs = { |
| 179 | + 'app1:Task1': { |
| 180 | + 'import_path': 'app1:Task1', |
| 181 | + 'worker_count': 2, |
| 182 | + 'original_spec': 'app1:Task1:2', |
| 183 | + }, |
| 184 | + 'app2:Task2': { |
| 185 | + 'import_path': 'app2:Task2', |
| 186 | + 'worker_count': 4, |
| 187 | + 'original_spec': 'app2:Task2:4', |
| 188 | + }, |
| 189 | + 'app3:Task3': { |
| 190 | + 'import_path': 'app3:Task3', |
| 191 | + 'worker_count': 3, |
| 192 | + 'original_spec': 'app3:Task3:3', |
| 193 | + }, |
| 194 | + } |
| 195 | + # Maximum of (2, 4, 3) = 4 |
| 196 | + assert arbiter._get_minimum_workers() == 4 |
| 197 | + |
| 198 | + def test_minimum_workers_mixed_limited_and_unlimited(self, arbiter): |
| 199 | + """Mixed apps should use max of limited apps only.""" |
| 200 | + arbiter.app_specs = { |
| 201 | + 'app1:Task1': { |
| 202 | + 'import_path': 'app1:Task1', |
| 203 | + 'worker_count': 2, |
| 204 | + 'original_spec': 'app1:Task1:2', |
| 205 | + }, |
| 206 | + 'app2:Task2': { |
| 207 | + 'import_path': 'app2:Task2', |
| 208 | + 'worker_count': None, |
| 209 | + 'original_spec': 'app2:Task2', |
| 210 | + }, |
| 211 | + 'app3:Task3': { |
| 212 | + 'import_path': 'app3:Task3', |
| 213 | + 'worker_count': 4, |
| 214 | + 'original_spec': 'app3:Task3:4', |
| 215 | + }, |
| 216 | + } |
| 217 | + # Maximum of (2, 4) = 4, None is ignored |
| 218 | + assert arbiter._get_minimum_workers() == 4 |
| 219 | + |
| 220 | + def test_minimum_workers_all_unlimited(self, arbiter): |
| 221 | + """All unlimited apps should use default minimum.""" |
| 222 | + arbiter.app_specs = { |
| 223 | + 'app1:Task1': { |
| 224 | + 'import_path': 'app1:Task1', |
| 225 | + 'worker_count': None, |
| 226 | + 'original_spec': 'app1:Task1', |
| 227 | + }, |
| 228 | + 'app2:Task2': { |
| 229 | + 'import_path': 'app2:Task2', |
| 230 | + 'worker_count': None, |
| 231 | + 'original_spec': 'app2:Task2', |
| 232 | + }, |
| 233 | + } |
| 234 | + assert arbiter._get_minimum_workers() == 1 |
0 commit comments