kombu/t/unit/test_compat.py

331 lines
11 KiB
Python
Raw Normal View History

import pytest
from case import Mock, patch
from kombu import Connection, Exchange, Queue
from kombu import compat
from t.mocks import Transport, Channel
2010-11-11 13:41:55 +00:00
2010-11-11 14:52:16 +00:00
class test_misc:
2010-11-11 13:41:55 +00:00
def test_iterconsume(self):
2010-11-11 14:52:16 +00:00
2020-07-13 13:58:06 +00:00
class MyConnection:
2010-11-11 13:41:55 +00:00
drained = 0
2010-11-11 14:52:16 +00:00
2010-11-11 13:41:55 +00:00
def drain_events(self, *args, **kwargs):
self.drained += 1
return self.drained
2020-07-13 13:58:06 +00:00
class Consumer:
2010-11-11 13:41:55 +00:00
active = False
def consume(self, *args, **kwargs):
self.active = True
2012-03-23 17:28:53 +00:00
conn = MyConnection()
2010-11-11 13:41:55 +00:00
consumer = Consumer()
it = compat._iterconsume(conn, consumer)
assert next(it) == 1
assert consumer.active
2010-11-11 13:41:55 +00:00
it2 = compat._iterconsume(conn, consumer, limit=10)
assert list(it2), [2, 3, 4, 5, 6, 7, 8, 9, 10 == 11]
2010-11-11 13:41:55 +00:00
def test_Queue_from_dict(self):
2012-06-15 17:32:40 +00:00
defs = {'binding_key': 'foo.#',
'exchange': 'fooex',
'exchange_type': 'topic',
'durable': True,
'auto_delete': False}
q1 = Queue.from_dict('foo', **dict(defs))
assert q1.name == 'foo'
assert q1.routing_key == 'foo.#'
assert q1.exchange.name == 'fooex'
assert q1.exchange.type == 'topic'
assert q1.durable
assert q1.exchange.durable
assert not q1.auto_delete
assert not q1.exchange.auto_delete
2010-11-11 13:41:55 +00:00
2012-06-15 17:32:40 +00:00
q2 = Queue.from_dict('foo', **dict(defs,
exchange_durable=False))
assert q2.durable
assert not q2.exchange.durable
2010-11-11 13:41:55 +00:00
2012-06-15 17:32:40 +00:00
q3 = Queue.from_dict('foo', **dict(defs,
exchange_auto_delete=True))
assert not q3.auto_delete
assert q3.exchange.auto_delete
2010-11-11 13:41:55 +00:00
2012-06-15 17:32:40 +00:00
q4 = Queue.from_dict('foo', **dict(defs,
queue_durable=False))
assert not q4.durable
assert q4.exchange.durable
2010-11-11 13:41:55 +00:00
2012-06-15 17:32:40 +00:00
q5 = Queue.from_dict('foo', **dict(defs,
queue_auto_delete=True))
assert q5.auto_delete
assert not q5.exchange.auto_delete
2010-11-11 13:41:55 +00:00
assert (Queue.from_dict('foo', **dict(defs)) ==
Queue.from_dict('foo', **dict(defs)))
2010-11-11 13:41:55 +00:00
class test_Publisher:
2010-11-11 13:41:55 +00:00
def setup(self):
2012-03-20 14:53:00 +00:00
self.connection = Connection(transport=Transport)
2010-11-11 13:41:55 +00:00
def test_constructor(self):
pub = compat.Publisher(self.connection,
2012-06-15 17:32:40 +00:00
exchange='test_Publisher_constructor',
routing_key='rkey')
assert isinstance(pub.backend, Channel)
assert pub.exchange.name == 'test_Publisher_constructor'
assert pub.exchange.durable
assert not pub.exchange.auto_delete
assert pub.exchange.type == 'direct'
2010-11-11 13:41:55 +00:00
pub2 = compat.Publisher(self.connection,
2012-06-15 17:32:40 +00:00
exchange='test_Publisher_constructor2',
routing_key='rkey',
2010-11-11 13:41:55 +00:00
auto_delete=True,
durable=False)
assert pub2.exchange.auto_delete
assert not pub2.exchange.durable
2010-11-11 13:41:55 +00:00
2012-06-15 17:32:40 +00:00
explicit = Exchange('test_Publisher_constructor_explicit',
2013-01-17 12:02:00 +00:00
type='topic')
2010-11-11 13:41:55 +00:00
pub3 = compat.Publisher(self.connection,
exchange=explicit)
assert pub3.exchange == explicit
2010-11-11 13:41:55 +00:00
2012-01-14 00:02:59 +00:00
compat.Publisher(self.connection,
2012-06-15 17:32:40 +00:00
exchange='test_Publisher_constructor3',
2012-01-14 00:02:59 +00:00
channel=self.connection.default_channel)
2010-11-11 13:41:55 +00:00
def test_send(self):
pub = compat.Publisher(self.connection,
2012-06-15 17:32:40 +00:00
exchange='test_Publisher_send',
routing_key='rkey')
pub.send({'foo': 'bar'})
assert 'basic_publish' in pub.backend
2010-11-11 13:41:55 +00:00
pub.close()
def test__enter__exit__(self):
pub = compat.Publisher(self.connection,
2012-06-15 17:32:40 +00:00
exchange='test_Publisher_send',
routing_key='rkey')
2010-11-11 13:41:55 +00:00
x = pub.__enter__()
assert x is pub
2010-11-11 13:41:55 +00:00
x.__exit__()
assert pub._closed
2010-11-11 13:41:55 +00:00
class test_Consumer:
2010-11-11 13:41:55 +00:00
def setup(self):
2012-03-20 14:53:00 +00:00
self.connection = Connection(transport=Transport)
2010-11-11 13:41:55 +00:00
2012-06-15 17:32:40 +00:00
@patch('kombu.compat._iterconsume')
def test_iterconsume_calls__iterconsume(self, it, n='test_iterconsume'):
2012-01-14 00:02:59 +00:00
c = compat.Consumer(self.connection, queue=n, exchange=n)
c.iterconsume(limit=10, no_ack=True)
it.assert_called_with(c.connection, c, True, 10)
2012-06-15 17:32:40 +00:00
def test_constructor(self, n='test_Consumer_constructor'):
2010-11-11 13:41:55 +00:00
c = compat.Consumer(self.connection, queue=n, exchange=n,
2012-06-15 17:32:40 +00:00
routing_key='rkey')
assert isinstance(c.backend, Channel)
2010-11-11 13:41:55 +00:00
q = c.queues[0]
assert q.durable
assert q.exchange.durable
assert not q.auto_delete
assert not q.exchange.auto_delete
assert q.name == n
assert q.exchange.name == n
2010-11-11 13:41:55 +00:00
2012-06-15 17:32:40 +00:00
c2 = compat.Consumer(self.connection, queue=n + '2',
exchange=n + '2',
routing_key='rkey', durable=False,
2010-11-11 13:41:55 +00:00
auto_delete=True, exclusive=True)
q2 = c2.queues[0]
assert not q2.durable
assert not q2.exchange.durable
assert q2.auto_delete
assert q2.exchange.auto_delete
2010-11-11 13:41:55 +00:00
2012-06-15 17:32:40 +00:00
def test__enter__exit__(self, n='test__enter__exit__'):
2010-11-11 13:41:55 +00:00
c = compat.Consumer(self.connection, queue=n, exchange=n,
2012-06-15 17:32:40 +00:00
routing_key='rkey')
2010-11-11 13:41:55 +00:00
x = c.__enter__()
assert x is c
2010-11-11 13:41:55 +00:00
x.__exit__()
assert c._closed
2010-11-11 13:41:55 +00:00
2012-06-15 17:32:40 +00:00
def test_revive(self, n='test_revive'):
2012-01-14 00:02:59 +00:00
c = compat.Consumer(self.connection, queue=n, exchange=n)
with self.connection.channel() as c2:
c.revive(c2)
assert c.backend is c2
2012-01-14 00:02:59 +00:00
2012-06-15 17:32:40 +00:00
def test__iter__(self, n='test__iter__'):
2012-01-14 00:02:59 +00:00
c = compat.Consumer(self.connection, queue=n, exchange=n)
c.iterqueue = Mock()
c.__iter__()
c.iterqueue.assert_called_with(infinite=True)
2012-06-15 17:32:40 +00:00
def test_iter(self, n='test_iterqueue'):
2010-11-11 13:41:55 +00:00
c = compat.Consumer(self.connection, queue=n, exchange=n,
2012-06-15 17:32:40 +00:00
routing_key='rkey')
2010-11-11 13:41:55 +00:00
c.close()
2012-06-15 17:32:40 +00:00
def test_process_next(self, n='test_process_next'):
2010-11-11 13:41:55 +00:00
c = compat.Consumer(self.connection, queue=n, exchange=n,
2012-06-15 17:32:40 +00:00
routing_key='rkey')
with pytest.raises(NotImplementedError):
2012-01-13 17:54:48 +00:00
c.process_next()
2010-11-11 13:41:55 +00:00
c.close()
2012-06-15 17:32:40 +00:00
def test_iterconsume(self, n='test_iterconsume'):
2010-11-11 13:41:55 +00:00
c = compat.Consumer(self.connection, queue=n, exchange=n,
2012-06-15 17:32:40 +00:00
routing_key='rkey')
2010-11-11 13:41:55 +00:00
c.close()
2012-06-15 17:32:40 +00:00
def test_discard_all(self, n='test_discard_all'):
2010-11-11 13:41:55 +00:00
c = compat.Consumer(self.connection, queue=n, exchange=n,
2012-06-15 17:32:40 +00:00
routing_key='rkey')
2010-11-11 13:41:55 +00:00
c.discard_all()
assert 'queue_purge' in c.backend
2010-11-11 13:41:55 +00:00
2012-06-15 17:32:40 +00:00
def test_fetch(self, n='test_fetch'):
2010-11-11 13:41:55 +00:00
c = compat.Consumer(self.connection, queue=n, exchange=n,
2012-06-15 17:32:40 +00:00
routing_key='rkey')
assert c.fetch() is None
assert c.fetch(no_ack=True) is None
assert 'basic_get' in c.backend
2010-11-11 13:41:55 +00:00
callback_called = [False]
def receive(payload, message):
callback_called[0] = True
2012-06-15 17:32:40 +00:00
c.backend.to_deliver.append('42')
2013-09-09 14:49:44 +00:00
payload = c.fetch().payload
assert payload == '42'
2012-06-15 17:32:40 +00:00
c.backend.to_deliver.append('46')
2010-11-11 13:41:55 +00:00
c.register_callback(receive)
assert c.fetch(enable_callbacks=True).payload == '46'
assert callback_called[0]
2010-11-11 13:41:55 +00:00
2012-06-15 17:32:40 +00:00
def test_discard_all_filterfunc_not_supported(self, n='xjf21j21'):
2010-11-11 13:41:55 +00:00
c = compat.Consumer(self.connection, queue=n, exchange=n,
2012-06-15 17:32:40 +00:00
routing_key='rkey')
with pytest.raises(NotImplementedError):
2012-01-13 17:54:48 +00:00
c.discard_all(filterfunc=lambda x: x)
2010-11-11 13:41:55 +00:00
c.close()
2012-06-15 17:32:40 +00:00
def test_wait(self, n='test_wait'):
2010-11-11 13:41:55 +00:00
class C(compat.Consumer):
def iterconsume(self, limit=None):
2020-07-13 13:58:06 +00:00
yield from range(limit)
2010-11-11 13:41:55 +00:00
2013-01-17 12:02:00 +00:00
c = C(self.connection,
queue=n, exchange=n, routing_key='rkey')
assert c.wait(10) == list(range(10))
2010-11-11 13:41:55 +00:00
c.close()
2012-06-15 17:32:40 +00:00
def test_iterqueue(self, n='test_iterqueue'):
2010-11-11 13:41:55 +00:00
i = [0]
class C(compat.Consumer):
def fetch(self, limit=None):
z = i[0]
i[0] += 1
return z
2013-01-17 12:02:00 +00:00
c = C(self.connection,
queue=n, exchange=n, routing_key='rkey')
assert list(c.iterqueue(limit=10)) == list(range(10))
2010-11-11 13:41:55 +00:00
c.close()
class test_ConsumerSet:
2010-11-11 13:41:55 +00:00
def setup(self):
2012-03-20 14:53:00 +00:00
self.connection = Connection(transport=Transport)
2010-11-11 13:41:55 +00:00
2013-09-12 12:10:42 +00:00
def test_providing_channel(self):
chan = Mock(name='channel')
cs = compat.ConsumerSet(self.connection, channel=chan)
assert cs._provided_channel
assert cs.backend is chan
2013-09-12 12:10:42 +00:00
cs.cancel = Mock(name='cancel')
cs.close()
2016-04-09 04:10:31 +00:00
chan.close.assert_not_called()
2013-09-12 12:10:42 +00:00
2012-06-15 17:32:40 +00:00
@patch('kombu.compat._iterconsume')
def test_iterconsume(self, _iterconsume, n='test_iterconsume'):
2012-01-14 00:02:59 +00:00
c = compat.Consumer(self.connection, queue=n, exchange=n)
cs = compat.ConsumerSet(self.connection, consumers=[c])
cs.iterconsume(limit=10, no_ack=True)
_iterconsume.assert_called_with(c.connection, cs, True, 10)
2012-06-15 17:32:40 +00:00
def test_revive(self, n='test_revive'):
2012-01-14 00:02:59 +00:00
c = compat.Consumer(self.connection, queue=n, exchange=n)
cs = compat.ConsumerSet(self.connection, consumers=[c])
with self.connection.channel() as c2:
cs.revive(c2)
assert cs.backend is c2
2012-01-14 00:02:59 +00:00
2012-06-15 17:32:40 +00:00
def test_constructor(self, prefix='0daf8h21'):
dcon = {'%s.xyx' % prefix: {'exchange': '%s.xyx' % prefix,
'routing_key': 'xyx'},
'%s.xyz' % prefix: {'exchange': '%s.xyz' % prefix,
'routing_key': 'xyz'}}
2010-11-11 13:41:55 +00:00
consumers = [compat.Consumer(self.connection, queue=prefix + str(i),
exchange=prefix + str(i))
2013-01-17 12:02:00 +00:00
for i in range(3)]
2010-11-11 13:41:55 +00:00
c = compat.ConsumerSet(self.connection, consumers=consumers)
c2 = compat.ConsumerSet(self.connection, from_dict=dcon)
assert len(c.queues) == 3
assert len(c2.queues) == 2
2010-11-11 13:41:55 +00:00
c.add_consumer(compat.Consumer(self.connection,
2012-06-15 17:32:40 +00:00
queue=prefix + 'xaxxxa',
exchange=prefix + 'xaxxxa'))
assert len(c.queues) == 4
2010-11-11 13:41:55 +00:00
for cq in c.queues:
assert cq.channel is c.channel
2010-11-11 13:41:55 +00:00
c2.add_consumer_from_dict(
'%s.xxx' % prefix,
exchange='%s.xxx' % prefix,
routing_key='xxx',
)
assert len(c2.queues) == 3
2010-11-11 13:41:55 +00:00
for c2q in c2.queues:
assert c2q.channel is c2.channel
2010-11-11 13:41:55 +00:00
c.discard_all()
assert c.channel.called.count('queue_purge') == 4
2010-11-11 13:41:55 +00:00
c.consume()
c.close()
c2.close()
assert 'basic_cancel' in c.channel
assert 'close' in c.channel
assert 'close' in c2.channel