-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_background_task_status.py
235 lines (183 loc) · 9.05 KB
/
test_background_task_status.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import json
from doajtest.fixtures.background import save_mock_bgjob
from doajtest.helpers import DoajTestCase, apply_test_case_config, patch_config
from portality import constants
from portality.bll import DOAJ
from portality.tasks.anon_export import AnonExportBackgroundTask
from portality.tasks.journal_csv import JournalCSVBackgroundTask
background_task_status = DOAJ.backgroundTaskStatusService()
is_stable = background_task_status.is_stable
# config BG_MONITOR_ERRORS_CONFIG
bg_monitor_errors_config__empty = {
'BG_MONITOR_ERRORS_CONFIG': {}
}
bg_monitor_errors_config__a = {
'BG_MONITOR_ERRORS_CONFIG': {
JournalCSVBackgroundTask.__action__: {
'check_sec': 3600,
'allowed_num_err': 0,
}
}
}
bg_monitor_errors_config__b = {
'BG_MONITOR_ERRORS_CONFIG': {
'kajdlaksj': {
'check_sec': 3600,
'allowed_num_err': 0,
}
}
}
# config BG_MONITOR_QUEUED_CONFIG
bg_monitor_queued_config__a = {
'BG_MONITOR_QUEUED_CONFIG': {
'journal_csv': {
'total': 99999999,
'oldest': 1200,
}
}
}
bg_monitor_queued_config__zero_total = {
'BG_MONITOR_QUEUED_CONFIG': {
'journal_csv': {
'total': 0,
'oldest': 1200,
}
}
}
# config BG_MONITOR_LAST_COMPLETED
bg_monitor_last_completed__now = {
'BG_MONITOR_LAST_COMPLETED': {
'main_queue': 0,
'long_running': 0,
}
}
bg_monitor_last_completed__a = {
'BG_MONITOR_LAST_COMPLETED': {
'main_queue': 10000,
'long_running': 10000,
}
}
class TestBackgroundTaskStatus(DoajTestCase):
@classmethod
def setUpClass(cls) -> None:
super().setUpClass()
cls.org_config = patch_config(cls.app_test, {
'HUEY_SCHEDULE': {
JournalCSVBackgroundTask.__action__: constants.CRON_NEVER,
AnonExportBackgroundTask.__action__: constants.CRON_NEVER,
},
})
@classmethod
def tearDownClass(cls) -> None:
super().tearDownClass()
patch_config(cls.app_test, cls.org_config)
@staticmethod
def assert_stable_dict(val: dict):
assert is_stable(val.get('status'))
assert len(val.get('err_msgs')) == 0
@staticmethod
def assert_unstable_dict(val):
assert not is_stable(val.get('status'))
assert len(val.get('err_msgs'))
@apply_test_case_config(bg_monitor_last_completed__now)
def test_create_background_status__invalid_last_completed__main_queue(self):
save_mock_bgjob(JournalCSVBackgroundTask.__action__,
queue_id=constants.BGJOB_QUEUE_ID_MAIN,
status=constants.BGJOB_STATUS_COMPLETE, )
status_dict = background_task_status.create_background_status()
assert not is_stable(status_dict['status'])
self.assert_unstable_dict(status_dict['queues'].get('main_queue', {}))
self.assert_stable_dict(status_dict['queues'].get('long_running', {}))
@apply_test_case_config(bg_monitor_last_completed__now)
def test_create_background_status__invalid_last_completed__long_running(self):
save_mock_bgjob(AnonExportBackgroundTask.__action__,
queue_id=constants.BGJOB_QUEUE_ID_LONG,
status=constants.BGJOB_STATUS_COMPLETE, )
status_dict = background_task_status.create_background_status()
assert not is_stable(status_dict['status'])
self.assert_stable_dict(status_dict['queues'].get('main_queue', {}))
self.assert_unstable_dict(status_dict['queues'].get('long_running', {}))
@apply_test_case_config(bg_monitor_last_completed__a)
def test_create_background_status__valid_last_completed(self):
save_mock_bgjob(JournalCSVBackgroundTask.__action__,
queue_id=constants.BGJOB_QUEUE_ID_MAIN,
status=constants.BGJOB_STATUS_COMPLETE, )
save_mock_bgjob(AnonExportBackgroundTask.__action__,
queue_id=constants.BGJOB_QUEUE_ID_LONG,
status=constants.BGJOB_STATUS_COMPLETE, )
status_dict = background_task_status.create_background_status()
assert is_stable(status_dict['status'])
self.assert_stable_dict(status_dict['queues'].get('main_queue', {}))
self.assert_stable_dict(status_dict['queues'].get('long_running', {}))
@apply_test_case_config(bg_monitor_last_completed__now)
def test_create_background_status__valid_last_completed__no_record(self):
status_dict = background_task_status.create_background_status()
assert is_stable(status_dict['status'])
@apply_test_case_config(bg_monitor_errors_config__empty)
def test_create_background_status__empty_errors_config(self):
save_mock_bgjob(JournalCSVBackgroundTask.__action__,
status=constants.BGJOB_STATUS_ERROR, )
status_dict = background_task_status.create_background_status()
journal_csv_dict = status_dict['queues']['main_queue']['errors'].get(JournalCSVBackgroundTask.__action__, {})
assert not is_stable(status_dict['status'])
assert journal_csv_dict
# unstable action should be on top of the list after sorting
first_key = next(iter(status_dict['queues']['main_queue']['errors']))
assert not is_stable(status_dict['queues']['main_queue']['errors'][first_key]['status'])
@apply_test_case_config(bg_monitor_errors_config__a)
def test_create_background_status__error_in_period_found(self):
save_mock_bgjob(JournalCSVBackgroundTask.__action__,
status=constants.BGJOB_STATUS_ERROR, )
status_dict = background_task_status.create_background_status()
journal_csv_dict = status_dict['queues']['main_queue']['errors'].get(JournalCSVBackgroundTask.__action__, {})
assert not is_stable(status_dict['status'])
self.assert_unstable_dict(journal_csv_dict)
assert journal_csv_dict.get('in_monitoring_period', 0) > 0
@apply_test_case_config(bg_monitor_errors_config__a)
def test_create_background_status__error_in_period_not_found(self):
save_mock_bgjob(JournalCSVBackgroundTask.__action__,
status=constants.BGJOB_STATUS_ERROR,
created_before_sec=1000000000)
status_dict = background_task_status.create_background_status()
journal_csv_dict = status_dict['queues']['main_queue']['errors'].get(JournalCSVBackgroundTask.__action__, {})
assert is_stable(status_dict['status'])
self.assert_stable_dict(journal_csv_dict)
assert journal_csv_dict.get('in_monitoring_period', 0) == 0
@apply_test_case_config(bg_monitor_queued_config__zero_total)
def test_create_background_status__queued_invalid_total(self):
save_mock_bgjob(JournalCSVBackgroundTask.__action__,
status=constants.BGJOB_STATUS_QUEUED, )
status_dict = background_task_status.create_background_status()
journal_csv_dict = status_dict['queues']['main_queue']['queued'].get(JournalCSVBackgroundTask.__action__, {})
assert not is_stable(status_dict['status'])
assert journal_csv_dict.get('total', 0)
self.assert_unstable_dict(journal_csv_dict)
@apply_test_case_config(bg_monitor_queued_config__zero_total)
def test_create_background_status__queued_valid_total(self):
save_mock_bgjob(JournalCSVBackgroundTask.__action__,
status=constants.BGJOB_STATUS_COMPLETE, )
status_dict = background_task_status.create_background_status()
journal_csv_dict = status_dict['queues']['main_queue']['queued'].get(JournalCSVBackgroundTask.__action__, {})
assert is_stable(status_dict['status'])
assert journal_csv_dict.get('total', 0) == 0
self.assert_stable_dict(journal_csv_dict)
@apply_test_case_config(bg_monitor_queued_config__a)
def test_create_background_status__queued_invalid_oldest(self):
save_mock_bgjob(JournalCSVBackgroundTask.__action__,
status=constants.BGJOB_STATUS_QUEUED,
created_before_sec=1000000000)
status_dict = background_task_status.create_background_status()
journal_csv_dict = status_dict['queues']['main_queue']['queued'].get(JournalCSVBackgroundTask.__action__, {})
assert not is_stable(status_dict['status'])
self.assert_unstable_dict(journal_csv_dict)
assert journal_csv_dict.get('oldest') is not None
@apply_test_case_config(bg_monitor_queued_config__a)
def test_create_background_status__queued_valid_oldest(self):
save_mock_bgjob(JournalCSVBackgroundTask.__action__,
status=constants.BGJOB_STATUS_QUEUED, )
status_dict = background_task_status.create_background_status()
print(json.dumps(status_dict, indent=4))
journal_csv_dict = status_dict['queues']['main_queue']['queued'].get(JournalCSVBackgroundTask.__action__, {})
assert is_stable(status_dict['status'])
self.assert_stable_dict(journal_csv_dict)
assert journal_csv_dict.get('oldest') is not None