|
2 | 2 | Tests for Prometheus metrics integration. |
3 | 3 | """ |
4 | 4 |
|
| 5 | +from datetime import datetime, timedelta, timezone |
5 | 6 | from unittest.mock import Mock, patch |
6 | 7 |
|
7 | 8 | import pytest |
@@ -135,3 +136,142 @@ def test_collect_handles_errors(self): |
135 | 136 |
|
136 | 137 | # Should return empty list on error |
137 | 138 | assert len(metrics) == 0 |
| 139 | + |
| 140 | + @pytest.mark.skipif( |
| 141 | + not _prometheus_available(), |
| 142 | + reason="prometheus-client not installed", |
| 143 | + ) |
| 144 | + def test_collect_with_ready_tasks_age_metrics(self): |
| 145 | + """Test that collect() includes age metrics for READY tasks.""" |
| 146 | + from django_tasks_redis.metrics.collectors import RedisTaskMetricsCollector |
| 147 | + from django_tasks_redis.utils import serialize_datetime |
| 148 | + |
| 149 | + mock_backend = Mock() |
| 150 | + mock_backend.alias = "test" |
| 151 | + mock_backend.get_status_counts.return_value = { |
| 152 | + TaskResultStatus.READY: 3, |
| 153 | + TaskResultStatus.RUNNING: 1, |
| 154 | + } |
| 155 | + |
| 156 | + # Create mock tasks with different ages |
| 157 | + now = datetime.now(timezone.utc) |
| 158 | + oldest_task_time = now - timedelta(minutes=10) |
| 159 | + middle_task_time = now - timedelta(minutes=5) |
| 160 | + newest_task_time = now - timedelta(minutes=1) |
| 161 | + |
| 162 | + mock_backend.get_all_tasks.return_value = ( |
| 163 | + [ |
| 164 | + {"enqueued_at": serialize_datetime(oldest_task_time)}, |
| 165 | + {"enqueued_at": serialize_datetime(middle_task_time)}, |
| 166 | + {"enqueued_at": serialize_datetime(newest_task_time)}, |
| 167 | + ], |
| 168 | + 3, |
| 169 | + ) |
| 170 | + |
| 171 | + collector = RedisTaskMetricsCollector(mock_backend) |
| 172 | + |
| 173 | + # Call collect |
| 174 | + metrics = list(collector.collect()) |
| 175 | + |
| 176 | + # Verify get_all_tasks was called with READY status |
| 177 | + mock_backend.get_all_tasks.assert_called_once_with( |
| 178 | + status="READY", |
| 179 | + limit=3 |
| 180 | + ) |
| 181 | + |
| 182 | + # Should return 3 metrics: queue_length, oldest_age, newest_age |
| 183 | + assert len(metrics) == 3 |
| 184 | + |
| 185 | + metric_names = {m.name for m in metrics} |
| 186 | + assert "django_tasks_queue_length" in metric_names |
| 187 | + assert "django_tasks_queue_oldest_ready_age_seconds" in metric_names |
| 188 | + assert "django_tasks_queue_newest_ready_age_seconds" in metric_names |
| 189 | + |
| 190 | + # Check the age metrics values |
| 191 | + oldest_metric = next( |
| 192 | + m for m in metrics |
| 193 | + if m.name == "django_tasks_queue_oldest_ready_age_seconds" |
| 194 | + ) |
| 195 | + newest_metric = next( |
| 196 | + m for m in metrics |
| 197 | + if m.name == "django_tasks_queue_newest_ready_age_seconds" |
| 198 | + ) |
| 199 | + |
| 200 | + # Get the metric values (samples are stored in the metric family) |
| 201 | + oldest_samples = list(oldest_metric.samples) |
| 202 | + newest_samples = list(newest_metric.samples) |
| 203 | + |
| 204 | + assert len(oldest_samples) == 1 |
| 205 | + assert len(newest_samples) == 1 |
| 206 | + |
| 207 | + # Oldest should be ~600 seconds (10 minutes) |
| 208 | + assert 590 <= oldest_samples[0].value <= 610 |
| 209 | + |
| 210 | + # Newest should be ~60 seconds (1 minute) |
| 211 | + assert 50 <= newest_samples[0].value <= 70 |
| 212 | + |
| 213 | + @pytest.mark.skipif( |
| 214 | + not _prometheus_available(), |
| 215 | + reason="prometheus-client not installed", |
| 216 | + ) |
| 217 | + def test_collect_without_ready_tasks_no_age_metrics(self): |
| 218 | + """Test that collect() doesn't include age metrics when no READY tasks.""" |
| 219 | + from django_tasks_redis.metrics.collectors import RedisTaskMetricsCollector |
| 220 | + |
| 221 | + mock_backend = Mock() |
| 222 | + mock_backend.alias = "test" |
| 223 | + mock_backend.get_status_counts.return_value = { |
| 224 | + TaskResultStatus.RUNNING: 5, |
| 225 | + TaskResultStatus.SUCCESSFUL: 10, |
| 226 | + } |
| 227 | + |
| 228 | + collector = RedisTaskMetricsCollector(mock_backend) |
| 229 | + |
| 230 | + # Call collect |
| 231 | + metrics = list(collector.collect()) |
| 232 | + |
| 233 | + # Should only return queue_length metric, no age metrics |
| 234 | + assert len(metrics) == 1 |
| 235 | + assert metrics[0].name == "django_tasks_queue_length" |
| 236 | + |
| 237 | + # get_all_tasks should not be called when no READY tasks |
| 238 | + mock_backend.get_all_tasks.assert_not_called() |
| 239 | + |
| 240 | + @pytest.mark.skipif( |
| 241 | + not _prometheus_available(), |
| 242 | + reason="prometheus-client not installed", |
| 243 | + ) |
| 244 | + def test_collect_with_invalid_enqueued_at(self): |
| 245 | + """Test that collect() handles tasks with invalid enqueued_at gracefully.""" |
| 246 | + from django_tasks_redis.metrics.collectors import RedisTaskMetricsCollector |
| 247 | + from django_tasks_redis.utils import serialize_datetime |
| 248 | + |
| 249 | + mock_backend = Mock() |
| 250 | + mock_backend.alias = "test" |
| 251 | + mock_backend.get_status_counts.return_value = { |
| 252 | + TaskResultStatus.READY: 3, |
| 253 | + } |
| 254 | + |
| 255 | + now = datetime.now(timezone.utc) |
| 256 | + valid_task_time = now - timedelta(minutes=5) |
| 257 | + |
| 258 | + # Mix of valid and invalid tasks |
| 259 | + mock_backend.get_all_tasks.return_value = ( |
| 260 | + [ |
| 261 | + {"enqueued_at": serialize_datetime(valid_task_time)}, |
| 262 | + {"enqueued_at": "invalid-datetime"}, |
| 263 | + {"enqueued_at": ""}, |
| 264 | + ], |
| 265 | + 3, |
| 266 | + ) |
| 267 | + |
| 268 | + collector = RedisTaskMetricsCollector(mock_backend) |
| 269 | + |
| 270 | + # Call collect - should not raise exception |
| 271 | + metrics = list(collector.collect()) |
| 272 | + |
| 273 | + # Should still return metrics based on valid tasks |
| 274 | + metric_names = {m.name for m in metrics} |
| 275 | + assert "django_tasks_queue_length" in metric_names |
| 276 | + assert "django_tasks_queue_oldest_ready_age_seconds" in metric_names |
| 277 | + assert "django_tasks_queue_newest_ready_age_seconds" in metric_names |
0 commit comments