Exemple #1
0
class TestConnectionMgr(unittest.TestCase):
    def setUp(self):
        self.cm = ConnectionManager()
        self.host = Mock()

    def test_get_available_conn(self):
        """
        Play with the pool, test, test... and test
        """
        self.cm._host_pool_size = 1  # Only a single connection
        self.assertEquals(0, len(self.cm._hostmap))
        self.assertEquals(0, len(self.cm._used_cons))
        self.assertEquals(0, len(self.cm._free_conns))
        # Get connection
        cf = lambda h: Mock()
        conn = self.cm.get_available_connection(self.host, cf)
        self.assertEquals(1, len(self.cm._hostmap))
        self.assertEquals(1, len(self.cm._used_cons))
        self.assertEquals(0, len(self.cm._free_conns))
        # Return it to the pool
        self.cm.free_connection(conn)
        self.assertEquals(1, len(self.cm._hostmap))
        self.assertEquals(0, len(self.cm._used_cons))
        self.assertEquals(1, len(self.cm._free_conns))
        # Ask for a conn again
        conn = self.cm.get_available_connection(self.host, cf)
        t0 = time.time()
        self.assertRaises(BaseFrameworkException,
                          self.cm.get_available_connection, self.host, cf)
        self.assertTrue(time.time() - t0 >= 2.9,
                        "Method returned before expected time")

    def test_replace_conn(self):
        cf = lambda h: Mock()
        bad_conn = Mock()
        self.cm.replace_connection(bad_conn, self.host, cf)
        bad_conn = self.cm.get_available_connection(self.host, cf)
        old_len = self.cm.get_connections_total()
        # Replace bad with a new one
        new_conn = self.cm.replace_connection(bad_conn, self.host, cf)
        # Must be different conn objects
        self.assertNotEquals(bad_conn, new_conn)
        # The len must be the same
        self.assertEquals(self.cm.get_connections_total(), old_len)

    def test_remove_conn(self):
        """
        Remove a non existing conn, nothing should happen.
        """
        self.assertEqual(self.cm.get_connections_total(), 0)

        conn = self.cm.get_available_connection(self.host, lambda h: Mock())
        self.assertEqual(self.cm.get_connections_total(), 0)
        non_exist_host = "non_host"

        # Remove ok
        self.cm.remove_connection(conn, non_exist_host)
        self.cm.remove_connection(conn, self.host)

        self.assertEqual(self.cm.get_connections_total(), 0)
Exemple #2
0
class test_connection_mgr(unittest.TestCase):

    def setUp(self):
        self.cm = ConnectionManager()
        self.host = Mock()

    def test_get_available_conn(self):
        """
        Play with the pool, test, test... and test
        """
        self.cm._host_pool_size = 1  # Only a single connection
        self.assertEquals(0, len(self.cm._hostmap))
        self.assertEquals(0, len(self.cm._used_cons))
        self.assertEquals(0, len(self.cm._free_conns))
        # Get connection
        cf = lambda h: Mock()
        conn = self.cm.get_available_connection(self.host, cf)
        self.assertEquals(1, len(self.cm._hostmap))
        self.assertEquals(1, len(self.cm._used_cons))
        self.assertEquals(0, len(self.cm._free_conns))
        # Return it to the pool
        self.cm.free_connection(conn)
        self.assertEquals(1, len(self.cm._hostmap))
        self.assertEquals(0, len(self.cm._used_cons))
        self.assertEquals(1, len(self.cm._free_conns))
        # Ask for a conn again
        conn = self.cm.get_available_connection(self.host, cf)
        t0 = time.time()
        self.assertRaises(
            BaseFrameworkException, self.cm.get_available_connection, self.host, cf)
        self.assertTrue(
            time.time() - t0 >= 2.9, "Method returned before expected time")

    def test_replace_conn(self):
        cf = lambda h: Mock()
        bad_conn = Mock()
        self.assertRaises(
            ValueError, self.cm.replace_connection, bad_conn, self.host, cf)
        bad_conn = self.cm.get_available_connection(self.host, cf)
        old_len = self.cm.get_connections_total()
        # Replace bad with a new one
        new_conn = self.cm.replace_connection(bad_conn, self.host, cf)
        # Must be different conn objects
        self.assertNotEquals(bad_conn, new_conn)
        # The len must be the same
        self.assertEquals(self.cm.get_connections_total(), old_len)

    def test_remove_conn(self):
        # Rem a non existing conn
        non_exist_conn = Mock()
        conn = self.cm.get_available_connection(self.host, lambda h: Mock())
        old_len = self.cm.get_connections_total()
        non_exist_host = "non_host"
        self.assertRaises(
            ValueError, self.cm.remove_connection, conn, non_exist_host)
        # Remove ok
        self.cm.remove_connection(conn, self.host)
        # curr_len = old_len - 1
        self.assertTrue(old_len - 1 == self.cm.get_connections_total() == 0)
