示例#1
0
    def test_get_element(self):

        pool = CachePool(generator, )

        element = pool.get()

        dd('pool stat: {0}'.format(pool.stat))
        self.assertEqual(1, pool.stat['new'])
        self.assertEqual(1, pool.stat['get'])

        pool.put(element)

        dd('pool stat: {0}'.format(pool.stat))
        self.assertEqual(1, pool.stat['put'])
示例#2
0
    def test_get_element_twice(self):

        pool = CachePool(generator, )

        element1 = pool.get()
        pool.put(element1)

        dd('pool stat: {0}'.format(pool.stat))
        self.assertEqual(1, pool.stat['new'])

        element2 = pool.get()
        pool.put(element2)

        dd('pool stat: {0}'.format(pool.stat))
        self.assertEqual(2, pool.stat['get'])
        self.assertIs(element1, element2)
示例#3
0
    def test_pool_size(self):

        pool = CachePool(
            generator,
            pool_size=1,
            close_callback=close_callback,
        )

        element1 = pool.get()
        element2 = pool.get()

        self.assertEqual(0, pool.queue.qsize())
        pool.put(element1)
        self.assertEqual(1, pool.queue.qsize())
        pool.put(element2)
        self.assertEqual(1, pool.queue.qsize())
示例#4
0
    def test_close_callback_when_queue_full(self):

        pool = CachePool(
            generator,
            pool_size=1,
            close_callback=close_callback,
        )

        dd('pool qsize: {0}'.format(pool.queue.qsize()))
        element1 = pool.get()
        element2 = pool.get()

        pool.put(element1)
        pool.put(element2)

        dd('pool qsize: {0}'.format(pool.queue.qsize()))
        self.assertFalse(element1.closed)
        self.assertTrue(element2.closed)