From 50d390ece4a914ae6742320ca374a1ebcf09b747 Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Fri, 25 Feb 2005 09:44:51 +0000 Subject: [PATCH] Added a reset method, a couple more tests, and fixed a slight bug (IndexError being raised from the wrong place) --- src/structures.py | 6 +++++- test/test_structures.py | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/structures.py b/src/structures.py index 67ad8f9a4..a66d8f965 100644 --- a/src/structures.py +++ b/src/structures.py @@ -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): diff --git a/test/test_structures.py b/test/test_structures.py index 790a0720b..c14d0e09b 100644 --- a/test/test_structures.py +++ b/test/test_structures.py @@ -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: