Skip to content

Commit d4e5c18

Browse files
committed
Replaced time.time usage with time.monotonic
1 parent 84e41b7 commit d4e5c18

6 files changed

Lines changed: 23 additions & 20 deletions

File tree

CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ Version 3.1
2525
short type-hint rendering in the Sphinx config.
2626
- SSL: hostname verification now on by default; unknown ``cert_reqs``
2727
URI values fall back to ``CERT_REQUIRED`` instead of ``CERT_NONE``.
28+
- Switched all elapsed-duration checks (RPC and connection-state
29+
timeouts) from ``time.time()`` to ``time.monotonic()`` so system
30+
clock changes can no longer cause spurious timeouts or hangs.
2831

2932
Version 3.0
3033
-----------

amqpstorm/connection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,9 @@ def _wait_for_connection_state(
430430
431431
:return:
432432
"""
433-
start_time = time.time()
433+
start_time = time.monotonic()
434434
while self.current_state != state:
435435
self.check_for_errors()
436-
if time.time() - start_time > rpc_timeout:
436+
if time.monotonic() - start_time > rpc_timeout:
437437
raise AMQPConnectionError('connection timed out')
438438
time.sleep(IDLE_WAIT)

amqpstorm/rpc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@ def _wait_for_request(
139139
:param obj connection_adapter: Provide custom connection adapter.
140140
:return:
141141
"""
142-
start_time = time.time()
142+
start_time = time.monotonic()
143143
while not self._response[uuid]:
144144
connection_adapter.check_for_errors()
145-
if time.time() - start_time > self._timeout:
145+
if time.monotonic() - start_time > self._timeout:
146146
self._raise_rpc_timeout_error(uuid)
147147
time.sleep(IDLE_WAIT)
148148

amqpstorm/tests/functional/ssl/test_reliability.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,9 @@ def test_functional_ssl_close_performance(self):
194194
SSL_HOST, USERNAME, PASSWORD, timeout=60, port=5671, ssl=True,
195195
ssl_options=ssl_options)
196196

197-
start_time = time.time()
197+
start_time = time.monotonic()
198198
self.connection.close()
199-
self.assertLess(time.time() - start_time, 3)
199+
self.assertLess(time.monotonic() - start_time, 3)
200200

201201
@setup(new_connection=False)
202202
def test_functional_ssl_uri_connection(self):
@@ -287,9 +287,9 @@ def test_functional_publish_1k_with_ssl(self):
287287
consumer_thread.daemon = True
288288
consumer_thread.start()
289289

290-
start_time = time.time()
290+
start_time = time.monotonic()
291291
while self.messages_consumed != self.messages_to_send:
292-
if time.time() - start_time >= 60:
292+
if time.monotonic() - start_time >= 60:
293293
break
294294
time.sleep(0.1)
295295

amqpstorm/tests/functional/test_generic.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,9 @@ def on_message_second(message):
339339
# Sleep for 0.1s to make sure RabbitMQ has time to catch up.
340340
time.sleep(0.1)
341341

342-
start_time = time.time()
342+
start_time = time.monotonic()
343343
while len(inbound_messages) != 1:
344-
if time.time() - start_time >= 30:
344+
if time.monotonic() - start_time >= 30:
345345
break
346346
time.sleep(0.1)
347347

@@ -373,10 +373,10 @@ def on_message(message):
373373
queue=self.queue_name,
374374
no_ack=True)
375375

376-
start_time = time.time()
376+
start_time = time.monotonic()
377377
while len(inbound_messages) == 0:
378378
self.channel.process_data_events()
379-
if time.time() - start_time >= 30:
379+
if time.monotonic() - start_time >= 30:
380380
break
381381
time.sleep(0.1)
382382

amqpstorm/tests/functional/test_reliability.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ def test_functional_close_performance(self):
136136
"""
137137
for _ in range(10):
138138
self.connection = self._make_connection()
139-
start_time = time.time()
139+
start_time = time.monotonic()
140140
self.connection.close()
141-
self.assertLess(time.time() - start_time, 3)
141+
self.assertLess(time.monotonic() - start_time, 3)
142142

143143
@setup(new_connection=False)
144144
def test_functional_close_after_channel_close_forced_by_server(self):
@@ -159,13 +159,13 @@ def test_functional_close_after_channel_close_forced_by_server(self):
159159
)
160160
except (AMQPConnectionError, AMQPChannelError):
161161
pass
162-
start_time = time.time()
162+
start_time = time.monotonic()
163163
channel.close()
164-
self.assertLess(time.time() - start_time, 3)
164+
self.assertLess(time.monotonic() - start_time, 3)
165165

166-
start_time = time.time()
166+
start_time = time.monotonic()
167167
connection.close()
168-
self.assertLess(time.time() - start_time, 3)
168+
self.assertLess(time.monotonic() - start_time, 3)
169169

170170
@setup(new_connection=False)
171171
def test_functional_uri_connection(self):
@@ -267,9 +267,9 @@ def test_functional_publish_and_consume_1k_messages(self):
267267
consumer_thread.daemon = True
268268
consumer_thread.start()
269269

270-
start_time = time.time()
270+
start_time = time.monotonic()
271271
while self.messages_consumed != self.messages_to_send:
272-
if time.time() - start_time >= 60:
272+
if time.monotonic() - start_time >= 60:
273273
break
274274
time.sleep(0.1)
275275

0 commit comments

Comments
 (0)