Exemple #3
0
class TestConnectionMgr(unittest.TestCase):
    def setUp(self):
        self.cm = ConnectionManager()
        self.request = Mock()

    def test_get_available_conn_reuse(self):
        # We don't need a new HTTPConnection for each request
        self.request.new_connection = False
        self.request.get_host = lambda: 'w3af.org'

        self.cm.MAX_CONNECTIONS = 1  # Only a single connection
        self.assertEquals(0, len(self.cm._used_conns))
        self.assertEquals(0, len(self.cm._free_conns))

        # Get connection
        def conn_factory(request):
            mock = Mock()
            mock.host = request.get_host()
            return mock

        conn_1 = self.cm.get_available_connection(self.request, conn_factory)
        self.assertEquals(1, len(self.cm._used_conns))
        self.assertEquals(0, len(self.cm._free_conns))

        # Return it to the pool
        self.cm.free_connection(conn_1)
        self.assertEquals(0, len(self.cm._used_conns))
        self.assertEquals(1, len(self.cm._free_conns))

        # Ask for a conn again, since we don't need a new connection, it should
        # return one from the pool
        conn_2 = self.cm.get_available_connection(self.request, conn_factory)
        self.assertIs(conn_2, conn_1)

    def test_get_available_conn_new_connection_requested(self):
        # We want a new HTTPConnection for each request
        self.request.new_connection = True

        self.cm.MAX_CONNECTIONS = 2
        self.assertEquals(0, len(self.cm._used_conns))
        self.assertEquals(0, len(self.cm._free_conns))

        # Get connection
        cf = lambda h: Mock()
        conn_1 = self.cm.get_available_connection(self.request, cf)
        self.assertEquals(1, len(self.cm._used_conns))
        self.assertEquals(0, len(self.cm._free_conns))

        # Return it to the pool
        self.cm.free_connection(conn_1)
        self.assertEquals(0, len(self.cm._used_conns))
        self.assertEquals(1, len(self.cm._free_conns))

        # Ask for another connection, it should return a new one
        conn_2 = self.cm.get_available_connection(self.request, cf)
        self.assertIsNot(conn_1, conn_2)

    def test_replace_conn(self):
        cf = lambda h: Mock()
        bad_conn = Mock()
        self.cm.replace_connection(bad_conn, self.request, cf)
        bad_conn = self.cm.get_available_connection(self.request, cf)
        old_len = self.cm.get_connections_total()

        # Replace bad with a new one
        new_conn = self.cm.replace_connection(bad_conn, self.request, cf)

        # Must be different conn objects
        self.assertNotEquals(bad_conn, new_conn)

        # The len must be the same
        self.assertEquals(self.cm.get_connections_total(), old_len)

    def test_remove_conn(self):
        """
        Remove a non existing conn, nothing should happen.
        """
        self.assertEqual(self.cm.get_connections_total(), 0)

        conn = self.cm.get_available_connection(self.request, lambda h: Mock())
        self.assertEqual(self.cm.get_connections_total(), 1)
        non_exist_host = "non_host"

        # Remove ok
        self.cm.remove_connection(conn, non_exist_host)
        self.cm.remove_connection(conn, self.request)

        self.assertEqual(self.cm.get_connections_total(), 0)
