Skip to content

Commit 0eef1a2

Browse files
authored
Make mostrecent gauge tests independent of clock resolution (#1219)
Signed-off-by: nischita44 <nischitasadananda@gmail.com>
1 parent acb7b14 commit 0eef1a2

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

‎tests/test_multiprocess.py‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import shutil
44
import tempfile
55
import unittest
6+
from unittest import mock
67
import warnings
78

89
from prometheus_client import mmap_dict, values
@@ -200,8 +201,11 @@ def test_gauge_mostrecent(self):
200201
g1 = Gauge('g', 'help', registry=None, multiprocess_mode='mostrecent')
201202
values.ValueClass = MultiProcessValue(lambda: 456)
202203
g2 = Gauge('g', 'help', registry=None, multiprocess_mode='mostrecent')
203-
g2.set(2)
204-
g1.set(1)
204+
# Give each set() a distinct timestamp so the result doesn't depend on
205+
# clock resolution or the order the .db files are read in.
206+
with mock.patch('prometheus_client.metrics.time.time', side_effect=[1.0, 2.0]):
207+
g2.set(2)
208+
g1.set(1)
205209
self.assertEqual(1, self.registry.get_sample_value('g'))
206210
values.close_all_multiprocess_files()
207211
mark_process_dead(123, os.environ['PROMETHEUS_MULTIPROC_DIR'])
@@ -211,8 +215,11 @@ def test_gauge_livemostrecent(self):
211215
g1 = Gauge('g', 'help', registry=None, multiprocess_mode='livemostrecent')
212216
values.ValueClass = MultiProcessValue(lambda: 456)
213217
g2 = Gauge('g', 'help', registry=None, multiprocess_mode='livemostrecent')
214-
g2.set(2)
215-
g1.set(1)
218+
# Give each set() a distinct timestamp so the result doesn't depend on
219+
# clock resolution or the order the .db files are read in.
220+
with mock.patch('prometheus_client.metrics.time.time', side_effect=[1.0, 2.0]):
221+
g2.set(2)
222+
g1.set(1)
216223
self.assertEqual(1, self.registry.get_sample_value('g'))
217224
values.close_all_multiprocess_files()
218225
mark_process_dead(123, os.environ['PROMETHEUS_MULTIPROC_DIR'])

0 commit comments

Comments
 (0)