From 6803ea3afc9c7aa9410ea5a994f5c7fb0a363e51 Mon Sep 17 00:00:00 2001 From: Ryan Williams Date: Thu, 6 May 2010 23:29:24 -0700 Subject: [PATCH] Included next_timers in the cleanup, thanks to redbo's suggestion. --- eventlet/hubs/hub.py | 3 ++- tests/hub_test.py | 21 ++++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/eventlet/hubs/hub.py b/eventlet/hubs/hub.py index deb942d..74efcaa 100644 --- a/eventlet/hubs/hub.py +++ b/eventlet/hubs/hub.py @@ -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): diff --git a/tests/hub_test.py b/tests/hub_test.py index 831673c..22526f3 100644 --- a/tests/hub_test.py +++ b/tests/hub_test.py @@ -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,