mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-26 20:59:27 +01:00
Added a reset method, a couple more tests, and fixed a slight bug (IndexError being raised from the wrong place)
This commit is contained in:
parent
b0e006ab12
commit
50d390ece4
@ -305,7 +305,11 @@ class TimeoutQueue(object):
|
||||
self.queue = queue
|
||||
self.timeout = timeout
|
||||
|
||||
def reset(self):
|
||||
self.queue.reset()
|
||||
|
||||
def __repr__(self):
|
||||
self._clearOldElements()
|
||||
return '%s(timeout=%r, queue=%r)' % (self.__class__.__name__,
|
||||
self.timeout, self.queue)
|
||||
|
||||
@ -317,7 +321,7 @@ class TimeoutQueue(object):
|
||||
|
||||
def _clearOldElements(self):
|
||||
now = time.time()
|
||||
while now - self.queue.peek()[0] > self._getTimeout():
|
||||
while self.queue and now - self.queue.peek()[0] > self._getTimeout():
|
||||
self.queue.dequeue()
|
||||
|
||||
def setTimeout(self, i):
|
||||
|
@ -605,10 +605,18 @@ class TestTimeoutQueue(SupyTestCase):
|
||||
q = TimeoutQueue(1)
|
||||
q.enqueue(1)
|
||||
self.failUnless(1 in q)
|
||||
self.failUnless(1 in q) # For some reason, the second one might fail.
|
||||
self.failIf(2 in q)
|
||||
time.sleep(1.1)
|
||||
self.failIf(1 in q)
|
||||
|
||||
def testReset(self):
|
||||
q = TimeoutQueue(10)
|
||||
q.enqueue(1)
|
||||
self.failUnless(1 in q)
|
||||
q.reset()
|
||||
self.failIf(1 in q)
|
||||
|
||||
|
||||
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user