Exemple #4
0
class TestConnectionMgr(unittest.TestCase):
    def setUp(self):
        self.cm = ConnectionManager()
        self.request = Mock()

    def test_get_available_conn(self):
        """
        Play with the pool, test, test... and test
        """
        # We don't need a new HTTPConnection for each request
        self.request.new_connection = False

        self.cm._host_pool_size = 1  # Only a single connection
        self.assertEquals(0, len(self.cm._hostmap))
        self.assertEquals(0, len(self.cm._used_cons))
        self.assertEquals(0, len(self.cm._free_conns))

        # Get connection
        cf = lambda h: Mock()
        conn_1 = self.cm.get_available_connection(self.request, cf)
        self.assertEquals(1, len(self.cm._hostmap))
        self.assertEquals(1, len(self.cm._used_cons))
        self.assertEquals(0, len(self.cm._free_conns))

        # Return it to the pool
        self.cm.free_connection(conn_1)
        self.assertEquals(1, len(self.cm._hostmap))
        self.assertEquals(0, len(self.cm._used_cons))
        self.assertEquals(1, len(self.cm._free_conns))

        # Ask for a conn again, since we don't need a new connection, it should
        # return one from the pool
        conn_2 = self.cm.get_available_connection(self.request, cf)
        self.assertIs(conn_2, conn_1)

        t0 = time.time()
        self.assertRaises(BaseFrameworkException, self.cm.get_available_connection, self.request, cf)
        self.assertTrue(time.time() - t0 >= 2.9, "Method returned before expected time")

    def test_get_available_conn_new_connection_requested(self):
        # We want a new HTTPConnection for each request
        self.request.new_connection = True

        self.cm._host_pool_size = 2
        self.assertEquals(0, len(self.cm._hostmap))
        self.assertEquals(0, len(self.cm._used_cons))
        self.assertEquals(0, len(self.cm._free_conns))

        # Get connection
        cf = lambda h: Mock()
        conn_1 = self.cm.get_available_connection(self.request, cf)
        self.assertEquals(1, len(self.cm._hostmap))
        self.assertEquals(1, len(self.cm._used_cons))
        self.assertEquals(0, len(self.cm._free_conns))

        # Return it to the pool
        self.cm.free_connection(conn_1)
        self.assertEquals(1, len(self.cm._hostmap))
        self.assertEquals(0, len(self.cm._used_cons))
        self.assertEquals(1, len(self.cm._free_conns))

        # Ask for another connection, it should return a new one
        conn_2 = self.cm.get_available_connection(self.request, cf)
        self.assertIsNot(conn_1, conn_2)

    def test_replace_conn(self):
        cf = lambda h: Mock()
        bad_conn = Mock()
        self.cm.replace_connection(bad_conn, self.request, cf)
        bad_conn = self.cm.get_available_connection(self.request, cf)
        old_len = self.cm.get_connections_total()

        # Replace bad with a new one
        new_conn = self.cm.replace_connection(bad_conn, self.request, cf)

        # Must be different conn objects
        self.assertNotEquals(bad_conn, new_conn)

        # The len must be the same
        self.assertEquals(self.cm.get_connections_total(), old_len)

    def test_remove_conn(self):
        """
        Remove a non existing conn, nothing should happen.
        """
        self.assertEqual(self.cm.get_connections_total(), 0)

        conn = self.cm.get_available_connection(self.request, lambda h: Mock())
        self.assertEqual(self.cm.get_connections_total(), 0)
        non_exist_host = "non_host"

        # Remove ok
        self.cm.remove_connection(conn, non_exist_host)
        self.cm.remove_connection(conn, self.request)

        self.assertEqual(self.cm.get_connections_total(), 0)