Included next_timers in the cleanup, thanks to redbo's suggestion.

This commit is contained in:
Ryan Williams
2010-05-06 23:29:24 -07:00
parent 80632c134c
commit 6803ea3afc
2 changed files with 20 additions and 4 deletions

View File

@@ -221,10 +221,11 @@ class BaseHub(object):
def timer_canceled(self, timer):
self.timers_canceled += 1
len_timers = len(self.timers)
len_timers = len(self.timers) + len(self.next_timers)
if len_timers > 1000 and len_timers/2 <= self.timers_canceled:
self.timers_canceled = 0
self.timers = [t for t in self.timers if not t[1].called]
self.next_timers = [t for t in self.next_timers if not t[1].called]
heapq.heapify(self.timers)
def prepare_timers(self):

View File

@@ -9,6 +9,21 @@ def noop():
pass
class TestTimerCleanup(LimitedTestCase):
@skip_with_pyevent
def test_cancel_immediate(self):
hub = hubs.get_hub()
stimers = hub.get_timers_count()
scanceled = hub.timers_canceled
for i in xrange(2000):
t = hubs.get_hub().schedule_call_global(60, noop)
t.cancel()
self.assert_less_than_equal(hub.timers_canceled - scanceled,
hub.get_timers_count() - stimers)
# there should be fewer than 1000 new timers and canceled
self.assert_less_than_equal(hub.get_timers_count(), 1000 + stimers)
self.assert_less_than_equal(hub.timers_canceled, 1000)
@skip_with_pyevent
def test_cancel_accumulated(self):
hub = hubs.get_hub()
@@ -23,7 +38,7 @@ class TestTimerCleanup(LimitedTestCase):
self.assert_less_than_equal(hub.timers_canceled - scanceled,
hub.get_timers_count() - stimers)
# there should be fewer than 1000 new timers and canceled
self.assert_less_than_equal(hub.get_timers_count(), stimers + 1000)
self.assert_less_than_equal(hub.get_timers_count(), 1000 + stimers)
self.assert_less_than_equal(hub.timers_canceled, 1000)
@skip_with_pyevent
@@ -49,8 +64,8 @@ class TestTimerCleanup(LimitedTestCase):
uncanceled_timers.append(t3)
# 3000 new timers, plus a few extras
self.assert_less_than_equal(stimers + 3000,
hub.get_timers_count())
self.assertEqual(hub.timers_canceled, scanceled + 1000)
stimers + hub.get_timers_count())
self.assertEqual(hub.timers_canceled, 1000)
for t in uncanceled_timers:
t.cancel()
self.assert_less_than_equal(hub.timers_canceled - scanceled,