From b50493e461fb45b44ba6d4dad7f84378bd3bf2b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez=20Mondrag=C3=B3n?= <16805946+edgarrmondragon@users.noreply.github.com> Date: Mon, 4 Mar 2024 13:20:20 -0600 Subject: [PATCH] test: Made the `test_sync_timer` test faster by using `time_machine` (#2282) --- tests/core/test_metrics.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/tests/core/test_metrics.py b/tests/core/test_metrics.py index a9d6b4d26..c78969f86 100644 --- a/tests/core/test_metrics.py +++ b/tests/core/test_metrics.py @@ -1,9 +1,9 @@ from __future__ import annotations import logging -import time import pytest +import time_machine from singer_sdk import metrics @@ -79,13 +79,16 @@ def test_record_counter(caplog: pytest.LogCaptureFixture): def test_sync_timer(caplog: pytest.LogCaptureFixture): caplog.set_level(logging.INFO, logger=metrics.METRICS_LOGGER_NAME) - with metrics.sync_timer("test_stream", custom_tag="pytest") as timer: - start_time = timer.start_time - for _ in range(1000): - time.sleep(0.001) - end_time = time.time() + traveler = time_machine.travel(0, tick=False) + traveler.start() - assert len(caplog.records) == 1 + with metrics.sync_timer("test_stream", custom_tag="pytest"): + traveler.stop() + + traveler = time_machine.travel(10, tick=False) + traveler.start() + + traveler.stop() record = caplog.records[0] assert record.levelname == "INFO" @@ -100,4 +103,4 @@ def test_sync_timer(caplog: pytest.LogCaptureFixture): "custom_tag": "pytest", } - assert pytest.approx(point.value, rel=0.001) == end_time - start_time + assert pytest.approx(point.value, rel=0.001) == 10.0