def test_extract_error_message(self): from MySQLdb._exceptions import OperationalError message = "Unknown table 'BIRTH_NAMES1' in information_schema" exception = OperationalError(message) extracted_message = MySQLEngineSpec._extract_error_message(exception) assert extracted_message == message exception = OperationalError(123, message) extracted_message = MySQLEngineSpec._extract_error_message(exception) assert extracted_message == message
def test_connect_one_down(self, mock_connect): "Ensure that connect() works when a peer is down but another is up." mock_connect.side_effect = [OperationalError(), mock.MagicMock()] # If any server is up, connect() should succeed. connection.connect() # One peer should be up, and one down. self.assertEqual(len(connection.state.peers_up), 1) self.assertEqual(len(connection.state.peers_down), 1)
def test_connect_all_down(self, mock_connect): "Ensure an error is raised when all peers are down." mock_connect.side_effect = OperationalError() # If all servers are down, we should raise an exception: with self.assertRaises(DatabaseError): connection.connect() # No peers should be up. self.assertEqual(len(connection.state.peers_up), 0) self.assertEqual(len(connection.state.peers_down), 2)
def test_connect_partial_recovery(self, mock_connect): "Ensure that a peer is retried after check_interval passes." mock_connect.side_effect = [ OperationalError(), OperationalError(), OperationalError(), mock.MagicMock(), mock.MagicMock(), mock.MagicMock() ] # If any server is up, connect() should succeed. with self.assertRaises(DatabaseError): connection.connect() # One peer should be up, and one down. self.assertEqual(len(connection.state.peers_up), 0) self.assertEqual(len(connection.state.peers_down), 2) # FFWD time, our failed server should be retried. future = datetime.now() + timedelta( seconds=connection.state.check_interval) with freeze_time(future): connection.connect() # NOTE: this will NOT bring the other peer online as it's retry # time has not yet transpired. connection.connect() # One peer should be up once again, while the other stays down. self.assertEqual(len(connection.state.peers_up), 1) self.assertEqual(len(connection.state.peers_down), 1) # FFWD time again, and reconnect. future += timedelta(seconds=connection.state.check_interval) with freeze_time(future): connection.connect() # All peers online once again. self.assertEqual(len(connection.state.peers_up), 2) self.assertEqual(len(connection.state.peers_down), 0)
def test_connect_recovery(self, mock_connect): "Ensure that a peer is retried after check_interval passes." mock_connect.side_effect = [ OperationalError(), mock.MagicMock(), mock.MagicMock() ] # If any server is up, connect() should succeed. connection.connect() # One peer should be up, and one down. self.assertEqual(len(connection.state.peers_up), 1) self.assertEqual(len(connection.state.peers_down), 1) # FFWD time, our failed server should be retried. future = datetime.now() + timedelta( seconds=connection.state.check_interval) with freeze_time(future): connection.connect() # all peers should be up once again. self.assertItemsEqual(connection.state.peers_up, connection.state.peers)