Included next_timers in the cleanup, thanks to redbo's suggestion.
This commit is contained in:
@@ -221,10 +221,11 @@ class BaseHub(object):
|
|||||||
|
|
||||||
def timer_canceled(self, timer):
|
def timer_canceled(self, timer):
|
||||||
self.timers_canceled += 1
|
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:
|
if len_timers > 1000 and len_timers/2 <= self.timers_canceled:
|
||||||
self.timers_canceled = 0
|
self.timers_canceled = 0
|
||||||
self.timers = [t for t in self.timers if not t[1].called]
|
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)
|
heapq.heapify(self.timers)
|
||||||
|
|
||||||
def prepare_timers(self):
|
def prepare_timers(self):
|
||||||
|
@@ -9,6 +9,21 @@ def noop():
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
class TestTimerCleanup(LimitedTestCase):
|
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
|
@skip_with_pyevent
|
||||||
def test_cancel_accumulated(self):
|
def test_cancel_accumulated(self):
|
||||||
hub = hubs.get_hub()
|
hub = hubs.get_hub()
|
||||||
@@ -23,7 +38,7 @@ class TestTimerCleanup(LimitedTestCase):
|
|||||||
self.assert_less_than_equal(hub.timers_canceled - scanceled,
|
self.assert_less_than_equal(hub.timers_canceled - scanceled,
|
||||||
hub.get_timers_count() - stimers)
|
hub.get_timers_count() - stimers)
|
||||||
# there should be fewer than 1000 new timers and canceled
|
# 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)
|
self.assert_less_than_equal(hub.timers_canceled, 1000)
|
||||||
|
|
||||||
@skip_with_pyevent
|
@skip_with_pyevent
|
||||||
@@ -49,8 +64,8 @@ class TestTimerCleanup(LimitedTestCase):
|
|||||||
uncanceled_timers.append(t3)
|
uncanceled_timers.append(t3)
|
||||||
# 3000 new timers, plus a few extras
|
# 3000 new timers, plus a few extras
|
||||||
self.assert_less_than_equal(stimers + 3000,
|
self.assert_less_than_equal(stimers + 3000,
|
||||||
hub.get_timers_count())
|
stimers + hub.get_timers_count())
|
||||||
self.assertEqual(hub.timers_canceled, scanceled + 1000)
|
self.assertEqual(hub.timers_canceled, 1000)
|
||||||
for t in uncanceled_timers:
|
for t in uncanceled_timers:
|
||||||
t.cancel()
|
t.cancel()
|
||||||
self.assert_less_than_equal(hub.timers_canceled - scanceled,
|
self.assert_less_than_equal(hub.timers_canceled - scanceled,
|
||||||
|
Reference in New Issue
Block a user