def test_channel_consume_ok_frame(self): tag = 'travis-ci' channel = Channel(0, None, rpc_timeout=1) channel.on_frame(specification.Basic.ConsumeOk(tag)) self.assertEqual(channel.consumer_tags[0], tag)
def test_channel_consume_ok_frame(self): tag = 'travis-ci' channel = Channel(0, Mock(name='Connection'), rpc_timeout=1) channel.on_frame(commands.Basic.ConsumeOk(tag)) self.assertEqual(channel.consumer_tags[0], tag)
def test_basic_get_invalid_parameter(self): channel = Channel(0, FakeConnection(), 360) channel.set_state(Channel.OPEN) basic = Basic(channel) self.assertRaisesRegexp( exception.AMQPInvalidArgument, 'queue should be a string', basic.get, None ) self.assertRaisesRegexp( exception.AMQPInvalidArgument, 'no_ack should be a boolean', basic.get, '', 'travis-ci' ) channel.consumer_tags.append('travis-ci') self.assertRaisesRegexp( exception.AMQPChannelError, "Cannot call 'get' when channel " "is set to consume", basic.get, '', True, 'travis-ci' )
def test_basic_consume_invalid_parameter(self): channel = Channel(0, FakeConnection(), 360) channel.set_state(Channel.OPEN) basic = Basic(channel) self.assertRaisesRegexp(exception.AMQPInvalidArgument, 'queue should be a string', basic.consume, None, 1) self.assertRaisesRegexp(exception.AMQPInvalidArgument, 'consumer_tag should be a string', basic.consume, None, '', 1) self.assertRaisesRegexp(exception.AMQPInvalidArgument, 'exclusive should be a boolean', basic.consume, None, '', '', None) self.assertRaisesRegexp(exception.AMQPInvalidArgument, 'no_ack should be a boolean', basic.consume, None, '', '', True, None) self.assertRaisesRegexp(exception.AMQPInvalidArgument, 'no_local should be a boolean', basic.consume, None, '', '', True, True, None) self.assertRaisesRegexp(exception.AMQPInvalidArgument, 'arguments should be a dict or None', basic.consume, None, '', '', True, True, True, [])
def test_channel_throw_exception_check_for_error(self): channel = Channel(0, FakeConnection(), 360) channel.set_state(channel.OPEN) channel.exceptions.append(AMQPConnectionError('travis-ci')) self.assertRaisesRegexp(AMQPConnectionError, 'travis-ci', channel.check_for_errors)
def test_build_empty_inbound_messages(self): channel = Channel(0, FakeConnection(), 360) channel.set_state(Channel.OPEN) result = None for message in channel.build_inbound_messages(break_on_empty=True): result = message self.assertIsNone(result)
def test_exchange_unbind_invalid_parameter(self): channel = Channel(0, FakeConnection(), 360) channel.set_state(Channel.OPEN) exchange = Exchange(channel) self.assertRaisesRegexp( exception.AMQPInvalidArgument, 'destination should be a string', exchange.unbind, None ) self.assertRaisesRegexp( exception.AMQPInvalidArgument, 'source should be a string', exchange.unbind, '', None ) self.assertRaisesRegexp( exception.AMQPInvalidArgument, 'routing_key should be a string', exchange.unbind, '', '', None ) self.assertRaisesRegexp( exception.AMQPInvalidArgument, 'arguments should be a dict or None', exchange.unbind, '', '', '', [] )
def test_channel_start_consuming(self): self.msg = None consumer_tag = 'travis-ci' channel = Channel(0, FakeConnection(), 360) channel.set_state(channel.OPEN) message = self.message.encode('utf-8') message_len = len(message) deliver = specification.Basic.Deliver(consumer_tag='travis-ci') header = ContentHeader(body_size=message_len) body = ContentBody(value=message) channel._inbound = [deliver, header, body] def callback(msg): self.msg = msg channel.set_state(channel.CLOSED) channel.add_consumer_tag(consumer_tag) channel._consumer_callbacks['travis-ci'] = callback channel.start_consuming() self.assertIsNotNone(self.msg, 'No message consumed') self.assertIsInstance(self.msg.body, str) self.assertEqual(self.msg.body.encode('utf-8'), message)
def test_queue_purge_invalid_parameter(self): channel = Channel(0, FakeConnection(), 360) channel.set_state(Channel.OPEN) queue = Queue(channel) self.assertRaisesRegexp(exception.AMQPInvalidArgument, 'queue should be a string', queue.purge, None)
def test_channel_start_consuming_no_consumer_tags(self): channel = Channel(0, FakeConnection(), 360) channel.set_state(channel.OPEN) channel._consumer_callbacks = ['fake'] self.assertIsNone(channel.start_consuming())
def test_queue_declare_invalid_parameter(self): channel = Channel(0, FakeConnection(), 360) channel.set_state(Channel.OPEN) queue = Queue(channel) self.assertRaisesRegexp(exception.AMQPInvalidArgument, 'queue should be a string', queue.declare, None) self.assertRaisesRegexp(exception.AMQPInvalidArgument, 'passive should be a boolean', queue.declare, 'travis-ci', None) self.assertRaisesRegexp(exception.AMQPInvalidArgument, 'durable should be a boolean', queue.declare, 'travis-ci', True, None) self.assertRaisesRegexp(exception.AMQPInvalidArgument, 'exclusive should be a boolean', queue.declare, 'travis-ci', True, True, None) self.assertRaisesRegexp(exception.AMQPInvalidArgument, 'auto_delete should be a boolean', queue.declare, 'travis-ci', True, True, True, None) self.assertRaisesRegexp(exception.AMQPInvalidArgument, 'arguments should be a dict or None', queue.declare, 'travis-ci', True, True, True, True, [])
def test_channel_consume_ok_frame(self): tag = 'hello-world' channel = Channel(0, None, rpc_timeout=360) channel.on_frame(specification.Basic.ConsumeOk(tag)) self.assertEqual(channel.consumer_tags[0], tag)
def test_basic_publish_invalid_parameter(self): channel = Channel(0, FakeConnection(), 360) channel.set_state(Channel.OPEN) basic = Basic(channel) self.assertRaisesRegexp(exception.AMQPInvalidArgument, 'body should be a string', basic.publish, None, '') self.assertRaisesRegexp(exception.AMQPInvalidArgument, 'routing_key should be a string', basic.publish, '', None) self.assertRaisesRegexp(exception.AMQPInvalidArgument, 'exchange should be a string', basic.publish, '', '', None) self.assertRaisesRegexp(exception.AMQPInvalidArgument, 'properties should be a dict or None', basic.publish, '', '', '', []) self.assertRaisesRegexp(exception.AMQPInvalidArgument, 'properties should be a dict or None', basic.publish, '', '', '', 1) self.assertRaisesRegexp(exception.AMQPInvalidArgument, 'mandatory should be a boolean', basic.publish, '', '', '', {}, None) self.assertRaisesRegexp(exception.AMQPInvalidArgument, 'immediate should be a boolean', basic.publish, '', '', '', {}, True, None)
def test_channel_raises_when_closed(self): channel = Channel(0, FakeConnection(FakeConnection.OPEN), 360) channel.set_state(channel.CLOSED) self.assertFalse(channel.is_open) self.assertRaisesRegexp(exception.AMQPChannelError, 'channel was closed', channel.check_for_errors) self.assertTrue(channel.is_closed)
def test_channel_cancel_ok_frame(self): tag = 'hello-world' channel = Channel(0, None, rpc_timeout=360) channel.add_consumer_tag(tag) channel.on_frame(specification.Basic.CancelOk(tag)) self.assertFalse(channel.consumer_tags)
def test_channel_id(self): channel = Channel(0, None, 360) self.assertEqual(int(channel), 0) channel = Channel(1557, None, 360) self.assertEqual(int(channel), 1557)
def test_channel_id(self): channel = Channel(0, Mock(name='Connection'), 360) self.assertEqual(int(channel), 0) channel = Channel(1557, Mock(name='Connection'), 360) self.assertEqual(int(channel), 1557)
def test_channel_build_empty_inbound_messages(self): channel = Channel(0, FakeConnection(), 360) channel.set_state(Channel.OPEN) result = None for message in channel.build_inbound_messages(break_on_empty=True): result = message self.assertIsNone(result)
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_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_channel_build_empty_inbound_messages(self): channel = Channel(0, FakeConnection(), 360) channel.set_state(Channel.OPEN) generator = channel.build_inbound_messages(break_on_empty=True) if hasattr(generator, 'next'): self.assertRaises(StopIteration, generator.next) else: self.assertRaises(StopIteration, generator.__next__)
def test_channel_build_inbound_raises(self, _): channel = Channel(0, FakeConnection(), 360) channel.set_state(Channel.OPEN) 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_closed_after_connection_closed(self): channel = Channel(0, FakeConnection(FakeConnection.CLOSED), 360) channel.set_state(channel.OPEN) self.assertTrue(channel.is_open) self.assertRaisesRegexp(exception.AMQPConnectionError, 'connection was closed', channel.check_for_errors) self.assertTrue(channel.is_closed)
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_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_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_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_throw_exception_check_for_error(self): channel = Channel(0, FakeConnection(), 360) channel.set_state(channel.OPEN) channel.exceptions.append(AMQPConnectionError('travis-ci')) self.assertRaisesRegexp( AMQPConnectionError, 'travis-ci', channel.check_for_errors )
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_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_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_channel_start_consuming_idle_wait(self): self.msg = None consumer_tag = 'travis-ci' channel = Channel(0, FakeConnection(), 360) channel.set_state(channel.OPEN) message = self.message.encode('utf-8') message_len = len(message) def add_inbound(): deliver = commands.Basic.Deliver(consumer_tag='travis-ci') header = ContentHeader(body_size=message_len) body = ContentBody(value=message) channel._inbound = [deliver, header, body] def callback(msg): self.msg = msg channel.set_state(channel.CLOSED) channel.add_consumer_tag(consumer_tag) channel._consumer_callbacks[consumer_tag] = callback threading.Timer(function=add_inbound, interval=1).start() channel.start_consuming() self.assertIsNotNone(self.msg, 'No message consumed') self.assertIsInstance(self.msg.body, str) self.assertEqual(self.msg.body.encode('utf-8'), message)
def test_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_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_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_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_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_return(self): channel = Channel(0, None, 360) basic_return = specification.Basic.Return(reply_code=500, reply_text=b'Error') channel._basic_return(basic_return) self.assertEqual(len(channel.exceptions), 1) why = channel.exceptions.pop(0) self.assertEqual(str(why), "Message not delivered: Error (500) " "to queue '' from exchange ''")
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_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, 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=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_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_closed_after_connection_exception(self): connection = amqpstorm.Connection('localhost', 'guest', 'guest', lazy=True) channel = Channel(0, connection, 360) connection.exceptions.append(AMQPConnectionError('error')) channel.set_state(channel.OPEN) self.assertTrue(connection.is_closed) self.assertTrue(channel.is_open) self.assertRaisesRegexp(exception.AMQPConnectionError, 'error', channel.check_for_errors) self.assertTrue(channel.is_closed)
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_channel_basic_return_frame(self): connection = amqpstorm.Connection('localhost', 'guest', 'guest', lazy=True) channel = Channel(0, connection, rpc_timeout=360) channel.on_frame(specification.Basic.Return(reply_code=500, reply_text='test', exchange='exchange', routing_key='routing_key')) self.assertEqual(str(channel.exceptions[0]), "Message not delivered: test (500) to queue " "'routing_key' from exchange 'exchange'")
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' )