def test_handle_exception_on_connection_error(self): rabbit_mq_mock = self._mock_rabbit_mq_client( [ConnectionClosed(), True]) handler = UpdateExceptionHandler() handler.handle_exception('DriverName', {'data': 'test'}) self.assertEqual(2, rabbit_mq_mock.call_count)
def test_SubscribeBadSingleQueueCreate(self, pikaMock): # ConnectionClosed pikaMock.return_value.channel.return_value.queue_declare.side_effect=[ConnectionClosed(1, "Boom"), pikaMock.return_value.channel.return_value.queue_declare.return_value] self.broker = self._getBroker() exchange = "testExchange" topic1 = "topic.one" topics = [topic1] self.broker.subscribe(self.testCallback, topics, exchange) self.broker.unsubscribe() self.broker.disconnect() pikaMock.return_value.channel.return_value.queue_declare.side_effect=[ChannelClosed(1, "Boom"), pikaMock.return_value.channel.return_value.queue_declare.return_value] self.broker.subscribe(self.testCallback, topics, exchange) self.broker.unsubscribe() self.broker.disconnect() pikaMock.return_value.channel.return_value.queue_declare.side_effect=[ChannelError("Boom"), pikaMock.return_value.channel.return_value.queue_declare.return_value] self.broker.subscribe(self.testCallback, topics, exchange) self.broker.unsubscribe() self.broker.disconnect() pikaMock.return_value.channel.return_value.queue_declare.side_effect=[AMQPConnectionError("Boom"), pikaMock.return_value.channel.return_value.queue_declare.return_value] self.broker.subscribe(self.testCallback, topics, exchange) self.broker.unsubscribe() self.broker.disconnect() pikaMock.return_value.channel.return_value.queue_declare.side_effect=[AttributeError("Boom"), pikaMock.return_value.channel.return_value.queue_declare.return_value] self.broker.subscribe(self.testCallback, topics, exchange) self.broker.unsubscribe() self.broker.disconnect()
def test_SubscribeBadDoubleQueueCreate(self, pikaMock): pikaMock.return_value.channel.return_value.queue_declare.side_effect=[ConnectionClosed(1, "Boom"), ConnectionClosed(1, "Boom2")] self.broker = self._getBroker() exchange = "testExchange" topic1 = "topic.one" topics = [topic1] with self.assertRaises(ConnectionClosed): self.broker.subscribe(self.testCallback, topics, exchange) self.broker.disconnect() pikaMock.return_value.channel.return_value.queue_declare.side_effect=[ChannelClosed(1, "Boom"), ChannelClosed(1, "Boom2")] with self.assertRaises(ChannelClosed): self.broker.subscribe(self.testCallback, topics, exchange) self.broker.disconnect() pikaMock.return_value.channel.return_value.queue_declare.side_effect=[ChannelError("Boom"), ChannelError("Boom2")] with self.assertRaises(ChannelError): self.broker.subscribe(self.testCallback, topics, exchange) self.broker.disconnect() pikaMock.return_value.channel.return_value.queue_declare.side_effect=[AMQPConnectionError("Boom"), AMQPConnectionError("Boom2")] with self.assertRaises(AMQPConnectionError): self.broker.subscribe(self.testCallback, topics, exchange) self.broker.disconnect() pikaMock.return_value.channel.return_value.queue_declare.side_effect=[AttributeError("Boom"), AttributeError("Boom2")] with self.assertRaises(AttributeError): self.broker.subscribe(self.testCallback, topics, exchange) self.broker.disconnect()
def notify_futures(self, connection, code, msg): try: for protocol in self.protocols.values(): protocol.resolve.set_exception(ConnectionClosed()) finally: self.connection.ioloop.stop()
def test_connection_closed_while_creating(self, mock_sleep, mock_blocking_connection): mock_blocking_connection.side_effect = ConnectionClosed( reply_code=200, reply_text='Normal Shutdown') with self.assertRaises(ConnectionClosed): connection = self.pool.create() self.pool.log.critical.assert_called_once() self.assertEqual(self.mock_sleep.call_count, CONNECTION_RETRIES - 1)
def test_connect_errs_upon_exception(self, bc_mock, log_mock): bc_mock.side_effect = ConnectionClosed('Connection refused') self.broker.connect() self.assertEqual(self.cp_mock.call_count, 2) bc_mock.assert_called_once_with(self.params) self.bc_mock.assert_called_once() log_mock.error.assert_called_once() self.assertIsNone(self.broker.connection) self.assertIsNone(self.broker.channel)
def teardown_loop(self, connection, code, msg): """ Connection close callback. By default notify all waiting threads about connection state, then teardown event loop. """ try: self.protocol_error(ConnectionClosed()) finally: self.connection.ioloop.stop()
def test_on_open_closed(self, mock_cxn: mock.MagicMock) -> None: with self.assertLogs("zulip.queue", "WARNING") as m: connection = TornadoQueueClient() connection.connection.channel.side_effect = ConnectionClosed("500", "test") connection._on_open(mock.MagicMock()) self.assertEqual( m.output, [ "WARNING:zulip.queue:TornadoQueueClient couldn't open channel: connection already closed" ], )
def notify_futures(self, connection, code, msg): """ Connection close callback. By default notify all waiting threads about connection state, then teardown event loop. """ try: for protocol in self.protocols.values(): protocol.resolve.set_exception(ConnectionClosed()) finally: self.connection.ioloop.stop()
def connection_lost(self, reason): # Let the caller know there's been an error d, self.ready = self.ready, None if d: # reason == None means that connection closed by this side or EOF is received # anyway, it is closed, and we should report it to caller, # because future.result() does not raise after future.set_exception(None) if reason is None: reason = ConnectionClosed('Connection was closed') d.set_exception(reason) else: # Let client to handle disconnect if self.connection_lost_callback: self.connection_lost_callback(reason)
def _publish(self, message, force_new_channel=False): try: channel = self._get_channel(force=force_new_channel) channel.basic_publish( exchange='', routing_key=settings.CHAT_AMQP_QUEUE, body=message, ) except ConnectionClosed: # Tries to reopen channel and publish again. # If channel already was reopened, than it not helpes and we should # pass ConnectionClosed exception. if not force_new_channel: self._publish(message, force_new_channel=True) else: raise ConnectionClosed()
def on_connection_closed(self, connection, code, msg): """ Connection close callback. By default notify all waiting threads about connection state, then teardown event loop or try to reconnect. """ try: self.protocol_error(ConnectionClosed()) except: logger.warning( 'Exception when notifying threads about closed ' 'connection', exc_info=True) if self.connection.is_closing: self.connection.ioloop.stop() else: logger.info('Connection closed, reopening in 5 seconds') self.connection.add_timeout(5, self.reconnect)
def test_on_open_closed(self, mock_cxn: mock.MagicMock, mock_get_logger: mock.MagicMock) -> None: connection = TornadoQueueClient() connection.connection.channel.side_effect = ConnectionClosed( '500', 'test') connection._on_open(mock.MagicMock())
class WhenConnectionClosedRaised(_BaseProcessingErrorTestCase): exc = ConnectionClosed(sentinel.arg0, sentinel.arg1) def should_reconnect(self): self.agent.reconnect.assert_called_once_with()
def test_on_open_closed(self, mock_cxn: mock.MagicMock) -> None: with self.assertLogs('zulip.queue', 'WARNING') as m: connection = TornadoQueueClient() connection.connection.channel.side_effect = ConnectionClosed('500', 'test') connection._on_open(mock.MagicMock()) self.assertEqual(m.output, ['WARNING:zulip.queue:TornadoQueueClient couldn\'t open channel: connection already closed'])