2022-04-14 11:02:52 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-08-15 20:51:02 +00:00
|
|
|
from unittest.mock import Mock
|
2016-08-22 18:17:59 +00:00
|
|
|
|
2021-07-20 13:07:49 +00:00
|
|
|
import pytest
|
|
|
|
|
2013-03-21 15:50:41 +00:00
|
|
|
from kombu import Connection, Consumer, Exchange, Producer, Queue
|
2013-09-24 13:38:34 +00:00
|
|
|
from kombu.message import Message
|
2021-07-20 13:07:49 +00:00
|
|
|
from kombu.transport.base import (Management, StdChannel, Transport,
|
|
|
|
to_rabbitmq_queue_arguments)
|
2016-10-14 23:53:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('args,input,expected', [
|
|
|
|
({}, {'message_ttl': 20}, {'x-message-ttl': 20000}),
|
|
|
|
({}, {'message_ttl': None}, {}),
|
|
|
|
({'foo': 'bar'}, {'expires': 30.3}, {'x-expires': 30300, 'foo': 'bar'}),
|
|
|
|
({'x-expires': 3}, {'expires': 4}, {'x-expires': 4000}),
|
|
|
|
({}, {'max_length': 10}, {'x-max-length': 10}),
|
|
|
|
({}, {'max_length_bytes': 1033}, {'x-max-length-bytes': 1033}),
|
|
|
|
({}, {'max_priority': 303}, {'x-max-priority': 303}),
|
|
|
|
])
|
|
|
|
def test_rabbitmq_queue_arguments(args, input, expected):
|
|
|
|
assert to_rabbitmq_queue_arguments(args, **input) == expected
|
2010-06-29 15:31:56 +00:00
|
|
|
|
2012-01-13 17:31:40 +00:00
|
|
|
|
2016-08-22 18:17:59 +00:00
|
|
|
class test_StdChannel:
|
2012-01-13 17:31:40 +00:00
|
|
|
|
2014-04-23 22:00:03 +00:00
|
|
|
def setup(self):
|
2012-06-24 15:32:17 +00:00
|
|
|
self.conn = Connection('memory://')
|
2012-01-13 17:31:40 +00:00
|
|
|
self.channel = self.conn.channel()
|
2012-04-10 16:10:31 +00:00
|
|
|
self.channel.queues.clear()
|
|
|
|
self.conn.connection.state.clear()
|
2012-01-13 17:31:40 +00:00
|
|
|
|
|
|
|
def test_Consumer(self):
|
2013-03-21 15:50:41 +00:00
|
|
|
q = Queue('foo', Exchange('foo'))
|
2012-01-13 17:31:40 +00:00
|
|
|
cons = self.channel.Consumer(q)
|
2016-08-22 18:17:59 +00:00
|
|
|
assert isinstance(cons, Consumer)
|
|
|
|
assert cons.channel is self.channel
|
2012-01-13 17:31:40 +00:00
|
|
|
|
|
|
|
def test_Producer(self):
|
|
|
|
prod = self.channel.Producer()
|
2016-08-22 18:17:59 +00:00
|
|
|
assert isinstance(prod, Producer)
|
|
|
|
assert prod.channel is self.channel
|
2012-01-13 17:31:40 +00:00
|
|
|
|
2012-06-11 17:09:13 +00:00
|
|
|
def test_interface_get_bindings(self):
|
2016-08-22 18:17:59 +00:00
|
|
|
with pytest.raises(NotImplementedError):
|
2012-06-11 17:09:13 +00:00
|
|
|
StdChannel().get_bindings()
|
2012-01-13 17:31:40 +00:00
|
|
|
|
|
|
|
def test_interface_after_reply_message_received(self):
|
2016-08-22 18:17:59 +00:00
|
|
|
assert StdChannel().after_reply_message_received(Queue('foo')) is None
|
2012-01-13 17:31:40 +00:00
|
|
|
|
|
|
|
|
2016-08-22 18:17:59 +00:00
|
|
|
class test_Message:
|
2012-01-13 17:31:40 +00:00
|
|
|
|
2014-04-23 22:00:03 +00:00
|
|
|
def setup(self):
|
2012-06-24 15:32:17 +00:00
|
|
|
self.conn = Connection('memory://')
|
2012-01-13 17:31:40 +00:00
|
|
|
self.channel = self.conn.channel()
|
2016-10-27 23:53:52 +00:00
|
|
|
self.message = Message(channel=self.channel, delivery_tag=313)
|
2012-01-13 17:31:40 +00:00
|
|
|
|
2013-09-12 12:10:42 +00:00
|
|
|
def test_postencode(self):
|
2020-07-23 13:33:40 +00:00
|
|
|
m = Message('FOO', channel=self.channel, postencode='ccyzz')
|
2016-08-22 18:17:59 +00:00
|
|
|
with pytest.raises(LookupError):
|
2014-01-28 13:54:04 +00:00
|
|
|
m._reraise_error()
|
|
|
|
m.ack()
|
2013-09-12 12:10:42 +00:00
|
|
|
|
2012-01-13 17:31:40 +00:00
|
|
|
def test_ack_respects_no_ack_consumers(self):
|
2014-05-19 21:27:36 +00:00
|
|
|
self.channel.no_ack_consumers = {'abc'}
|
2012-06-15 17:32:40 +00:00
|
|
|
self.message.delivery_info['consumer_tag'] = 'abc'
|
2012-01-13 17:31:40 +00:00
|
|
|
ack = self.channel.basic_ack = Mock()
|
|
|
|
|
|
|
|
self.message.ack()
|
2016-08-22 18:17:59 +00:00
|
|
|
assert self.message._state != 'ACK'
|
2016-04-09 04:10:31 +00:00
|
|
|
ack.assert_not_called()
|
2012-01-13 17:31:40 +00:00
|
|
|
|
|
|
|
def test_ack_missing_consumer_tag(self):
|
2014-05-19 21:27:36 +00:00
|
|
|
self.channel.no_ack_consumers = {'abc'}
|
2012-01-13 17:31:40 +00:00
|
|
|
self.message.delivery_info = {}
|
|
|
|
ack = self.channel.basic_ack = Mock()
|
|
|
|
|
|
|
|
self.message.ack()
|
2015-12-18 22:52:33 +00:00
|
|
|
ack.assert_called_with(self.message.delivery_tag, multiple=False)
|
2012-01-13 17:31:40 +00:00
|
|
|
|
|
|
|
def test_ack_not_no_ack(self):
|
|
|
|
self.channel.no_ack_consumers = set()
|
2012-06-15 17:32:40 +00:00
|
|
|
self.message.delivery_info['consumer_tag'] = 'abc'
|
2012-01-13 17:31:40 +00:00
|
|
|
ack = self.channel.basic_ack = Mock()
|
|
|
|
|
|
|
|
self.message.ack()
|
2015-12-18 22:52:33 +00:00
|
|
|
ack.assert_called_with(self.message.delivery_tag, multiple=False)
|
2012-01-13 17:31:40 +00:00
|
|
|
|
|
|
|
def test_ack_log_error_when_no_error(self):
|
|
|
|
ack = self.message.ack = Mock()
|
|
|
|
self.message.ack_log_error(Mock(), KeyError)
|
2015-12-18 22:52:33 +00:00
|
|
|
ack.assert_called_with(multiple=False)
|
2012-01-13 17:31:40 +00:00
|
|
|
|
|
|
|
def test_ack_log_error_when_error(self):
|
|
|
|
ack = self.message.ack = Mock()
|
2012-06-15 17:32:40 +00:00
|
|
|
ack.side_effect = KeyError('foo')
|
2012-01-13 17:31:40 +00:00
|
|
|
logger = Mock()
|
|
|
|
self.message.ack_log_error(logger, KeyError)
|
2015-12-18 22:52:33 +00:00
|
|
|
ack.assert_called_with(multiple=False)
|
2016-04-09 04:10:31 +00:00
|
|
|
logger.critical.assert_called()
|
2016-08-22 18:17:59 +00:00
|
|
|
assert "Couldn't ack" in logger.critical.call_args[0][0]
|
2010-06-29 15:31:56 +00:00
|
|
|
|
2013-09-12 12:10:42 +00:00
|
|
|
def test_reject_log_error_when_no_error(self):
|
|
|
|
reject = self.message.reject = Mock()
|
2013-10-07 16:57:39 +00:00
|
|
|
self.message.reject_log_error(Mock(), KeyError, requeue=True)
|
|
|
|
reject.assert_called_with(requeue=True)
|
2013-09-12 12:10:42 +00:00
|
|
|
|
|
|
|
def test_reject_log_error_when_error(self):
|
|
|
|
reject = self.message.reject = Mock()
|
|
|
|
reject.side_effect = KeyError('foo')
|
|
|
|
logger = Mock()
|
|
|
|
self.message.reject_log_error(logger, KeyError)
|
2013-10-07 16:57:39 +00:00
|
|
|
reject.assert_called_with(requeue=False)
|
2016-04-09 04:10:31 +00:00
|
|
|
logger.critical.assert_called()
|
2016-08-22 18:17:59 +00:00
|
|
|
assert "Couldn't reject" in logger.critical.call_args[0][0]
|
2010-06-29 15:31:56 +00:00
|
|
|
|
2013-09-12 16:32:25 +00:00
|
|
|
|
2016-08-22 18:17:59 +00:00
|
|
|
class test_interface:
|
2010-06-29 15:31:56 +00:00
|
|
|
|
|
|
|
def test_establish_connection(self):
|
2016-08-22 18:17:59 +00:00
|
|
|
with pytest.raises(NotImplementedError):
|
2012-01-13 17:54:48 +00:00
|
|
|
Transport(None).establish_connection()
|
2010-06-29 15:31:56 +00:00
|
|
|
|
|
|
|
def test_close_connection(self):
|
2016-08-22 18:17:59 +00:00
|
|
|
with pytest.raises(NotImplementedError):
|
2012-01-13 17:54:48 +00:00
|
|
|
Transport(None).close_connection(None)
|
2010-11-09 14:27:43 +00:00
|
|
|
|
|
|
|
def test_create_channel(self):
|
2016-08-22 18:17:59 +00:00
|
|
|
with pytest.raises(NotImplementedError):
|
2012-01-13 17:54:48 +00:00
|
|
|
Transport(None).create_channel(None)
|
2010-11-09 14:27:43 +00:00
|
|
|
|
|
|
|
def test_close_channel(self):
|
2016-08-22 18:17:59 +00:00
|
|
|
with pytest.raises(NotImplementedError):
|
2012-01-13 17:54:48 +00:00
|
|
|
Transport(None).close_channel(None)
|
2010-11-09 14:27:43 +00:00
|
|
|
|
|
|
|
def test_drain_events(self):
|
2016-08-22 18:17:59 +00:00
|
|
|
with pytest.raises(NotImplementedError):
|
2012-01-13 17:54:48 +00:00
|
|
|
Transport(None).drain_events(None)
|
2013-09-12 12:10:42 +00:00
|
|
|
|
|
|
|
def test_heartbeat_check(self):
|
|
|
|
Transport(None).heartbeat_check(Mock(name='connection'))
|
|
|
|
|
|
|
|
def test_driver_version(self):
|
2016-08-22 18:17:59 +00:00
|
|
|
assert Transport(None).driver_version()
|
2013-09-12 12:10:42 +00:00
|
|
|
|
2013-09-23 16:32:36 +00:00
|
|
|
def test_register_with_event_loop(self):
|
2014-04-23 22:00:03 +00:00
|
|
|
Transport(None).register_with_event_loop(
|
|
|
|
Mock(name='connection'), Mock(name='loop'),
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_unregister_from_event_loop(self):
|
|
|
|
Transport(None).unregister_from_event_loop(
|
|
|
|
Mock(name='connection'), Mock(name='loop'),
|
|
|
|
)
|
2013-09-12 12:10:42 +00:00
|
|
|
|
|
|
|
def test_manager(self):
|
2016-08-22 18:17:59 +00:00
|
|
|
assert Transport(None).manager
|
2013-09-12 12:10:42 +00:00
|
|
|
|
|
|
|
|
2016-08-22 18:17:59 +00:00
|
|
|
class test_Management:
|
2013-09-12 12:10:42 +00:00
|
|
|
|
|
|
|
def test_get_bindings(self):
|
|
|
|
m = Management(Mock(name='transport'))
|
2016-08-22 18:17:59 +00:00
|
|
|
with pytest.raises(NotImplementedError):
|
2013-09-12 12:10:42 +00:00
|
|
|
m.get_bindings()
|