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
|
2012-03-20 14:53:00 +00:00
|
|
|
|
2021-07-20 13:07:49 +00:00
|
|
|
import pytest
|
|
|
|
|
2016-08-22 18:17:59 +00:00
|
|
|
from kombu import Connection, Exchange, Queue
|
2021-05-22 19:28:05 +00:00
|
|
|
from kombu.exceptions import ContentDisallowed
|
2010-11-11 11:53:51 +00:00
|
|
|
|
|
|
|
|
2016-08-22 18:17:59 +00:00
|
|
|
class SimpleBase:
|
2010-11-11 11:53:51 +00:00
|
|
|
|
|
|
|
def Queue(self, name, *args, **kwargs):
|
|
|
|
q = name
|
|
|
|
if not isinstance(q, Queue):
|
|
|
|
q = self.__class__.__name__
|
|
|
|
if name:
|
2020-07-13 13:58:06 +00:00
|
|
|
q = f'{q}.{name}'
|
2010-11-11 11:53:51 +00:00
|
|
|
return self._Queue(q, *args, **kwargs)
|
|
|
|
|
|
|
|
def _Queue(self, *args, **kwargs):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2014-04-23 22:00:03 +00:00
|
|
|
def setup(self):
|
2016-08-22 18:17:59 +00:00
|
|
|
self.connection = Connection(transport='memory')
|
2016-10-27 21:22:19 +00:00
|
|
|
self.connection.default_channel.exchange_declare('amq.direct')
|
2010-11-11 11:53:51 +00:00
|
|
|
|
2014-04-23 22:00:03 +00:00
|
|
|
def teardown(self):
|
2016-08-22 18:17:59 +00:00
|
|
|
self.connection.close()
|
2016-10-27 21:22:19 +00:00
|
|
|
self.connection = None
|
2010-11-11 11:53:51 +00:00
|
|
|
|
|
|
|
def test_produce__consume(self):
|
2012-06-15 17:32:40 +00:00
|
|
|
q = self.Queue('test_produce__consume', no_ack=True)
|
2010-11-11 11:53:51 +00:00
|
|
|
|
2012-06-15 17:32:40 +00:00
|
|
|
q.put({'hello': 'Simple'})
|
2010-11-11 11:53:51 +00:00
|
|
|
|
2016-08-22 18:17:59 +00:00
|
|
|
assert q.get(timeout=1).payload == {'hello': 'Simple'}
|
|
|
|
with pytest.raises(q.Empty):
|
2012-01-13 17:54:48 +00:00
|
|
|
q.get(timeout=0.1)
|
2010-11-11 11:53:51 +00:00
|
|
|
|
|
|
|
def test_produce__basic_get(self):
|
2012-06-15 17:32:40 +00:00
|
|
|
q = self.Queue('test_produce__basic_get', no_ack=True)
|
|
|
|
q.put({'hello': 'SimpleSync'})
|
2016-08-22 18:17:59 +00:00
|
|
|
assert q.get_nowait().payload == {'hello': 'SimpleSync'}
|
|
|
|
with pytest.raises(q.Empty):
|
2012-01-13 17:54:48 +00:00
|
|
|
q.get_nowait()
|
2010-11-11 11:53:51 +00:00
|
|
|
|
2012-06-15 17:32:40 +00:00
|
|
|
q.put({'hello': 'SimpleSync'})
|
2016-08-22 18:17:59 +00:00
|
|
|
assert q.get(block=False).payload == {'hello': 'SimpleSync'}
|
|
|
|
with pytest.raises(q.Empty):
|
2012-01-13 17:54:48 +00:00
|
|
|
q.get(block=False)
|
2010-11-11 11:53:51 +00:00
|
|
|
|
2021-05-22 19:28:05 +00:00
|
|
|
def test_get_nowait_accept(self):
|
|
|
|
q = self.Queue('test_accept', serializer='pickle', accept=['json'])
|
|
|
|
q.put({'hello': 'SimpleSync'})
|
|
|
|
with pytest.raises(ContentDisallowed):
|
|
|
|
q.get_nowait().payload
|
|
|
|
|
|
|
|
q = self.Queue('test_accept1', serializer='json', accept=[])
|
|
|
|
q.put({'hello': 'SimpleSync'})
|
|
|
|
with pytest.raises(ContentDisallowed):
|
|
|
|
q.get_nowait().payload
|
|
|
|
|
|
|
|
q = self.Queue(
|
|
|
|
'test_accept2', serializer='pickle', accept=['json', 'pickle'])
|
|
|
|
q.put({'hello': 'SimpleSync'})
|
|
|
|
assert q.get_nowait().payload == {'hello': 'SimpleSync'}
|
|
|
|
|
|
|
|
def test_get_accept(self):
|
|
|
|
q = self.Queue('test_accept', serializer='pickle', accept=['json'])
|
|
|
|
q.put({'hello': 'SimpleSync'})
|
|
|
|
with pytest.raises(ContentDisallowed):
|
|
|
|
q.get().payload
|
|
|
|
|
|
|
|
q = self.Queue('test_accept1', serializer='pickle', accept=[])
|
|
|
|
q.put({'hello': 'SimpleSync'})
|
|
|
|
with pytest.raises(ContentDisallowed):
|
|
|
|
q.get().payload
|
|
|
|
|
|
|
|
q = self.Queue(
|
|
|
|
'test_accept2', serializer='pickle', accept=['json', 'pickle'])
|
|
|
|
q.put({'hello': 'SimpleSync'})
|
|
|
|
assert q.get().payload == {'hello': 'SimpleSync'}
|
|
|
|
|
2010-11-11 11:53:51 +00:00
|
|
|
def test_clear(self):
|
2012-06-15 17:32:40 +00:00
|
|
|
q = self.Queue('test_clear', no_ack=True)
|
2010-11-11 11:53:51 +00:00
|
|
|
|
|
|
|
for i in range(10):
|
2015-07-13 19:22:16 +00:00
|
|
|
q.put({'hello': 'SimplePurge%d' % (i,)})
|
2010-11-11 11:53:51 +00:00
|
|
|
|
2016-08-22 18:17:59 +00:00
|
|
|
assert q.clear() == 10
|
2010-11-11 11:53:51 +00:00
|
|
|
|
2012-01-13 19:04:49 +00:00
|
|
|
def test_enter_exit(self):
|
2012-06-15 17:32:40 +00:00
|
|
|
q = self.Queue('test_enter_exit')
|
2012-01-13 19:04:49 +00:00
|
|
|
q.close = Mock()
|
2022-04-09 18:42:59 +00:00
|
|
|
with q as x:
|
|
|
|
assert x is q
|
2012-01-13 19:04:49 +00:00
|
|
|
q.close.assert_called_with()
|
|
|
|
|
2010-11-11 11:53:51 +00:00
|
|
|
def test_qsize(self):
|
2012-06-15 17:32:40 +00:00
|
|
|
q = self.Queue('test_clear', no_ack=True)
|
2010-11-11 11:53:51 +00:00
|
|
|
|
|
|
|
for i in range(10):
|
2015-07-13 19:22:16 +00:00
|
|
|
q.put({'hello': 'SimplePurge%d' % (i,)})
|
2010-11-11 11:53:51 +00:00
|
|
|
|
2016-08-22 18:17:59 +00:00
|
|
|
assert q.qsize() == 10
|
|
|
|
assert len(q) == 10
|
2010-11-11 11:53:51 +00:00
|
|
|
|
|
|
|
def test_autoclose(self):
|
|
|
|
channel = self.connection.channel()
|
2012-06-15 17:32:40 +00:00
|
|
|
q = self.Queue('test_autoclose', no_ack=True, channel=channel)
|
2010-11-11 11:53:51 +00:00
|
|
|
q.close()
|
|
|
|
|
|
|
|
def test_custom_Queue(self):
|
|
|
|
n = self.__class__.__name__
|
2020-07-13 13:58:06 +00:00
|
|
|
exchange = Exchange(f'{n}-test.custom.Queue')
|
|
|
|
queue = Queue(f'{n}-test.custom.Queue',
|
2010-11-11 11:53:51 +00:00
|
|
|
exchange,
|
2012-06-15 17:32:40 +00:00
|
|
|
'my.routing.key')
|
2010-11-11 11:53:51 +00:00
|
|
|
|
|
|
|
q = self.Queue(queue)
|
2016-08-22 18:17:59 +00:00
|
|
|
assert q.consumer.queues[0] == queue
|
2010-11-11 11:53:51 +00:00
|
|
|
q.close()
|
|
|
|
|
|
|
|
def test_bool(self):
|
2012-06-15 17:32:40 +00:00
|
|
|
q = self.Queue('test_nonzero')
|
2016-08-22 18:17:59 +00:00
|
|
|
assert q
|
2010-11-11 11:53:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
class test_SimpleQueue(SimpleBase):
|
|
|
|
|
|
|
|
def _Queue(self, *args, **kwargs):
|
|
|
|
return self.connection.SimpleQueue(*args, **kwargs)
|
|
|
|
|
|
|
|
def test_is_ack(self):
|
2012-06-15 17:32:40 +00:00
|
|
|
q = self.Queue('test_is_no_ack')
|
2016-08-22 18:17:59 +00:00
|
|
|
assert not q.no_ack
|
2010-11-11 11:53:51 +00:00
|
|
|
|
2018-08-16 09:00:50 +00:00
|
|
|
def test_queue_args(self):
|
2019-11-22 18:19:03 +00:00
|
|
|
q = self.Queue('test_queue_args', queue_args={'x-queue-mode': 'lazy'})
|
|
|
|
assert len(q.queue.queue_arguments) == 1
|
2018-08-16 09:00:50 +00:00
|
|
|
assert q.queue.queue_arguments['x-queue-mode'] == 'lazy'
|
|
|
|
|
2019-11-22 18:19:03 +00:00
|
|
|
q = self.Queue('test_queue_args')
|
|
|
|
assert q.queue.queue_arguments == {}
|
|
|
|
|
|
|
|
def test_exchange_opts(self):
|
|
|
|
q = self.Queue('test_exchange_opts_a',
|
|
|
|
exchange_opts={'durable': True, 'type': 'fanout',
|
|
|
|
'delivery_mode': 'persistent'})
|
|
|
|
assert q.queue.exchange.type == 'fanout'
|
|
|
|
assert q.queue.exchange.durable
|
|
|
|
assert not q.queue.exchange.auto_delete
|
|
|
|
delivery_mode_code = q.queue.exchange.PERSISTENT_DELIVERY_MODE
|
|
|
|
assert q.queue.exchange.delivery_mode == delivery_mode_code
|
|
|
|
|
|
|
|
q = self.Queue('test_exchange_opts_b')
|
|
|
|
assert q.queue.exchange.type == 'direct'
|
|
|
|
assert q.queue.exchange.durable
|
|
|
|
assert not q.queue.exchange.auto_delete
|
|
|
|
|
|
|
|
def test_queue_opts(self):
|
|
|
|
q = self.Queue('test_queue_opts', queue_opts={'auto_delete': False})
|
|
|
|
assert not q.queue.auto_delete
|
|
|
|
|
2010-11-11 11:53:51 +00:00
|
|
|
|
|
|
|
class test_SimpleBuffer(SimpleBase):
|
|
|
|
|
|
|
|
def Queue(self, *args, **kwargs):
|
|
|
|
return self.connection.SimpleBuffer(*args, **kwargs)
|
|
|
|
|
|
|
|
def test_is_no_ack(self):
|
2012-06-15 17:32:40 +00:00
|
|
|
q = self.Queue('test_is_no_ack')
|
2016-08-22 18:17:59 +00:00
|
|
|
assert q.no_ack
|
2019-11-22 18:19:03 +00:00
|
|
|
|
|
|
|
def test_queue_args(self):
|
|
|
|
q = self.Queue('test_queue_args', queue_args={'x-queue-mode': 'lazy'})
|
|
|
|
assert len(q.queue.queue_arguments) == 1
|
|
|
|
assert q.queue.queue_arguments['x-queue-mode'] == 'lazy'
|
|
|
|
|
|
|
|
def test_exchange_opts(self):
|
|
|
|
q = self.Queue('test_exchange_opts_a',
|
|
|
|
exchange_opts={'durable': True, 'auto_delete': True,
|
|
|
|
'delivery_mode': 'persistent'})
|
|
|
|
assert q.queue.exchange.type == 'direct'
|
|
|
|
assert q.queue.exchange.durable
|
|
|
|
assert q.queue.exchange.auto_delete
|
|
|
|
delivery_mode_code = q.queue.exchange.PERSISTENT_DELIVERY_MODE
|
|
|
|
assert q.queue.exchange.delivery_mode == delivery_mode_code
|
|
|
|
|
|
|
|
q = self.Queue('test_exchange_opts_b')
|
|
|
|
assert q.queue.exchange.type == 'direct'
|
|
|
|
assert not q.queue.exchange.durable
|
|
|
|
assert q.queue.exchange.auto_delete
|
|
|
|
|
|
|
|
def test_queue_opts(self):
|
|
|
|
q = self.Queue('test_queue_opts', queue_opts={'auto_delete': False})
|
|
|
|
assert not q.queue.durable
|
|
|
|
assert not q.queue.auto_delete
|
|
|
|
|
|
|
|
q = self.Queue('test_queue_opts')
|
|
|
|
assert not q.queue.durable
|
|
|
|
assert q.queue.auto_delete
|