def test_browser(self):
     starting_ff_count = self.get_process_count()
     print(starting_ff_count)
     self.pool = ObjectPool(self.klass, min_init=1, expires=0)
     time.sleep(10)
     after_ff_count = self.get_process_count()
     print(after_ff_count)
     final_count = after_ff_count - starting_ff_count
     self.assertEqual(final_count, 1)
Exemple #2
0
    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_browser_without_cloning(self):
        starting_ff_count = self.get_process_count()
        self.pool = ObjectPool(self.klass, min_init=2, expires=0)
        after_ff_count = self.get_process_count()
        final_count = after_ff_count - starting_ff_count

        with self.pool.get() as (browser, browser_stats):
            title = browser.get_page_title('https://www.google.co.in/')

        with self.pool.get() as (browser1, browser_stats):
            title1 = browser1.get_page_title('https://www.facebook.com/')

        self.assertNotEqual(browser, browser1)
Exemple #4
0
    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)
Exemple #5
0
    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)
Exemple #6
0
    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)
Exemple #7
0
    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)
Exemple #8
0
class ObjectPoolFunctionTest(unittest.TestCase):
    def setUp(self):
        self.klass = Browser
        self.skip_teardown = False

    def test_is_full_with_same_min_max(self):
        """testing is_pool_full with same min_init and max"""
        self.pool = ObjectPool(self.klass, min_init=2, max_capacity=2)
        self.assertTrue(self.pool.is_pool_full())

    def test_is_full_with_not_same_min_max(self):
        """testing is_pool_full method with max > min_init"""
        self.pool = ObjectPool(self.klass, min_init=1, max_capacity=2)
        self.assertFalse(self.pool.is_pool_full())

    def test_is_full_with_max_zero(self):
        """when max_capacity=0, is_pool_full always return False."""
        self.pool = ObjectPool(self.klass, min_init=3, max_capacity=0)
        self.assertFalse(self.pool.is_pool_full())

    def test_destroy(self):
        """after destroy, pool should not be available or exist."""
        self.pool = ObjectPool(self.klass, min_init=1, expires=0)
        self.pool.destroy()
        self.assertFalse(ObjectPool.pool_exists(self.klass))

    def test_exists(self):
        """pool become available after it created."""
        self.pool = ObjectPool(self.klass, min_init=1, expires=0)
        exists = ObjectPool.pool_exists(self.klass)
        self.assertTrue(exists)

    def test_not_exist(self):
        """pool will not be available if that is not created for a class"""
        self.skip_teardown = True
        exists = ObjectPool.pool_exists(Browser)
        self.assertFalse(exists)

    def tearDown(self):
        if not self.skip_teardown:
            self.pool.destroy()
            self.klass = None
Exemple #9
0
 def test_not_exist(self):
     """pool will not be available if that is not created for a class"""
     self.skip_teardown = True
     exists = ObjectPool.pool_exists(Browser)
     self.assertFalse(exists)
Exemple #10
0
 def test_exists(self):
     """pool become available after it created."""
     self.pool = ObjectPool(self.klass, min_init=1, expires=0)
     exists = ObjectPool.pool_exists(self.klass)
     self.assertTrue(exists)
Exemple #11
0
 def test_destroy(self):
     """after destroy, pool should not be available or exist."""
     self.pool = ObjectPool(self.klass, min_init=1, expires=0)
     self.pool.destroy()
     self.assertFalse(ObjectPool.pool_exists(self.klass))
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
 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_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()
Exemple #15
0
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
 def test_with_default_values(self):
     """pool should be created with default values"""
     self.pool = ObjectPool(self.klass)
     self.assertIsNotNone(self.pool)
Exemple #17
0
 def test_is_full_with_same_min_max(self):
     """testing is_pool_full with same min_init and max"""
     self.pool = ObjectPool(self.klass, min_init=2, max_capacity=2)
     self.assertTrue(self.pool.is_pool_full())
 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)
Exemple #19
0
 def test_is_full_with_not_same_min_max(self):
     """testing is_pool_full method with max > min_init"""
     self.pool = ObjectPool(self.klass, min_init=1, max_capacity=2)
     self.assertFalse(self.pool.is_pool_full())
 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)
Exemple #21
0
 def test_is_full_with_max_zero(self):
     """when max_capacity=0, is_pool_full always return False."""
     self.pool = ObjectPool(self.klass, min_init=3, max_capacity=0)
     self.assertFalse(self.pool.is_pool_full())
 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()
class ObjectPoolBehaviourTest(unittest.TestCase):
    def setUp(self):
        self.klass = FirefoxBrowser
        self.skip_test = False
        self.skip_teardown = False

    def get_process_count(self, key='firefox'):
        ff_process = subprocess.Popen(['ps', '-ef'], stdout=subprocess.PIPE)
        grep_process = subprocess.Popen(['grep', key],
                                        stdin=ff_process.stdout,
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.PIPE)
        awk_process = subprocess.Popen(['awk', '{print $2}'],
                                       stdin=grep_process.stdout,
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.PIPE)
        wc_process = subprocess.Popen(['wc', '-l'],
                                      stdin=awk_process.stdout,
                                      stdout=subprocess.PIPE,
                                      stderr=subprocess.PIPE)

        ff_process.stdout.close()
        grep_process.stdout.close()
        awk_process.stdout.close()

        cnt, err = wc_process.communicate()

        if cnt:
            try:
                cnt = int(cnt.decode().strip()) if cnt.decode().strip() else 0
            except:
                cnt = 0

        return cnt

    @unittest.skipIf(skip_browser_test, "to test as a stand alone test")
    def test_browser(self):
        starting_ff_count = self.get_process_count()
        print(starting_ff_count)
        self.pool = ObjectPool(self.klass, min_init=1, expires=0)
        time.sleep(10)
        after_ff_count = self.get_process_count()
        print(after_ff_count)
        final_count = after_ff_count - starting_ff_count
        self.assertEqual(final_count, 1)

    @unittest.skipIf(skip_browser_test, "to test as a stand alone test")
    def test_browser_without_cloning(self):
        starting_ff_count = self.get_process_count()
        self.pool = ObjectPool(self.klass, min_init=2, expires=0)
        after_ff_count = self.get_process_count()
        final_count = after_ff_count - starting_ff_count

        with self.pool.get() as (browser, browser_stats):
            title = browser.get_page_title('https://www.google.co.in/')

        with self.pool.get() as (browser1, browser_stats):
            title1 = browser1.get_page_title('https://www.facebook.com/')

        self.assertNotEqual(browser, browser1)

    def tearDown(self):
        if not self.skip_teardown:
            self.pool.destroy()
            self.klass = None