def test_sweep_and_clean_cache_schedules_again(self): """ Sweep and clean should schedule a new sweep and clean in clean_interval seconds. """ event_loop = mock.MagicMock() connector = HttpConnector(event_loop) event_loop.call_later = mock.MagicMock() connector._sweep_and_clean_cache(event_loop, 300) sweep = connector._sweep_and_clean_cache event_loop.call_later.assert_called_once_with(300, sweep, event_loop, 300)
def test_sweep_and_clean_cache_stale_items_only(self): """ If the lookup cache is only full of stale items then the cache should be emtied. """ event_loop = mock.MagicMock() connector = HttpConnector(event_loop) event_loop.call_later = mock.MagicMock() now = time.time() for i in range(10): connector.lookups[str(i)] = { 'last_access': now - 500, 'lookup': mock.MagicMock() } connector._sweep_and_clean_cache(event_loop, 300) self.assertEqual(len(connector.lookups), 0)
def test_sweep_and_clean_cache_fresh_items_only(self): """ If the lookup cache is only full of fresh items then the cache remains the same (nothing to delete). """ event_loop = mock.MagicMock() connector = HttpConnector(event_loop) event_loop.call_later = mock.MagicMock() now = time.time() for i in range(10): connector.lookups[str(i)] = { 'last_access': now, 'lookup': mock.MagicMock() } connector._sweep_and_clean_cache(event_loop, 300) self.assertEqual(len(connector.lookups), 10)
def test_sweep_and_clean_cache_mixed_fresh_and_stale_items(self): """ Only the stale items should be removed from the cache. """ event_loop = mock.MagicMock() connector = HttpConnector(event_loop) event_loop.call_later = mock.MagicMock() now = time.time() for i in range(10): if i % 2: access = now - 500 else: access = now connector.lookups[str(i)] = { 'last_access': access, 'lookup': mock.MagicMock() } connector._sweep_and_clean_cache(event_loop, 300) self.assertEqual(len(connector.lookups), 5)