Skip to content

Commit dfc06d3

Browse files
authored
[RAI-25151]: Fix polling duration in poll_with_specified_overhead function (#151)
* Fix polling duration calculation in poll_with_specified_overhead function * add more tests * fix test * move decorator to the specific test * fix test
1 parent a218395 commit dfc06d3

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

.github/actions/test/action.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,5 @@ runs:
6161
CUSTOM_HEADERS: ${{ inputs.custom_headers }}
6262
run: |
6363
mkdir -p ~/.rai
64-
python -m unittest
64+
python -m unittest -v
6565
shell: bash

railib/api.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -331,27 +331,32 @@ def poll_with_specified_overhead(
331331
max_tries: int = None,
332332
max_delay: int = 120,
333333
):
334+
if overhead_rate < 0:
335+
raise ValueError("overhead_rate must be non-negative")
336+
334337
if start_time is None:
335338
start_time = time.time()
339+
336340
tries = 0
337341
max_time = time.time() + timeout if timeout else None
338342

339343
while True:
340344
if f():
341345
break
342346

347+
current_time = time.time()
348+
343349
if max_tries is not None and tries >= max_tries:
344350
raise Exception(f'max tries {max_tries} exhausted')
345351

346-
if max_time is not None and time.time() >= max_time:
352+
if max_time is not None and current_time >= max_time:
347353
raise Exception(f'timed out after {timeout} seconds')
348354

355+
duration = (current_time - start_time) * overhead_rate
356+
duration = min(duration, max_delay)
357+
358+
time.sleep(duration)
349359
tries += 1
350-
duration = min((time.time() - start_time) * overhead_rate, max_delay)
351-
if tries == 1:
352-
time.sleep(0.5)
353-
else:
354-
time.sleep(duration)
355360

356361

357362
def is_engine_term_state(state: str) -> bool:

test/test_unit.py

+43
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import socket
2+
import time
23
import unittest
34
from unittest.mock import patch, MagicMock
45
from urllib.error import URLError
@@ -27,6 +28,48 @@ def test_validation(self):
2728
api.poll_with_specified_overhead(lambda: True, overhead_rate=0.1, max_tries=1)
2829
api.poll_with_specified_overhead(lambda: True, overhead_rate=0.1, timeout=1, max_tries=1)
2930

31+
@patch('time.sleep', return_value=None)
32+
@patch('time.time')
33+
def test_initial_delay(self, mock_time, mock_sleep):
34+
start_time = 100 # Fixed start time
35+
increment_time = 0.0001
36+
mock_time.side_effect = [start_time, start_time + increment_time] # Simulate time progression
37+
38+
try:
39+
api.poll_with_specified_overhead(lambda: False, overhead_rate=0.1, max_tries=2)
40+
except Exception as e:
41+
pass # Ignore the exception for this test
42+
43+
expected_sleep_time = (start_time + increment_time - start_time) * 0.1
44+
mock_sleep.assert_called_with(expected_sleep_time)
45+
46+
def test_max_delay_cap(self):
47+
with patch('time.sleep') as mock_sleep:
48+
try:
49+
api.poll_with_specified_overhead(lambda: False, overhead_rate=1, max_tries=2, start_time=time.time() - 200)
50+
except Exception:
51+
pass
52+
# Ensure that the maximum delay of 120 seconds is not exceeded
53+
mock_sleep.assert_called_with(120)
54+
55+
def test_polling_success(self):
56+
with patch('time.sleep', return_value=None) as mock_sleep:
57+
api.poll_with_specified_overhead(lambda: True, overhead_rate=0.1)
58+
mock_sleep.assert_not_called()
59+
60+
def test_negative_overhead_rate(self):
61+
with self.assertRaises(ValueError):
62+
api.poll_with_specified_overhead(lambda: True, overhead_rate=-0.1)
63+
64+
def test_realistic_scenario(self):
65+
responses = [False, False, True]
66+
def side_effect():
67+
return responses.pop(0)
68+
69+
with patch('time.sleep') as mock_sleep:
70+
api.poll_with_specified_overhead(side_effect, overhead_rate=0.1, max_tries=3)
71+
# Ensure that `time.sleep` was called twice (for two false returns)
72+
self.assertEqual(mock_sleep.call_count, 2)
3073

3174
@patch('railib.rest.urlopen')
3275
class TestURLOpenWithRetry(unittest.TestCase):

0 commit comments

Comments
 (0)