From 7f150612af277cd999c492f392905ab6e107d2e4 Mon Sep 17 00:00:00 2001 From: Bill Wei Date: Tue, 10 Jun 2025 14:16:00 -0400 Subject: [PATCH] fix: patch rather than sleep for time sensitive pytest --- tests/unit/test_application_settings.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/tests/unit/test_application_settings.py b/tests/unit/test_application_settings.py index dcde38643..7f12e7310 100644 --- a/tests/unit/test_application_settings.py +++ b/tests/unit/test_application_settings.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import time +import datetime from unittest.mock import Mock, patch import pytest @@ -124,7 +124,20 @@ def test_read_remote_setting(eda_caplog): }, ] - with patch("aap_eda.analytics.utils.requests.get") as mock_get: + now = datetime.datetime(2025, 1, 1, 0, 0) + now_effect = [ + now, + now + datetime.timedelta(milliseconds=100), + now + datetime.timedelta(milliseconds=200), + now + datetime.timedelta(milliseconds=300), + now + datetime.timedelta(milliseconds=1400), + now + datetime.timedelta(milliseconds=1500), + ] + + with ( + patch("django.utils.timezone.now", side_effect=now_effect), + patch("aap_eda.analytics.utils.requests.get") as mock_get, + ): mock_get.return_value = mock_resp assert application_settings.AUTOMATION_ANALYTICS_GATHER_INTERVAL == 500 assert application_settings.REDHAT_USERNAME == "foo" @@ -132,7 +145,6 @@ def test_read_remote_setting(eda_caplog): assert application_settings.AUTOMATION_ANALYTICS_GATHER_INTERVAL == 500 assert application_settings.REDHAT_USERNAME == "foo" - time.sleep(1) # repeat after interval assert ( application_settings.AUTOMATION_ANALYTICS_GATHER_INTERVAL == 1000