Commits

Andriy Kornatskyy committed 0e749d0

Prefer LIFO queue for LazyPool.

  • Participants
  • Parent commits a12c4c9

Comments (0)

Files changed (3)

File src/wheezy/core/comp.py

 
 if PY3:  # pragma: nocover
     from queue import Queue
+    from queue import LifoQueue
     xrange = range
     str_type = str
 
 
 else:  # pragma: nocover
     from Queue import Queue  # noqa
+    try:
+        from Queue import LifoQueue  # noqa
+    except ImportError:
+        class LifoQueue(Queue):  # noqa
+            def _init(self, maxsize):
+                self.queue = []
+                self.maxsize = maxsize
+
+            def _qsize(self, len=len):
+                return len(self.queue)
+
+            def _put(self, item):
+                self.queue.append(item)
+
+            def _get(self):
+                return self.queue.pop()
+    xrange = range
     xrange = xrange
     str_type = unicode
 

File src/wheezy/core/pooling.py

 """ ``pooling`` module.
 """
 
+from wheezy.core.comp import LifoQueue
 from wheezy.core.comp import Queue
 from wheezy.core.comp import xrange
 
             this allows control `item` status before returning.
         """
         self.size = size
-        items = Queue(size)
+        items = LifoQueue(size)
         for i in xrange(size):
             items.put(None)
         self.__items = items

File src/wheezy/core/tests/test_pooling.py

         assert pool.size == 10
         assert pool.count == 10
 
+    def test_fifo(self):
+        """ Pool items are FIFO cycled.
+        """
+        from wheezy.core.pooling import EagerPool
+        items = [3, 2, 1]
+
+        def create_factory():
+            return items.pop()
+        pool = EagerPool(create_factory, 3)
+
+        assert 1 == pool.acquire()
+        pool.get_back(1)
+        assert 2 == pool.acquire()
+
 
 class LazyPoolTestCase(unittest.TestCase):
 
         assert pool.size == 10
         assert pool.count == 10
 
+    def test_lifo(self):
+        """ Pool items are LIFO cycled.
+        """
+        from wheezy.core.pooling import LazyPool
+        items = [3, 2, 1]
+
+        def create_factory(i):
+            return i or items.pop()
+        pool = LazyPool(create_factory, 3)
+
+        assert 1 == pool.acquire()
+        pool.get_back(1)
+        assert 1 == pool.acquire()
+
 
 class PooledTestCase(unittest.TestCase):