|
| 1 | + |
| 2 | +import pytest |
| 3 | +from unittest.mock import MagicMock, patch, ANY |
| 4 | +import datetime |
| 5 | +import sys |
| 6 | + |
| 7 | +# Mock modules that might cause side effects on import or are hard to set up |
| 8 | +sys.modules['augur.tasks.init'] = MagicMock() |
| 9 | +sys.modules['augur.tasks.init'].get_redis_conn_values = MagicMock(return_value=(0, 'redis://localhost:6379/')) |
| 10 | +sys.modules['augur.tasks.init'].get_rabbitmq_conn_string = MagicMock(return_value='amqp://guest:guest@localhost:5672//') |
| 11 | + |
| 12 | +# We need to ensure we can import the module now |
| 13 | +# The module augur.tasks.init.celery_app imports get_redis_conn_values etc from augur.tasks.init |
| 14 | +# which we just mocked. |
| 15 | + |
| 16 | +from augur.tasks.init.celery_app import setup_periodic_tasks, split_tasks_into_groups |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def mock_sender(): |
| 20 | + sender = MagicMock() |
| 21 | + return sender |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def mock_config_factory(): |
| 25 | + def _create_config(settings): |
| 26 | + config_mock = MagicMock() |
| 27 | + |
| 28 | + def get_value(section, key): |
| 29 | + val = settings.get(section, {}).get(key) |
| 30 | + return val |
| 31 | + |
| 32 | + config_mock.get_value.side_effect = get_value |
| 33 | + return config_mock |
| 34 | + return _create_config |
| 35 | + |
| 36 | +class TestCeleryScheduler: |
| 37 | + |
| 38 | + @patch('augur.tasks.init.celery_app.AugurConfig') |
| 39 | + @patch('augur.tasks.init.celery_app.DatabaseSession') |
| 40 | + @patch('augur.tasks.init.celery_app.temporary_database_engine') |
| 41 | + @patch('augur.tasks.init.celery_app.non_repo_domain_tasks') |
| 42 | + @patch('augur.tasks.init.celery_app.retry_errored_repos') |
| 43 | + @patch('augur.tasks.init.celery_app.process_contributors') |
| 44 | + @patch('augur.tasks.init.celery_app.create_collection_status_records') |
| 45 | + @patch('augur.tasks.init.celery_app.refresh_materialized_views') |
| 46 | + @patch('augur.tasks.init.celery_app.augur_collection_monitor') |
| 47 | + def test_setup_periodic_tasks_scheduling( |
| 48 | + self, |
| 49 | + mock_monitor, |
| 50 | + mock_refresh_views, |
| 51 | + mock_create_status, |
| 52 | + mock_process_contributors, |
| 53 | + mock_retry_repos, |
| 54 | + mock_non_repo_tasks, |
| 55 | + mock_temp_engine, |
| 56 | + mock_db_session, |
| 57 | + mock_AugurConfig, |
| 58 | + mock_sender, |
| 59 | + mock_config_factory |
| 60 | + ): |
| 61 | + # Setup config |
| 62 | + settings = { |
| 63 | + 'Tasks': { |
| 64 | + 'collection_interval': 30, |
| 65 | + 'non_repo_domain_tasks_interval_in_days': 15, |
| 66 | + 'retry_errored_repos_cron_hour': 2, |
| 67 | + 'retry_errored_repos_cron_minute': 30, |
| 68 | + 'process_contributors_interval_in_seconds': 1200, |
| 69 | + 'create_collection_status_records_interval_in_seconds': 4000 |
| 70 | + }, |
| 71 | + 'Celery': { |
| 72 | + 'refresh_materialized_views_interval_in_days': 1 |
| 73 | + } |
| 74 | + } |
| 75 | + mock_AugurConfig.return_value = mock_config_factory(settings) |
| 76 | + |
| 77 | + # Run setup |
| 78 | + setup_periodic_tasks(mock_sender) |
| 79 | + |
| 80 | + # Verify calls |
| 81 | + |
| 82 | + # 1. non_repo_domain_tasks: 15 days |
| 83 | + # The code usually converts days to seconds or timedelta |
| 84 | + # 15 days = 15 * 24 * 60 * 60 = 1296000 seconds |
| 85 | + |
| 86 | + # We need to inspect what arguments add_periodic_task was called with. |
| 87 | + # It's called multiple times. |
| 88 | + |
| 89 | + # Look for non_repo_domain_tasks |
| 90 | + # We can check if it was called with approximately the right value or check if the task signature was passed |
| 91 | + |
| 92 | + # Let's verify that the task was scheduled with the correct interval |
| 93 | + # Since we haven't implemented the change yet, we expect this to fail if we ran it, or pass if we check for the NEW behavior we want. |
| 94 | + # This test defines the expected behavior. |
| 95 | + |
| 96 | + # Expected: |
| 97 | + # non_repo_domain_tasks.s() passed as second arg |
| 98 | + # 15 * 86400 or timedelta(days=15) passed as first arg |
| 99 | + |
| 100 | + # Find call for non_repo_domain_tasks |
| 101 | + found_non_repo = False |
| 102 | + for call in mock_sender.add_periodic_task.call_args_list: |
| 103 | + args, _ = call |
| 104 | + interval = args[0] |
| 105 | + task_sig = args[1] |
| 106 | + |
| 107 | + if task_sig == mock_non_repo_tasks.s(): |
| 108 | + found_non_repo = True |
| 109 | + # Check interval. |
| 110 | + # Our implementation will likely use timedelta or seconds. |
| 111 | + # 15 days in seconds is 1296000 |
| 112 | + if isinstance(interval, (int, float)): |
| 113 | + assert interval == 1296000 |
| 114 | + elif isinstance(interval, datetime.timedelta): |
| 115 | + assert interval.days == 15 |
| 116 | + |
| 117 | + assert found_non_repo, "non_repo_domain_tasks was not scheduled" |
| 118 | + |
| 119 | + @patch('augur.tasks.init.celery_app.AugurConfig') |
| 120 | + @patch('augur.tasks.init.celery_app.DatabaseSession') |
| 121 | + @patch('augur.tasks.init.celery_app.temporary_database_engine') |
| 122 | + @patch('augur.tasks.init.celery_app.process_contributors') |
| 123 | + def test_disabled_tasks( |
| 124 | + self, |
| 125 | + mock_process_contributors, |
| 126 | + mock_temp_engine, |
| 127 | + mock_db_session, |
| 128 | + mock_AugurConfig, |
| 129 | + mock_sender, |
| 130 | + mock_config_factory |
| 131 | + ): |
| 132 | + settings = { |
| 133 | + 'Tasks': { |
| 134 | + 'collection_interval': 30, |
| 135 | + 'process_contributors_interval_in_seconds': 0, # Disabled |
| 136 | + }, |
| 137 | + 'Celery': { |
| 138 | + 'refresh_materialized_views_interval_in_days': 1 |
| 139 | + } |
| 140 | + } |
| 141 | + mock_AugurConfig.return_value = mock_config_factory(settings) |
| 142 | + |
| 143 | + setup_periodic_tasks(mock_sender) |
| 144 | + |
| 145 | + # Verify process_contributors was NOT scheduled |
| 146 | + found_process = False |
| 147 | + for call in mock_sender.add_periodic_task.call_args_list: |
| 148 | + args, _ = call |
| 149 | + task_sig = args[1] |
| 150 | + if task_sig == mock_process_contributors.s(): |
| 151 | + found_process = True |
| 152 | + |
| 153 | + assert not found_process, "process_contributors should be disabled" |
0 commit comments