Use monotonic time to protect from system time jumps

Change-Id: I44a51bef79109600a3f93844ac51a1f5738508b9
This commit is contained in:
Erik Olof Gunnar Andersson 2023-05-07 18:47:39 -07:00
parent 118294ac14
commit 437d2d9639
4 changed files with 10 additions and 10 deletions

View File

@ -202,11 +202,11 @@ class DynClient(object):
self._http_log_req(method, url, kwargs)
if self.timings:
start_time = time.time()
start_time = time.monotonic()
resp = self.http.request(method, url, **kwargs)
if self.timings:
self.times.append(("%s %s" % (method, url),
start_time, time.time()))
start_time, time.monotonic()))
self._http_log_resp(resp)
if resp.status_code >= 400:

View File

@ -203,12 +203,12 @@ class ZoneLock(object):
with self.lock:
# If no one holds the lock for the zone, grant it
if zone not in self.data:
self.data[zone] = time.time()
self.data[zone] = time.monotonic()
return True
# Otherwise, get the time that it was locked
locktime = self.data[zone]
now = time.time()
now = time.monotonic()
period = now - locktime

View File

@ -820,7 +820,7 @@ class TestCase(base.BaseTestCase):
Zone imports spawn a thread to parse the zone file and
insert the data. This waits for this process before continuing
"""
start_time = time.time()
start_time = time.monotonic()
while True:
# Retrieve it, and ensure it's the same
zone_import = self.central_service.get_zone_import(
@ -835,7 +835,7 @@ class TestCase(base.BaseTestCase):
if error_is_ok and zone_import.status != 'PENDING':
break
if (time.time() - start_time) > max_wait:
if (time.monotonic() - start_time) > max_wait:
break
time.sleep(0.5)
@ -870,8 +870,8 @@ class TestCase(base.BaseTestCase):
Poll every `interval` seconds. `condition` can be a callable.
(Caution: some mocks behave both as values and callables.)
"""
t_max = time.time() + timeout
while time.time() < t_max:
t_max = time.monotonic() + timeout
while time.monotonic() < t_max:
if callable(condition):
result = condition()
else:

View File

@ -70,9 +70,9 @@ class Executor(object):
if callable(tasks):
tasks = [tasks]
start_time = time.time()
start_time = time.monotonic()
results = [r for r in self._executor.map(self.do, tasks)]
elapsed_time = time.time() - start_time
elapsed_time = time.monotonic() - start_time
LOG.debug(
'Finished Task(s): %(tasks)s in %(time)fs',