def test_queue_purge(self): # Test verifying purging queue frame_writer_cls_mock = Mock() conn = Connection(frame_writer=frame_writer_cls_mock) with patch.object(conn, 'Transport') as transport_mock: handshake(conn, transport_mock) ch = create_channel(1, conn, transport_mock) transport_mock().read_frame.return_value = build_frame_type_1( spec.Queue.PurgeOk, channel=1, arg_format='l', args=(4, )) frame_writer_mock = frame_writer_cls_mock() frame_writer_mock.reset_mock() msg_count = ch.queue_purge('foo') assert msg_count == 4 frame_writer_mock.assert_called_once_with( 1, 1, spec.Queue.Purge, dumps( 'Bsb', # queue, nowait (0, 'foo', False)), None)
def test_received_channel_Close_during_connection_close(self): # This test verifies that library handles correctly closing channel # during closing of connection: # 1. User requests closing connection - client sends Connection.Close # 2. Broker requests closing Channel - client receives Channel.Close # 3. Broker sends Connection.CloseOk # see GitHub issue #218 conn = Connection() with patch.object(conn, 'Transport') as transport_mock: handshake(conn, transport_mock) channel_id = 1 create_channel(channel_id, conn, transport_mock) # Replies sent by broker transport_mock().read_frame.side_effect = [ # Inject close methods build_frame_type_1(spec.Channel.Close, channel=channel_id, args=(1, False), arg_format='Lb'), build_frame_type_1(spec.Connection.CloseOk) ] conn.close()
def test_connecion_ignore_methods_during_close(self, on_blocked_mock): # Test checking that py-amqp will discard any received methods # except Close and Close-OK after sending Connecion.Close method # to server. frame_writer_cls_mock = Mock() frame_writer_mock = frame_writer_cls_mock() conn = Connection(frame_writer=frame_writer_cls_mock) with patch.object(conn, 'Transport') as transport_mock: handshake(conn, transport_mock) frame_writer_mock.reset_mock() # Inject CloseOk response from broker transport_mock().read_frame.side_effect = [ build_frame_type_1(spec.Connection.Blocked, channel=0), build_frame_type_1(spec.Connection.CloseOk) ] t = conn.transport conn.close() on_blocked_mock.assert_not_called() frame_writer_mock.assert_called_once_with( 1, 0, spec.Connection.Close, dumps('BsBB', (0, '', 0, 0)), None) t.close.assert_called_once_with()
def test_queue_get_empty(self): # Test verifying getting message from empty queue frame_writer_cls_mock = Mock() conn = Connection(frame_writer=frame_writer_cls_mock) with patch.object(conn, 'Transport') as transport_mock: handshake(conn, transport_mock) ch = create_channel(1, conn, transport_mock) transport_mock().read_frame.return_value = build_frame_type_1( spec.Basic.GetEmpty, channel=1, arg_format='s', args=('s')) frame_writer_mock = frame_writer_cls_mock() frame_writer_mock.reset_mock() ret = ch.basic_get('foo') assert ret is None frame_writer_mock.assert_called_once_with( 1, 1, spec.Basic.Get, dumps( 'Bsb', # queue, nowait (0, 'foo', False)), None)
def test_login_method_response(self): # An old way of doing things.: login_method, login_response = b'foo', b'bar' with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") self.conn = Connection(login_method=login_method, login_response=login_response) self.conn.send_method = Mock(name='send_method') self.conn._on_start(3, 4, {'foo': 'bar'}, login_method, 'en_US en_GB') assert len(w) == 1 assert issubclass(w[0].category, DeprecationWarning) self.conn.send_method.assert_called_with( spec.Connection.StartOk, 'FsSs', ( self.conn.client_properties, login_method, login_response, self.conn.locale, ), )
def test_channel_open_close(self): # Test checking opening and closing channel frame_writer_cls_mock = Mock() conn = Connection(frame_writer=frame_writer_cls_mock) with patch.object(conn, 'Transport') as transport_mock: handshake(conn, transport_mock) channel_id = 1 transport_mock().read_frame.side_effect = [ # Inject Open Handshake ret_factory(spec.Channel.OpenOk, channel=channel_id, args=(1, False), arg_format='Lb'), # Inject close method ret_factory(spec.Channel.CloseOk, channel=channel_id, args=(1, False), arg_format='Lb') ] frame_writer_mock = frame_writer_cls_mock() frame_writer_mock.reset_mock() on_open_mock = Mock() ch = conn.channel(channel_id=channel_id, callback=on_open_mock) on_open_mock.assert_called_once_with(ch) assert ch.is_open is True ch.close() frame_writer_mock.assert_has_calls([ call(1, 1, spec.Channel.Open, dumps('s', ('', )), None), call(1, 1, spec.Channel.Close, dumps('BsBB', (0, '', 0, 0)), None) ]) assert ch.is_open is False
def test_queue_declare(self): # Test verifying declaring queue frame_writer_cls_mock = Mock() conn = Connection(frame_writer=frame_writer_cls_mock) with patch.object(conn, 'Transport') as transport_mock: handshake(conn, transport_mock) ch = create_channel(1, conn, transport_mock) transport_mock().read_frame.return_value = build_frame_type_1( spec.Queue.DeclareOk, channel=1, arg_format='sll', args=('foo', 1, 2)) frame_writer_mock = frame_writer_cls_mock() frame_writer_mock.reset_mock() ret = ch.queue_declare('foo') assert ret == queue_declare_ok_t(queue='foo', message_count=1, consumer_count=2) frame_writer_mock.assert_called_once_with( 1, 1, spec.Queue.Declare, dumps( 'BsbbbbbF', ( 0, # queue, passive, durable, exclusive, 'foo', False, False, False, # auto_delete, nowait, arguments True, False, None)), None)
def test_sasl_authentication(self): authentication = SASL() self.conn = Connection(authentication=authentication) assert self.conn.authentication == (authentication, )
def test_channel_ignore_methods_during_close(self): # Test checking that py-amqp will discard any received methods # except Close and Close-OK after sending Channel.Close method # to server. frame_writer_cls_mock = Mock() conn = Connection(frame_writer=frame_writer_cls_mock) consumer_tag = 'amq.ctag-PCmzXGkhCw_v0Zq7jXyvkg' with patch.object(conn, 'Transport') as transport_mock: handshake(conn, transport_mock) channel_id = 1 transport_mock().read_frame.side_effect = [ # Inject Open Handshake build_frame_type_1( spec.Channel.OpenOk, channel=channel_id, args=(1, False), arg_format='Lb' ), # Inject basic-deliver response build_frame_type_1( spec.Basic.Deliver, channel=1, arg_format='sLbss', args=( # consumer-tag, delivery-tag, redelivered, consumer_tag, 1, False, # exchange-name, routing-key 'foo_exchange', 'routing-key' ) ), build_frame_type_2( channel=1, body_len=12, properties=b'0\x00\x00\x00\x00\x00\x01' ), build_frame_type_3( channel=1, body=b'Hello World!' ), # Inject close method build_frame_type_1( spec.Channel.CloseOk, channel=channel_id ), ] frame_writer_mock = frame_writer_cls_mock() frame_writer_mock.reset_mock() with patch('amqp.Channel._on_basic_deliver') as on_deliver_mock: ch = conn.channel(channel_id=channel_id) ch.close() on_deliver_mock.assert_not_called() frame_writer_mock.assert_has_calls( [ call( 1, 1, spec.Channel.Open, dumps('s', ('',)), None ), call( 1, 1, spec.Channel.Close, dumps('BsBB', (0, '', 0, 0)), None ) ] ) assert ch.is_open is False
def test_invalid_method(self): with pytest.raises(ValueError): self.conn = Connection(login_method='any')
def test_amqplain(self): self.conn = Connection(userid='foo', password='******') assert isinstance(self.conn.authentication[1], AMQPLAIN) assert self.conn.authentication[1].username == 'foo' assert self.conn.authentication[1].password == 'bar'
def test_missing_credentials(self): with pytest.raises(ValueError): self.conn = Connection(userid=None, password=None) with pytest.raises(ValueError): self.conn = Connection(password=None)
def get_conn(self): from amqp import Connection return Connection(**self.conn_info)
def test_login_method_external(self): self.conn = Connection(userid=None, password=None, login_method='EXTERNAL') auths = self.conn.authentication assert len(auths) == 1 assert isinstance(auths[0], EXTERNAL)
def test_login_method_plain(self): self.conn = Connection(login_method='PLAIN') auths = self.conn.authentication assert len(auths) == 1 assert isinstance(auths[0], PLAIN)
def test_external(self): self.conn = Connection() assert isinstance(self.conn.authentication[1], EXTERNAL)
def test_collect_again(self): self.conn = Connection() self.conn.collect() self.conn.collect()
import requests from amqp import Message, Connection, ConnectionError, ChannelError import nb_log TEST_QUEUE = 'pyrabbit.testq' connection = Connection(host='localhost:5672', userid='guest', password='******', virtual_host='/') channel = connection.channel() channel.queue_delete(TEST_QUEUE) channel.exchange_declare(TEST_QUEUE, 'direct') x = channel.queue_declare(TEST_QUEUE) # self.assertEqual(x.message_count, x[1]) # self.assertEqual(x.consumer_count, x[2]) # self.assertEqual(x.queue, TEST_QUEUE) channel.queue_bind(TEST_QUEUE, TEST_QUEUE, TEST_QUEUE)
def test_basic_deliver(self): # Test checking delivering single message callback_mock = Mock() frame_writer_cls_mock = Mock() conn = Connection(frame_writer=frame_writer_cls_mock) consumer_tag = 'amq.ctag-PCmzXGkhCw_v0Zq7jXyvkg' with patch.object(conn, 'Transport') as transport_mock: handshake(conn, transport_mock) ch = create_channel(1, conn, transport_mock) # Inject ConsumeOk response from Broker transport_mock().read_frame.side_effect = [ # Inject Consume-ok response build_frame_type_1( spec.Basic.ConsumeOk, channel=1, args=(consumer_tag,), arg_format='s' ), # Inject basic-deliver response build_frame_type_1( spec.Basic.Deliver, channel=1, arg_format='sLbss', args=( # consumer-tag, delivery-tag, redelivered, consumer_tag, 1, False, # exchange-name, routing-key 'foo_exchange', 'routing-key' ) ), build_frame_type_2( channel=1, body_len=12, properties=b'0\x00\x00\x00\x00\x00\x01' ), build_frame_type_3( channel=1, body=b'Hello World!' ), ] frame_writer_mock = frame_writer_cls_mock() frame_writer_mock.reset_mock() ch.basic_consume('my_queue', callback=callback_mock) conn.drain_events() callback_mock.assert_called_once_with(ANY) msg = callback_mock.call_args[0][0] assert isinstance(msg, Message) assert msg.body_size == 12 assert msg.body == b'Hello World!' assert msg.frame_method == spec.Basic.Deliver assert msg.delivery_tag == 1 assert msg.ready is True assert msg.delivery_info == { 'consumer_tag': 'amq.ctag-PCmzXGkhCw_v0Zq7jXyvkg', 'delivery_tag': 1, 'redelivered': False, 'exchange': 'foo_exchange', 'routing_key': 'routing-key' } assert msg.properties == { 'application_headers': {}, 'delivery_mode': 1 }
def test_connect_missing_capabilities(self): # Test checking connection handshake with broker # supporting subset of capabilities frame_writer_cls_mock = Mock() on_open_mock = Mock() frame_writer_mock = frame_writer_cls_mock() conn = Connection( frame_writer=frame_writer_cls_mock, on_open=on_open_mock ) with patch.object(conn, 'Transport') as transport_mock: server_properties = dict(SERVER_PROPERTIES) server_properties['capabilities'] = { # This capability is not supported by client 'basic.nack': True, 'consumer_cancel_notify': True, 'connection.blocked': False, # server does not support 'authentication_failure_close' # which is supported by client } client_properties = dict(CLIENT_PROPERTIES) client_properties['capabilities'] = { 'consumer_cancel_notify': True, } handshake( conn, transport_mock, server_properties=server_properties ) on_open_mock.assert_called_once_with(conn) security_mechanism = sasl.AMQPLAIN( 'guest', 'guest' ).start(conn).decode('utf-8', 'surrogatepass') # Expected responses from client frame_writer_mock.assert_has_calls( [ call( 1, 0, spec.Connection.StartOk, # Due Table type, we cannot compare bytestream directly DataComparator( 'FsSs', ( client_properties, 'AMQPLAIN', security_mechanism, 'en_US' ) ), None ), call( 1, 0, spec.Connection.TuneOk, dumps( 'BlB', (conn.channel_max, conn.frame_max, conn.heartbeat) ), None ), call( 1, 0, spec.Connection.Open, dumps('ssb', (conn.virtual_host, '', False)), None ) ] ) assert conn.client_properties == client_properties
def test_collect_no_transport(self): self.conn = Connection() self.conn.connect = Mock(name='connect') assert not self.conn.connected self.conn.collect() assert not self.conn.connect.called
def test_repr_connected(self, conn_kwargs, expected_vhost): c = Connection(host='broker.com:1234', **conn_kwargs) c._transport = Mock(name='transport') assert re.fullmatch( r'<AMQP Connection: broker.com:1234/{} using {} at 0x.*>'.format( expected_vhost, repr(c.transport)), repr(c))
def test_sasl_authentication_iterable(self): authentication = SASL() self.conn = Connection(authentication=(authentication, )) assert self.conn.authentication == (authentication, )
def test_missing_credentials(self): with pytest.raises(ValueError): self.conn = Connection(userid=None, password=None, login_method='AMQPLAIN') with pytest.raises(ValueError): self.conn = Connection(password=None, login_method='PLAIN')
def test_gssapi(self): self.conn = Connection() assert isinstance(self.conn.authentication[0], GSSAPI)
def setUp(self): self.conn = Connection(**settings.connect_args)
def test_plain(self): self.conn = Connection(userid='foo', password='******') auth = self.conn.authentication[3] assert isinstance(auth, PLAIN) assert auth.username == 'foo' assert auth.password == 'bar'
def setUp(self): self.conn = Connection(**settings.connect_args) self.ch = self.conn.channel()
def test_login_response(self): self.conn = Connection(login_response='foo') assert self.conn.login_response == 'foo'
def test_login_response(self): self.conn = Connection(login_response='foo') self.assertEqual(self.conn.login_response, 'foo')