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( w3afException, 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)
def setUp(self): self.cm = ConnectionManager() self.host = Mock()
def setUp(self): PyMockTestCase.setUp(self) self.cm = ConnectionManager() self.host = dummy()