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:
Jeremy Fincher 2005-02-25 09:44:51 +00:00
parent b0e006ab12
commit 50d390ece4
2 changed files with 13 additions and 1 deletions

View File

@ -305,7 +305,11 @@ class TimeoutQueue(object):
self.queue = queue self.queue = queue
self.timeout = timeout self.timeout = timeout
def reset(self):
self.queue.reset()
def __repr__(self): def __repr__(self):
self._clearOldElements()
return '%s(timeout=%r, queue=%r)' % (self.__class__.__name__, return '%s(timeout=%r, queue=%r)' % (self.__class__.__name__,
self.timeout, self.queue) self.timeout, self.queue)
@ -317,7 +321,7 @@ class TimeoutQueue(object):
def _clearOldElements(self): def _clearOldElements(self):
now = time.time() 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() self.queue.dequeue()
def setTimeout(self, i): def setTimeout(self, i):

View File

@ -605,10 +605,18 @@ class TestTimeoutQueue(SupyTestCase):
q = TimeoutQueue(1) q = TimeoutQueue(1)
q.enqueue(1) q.enqueue(1)
self.failUnless(1 in q) self.failUnless(1 in q)
self.failUnless(1 in q) # For some reason, the second one might fail.
self.failIf(2 in q) self.failIf(2 in q)
time.sleep(1.1) time.sleep(1.1)
self.failIf(1 in q) 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: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: