class ObjectPoolCreationTest(unittest.TestCase): def setUp(self): self.klass = Browser self.skip_teardown = False def test_with_default_values(self): """pool should be created with default values""" self.pool = ObjectPool(self.klass) self.assertIsNotNone(self.pool) def test_multiple_pool_creation(self): self.pool = ObjectPool(self.klass, min_init=2) dpool = ObjectPool(Browser1, min_init=3) self.assertNotEqual(self.pool, dpool) dpool.destroy() def test_with_lazy_option(self): """lazy pool will be created with zero resources upon creation""" self.pool = ObjectPool(self.klass, lazy=True) self.assertEqual(self.pool.get_pool_size(), 0) def test_without_cloning_option(self): """resource will be created by cloning the instance""" self.pool = ObjectPool(self.klass, min_init=2, cloning=False) self.assertEqual(self.pool.get_pool_size(), 2) def test_with_min_zero(self): """min_init can not be 0 with lazy=False option else exception will be raised""" self.skip_teardown = True self.assertRaises(InvalidMinInitCapacity, ObjectPool, self.klass, min_init=0) def test_with_max(self): """max_capacity should be positive number else exception will be raised""" self.skip_teardown = True self.assertRaises(InvalidMaxCapacity, ObjectPool, self.klass, max_capacity=-1) def test_with_invalid_class(self): """max_capacity should be positive number else exception will be raised""" self.skip_teardown = True a = 2 self.assertRaises(InvalidClass, ObjectPool, a, max_capacity=-1) def test_with_max_zero(self): """pool size should be same as min_init right after creation""" self.pool = ObjectPool(self.klass, min_init=3, max_capacity=0) self.assertEqual(self.pool.get_pool_size(), 3) def test_creation_same_pool(self): """pool size should be same as min_init right after creation""" self.pool = ObjectPool(self.klass) self.pool1 = ObjectPool(self.klass) self.assertEqual(self.pool, self.pool1) self.pool1.destroy() def tearDown(self): if not self.skip_teardown: self.pool.destroy() self.klass = None
class ObjectPoolBehaviourTest(unittest.TestCase): def setUp(self): self.klass = Browser self.skip_teardown = False def test_with_non_expiry(self): self.pool = ObjectPool(self.klass, min_init=1, expires=0) with self.pool.get() as (item, item_stats): t = item_stats['created_at'] time.sleep(13) with self.pool.get() as (item1, item_stats1): t1 = item_stats1['created_at'] self.assertEqual(item, item1) def test_with_expire_true_pre_check(self): self.pool = ObjectPool(self.klass, min_init=1, expires=10, pre_check=True, post_check=False) with self.pool.get() as (item, item_stats): t = item_stats['created_at'] time.sleep(13) with self.pool.get() as (item1, item_stats1): t1 = item_stats1['created_at'] self.assertNotEqual(item, item1) def test_with_expire_true_post_check(self): self.pool = ObjectPool(self.klass, min_init=1, expires=10) with self.pool.get() as (item, item_stats): t = item_stats['created_at'] time.sleep(13) with self.pool.get() as (item1, item_stats1): t1 = item_stats1['created_at'] with self.pool.get() as (item2, item_stats2): t2 = item_stats2['created_at'] if item1 is item: self.assertNotEqual(item2, item) else: self.assertEqual(item1, item) def test_with_expire_false_pre_check(self): self.pool = ObjectPool(self.klass, min_init=1, expires=10) with self.pool.get() as (item, item_stats): t = item_stats['created_at'] time.sleep(13) with self.pool.get() as (item1, item_stats1): t1 = item_stats1['created_at'] self.assertEqual(item1, item) def test_with_expire_false_post_check(self): self.pool = ObjectPool(self.klass, min_init=1, expires=10, post_check=False, pre_check=True) with self.pool.get() as (item, item_stats): t = item_stats['created_at'] time.sleep(13) with self.pool.get() as (item1, item_stats1): t1 = item_stats1['created_at'] self.assertNotEqual(item1, item) def test_multiple_pool_invocation(self): self.pool = ObjectPool(self.klass, min_init=2) dpool = ObjectPool(Browser1, min_init=3) with self.pool.get() as (item, item_stats): t = item.do_work() with dpool.get() as (item1, item_stats1): t1 = item1.do_work() dpool.destroy() self.assertNotEqual(t1, t) def test_pool_size_growth(self): """pool size will grow up to max. This test case is a simulation of concurrent access and pool growth""" self.pool = ObjectPool(self.klass, min_init=1, max_capacity=1) p1, stats = self.pool._get_resource() p1_size = self.pool.get_pool_size() with self.pool.get() as (item, item_stats): t = item.do_work() p2_size = self.pool.get_pool_size() self.pool._queue_resource(p1, stats) p3_size = self.pool.get_pool_size() self.assertEqual(p3_size, 1) def test_pool_with_reusable(self): """pool size will grow up to max. This test case is a simulation of concurrent access and pool growth""" self.pool = ObjectPool(self.klass, min_init=1, max_capacity=1, max_reusable=2) with self.pool.get() as (item0, item_stats): t = item0.do_work() with self.pool.get() as (item, item_stats): t = item.do_work() with self.pool.get() as (item1, item_stats): t = item1.do_work() with self.pool.get() as (item2, item_stats): t = item2.do_work() self.assertEqual(item0, item) self.assertNotEqual(item1, item) self.assertEqual(item1, item2) def tearDown(self): if not self.skip_teardown: self.pool.destroy() self.klass = None