def create_database_and_proxy(self): """Set up the TCP proxy and database object. The TCP proxy should forward requests on to the database. The database object should point at the TCP proxy. """ uri = self.get_uri() self.proxy = ProxyTCPServer((uri.host, uri.port)) uri.host, uri.port = self.proxy.server_address self.database = create_database(uri)
def create_database_and_proxy(self): """Set up the TCP proxy and database object. The TCP proxy should forward requests on to the database. The database object should point at the TCP proxy. """ uri = self.get_uri() self.proxy = ProxyTCPServer((uri.host, uri.port)) uri.host, uri.port = self.proxy.server_address self.proxy_uri = uri self.database = create_database(uri)
class DatabaseDisconnectionTest(object): environment_variable = "" host_environment_variable = "" default_port = None def setUp(self): super(DatabaseDisconnectionTest, self).setUp() self.create_database_and_proxy() self.create_connection() def tearDown(self): self.drop_database() self.proxy.close() super(DatabaseDisconnectionTest, self).tearDown() def is_supported(self): return bool(self.get_uri()) def get_uri(self): """Return URI instance with a defined host and port.""" if not self.environment_variable or not self.default_port: raise RuntimeError("Define at least %s.environment_variable and " "%s.default_port" % (type(self).__name__, type(self).__name__)) uri_str = os.environ.get(self.host_environment_variable) if uri_str: uri = URI(uri_str) if not uri.host: raise RuntimeError("The URI in %s must include a host." % self.host_environment_variable) if not uri.port: uri.port = self.default_port return uri else: uri_str = os.environ.get(self.environment_variable) if uri_str: uri = URI(uri_str) if uri.host: if not uri.port: uri.port = self.default_port return uri return None def create_database_and_proxy(self): """Set up the TCP proxy and database object. The TCP proxy should forward requests on to the database. The database object should point at the TCP proxy. """ uri = self.get_uri() self.proxy = ProxyTCPServer((uri.host, uri.port)) uri.host, uri.port = self.proxy.server_address self.database = create_database(uri) def create_connection(self): self.connection = self.database.connect() def drop_database(self): pass def test_proxy_works(self): """Ensure that we can talk to the database through the proxy.""" result = self.connection.execute("SELECT 1") self.assertEqual(result.get_one(), (1,)) def test_catch_disconnect_on_execute(self): """Test that database disconnections get caught on execute().""" result = self.connection.execute("SELECT 1") self.assertTrue(result.get_one()) self.proxy.restart() self.assertRaises(DisconnectionError, self.connection.execute, "SELECT 1") def test_catch_disconnect_on_commit(self): """Test that database disconnections get caught on commit().""" result = self.connection.execute("SELECT 1") self.assertTrue(result.get_one()) self.proxy.restart() self.assertRaises(DisconnectionError, self.connection.commit) def test_wb_catch_already_disconnected_on_rollback(self): """Connection.rollback() swallows disconnection errors. If the connection is being used outside of Storm's control, then it is possible that Storm won't see the disconnection. It should be able to recover from this situation though. """ result = self.connection.execute("SELECT 1") self.assertTrue(result.get_one()) self.proxy.restart() # Perform an action that should result in a disconnection. try: cursor = self.connection._raw_connection.cursor() cursor.execute("SELECT 1") cursor.fetchone() except Error, exc: self.assertTrue(self.connection.is_disconnection_error(exc)) else: