Commits

Andriy Kornatskyy committed 8a1dc21

Introduced LazyPool.

  • Participants
  • Parent commits e74e9b5

Comments (0)

Files changed (2)

File src/wheezy/core/pooling.py

         return self.__items.qsize()
 
 
+class LazyPool(object):
+    """ Lazy pool implementation.
+
+        Allocates pool items as necessary.
+    """
+
+    def __init__(self, create_factory, size):
+        """
+            `create_factory` is a callable with an `item` as argument,
+            this allows control `item` status before returning.
+        """
+        self.size = size
+        items = Queue(size)
+        for i in xrange(size):
+            items.put(None)
+        self.__items = items
+        self.get_back = items.put
+        self.create_factory = create_factory
+
+    def acquire(self):
+        """ Return an item from pool. Blocks until an item
+            is available.
+        """
+        return self.create_factory(self.__items.get())
+
+    @property
+    def count(self):
+        """ Returns a number of available items in the pool.
+        """
+        return self.__items.qsize()
+
+
 class Pooled(object):
     """ ``Pooled`` serves context manager purpose, effectively acquiring and
         returning item to the pool.

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

 
 import unittest
 
+from mock import Mock
+
 
 class EagerPoolTestCase(unittest.TestCase):
 
         """ Tests if pool fills items per size requested.
         """
         from wheezy.core.pooling import EagerPool
-        pool = EagerPool(lambda: 1, 10)
+        mock_create_factory = Mock()
+        mock_create_factory.return_value = 1
+        pool = EagerPool(mock_create_factory, 10)
         assert pool.size == 10
         assert pool.count == 10
+        assert 10 == mock_create_factory.call_count
 
     def test_acquire(self):
         """ If an item is aquired it is removed from pool.
         assert pool.count == 10
 
 
+class LazyPoolTestCase(unittest.TestCase):
+
+    def test_init(self):
+        """ Tests if pool fills items per size requested.
+        """
+        from wheezy.core.pooling import LazyPool
+        mock_create_factory = Mock()
+        mock_create_factory.return_value = 1
+        pool = LazyPool(mock_create_factory, 10)
+        assert pool.size == 10
+        assert pool.count == 10
+        assert not mock_create_factory.called
+
+    def test_acquire(self):
+        """ If an item is aquired it is removed from pool.
+        """
+        from wheezy.core.pooling import LazyPool
+        pool = LazyPool(lambda x: 1, 10)
+
+        assert 1 == pool.acquire()
+        assert pool.size == 10
+        assert pool.count == 9
+
+    def test_get_back(self):
+        """ An item is returned back to pool.
+        """
+        from wheezy.core.pooling import LazyPool
+        pool = LazyPool(lambda x: 1, 10)
+
+        item = pool.acquire()
+        pool.get_back(item)
+
+        assert pool.size == 10
+        assert pool.count == 10
+
+
 class PooledTestCase(unittest.TestCase):
 
     def test_scope(self):