def test_channel_flow_frame(self): connection = FakeConnection() connection.set_state(connection.OPEN) channel = Channel(0, connection, rpc_timeout=1) channel.set_state(channel.OPEN) channel.on_frame(commands.Channel.Flow()) self.assertIsInstance(connection.get_last_frame(), commands.Channel.FlowOk)
def test_channel_flow_frame(self): connection = FakeConnection() connection.set_state(connection.OPEN) channel = Channel(0, connection, rpc_timeout=360) channel.set_state(channel.OPEN) channel.on_frame(specification.Channel.Flow()) self.assertIsInstance(connection.frames_out[0][1], specification.Channel.FlowOk)
def test_basic_recover_invalid_parameter(self): channel = Channel(0, FakeConnection(), 360) channel.set_state(Channel.OPEN) basic = Basic(channel) self.assertRaisesRegexp( exception.AMQPInvalidArgument, 'requeue should be a boolean', basic.recover, None )
def test_exchange_delete_invalid_parameter(self): channel = Channel(0, FakeConnection(), 360) channel.set_state(Channel.OPEN) exchange = Exchange(channel) self.assertRaisesRegexp( exception.AMQPInvalidArgument, 'exchange should be a string', exchange.delete, None )
def test_basic_cancel_invalid_parameter(self): channel = Channel(0, FakeConnection(), 360) channel.set_state(Channel.OPEN) basic = Basic(channel) self.assertRaisesRegexp( exception.AMQPInvalidArgument, 'consumer_tag should be a string', basic.cancel, None )
def test_connection_cleanup_multiple_channels(self): connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True) for index in range(1, 10): connection._channels[index] = Channel(index, connection, 0.1) for index in range(1, 10): connection._cleanup_channel(index) self.assertFalse(connection._channels)
def test_channel_build_message_body_break_on_empty_value(self): channel = Channel(0, Mock(name='Connection'), 360) message = self.message message_len = len(message) body = ContentBody(value=b'') channel._inbound = [body] result = channel._build_message_body(message_len) self.assertEqual(result, b'')
def test_channel_build_message_body(self): channel = Channel(0, Mock(name='Connection'), 360) message = self.message.encode('utf-8') message_len = len(message) body = ContentBody(value=message) channel._inbound = [body] result = channel._build_message_body(message_len) self.assertEqual(message, result)
def test_basic_get_content_body_timeout_error(self): body = ContentBody(value=self.message) channel = Channel(0, FakeConnection(), 0.01) channel.set_state(Channel.OPEN) basic = Basic(channel) uuid = channel.rpc.register_request([body.name]) self.assertRaisesRegexp(exception.AMQPChannelError, 'rpc requests .* \(.*\) took too long', basic._get_content_body, uuid, len(self.message))
def test_channel_build_message_body_break_on_none_value(self): channel = Channel(0, None, 360) message = self.message message_len = len(message) body = ContentBody(value=None) channel._inbound = [body] result = channel._build_message_body(message_len) self.assertEqual(result, b'')
def test_channel_close_frame_when_connection_closed(self): connection = FakeConnection(state=FakeConnection.CLOSED) channel = Channel(0, connection, rpc_timeout=1) channel.set_state(channel.OPEN) channel.on_frame( commands.Channel.Close(reply_code=500, reply_text='travis-ci')) self.assertIsNone(connection.get_last_frame()) self.assertEqual(str(channel.exceptions[0]), 'Channel 0 was closed by remote server: travis-ci')
def test_channel_build_message_body(self): channel = Channel(0, None, 360) message = b'Hello World!' message_len = len(message) body = ContentBody(value=message) channel._inbound = [body] result = channel._build_message_body(message_len) self.assertEqual(message, result)
def test_channel_basic_cancel_frame(self): connection = amqpstorm.Connection('localhost', 'guest', 'guest', lazy=True) channel = Channel(0, connection, rpc_timeout=360) channel.on_frame(specification.Basic.Cancel('unit-test')) self.assertEqual(self.logging_handler.messages['warning'][0], 'Received Basic.Cancel on consumer_tag: unit-test')
def test_channel_basic_cancel_frame(self): connection = amqpstorm.Connection('localhost', 'guest', 'guest', lazy=True) channel = Channel(0, connection, rpc_timeout=1) channel.on_frame(specification.Basic.Cancel('travis-ci')) self.assertEqual(self.get_last_log(), 'Received Basic.Cancel on consumer_tag: travis-ci')
def test_channel_build_inbound_raises(self): channel = Channel(0, FakeConnection(), 360) channel.set_state(Channel.OPEN) with mock.patch('amqpstorm.Channel._build_message', side_effect=AMQPChannelError()): generator = channel.build_inbound_messages(break_on_empty=False) if hasattr(generator, 'next'): self.assertRaises(AMQPChannelError, generator.next) else: self.assertRaises(AMQPChannelError, generator.__next__)
def test_channel_confirm_deliveries(self): def on_select_ok(*_): channel.rpc.on_frame(specification.Confirm.SelectOk()) connection = FakeConnection(on_write=on_select_ok) channel = Channel(0, connection, 0.01) channel.set_state(Channel.OPEN) self.assertFalse(channel.confirming_deliveries) self.assertEqual(channel.confirm_deliveries(), {}) self.assertTrue(channel.confirming_deliveries)
def test_channel_raises_with_return_reply_code_500(self): channel = Channel(0, FakeConnection(), 360) channel.set_state(channel.OPEN) basic_return = specification.Basic.Return(reply_code=500, reply_text='Error') channel._basic_return(basic_return) self.assertRaisesRegexp( AMQPMessageError, "Message not delivered: Error \(500\) to queue " "'' from exchange ''", channel.check_for_errors)
def test_channel_open(self): def on_open_ok(_, frame_out): self.assertIsInstance(frame_out, specification.Channel.Open) channel.rpc.on_frame(specification.Channel.OpenOk()) channel = Channel(0, FakeConnection(on_write=on_open_ok), 360) # Close Channel. channel.open() self.assertEqual(channel._state, channel.OPEN)
def test_channel_unhandled_frame(self): connection = amqpstorm.Connection('localhost', 'guest', 'guest', lazy=True) channel = Channel(0, connection, rpc_timeout=1) channel.on_frame(FakeFrame()) self.assertEqual( self.get_last_log(), "[Channel0] Unhandled Frame: FakeFrame -- " "{'data_1': 'hello world'}")
def test_channel_close_frame(self): connection = FakeConnection(state=FakeConnection.OPEN) channel = Channel(0, connection, rpc_timeout=1) channel.set_state(channel.OPEN) channel.on_frame( commands.Channel.Close(reply_code=500, reply_text='travis-ci')) self.assertRaisesRegexp( AMQPChannelError, 'Channel 0 was closed by remote server: travis-ci', channel.check_for_errors)
def test_channel_with_statement_when_failing(self): connection = FakeConnection() try: with Channel(0, connection, 360) as channel: channel.exceptions.append(AMQPChannelError('error')) channel.check_for_errors() except AMQPChannelError as why: self.assertIsInstance(why, AMQPChannelError) self.assertEqual(self.get_last_log(), 'Closing channel due to an unhandled exception: ' 'error')
def test_channel_close_frame(self): connection = amqpstorm.Connection('localhost', 'guest', 'guest', lazy=True) channel = Channel(0, connection, rpc_timeout=360) channel.on_frame( specification.Channel.Close(reply_code=500, reply_text='test')) self.assertEqual(str(channel.exceptions[0]), 'Channel 0 was closed by remote server: test')
def test_basic_publish_confirms_raises_on_timeout(self): connection = FakeConnection() channel = Channel(9, connection, 0.01) channel._confirming_deliveries = True channel.set_state(Channel.OPEN) basic = Basic(channel) self.assertRaisesRegexp(exception.AMQPChannelError, 'rpc requests .* \(.*\) took too long', basic.publish, body=self.message, routing_key='travis-ci')
def test_channel_build_message_headers(self): channel = Channel(0, Mock(name='Connection'), 360) deliver = specification.Basic.Deliver() header = ContentHeader(body_size=10) channel._inbound = [deliver, header] result = channel._build_message_headers() self.assertIsInstance(result[0], specification.Basic.Deliver) self.assertIsInstance(result[1], ContentHeader) self.assertEqual(result[1].body_size, 10)
def test_connection_close_channels(self): connection = Connection('127.0.0.1', 'guest', 'guest', lazy=True) channel_1 = Channel(1, connection, 360) channel_2 = Channel(2, connection, 360) channel_3 = Channel(3, connection, 360) channel_1.set_state(Channel.CLOSED) channel_2.set_state(Channel.CLOSED) channel_2.set_state(Channel.OPEN) connection._channels[1] = channel_1 connection._channels[2] = channel_2 connection._channels[3] = channel_3 connection._close_remaining_channels() self.assertTrue(channel_1.is_closed) self.assertTrue(channel_2.is_closed) self.assertTrue(channel_3.is_closed) self.assertFalse(connection._channels)
def test_channel_unhandled_frame(self): connection = amqpstorm.Connection('localhost', 'guest', 'guest', lazy=True) channel = Channel(0, connection, rpc_timeout=360) channel.on_frame(FakeFrame()) self.assertEqual( self.logging_handler.messages['error'][0], "[Channel0] Unhandled Frame: FakeFrame -- " "{'data_1': 'hello world'}")
def test_channel_basic_return_raises_when_500(self): channel = Channel(0, None, 360) basic_return = specification.Basic.Return(reply_code=500, reply_text='Error') channel._basic_return(basic_return) self.assertEqual(len(channel.exceptions), 1) why = channel.exceptions.pop(0) self.assertIsInstance(why, AMQPMessageError) self.assertEqual( str(why), "Message not delivered: Error (500) " "to queue '' from exchange ''")
def test_channel_closed_after_connection_exception(self): connection = amqpstorm.Connection('localhost', 'guest', 'guest', lazy=True) channel = Channel(0, connection, 360) connection.exceptions.append(AMQPConnectionError('travis-ci')) channel.set_state(channel.OPEN) self.assertTrue(connection.is_closed) self.assertTrue(channel.is_open) self.assertRaisesRegexp(exception.AMQPConnectionError, 'travis-ci', channel.check_for_errors) self.assertTrue(channel.is_closed)
def test_channel_close(self): channel = Channel(0, None, 360) # Set up Fake Channel. channel._inbound = [1, 2, 3] channel.set_state(channel.OPEN) channel._consumer_tags = [1, 2, 3] # Close Channel. channel._close_channel(specification.Channel.Close(reply_text='')) self.assertEqual(channel._inbound, []) self.assertEqual(channel._consumer_tags, []) self.assertEqual(channel._state, channel.CLOSED)
def test_channel_build_out_of_order_message(self): channel = Channel(0, None, 360) message = b'Hello World!' message_len = len(message) deliver = specification.Basic.Deliver() header = ContentHeader(body_size=message_len) body = ContentBody(value=message) channel._inbound = [deliver, deliver, header, body] result = channel._build_message() self.assertEqual(result, None)