From 7bc7090f614a260b4ed0df0a6c629f80e158bcc7 Mon Sep 17 00:00:00 2001 From: nischita44 Date: Fri, 9 Oct 2026 16:26:11 -0700 Subject: [PATCH] Make mostrecent gauge tests independent of clock resolution Signed-off-by: nischita44 --- tests/test_multiprocess.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tests/test_multiprocess.py b/tests/test_multiprocess.py index eab93f50..6451060c 100644 --- a/tests/test_multiprocess.py +++ b/tests/test_multiprocess.py @@ -3,6 +3,7 @@ import shutil import tempfile import unittest +from unittest import mock import warnings from prometheus_client import mmap_dict, values @@ -200,8 +201,11 @@ def test_gauge_mostrecent(self): g1 = Gauge('g', 'help', registry=None, multiprocess_mode='mostrecent') values.ValueClass = MultiProcessValue(lambda: 456) g2 = Gauge('g', 'help', registry=None, multiprocess_mode='mostrecent') - g2.set(2) - g1.set(1) + # Give each set() a distinct timestamp so the result doesn't depend on + # clock resolution or the order the .db files are read in. + with mock.patch('prometheus_client.metrics.time.time', side_effect=[1.0, 2.0]): + g2.set(2) + g1.set(1) self.assertEqual(1, self.registry.get_sample_value('g')) values.close_all_multiprocess_files() mark_process_dead(123, os.environ['PROMETHEUS_MULTIPROC_DIR']) @@ -211,8 +215,11 @@ def test_gauge_livemostrecent(self): g1 = Gauge('g', 'help', registry=None, multiprocess_mode='livemostrecent') values.ValueClass = MultiProcessValue(lambda: 456) g2 = Gauge('g', 'help', registry=None, multiprocess_mode='livemostrecent') - g2.set(2) - g1.set(1) + # Give each set() a distinct timestamp so the result doesn't depend on + # clock resolution or the order the .db files are read in. + with mock.patch('prometheus_client.metrics.time.time', side_effect=[1.0, 2.0]): + g2.set(2) + g1.set(1) self.assertEqual(1, self.registry.get_sample_value('g')) values.close_all_multiprocess_files() mark_process_dead(123, os.environ['PROMETHEUS_MULTIPROC_DIR'])