From 5cf7bf02ff5d7433503a48680b34198313f24b00 Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Thu, 14 Nov 2024 13:07:54 +0000 Subject: [PATCH] update testing to use parameterised tests and exhaustively test all the background status variations --- doajtest/fixtures/background.py | 8 +- .../background_task_status.matrix.csv | 36 ++ .../background_task_status.settings.csv | 17 +- .../background_task_status.settings.json | 150 +++++++ doajtest/unit/test_background_task_status.py | 288 -------------- ...st_background_task_status_parameterised.py | 366 ++++++------------ 6 files changed, 314 insertions(+), 551 deletions(-) create mode 100644 doajtest/matrices/background_task_status/background_task_status.matrix.csv create mode 100644 doajtest/matrices/background_task_status/background_task_status.settings.json delete mode 100644 doajtest/unit/test_background_task_status.py diff --git a/doajtest/fixtures/background.py b/doajtest/fixtures/background.py index 9f89581c7d..4733c9b5a1 100644 --- a/doajtest/fixtures/background.py +++ b/doajtest/fixtures/background.py @@ -52,12 +52,14 @@ def cleanup(self): def save_mock_bgjob(action=None, status=None, created_before_sec=0, is_save=True, - queue_id=None): + blocking=True, queue_id=None): bgjob = BackgroundJob() - if action: + if not action: from portality.tasks.journal_csv import JournalCSVBackgroundTask bgjob.action = JournalCSVBackgroundTask.__action__ + else: + bgjob.action = action if status: bgjob._set_with_struct("status", status) @@ -69,6 +71,6 @@ def save_mock_bgjob(action=None, status=None, created_before_sec=0, is_save=True bgjob.queue_id = queue_id if is_save: - bgjob.save(blocking=True) + bgjob.save(blocking=blocking) return bgjob diff --git a/doajtest/matrices/background_task_status/background_task_status.matrix.csv b/doajtest/matrices/background_task_status/background_task_status.matrix.csv new file mode 100644 index 0000000000..ec93701d08 --- /dev/null +++ b/doajtest/matrices/background_task_status/background_task_status.matrix.csv @@ -0,0 +1,36 @@ +test_id,in_queue,oldest_queued,error_count,error_age,lrs_success_or_error,queued,errors,lrs +1,0,young,0,out_of_period,complete,stable,stable,stable +2,0,young,0,out_of_period,error,stable,stable,unstable +3,0,young,0,out_of_period,empty,stable,stable,unstable +4,0,young,1,in_period,complete,stable,unstable,stable +5,0,young,1,in_period,error,stable,unstable,unstable +6,0,young,1,out_of_period,complete,stable,stable,stable +7,0,young,1,out_of_period,error,stable,stable,unstable +8,1,old,0,out_of_period,complete,unstable,stable,stable +9,1,old,0,out_of_period,error,unstable,stable,unstable +10,1,old,0,out_of_period,empty,unstable,stable,unstable +11,1,old,1,in_period,complete,unstable,unstable,stable +12,1,old,1,in_period,error,unstable,unstable,unstable +13,1,old,1,out_of_period,complete,unstable,stable,stable +14,1,old,1,out_of_period,error,unstable,stable,unstable +15,1,young,0,out_of_period,complete,stable,stable,stable +16,1,young,0,out_of_period,error,stable,stable,unstable +17,1,young,0,out_of_period,empty,stable,stable,unstable +18,1,young,1,in_period,complete,stable,unstable,stable +19,1,young,1,in_period,error,stable,unstable,unstable +20,1,young,1,out_of_period,complete,stable,stable,stable +21,1,young,1,out_of_period,error,stable,stable,unstable +22,2,old,0,out_of_period,complete,unstable,stable,stable +23,2,old,0,out_of_period,error,unstable,stable,unstable +24,2,old,0,out_of_period,empty,unstable,stable,unstable +25,2,old,1,in_period,complete,unstable,unstable,stable +26,2,old,1,in_period,error,unstable,unstable,unstable +27,2,old,1,out_of_period,complete,unstable,stable,stable +28,2,old,1,out_of_period,error,unstable,stable,unstable +29,2,young,0,out_of_period,complete,unstable,stable,stable +30,2,young,0,out_of_period,error,unstable,stable,unstable +31,2,young,0,out_of_period,empty,unstable,stable,unstable +32,2,young,1,in_period,complete,unstable,unstable,stable +33,2,young,1,in_period,error,unstable,unstable,unstable +34,2,young,1,out_of_period,complete,unstable,stable,stable +35,2,young,1,out_of_period,error,unstable,stable,unstable diff --git a/doajtest/matrices/background_task_status/background_task_status.settings.csv b/doajtest/matrices/background_task_status/background_task_status.settings.csv index 8fdcfd549f..755877f2bb 100644 --- a/doajtest/matrices/background_task_status/background_task_status.settings.csv +++ b/doajtest/matrices/background_task_status/background_task_status.settings.csv @@ -2,13 +2,16 @@ field,test_id,in_queue,oldest_queued,error_count,error_age,lrs_success_or_error, type,index,generated,generated,generated,generated,generated,conditional,conditional,conditional default,,,,,,,stable,stable,stable ,,,,,,,,, -values,,0,in_period,0,in_period,success,stable,stable,stable -values,,1,out_of_period,1,out_of_period,error,unstable,unstable,unstable -values,,,,,,empty,,, +values,,0,old,0,in_period,complete,stable,stable,stable +values,,1,young,1,out_of_period,error,unstable,unstable,unstable +values,,2,,,,empty,,, ,,,,,,,,, -constraint event_in_queue,,0,out_of_period,,,,,, -constraint event_error_count,,,,0,out_of_period,,,, +constraint in_queue,,0,young,,,,,, +constraint error_count,,,,0,out_of_period,,,, +constraint error_count,,,,1,,!empty,,, ,,,,,,,,, -conditional queued,,1,in_period,,,,unstable,, +conditional queued,,2,,,,,unstable,, +conditional queued,,,old,,,,unstable,, conditional errors,,,,1,in_period,,,unstable, -conditional lrs,,,,,,error,,,unstable \ No newline at end of file +conditional lrs,,,,,,error,,,unstable +conditional lrs,,,,,,empty,,,unstable \ No newline at end of file diff --git a/doajtest/matrices/background_task_status/background_task_status.settings.json b/doajtest/matrices/background_task_status/background_task_status.settings.json new file mode 100644 index 0000000000..f103b658bc --- /dev/null +++ b/doajtest/matrices/background_task_status/background_task_status.settings.json @@ -0,0 +1,150 @@ +{ + "parameters": [ + { + "name": "test_id", + "type": "index" + }, + { + "name": "in_queue", + "type": "generated", + "values": { + "0": { + "constraints": { + "oldest_queued": { + "or": [ + "young" + ] + } + } + }, + "1": {}, + "2": {} + } + }, + { + "name": "oldest_queued", + "type": "generated", + "values": { + "old": {}, + "young": {} + } + }, + { + "name": "error_count", + "type": "generated", + "values": { + "0": { + "constraints": { + "error_age": { + "or": [ + "out_of_period" + ] + } + } + }, + "1": { + "constraints": { + "lrs_success_or_error": { + "nor": [ + "empty" + ] + } + } + } + } + }, + { + "name": "error_age", + "type": "generated", + "values": { + "in_period": {}, + "out_of_period": {} + } + }, + { + "name": "lrs_success_or_error", + "type": "generated", + "values": { + "complete": {}, + "error": {}, + "empty": {} + } + }, + { + "name": "queued", + "type": "conditional", + "default": "stable", + "values": { + "stable": {}, + "unstable": { + "conditions": [ + { + "in_queue": { + "or": [ + "2" + ] + } + }, + { + "oldest_queued": { + "or": [ + "old" + ] + } + } + ] + } + } + }, + { + "name": "errors", + "type": "conditional", + "default": "stable", + "values": { + "stable": {}, + "unstable": { + "conditions": [ + { + "error_count": { + "or": [ + "1" + ] + }, + "error_age": { + "or": [ + "in_period" + ] + } + } + ] + } + } + }, + { + "name": "lrs", + "type": "conditional", + "default": "stable", + "values": { + "stable": {}, + "unstable": { + "conditions": [ + { + "lrs_success_or_error": { + "or": [ + "error" + ] + } + }, + { + "lrs_success_or_error": { + "or": [ + "empty" + ] + } + } + ] + } + } + } + ] +} \ No newline at end of file diff --git a/doajtest/unit/test_background_task_status.py b/doajtest/unit/test_background_task_status.py deleted file mode 100644 index 01c443fcd4..0000000000 --- a/doajtest/unit/test_background_task_status.py +++ /dev/null @@ -1,288 +0,0 @@ -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': { - 'events_queue': 0, - 'scheduled_long': 0, - 'scheduled_short': 0 - } -} - -bg_monitor_last_completed__a = { - 'BG_MONITOR_LAST_COMPLETED': { - 'events_queue': 10000, - 'scheduled_long': 10000, - 'scheduled_short': 10000 - } -} - -bg_monitor_last_successful = { - 'BG_MONITOR_LAST_SUCCESSFULLY_RUN_CONFIG': { - 'journal_csv': { - 'last_run_successful_in': 100 - } - } -} - - -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')) - - def test_various_combinations(self): - save_mock_bgjob(JournalCSVBackgroundTask.__action__, - queue_id=constants.BGJOB_QUEUE_ID_EVENTS, - status=constants.BGJOB_STATUS_COMPLETE, ) - save_mock_bgjob(AnonExportBackgroundTask.__action__, - queue_id=constants.BGJOB_QUEUE_ID_SCHEDULED_LONG, - status=constants.BGJOB_STATUS_COMPLETE, ) - status_dict = background_task_status.create_background_status() - print(json.dumps(status_dict, indent=4)) - - @apply_test_case_config(bg_monitor_last_completed__now) - def test_create_background_status__invalid_last_completed__events_queue(self): - save_mock_bgjob(JournalCSVBackgroundTask.__action__, - queue_id=constants.BGJOB_QUEUE_ID_EVENTS, - 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('events', {})) - self.assert_stable_dict(status_dict['queues'].get('scheduled_long', {})) - - @apply_test_case_config(bg_monitor_last_completed__now) - def test_create_background_status__invalid_last_completed__scheduled_long(self): - save_mock_bgjob(AnonExportBackgroundTask.__action__, - queue_id=constants.BGJOB_QUEUE_ID_SCHEDULED_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('events', {})) - self.assert_unstable_dict(status_dict['queues'].get('scheduled_long', {})) - - @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_EVENTS, - status=constants.BGJOB_STATUS_COMPLETE, ) - save_mock_bgjob(AnonExportBackgroundTask.__action__, - queue_id=constants.BGJOB_QUEUE_ID_SCHEDULED_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('events', {})) - self.assert_stable_dict(status_dict['queues'].get('scheduled_long', {})) - - @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']['scheduled_short']['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']['scheduled_short']['errors'])) - assert not is_stable(status_dict['queues']['scheduled_short']['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']['scheduled_short']['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']['scheduled_short']['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']['scheduled_short']['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']['scheduled_short']['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']['scheduled_short']['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']['scheduled_short']['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 - - @apply_test_case_config(bg_monitor_last_successful) - def test_create_background_status__last_run_successful(self): - save_mock_bgjob(JournalCSVBackgroundTask.__action__, - status=constants.BGJOB_STATUS_COMPLETE, ) - - status_dict = background_task_status.create_background_status() - print(json.dumps(status_dict, indent=4)) - - journal_csv_dict = status_dict['queues']['scheduled_short']['last_run_successful'].get(JournalCSVBackgroundTask.__action__, {}) - - #assert is_stable(status_dict['status']) - self.assert_stable_dict(journal_csv_dict) - assert journal_csv_dict.get('last_run') is not None - assert journal_csv_dict.get('last_run_status') == constants.BGJOB_STATUS_COMPLETE - - @apply_test_case_config(bg_monitor_last_successful) - def test_create_background_status__last_run_unsuccessful(self): - save_mock_bgjob(JournalCSVBackgroundTask.__action__, - status=constants.BGJOB_STATUS_ERROR, ) - - status_dict = background_task_status.create_background_status() - print(json.dumps(status_dict, indent=4)) - - journal_csv_dict = status_dict['queues']['scheduled_short']['last_run_successful'].get( - JournalCSVBackgroundTask.__action__, {}) - - #assert not is_stable(status_dict['status']) - self.assert_unstable_dict(journal_csv_dict) - assert journal_csv_dict.get('last_run') is not None - assert journal_csv_dict.get('last_run_status') == constants.BGJOB_STATUS_ERROR - - diff --git a/doajtest/unit/test_background_task_status_parameterised.py b/doajtest/unit/test_background_task_status_parameterised.py index 4c8504271e..7b78538f90 100644 --- a/doajtest/unit/test_background_task_status_parameterised.py +++ b/doajtest/unit/test_background_task_status_parameterised.py @@ -1,12 +1,13 @@ from parameterized import parameterized from combinatrix.testintegration import load_parameter_sets -from unittest import TestCase +from portality.models.background import BackgroundJob from portality.lib.paths import rel2abs -from portality.lib.normalise import normalise_doi def load_cases(): - return load_parameter_sets(rel2abs(__file__, "..", "matrices", "lib_normalise_doi"), "normalise_doi", "test_id", + return load_parameter_sets(rel2abs(__file__, "..", "matrices", "background_task_status"), + "background_task_status", + "test_id", {"test_id" : []}) import json @@ -19,72 +20,50 @@ def load_cases(): 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, - } +# Configures the monitoring period and the allowed number of errors in that period before a queue is marked +# as unstable +BG_MONITOR_ERRORS_CONFIG = { + 'set_in_doaj': { + 'check_sec': 3000, + 'allowed_num_err': 0 + }, + 'anon_export': { + 'check_sec': 3000, + 'allowed_num_err': 0, + }, + 'journal_csv': { + 'check_sec': 3000, + 'allowed_num_err': 0 } } -bg_monitor_queued_config__zero_total = { - 'BG_MONITOR_QUEUED_CONFIG': { - 'journal_csv': { - 'total': 0, - 'oldest': 1200, - } +# Configures the total number of queued items and the age of the oldest of those queued items allowed +# before the queue is marked as unstable. This is provided by type, so we can monitor all types separately +BG_MONITOR_QUEUED_CONFIG = { + 'set_in_doaj': { + 'total': 1, + 'oldest': 3000, + }, + 'anon_export': { + 'total': 1, + 'oldest': 3000, + }, + 'journal_csv': { + 'total': 1, + 'oldest': 3000, } } -# config BG_MONITOR_LAST_COMPLETED -bg_monitor_last_completed__now = { - 'BG_MONITOR_LAST_COMPLETED': { - 'events_queue': 0, - 'scheduled_long': 0, - 'scheduled_short': 0 - } -} - -bg_monitor_last_completed__a = { - 'BG_MONITOR_LAST_COMPLETED': { - 'events_queue': 10000, - 'scheduled_long': 10000, - 'scheduled_short': 10000 - } -} - -bg_monitor_last_successful = { - 'BG_MONITOR_LAST_SUCCESSFULLY_RUN_CONFIG': { - 'journal_csv': { - 'last_run_successful_in': 100 - } +BG_MONITOR_LAST_SUCCESSFULLY_RUN_CONFIG = { + 'anon_export': { + 'last_run_successful_in': 5000 + }, + 'set_in_doaj': { + 'last_run_successful_in': 5000 + }, + 'journal_csv': { + 'last_run_successful_in': 5000 } } @@ -98,6 +77,9 @@ def setUpClass(cls) -> None: JournalCSVBackgroundTask.__action__: constants.CRON_NEVER, AnonExportBackgroundTask.__action__: constants.CRON_NEVER, }, + "BG_MONITOR_ERRORS_CONFIG": BG_MONITOR_ERRORS_CONFIG, + "BG_MONITOR_QUEUED_CONFIG": BG_MONITOR_QUEUED_CONFIG, + "BG_MONITOR_LAST_SUCCESSFULLY_RUN_CONFIG": BG_MONITOR_LAST_SUCCESSFULLY_RUN_CONFIG }) @classmethod @@ -105,195 +87,73 @@ 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')) - - def test_various_combinations(self): - save_mock_bgjob(JournalCSVBackgroundTask.__action__, - queue_id=constants.BGJOB_QUEUE_ID_EVENTS, - status=constants.BGJOB_STATUS_COMPLETE, ) - save_mock_bgjob(AnonExportBackgroundTask.__action__, - queue_id=constants.BGJOB_QUEUE_ID_SCHEDULED_LONG, - status=constants.BGJOB_STATUS_COMPLETE, ) - status_dict = background_task_status.create_background_status() - print(json.dumps(status_dict, indent=4)) - - @apply_test_case_config(bg_monitor_last_completed__now) - def test_create_background_status__invalid_last_completed__events_queue(self): - save_mock_bgjob(JournalCSVBackgroundTask.__action__, - queue_id=constants.BGJOB_QUEUE_ID_EVENTS, - 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('events', {})) - self.assert_stable_dict(status_dict['queues'].get('scheduled_long', {})) - - @apply_test_case_config(bg_monitor_last_completed__now) - def test_create_background_status__invalid_last_completed__scheduled_long(self): - save_mock_bgjob(AnonExportBackgroundTask.__action__, - queue_id=constants.BGJOB_QUEUE_ID_SCHEDULED_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('events', {})) - self.assert_unstable_dict(status_dict['queues'].get('scheduled_long', {})) - - @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_EVENTS, - status=constants.BGJOB_STATUS_COMPLETE, ) - save_mock_bgjob(AnonExportBackgroundTask.__action__, - queue_id=constants.BGJOB_QUEUE_ID_SCHEDULED_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('events', {})) - self.assert_stable_dict(status_dict['queues'].get('scheduled_long', {})) - - @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']['scheduled_short']['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']['scheduled_short']['errors'])) - assert not is_stable(status_dict['queues']['scheduled_short']['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']['scheduled_short']['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']['scheduled_short']['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']['scheduled_short']['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']['scheduled_short']['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']['scheduled_short']['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']['scheduled_short']['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 - - @apply_test_case_config(bg_monitor_last_successful) - def test_create_background_status__last_run_successful(self): - save_mock_bgjob(JournalCSVBackgroundTask.__action__, - status=constants.BGJOB_STATUS_COMPLETE, ) - - status_dict = background_task_status.create_background_status() - print(json.dumps(status_dict, indent=4)) - - journal_csv_dict = status_dict['queues']['scheduled_short']['last_run_successful'].get(JournalCSVBackgroundTask.__action__, {}) - - #assert is_stable(status_dict['status']) - self.assert_stable_dict(journal_csv_dict) - assert journal_csv_dict.get('last_run') is not None - assert journal_csv_dict.get('last_run_status') == constants.BGJOB_STATUS_COMPLETE - - @apply_test_case_config(bg_monitor_last_successful) - def test_create_background_status__last_run_unsuccessful(self): - save_mock_bgjob(JournalCSVBackgroundTask.__action__, - status=constants.BGJOB_STATUS_ERROR, ) - - status_dict = background_task_status.create_background_status() - print(json.dumps(status_dict, indent=4)) - - journal_csv_dict = status_dict['queues']['scheduled_short']['last_run_successful'].get( - JournalCSVBackgroundTask.__action__, {}) - - #assert not is_stable(status_dict['status']) - self.assert_unstable_dict(journal_csv_dict) - assert journal_csv_dict.get('last_run') is not None - assert journal_csv_dict.get('last_run_status') == constants.BGJOB_STATUS_ERROR - + @parameterized.expand(load_cases) + def test_01_background_task_status(self, name, kwargs): + in_queue_arg = kwargs.get("in_queue") + oldest_queued_arg = kwargs.get("oldest_queued") + error_count_arg = kwargs.get("error_count") + error_age_arg = kwargs.get("error_age") + lrs_success_or_error_arg = kwargs.get("lrs_success_or_error") + queued_arg = kwargs.get("queued") + errors_arg = kwargs.get("errors") + lrs_arg = kwargs.get("lrs") + + in_queue = int(in_queue_arg) + oldest_queued = 3600 if oldest_queued_arg == "old" else 600 + error_count = int(error_count_arg) + error_age = 600 if error_age_arg == "in_period" else 3600 + + queues = [("events", "set_in_doaj"), + ("scheduled_long", "anon_export"), + ("scheduled_short", "journal_csv")] + + # set up + ########################################### + blocks = [] + for q, a in queues: + if in_queue > 0: + # create the number of jobs that should be in status "queued" + # their ages are set to the oldest allowed age + the index number for the purposes of disambiguation + for i in range(in_queue): + job = save_mock_bgjob(action=a, status="queued", created_before_sec=oldest_queued + i, is_save=True, blocking=False, queue_id=q) + blocks.append((job.id, job.last_updated)) + + if error_count > 0: + # create a single error job if the requested age + job = save_mock_bgjob(action=a, status="error", created_before_sec=error_age, is_save=True, blocking=False, queue_id=q) + blocks.append((job.id, job.last_updated)) + + if lrs_success_or_error_arg != "empty": + age = 4000 + # if there is an error that's being tested (see above), we need to make sure that the more recent job is "complete" + # to deactivate this test, if the lrs test is supposed to be "stable". + if error_count > 0 and lrs_success_or_error_arg == "complete": + age = 100 + job = save_mock_bgjob(action=a, status=lrs_success_or_error_arg, created_before_sec=age, is_save=True, blocking=False, queue_id=q) + blocks.append((job.id, job.last_updated)) + + BackgroundJob.blockall(blocks) + + # Execute + ########################################### + status = background_task_status.create_background_status() + + + # Assert + ########################################### + + for q, a in queues: + assert status['queues'][q]["errors"][a]["status"] == errors_arg + assert status['queues'][q]["queued"][a]["status"] == queued_arg + assert status['queues'][q]["last_run_successful"][a]["status"] == lrs_arg + + if "unstable" in [errors_arg, queued_arg, lrs_arg]: + assert status['queues'][q]["status"] == "unstable" + else: + assert status['queues'][q]["status"] == "stable" + + if "unstable" in [errors_arg, queued_arg, lrs_arg]: + assert status['status'] == "unstable" + else: + assert status['status'] == "stable"