Skip to content

Commit bfc692c

Browse files
authored
chore: pytestify exponential_backoff (#1727)
Co-authored-by: Jordan Woods <[email protected]>
1 parent e6b0ba1 commit bfc692c

File tree

1 file changed

+52
-50
lines changed

1 file changed

+52
-50
lines changed

test/test_exponential_backoff.py

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,62 @@
1-
import unittest
1+
import pytest
22

33
from tableauserverclient.exponential_backoff import ExponentialBackoffTimer
44
from ._utils import mocked_time
55

66

7-
class ExponentialBackoffTests(unittest.TestCase):
8-
def test_exponential(self):
9-
with mocked_time() as mock_time:
10-
exponentialBackoff = ExponentialBackoffTimer()
11-
# The creation of our mock shouldn't sleep
12-
self.assertAlmostEqual(mock_time(), 0)
13-
# The first sleep sleeps for a rather short time, the following sleeps become longer
14-
exponentialBackoff.sleep()
15-
self.assertAlmostEqual(mock_time(), 0.5)
7+
def test_exponential() -> None:
8+
with mocked_time() as mock_time:
9+
exponentialBackoff = ExponentialBackoffTimer()
10+
# The creation of our mock shouldn't sleep
11+
pytest.approx(mock_time(), 0)
12+
# The first sleep sleeps for a rather short time, the following sleeps become longer
13+
exponentialBackoff.sleep()
14+
pytest.approx(mock_time(), 0.5)
15+
exponentialBackoff.sleep()
16+
pytest.approx(mock_time(), 1.2)
17+
exponentialBackoff.sleep()
18+
pytest.approx(mock_time(), 2.18)
19+
exponentialBackoff.sleep()
20+
pytest.approx(mock_time(), 3.552)
21+
exponentialBackoff.sleep()
22+
pytest.approx(mock_time(), 5.4728)
23+
24+
25+
def test_exponential_saturation() -> None:
26+
with mocked_time() as mock_time:
27+
exponentialBackoff = ExponentialBackoffTimer()
28+
for _ in range(99):
1629
exponentialBackoff.sleep()
17-
self.assertAlmostEqual(mock_time(), 1.2)
30+
# We don't increase the sleep time above 30 seconds.
31+
# Otherwise, the exponential sleep time could easily
32+
# reach minutes or even hours between polls
33+
for _ in range(5):
34+
s = mock_time()
1835
exponentialBackoff.sleep()
19-
self.assertAlmostEqual(mock_time(), 2.18)
36+
slept = mock_time() - s
37+
pytest.approx(slept, 30)
38+
39+
40+
def test_timeout() -> None:
41+
with mocked_time() as mock_time:
42+
exponentialBackoff = ExponentialBackoffTimer(timeout=4.5)
43+
for _ in range(4):
2044
exponentialBackoff.sleep()
21-
self.assertAlmostEqual(mock_time(), 3.552)
45+
pytest.approx(mock_time(), 3.552)
46+
# Usually, the following sleep would sleep until 5.5, but due to
47+
# the timeout we wait less; thereby we make sure to take the timeout
48+
# into account as good as possible
49+
exponentialBackoff.sleep()
50+
pytest.approx(mock_time(), 4.5)
51+
# The next call to `sleep` will raise a TimeoutError
52+
with pytest.raises(TimeoutError):
2253
exponentialBackoff.sleep()
23-
self.assertAlmostEqual(mock_time(), 5.4728)
24-
25-
def test_exponential_saturation(self):
26-
with mocked_time() as mock_time:
27-
exponentialBackoff = ExponentialBackoffTimer()
28-
for _ in range(99):
29-
exponentialBackoff.sleep()
30-
# We don't increase the sleep time above 30 seconds.
31-
# Otherwise, the exponential sleep time could easily
32-
# reach minutes or even hours between polls
33-
for _ in range(5):
34-
s = mock_time()
35-
exponentialBackoff.sleep()
36-
slept = mock_time() - s
37-
self.assertAlmostEqual(slept, 30)
38-
39-
def test_timeout(self):
40-
with mocked_time() as mock_time:
41-
exponentialBackoff = ExponentialBackoffTimer(timeout=4.5)
42-
for _ in range(4):
43-
exponentialBackoff.sleep()
44-
self.assertAlmostEqual(mock_time(), 3.552)
45-
# Usually, the following sleep would sleep until 5.5, but due to
46-
# the timeout we wait less; thereby we make sure to take the timeout
47-
# into account as good as possible
54+
55+
56+
def test_timeout_zero() -> None:
57+
with mocked_time() as mock_time:
58+
# The construction of the timer doesn't throw, yet
59+
exponentialBackoff = ExponentialBackoffTimer(timeout=0)
60+
# But the first `sleep` immediately throws
61+
with pytest.raises(TimeoutError):
4862
exponentialBackoff.sleep()
49-
self.assertAlmostEqual(mock_time(), 4.5)
50-
# The next call to `sleep` will raise a TimeoutError
51-
with self.assertRaises(TimeoutError):
52-
exponentialBackoff.sleep()
53-
54-
def test_timeout_zero(self):
55-
with mocked_time() as mock_time:
56-
# The construction of the timer doesn't throw, yet
57-
exponentialBackoff = ExponentialBackoffTimer(timeout=0)
58-
# But the first `sleep` immediately throws
59-
with self.assertRaises(TimeoutError):
60-
exponentialBackoff.sleep()

0 commit comments

Comments
 (0)