|
| 1 | +import datetime |
| 2 | + |
1 | 3 | from unittest.mock import patch
|
2 | 4 |
|
3 | 5 | from django.test import TestCase
|
| 6 | +from django.utils import timezone |
4 | 7 | from django_dynamic_fixture import get
|
5 | 8 |
|
6 |
| -from readthedocs.builds.constants import BUILD_STATUS_SUCCESS, EXTERNAL |
| 9 | +from readthedocs.builds.constants import ( |
| 10 | + BUILD_STATUS_SUCCESS, |
| 11 | + BUILD_STATE_CLONING, |
| 12 | + BUILD_STATE_CANCELLED, |
| 13 | + BUILD_STATE_TRIGGERED, |
| 14 | + EXTERNAL, |
| 15 | +) |
7 | 16 | from readthedocs.builds.models import Build, Version
|
8 |
| -from readthedocs.projects.models import Project |
9 |
| -from readthedocs.projects.tasks.utils import send_external_build_status |
| 17 | +from readthedocs.projects.models import Feature, Project |
| 18 | +from readthedocs.projects.tasks.utils import finish_unhealthy_builds, send_external_build_status |
10 | 19 |
|
11 | 20 |
|
12 | 21 | class SendBuildStatusTests(TestCase):
|
@@ -46,3 +55,53 @@ def test_send_external_build_status_with_internal_version(self, send_build_statu
|
46 | 55 | )
|
47 | 56 |
|
48 | 57 | send_build_status.delay.assert_not_called()
|
| 58 | + |
| 59 | +class TestFinishInactiveBuildsTask(TestCase): |
| 60 | + |
| 61 | + @patch("readthedocs.projects.tasks.utils.app") |
| 62 | + def test_finish_unhealthy_builds_task(self, mocked_app): |
| 63 | + project = get(Project) |
| 64 | + feature = get(Feature, feature_id=Feature.BUILD_HEALTHCHECK) |
| 65 | + feature.projects.add(project) |
| 66 | + |
| 67 | + # Build just started with the default time and healthcheck now |
| 68 | + build_1 = get( |
| 69 | + Build, |
| 70 | + project=project, |
| 71 | + version=project.get_stable_version(), |
| 72 | + state=BUILD_STATE_CLONING, |
| 73 | + healthcheck=timezone.now(), |
| 74 | + ) |
| 75 | + |
| 76 | + # Build started an hour ago with default time and healthcheck 59s ago |
| 77 | + build_2 = get( |
| 78 | + Build, |
| 79 | + project=project, |
| 80 | + version=project.get_stable_version(), |
| 81 | + state=BUILD_STATE_TRIGGERED, |
| 82 | + date=timezone.now() - datetime.timedelta(hours=1), |
| 83 | + healthcheck=timezone.now() - datetime.timedelta(seconds=59), |
| 84 | + ) |
| 85 | + |
| 86 | + # Build started an hour ago with custom time (2 hours) and healthcheck 15m ago |
| 87 | + build_3 = get( |
| 88 | + Build, |
| 89 | + project=project, |
| 90 | + version=project.get_stable_version(), |
| 91 | + state=BUILD_STATE_TRIGGERED, |
| 92 | + date=timezone.now() - datetime.timedelta(hours=2), |
| 93 | + healthcheck=timezone.now() - datetime.timedelta(minutes=15), |
| 94 | + ) |
| 95 | + |
| 96 | + finish_unhealthy_builds() |
| 97 | + |
| 98 | + build_1.refresh_from_db() |
| 99 | + self.assertEqual(build_1.state, BUILD_STATE_CLONING) |
| 100 | + |
| 101 | + build_2.refresh_from_db() |
| 102 | + self.assertEqual(build_2.state, BUILD_STATE_TRIGGERED) |
| 103 | + |
| 104 | + build_3.refresh_from_db() |
| 105 | + self.assertEqual(build_3.state, BUILD_STATE_CANCELLED) |
| 106 | + self.assertEqual(build_3.success, False) |
| 107 | + self.assertEqual(build_3.notifications.count(), 1) |
0 commit comments