def test_request_finished_failed_connection(self): # Force closing connection on request end connection.settings_dict['CONN_MAX_AGE'] = 0 connection.enter_transaction_management() connection.set_dirty() # Test that the rollback doesn't succeed (for example network failure # could cause this). def fail_horribly(): raise Exception("Horrible failure!") connection._rollback = fail_horribly try: with self.assertRaises(Exception): signals.request_finished.send(sender=self.__class__) # The connection's state wasn't cleaned up self.assertEqual(len(connection.transaction_state), 1) finally: del connection._rollback # The connection will be cleaned on next request where the conn # works again. signals.request_finished.send(sender=self.__class__) self.assertEqual(len(connection.transaction_state), 0)