|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from datetime import datetime, timedelta |
| 4 | +from unittest.mock import MagicMock, patch |
| 5 | + |
| 6 | +from django.utils import timezone |
| 7 | + |
| 8 | +from sentry.models.group import Group, GroupStatus |
| 9 | +from sentry.tasks.delete_pending_groups import ( |
| 10 | + MAX_LAST_SEEN_DAYS, |
| 11 | + MIN_LAST_SEEN_DAYS, |
| 12 | + delete_pending_groups, |
| 13 | +) |
| 14 | +from sentry.testutils.cases import TestCase |
| 15 | +from sentry.types.group import GroupSubStatus |
| 16 | + |
| 17 | + |
| 18 | +class DeletePendingGroupsTest(TestCase): |
| 19 | + def _count_groups_in_deletion_status(self) -> int: |
| 20 | + """Count groups with deletion statuses in the valid date range.""" |
| 21 | + return Group.objects.filter( |
| 22 | + status__in=[GroupStatus.PENDING_DELETION, GroupStatus.DELETION_IN_PROGRESS], |
| 23 | + last_seen__gte=self._days_ago(MAX_LAST_SEEN_DAYS), |
| 24 | + last_seen__lte=self._days_ago(MIN_LAST_SEEN_DAYS), |
| 25 | + ).count() |
| 26 | + |
| 27 | + def _days_ago(self, days: int) -> datetime: |
| 28 | + return timezone.now() - timedelta(days=days) |
| 29 | + |
| 30 | + def test_schedules_only_groups_within_valid_date_range(self) -> None: |
| 31 | + """Test that only groups with last_seen between 24h-90d are scheduled for deletion.""" |
| 32 | + project = self.create_project() |
| 33 | + |
| 34 | + # Too recent - within 24 hours (should NOT be scheduled) |
| 35 | + too_recent = self.create_group( |
| 36 | + project=project, status=GroupStatus.PENDING_DELETION, last_seen=self._days_ago(0) |
| 37 | + ) |
| 38 | + |
| 39 | + # Valid range - should be scheduled |
| 40 | + valid_group = self.create_group( |
| 41 | + project=project, status=GroupStatus.PENDING_DELETION, last_seen=self._days_ago(2) |
| 42 | + ) |
| 43 | + |
| 44 | + # Too old - over 90 days (should NOT be scheduled) |
| 45 | + too_old = self.create_group( |
| 46 | + project=project, status=GroupStatus.DELETION_IN_PROGRESS, last_seen=self._days_ago(91) |
| 47 | + ) |
| 48 | + |
| 49 | + # Wrong status - should NOT be scheduled |
| 50 | + wrong_status = self.create_group( |
| 51 | + project=project, |
| 52 | + status=GroupStatus.UNRESOLVED, |
| 53 | + substatus=GroupSubStatus.NEW, |
| 54 | + last_seen=self._days_ago(5), |
| 55 | + ) |
| 56 | + |
| 57 | + with patch( |
| 58 | + "sentry.tasks.delete_pending_groups.delete_groups_for_project.apply_async" |
| 59 | + ) as mock_delete_task: |
| 60 | + delete_pending_groups() |
| 61 | + |
| 62 | + # Verify only the valid group was scheduled |
| 63 | + mock_delete_task.assert_called_once() |
| 64 | + call_kwargs = mock_delete_task.call_args.kwargs["kwargs"] |
| 65 | + assert call_kwargs["object_ids"] == [valid_group.id] |
| 66 | + assert call_kwargs["project_id"] == project.id |
| 67 | + |
| 68 | + assert self._count_groups_in_deletion_status() != 0 |
| 69 | + with self.tasks(): |
| 70 | + delete_pending_groups() |
| 71 | + |
| 72 | + assert self._count_groups_in_deletion_status() == 0 |
| 73 | + assert list(Group.objects.all().values_list("id", flat=True).order_by("id")) == [ |
| 74 | + too_recent.id, |
| 75 | + too_old.id, |
| 76 | + wrong_status.id, |
| 77 | + ] |
| 78 | + |
| 79 | + @patch("sentry.tasks.delete_pending_groups.delete_groups_for_project.apply_async") |
| 80 | + def test_groups_by_project(self, mock_delete_task: MagicMock) -> None: |
| 81 | + """Test that groups are properly grouped by project when scheduling deletion.""" |
| 82 | + project1 = self.create_project() |
| 83 | + project2 = self.create_project() |
| 84 | + |
| 85 | + group1 = self.create_group( |
| 86 | + project=project1, status=GroupStatus.PENDING_DELETION, last_seen=self._days_ago(2) |
| 87 | + ) |
| 88 | + group2 = self.create_group( |
| 89 | + project=project1, status=GroupStatus.PENDING_DELETION, last_seen=self._days_ago(2) |
| 90 | + ) |
| 91 | + group3 = self.create_group( |
| 92 | + project=project2, status=GroupStatus.PENDING_DELETION, last_seen=self._days_ago(2) |
| 93 | + ) |
| 94 | + |
| 95 | + delete_pending_groups() |
| 96 | + |
| 97 | + assert mock_delete_task.call_count == 2 |
| 98 | + |
| 99 | + # Verify both projects got their deletion tasks scheduled |
| 100 | + all_calls = mock_delete_task.call_args_list |
| 101 | + project_ids = {call.kwargs["kwargs"]["project_id"] for call in all_calls} |
| 102 | + assert project_ids == {project1.id, project2.id} |
| 103 | + |
| 104 | + # Verify correct groups are in each call |
| 105 | + for call in all_calls: |
| 106 | + call_kwargs = call.kwargs["kwargs"] |
| 107 | + if call_kwargs["project_id"] == project1.id: |
| 108 | + assert set(call_kwargs["object_ids"]) == {group1.id, group2.id} |
| 109 | + elif call_kwargs["project_id"] == project2.id: |
| 110 | + assert set(call_kwargs["object_ids"]) == {group3.id} |
| 111 | + |
| 112 | + @patch("sentry.tasks.delete_pending_groups.GROUP_CHUNK_SIZE", new=10) |
| 113 | + @patch("sentry.tasks.delete_pending_groups.delete_groups_for_project.apply_async") |
| 114 | + @patch("sentry.tasks.delete_pending_groups.metrics.incr") |
| 115 | + def test_chunks_large_batches( |
| 116 | + self, |
| 117 | + mock_metrics_incr: MagicMock, |
| 118 | + mock_delete_task: MagicMock, |
| 119 | + ) -> None: |
| 120 | + """Test that groups are chunked according to GROUP_CHUNK_SIZE when scheduling deletion.""" |
| 121 | + GROUP_CHUNK_SIZE = 10 |
| 122 | + GROUPS_MORE_THAN_CHUNK_SIZE = 5 |
| 123 | + project = self.create_project() |
| 124 | + |
| 125 | + # Create more groups than GROUP_CHUNK_SIZE (10 in this test) |
| 126 | + num_groups = GROUPS_MORE_THAN_CHUNK_SIZE + GROUP_CHUNK_SIZE |
| 127 | + for _ in range(num_groups): |
| 128 | + self.create_group( |
| 129 | + project=project, status=GroupStatus.PENDING_DELETION, last_seen=self._days_ago(2) |
| 130 | + ) |
| 131 | + |
| 132 | + delete_pending_groups() |
| 133 | + |
| 134 | + # Should be called twice: one chunk of 10 and one of 5 |
| 135 | + assert mock_delete_task.call_count == 2 |
| 136 | + |
| 137 | + # Verify first chunk has GROUP_CHUNK_SIZE groups |
| 138 | + first_call_kwargs = mock_delete_task.call_args_list[0].kwargs["kwargs"] |
| 139 | + assert len(first_call_kwargs["object_ids"]) == GROUP_CHUNK_SIZE |
| 140 | + |
| 141 | + # Verify second chunk has remaining groups |
| 142 | + second_call_kwargs = mock_delete_task.call_args_list[1].kwargs["kwargs"] |
| 143 | + assert len(second_call_kwargs["object_ids"]) == GROUPS_MORE_THAN_CHUNK_SIZE |
| 144 | + |
| 145 | + # Assert metrics are called with correct totals |
| 146 | + incr_calls = mock_metrics_incr.call_args_list |
| 147 | + incr_names = [c.args[0] for c in incr_calls] |
| 148 | + assert "delete_pending_groups.groups_scheduled" in incr_names |
| 149 | + assert "delete_pending_groups.tasks_scheduled" in incr_names |
| 150 | + |
| 151 | + groups_scheduled_call = next( |
| 152 | + c for c in incr_calls if c.args[0] == "delete_pending_groups.groups_scheduled" |
| 153 | + ) |
| 154 | + assert groups_scheduled_call.kwargs["amount"] == num_groups |
| 155 | + |
| 156 | + tasks_scheduled_call = next( |
| 157 | + c for c in incr_calls if c.args[0] == "delete_pending_groups.tasks_scheduled" |
| 158 | + ) |
| 159 | + assert tasks_scheduled_call.kwargs["amount"] == 2 |
0 commit comments