Improve rate limiting and tests, for #80
This commit is contained in:
parent
a1867cc8d4
commit
40ea12a22f
|
@ -302,8 +302,10 @@ class _Network(object):
|
|||
DELAY_TIME = 0.2
|
||||
now = time.time()
|
||||
|
||||
if (now - self.last_call_time) < DELAY_TIME:
|
||||
time.sleep(now - self.last_call_time - DELAY_TIME)
|
||||
time_since_last = now - self.last_call_time
|
||||
|
||||
if time_since_last < DELAY_TIME:
|
||||
time.sleep(DELAY_TIME - time_since_last)
|
||||
|
||||
self.last_call_time = now
|
||||
|
||||
|
|
|
@ -541,9 +541,16 @@ class TestPyLast(unittest.TestCase):
|
|||
|
||||
# Act
|
||||
self.network.enable_rate_limit()
|
||||
then = time.time()
|
||||
# Make some network call, limit not applied first time
|
||||
self.network.get_user(self.username)
|
||||
# Make a second network call, limiting should be applied
|
||||
self.network.get_top_artists()
|
||||
now = time.time()
|
||||
|
||||
# Assert
|
||||
self.assertTrue(self.network.is_rate_limited())
|
||||
self.assertGreaterEqual(now - then, 0.2)
|
||||
|
||||
|
||||
def test_disable_rate_limiting(self):
|
||||
|
@ -553,6 +560,12 @@ class TestPyLast(unittest.TestCase):
|
|||
|
||||
# Act
|
||||
self.network.disable_rate_limit()
|
||||
then = time.time()
|
||||
# Make some network call, limit not applied first time
|
||||
self.network.get_user(self.username)
|
||||
# Make a second network call, limiting should be applied
|
||||
self.network.get_top_artists()
|
||||
now = time.time()
|
||||
|
||||
# Assert
|
||||
self.assertFalse(self.network.is_rate_limited())
|
||||
|
|
Loading…
Reference in a new issue