2022-04-14 11:02:52 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-12-23 14:41:30 +00:00
|
|
|
from array import array
|
2020-07-23 13:33:40 +00:00
|
|
|
from queue import Empty
|
2020-08-15 20:51:02 +00:00
|
|
|
from unittest.mock import Mock
|
2016-04-11 12:56:58 +00:00
|
|
|
|
2021-07-20 13:07:49 +00:00
|
|
|
import pytest
|
|
|
|
|
2016-08-22 18:17:59 +00:00
|
|
|
from kombu.transport.consul import Channel, Transport
|
2016-04-11 12:56:58 +00:00
|
|
|
|
2020-08-16 20:20:19 +00:00
|
|
|
pytest.importorskip('consul')
|
|
|
|
|
2016-04-11 12:56:58 +00:00
|
|
|
|
2016-08-22 18:17:59 +00:00
|
|
|
class test_Consul:
|
2016-04-11 12:56:58 +00:00
|
|
|
|
2016-06-16 02:21:01 +00:00
|
|
|
def setup(self):
|
|
|
|
self.connection = Mock()
|
2021-12-23 14:41:30 +00:00
|
|
|
self.connection._used_channel_ids = array('H')
|
|
|
|
self.connection.channel_max = 65535
|
2016-06-16 02:21:01 +00:00
|
|
|
self.connection.client.transport_options = {}
|
|
|
|
self.connection.client.port = 303
|
2016-08-22 18:17:59 +00:00
|
|
|
self.consul = self.patching('consul.Consul').return_value
|
2016-06-16 02:21:01 +00:00
|
|
|
self.channel = Channel(connection=self.connection)
|
2016-04-11 12:56:58 +00:00
|
|
|
|
|
|
|
def test_driver_version(self):
|
2016-08-22 18:17:59 +00:00
|
|
|
assert Transport(self.connection.client).driver_version()
|
2016-04-11 12:56:58 +00:00
|
|
|
|
|
|
|
def test_failed_get(self):
|
2016-06-16 02:21:01 +00:00
|
|
|
self.channel._acquire_lock = Mock(return_value=False)
|
|
|
|
self.channel.client.kv.get.return_value = (1, None)
|
2016-08-22 18:17:59 +00:00
|
|
|
with pytest.raises(Empty):
|
2016-06-16 02:21:01 +00:00
|
|
|
self.channel._get('empty')()
|
2016-04-11 12:56:58 +00:00
|
|
|
|
|
|
|
def test_test_purge(self):
|
|
|
|
self.channel._destroy_session = Mock(return_value=True)
|
2016-06-16 02:21:01 +00:00
|
|
|
self.consul.kv.delete = Mock(return_value=True)
|
2016-08-22 18:17:59 +00:00
|
|
|
assert self.channel._purge('foo')
|
2016-04-11 12:56:58 +00:00
|
|
|
|
|
|
|
def test_variables(self):
|
2016-08-22 18:17:59 +00:00
|
|
|
assert self.channel.session_ttl == 30
|
|
|
|
assert self.channel.timeout == '10s'
|
2016-04-11 12:56:58 +00:00
|
|
|
|
|
|
|
def test_lock_key(self):
|
|
|
|
key = self.channel._lock_key('myqueue')
|
2016-08-22 18:17:59 +00:00
|
|
|
assert key == 'kombu/myqueue.lock'
|
2016-04-11 12:56:58 +00:00
|
|
|
|
|
|
|
def test_key_prefix(self):
|
|
|
|
key = self.channel._key_prefix('myqueue')
|
2016-08-22 18:17:59 +00:00
|
|
|
assert key == 'kombu/myqueue'
|
2016-04-11 12:56:58 +00:00
|
|
|
|
|
|
|
def test_get_or_create_session(self):
|
|
|
|
queue = 'myqueue'
|
|
|
|
session_id = '123456'
|
2016-06-16 02:21:01 +00:00
|
|
|
self.consul.session.create.return_value = session_id
|
2016-08-22 18:17:59 +00:00
|
|
|
assert self.channel._get_or_create_session(queue) == session_id
|
2016-04-11 12:56:58 +00:00
|
|
|
|
|
|
|
def test_create_delete_queue(self):
|
|
|
|
queue = 'mynewqueue'
|
|
|
|
|
2016-06-16 02:21:01 +00:00
|
|
|
self.consul.kv.put.return_value = True
|
2016-08-22 18:17:59 +00:00
|
|
|
assert self.channel._new_queue(queue)
|
2016-04-11 12:56:58 +00:00
|
|
|
|
2016-06-16 02:21:01 +00:00
|
|
|
self.consul.kv.delete.return_value = True
|
2016-04-11 12:56:58 +00:00
|
|
|
self.channel._destroy_session = Mock()
|
2016-06-16 02:21:01 +00:00
|
|
|
self.channel._delete(queue)
|
2016-04-11 12:56:58 +00:00
|
|
|
|
|
|
|
def test_size(self):
|
2016-06-16 02:21:01 +00:00
|
|
|
self.consul.kv.get.return_value = [(1, {}), (2, {})]
|
2016-08-22 18:17:59 +00:00
|
|
|
assert self.channel._size('q') == 2
|
2016-04-11 12:56:58 +00:00
|
|
|
|
|
|
|
def test_get(self):
|
|
|
|
self.channel._obtain_lock = Mock(return_value=True)
|
|
|
|
self.channel._release_lock = Mock(return_value=True)
|
|
|
|
|
2016-06-16 02:21:01 +00:00
|
|
|
self.consul.kv.get.return_value = [1, [
|
|
|
|
{'Key': 'myqueue', 'ModifyIndex': 1, 'Value': '1'},
|
|
|
|
]]
|
2016-04-11 12:56:58 +00:00
|
|
|
|
2016-06-16 02:21:01 +00:00
|
|
|
self.consul.kv.delete.return_value = True
|
2016-04-11 12:56:58 +00:00
|
|
|
|
2016-08-22 18:17:59 +00:00
|
|
|
assert self.channel._get('myqueue') is not None
|
2016-04-11 12:56:58 +00:00
|
|
|
|
|
|
|
def test_put(self):
|
2016-06-16 02:21:01 +00:00
|
|
|
self.consul.kv.put.return_value = True
|
2016-08-22 18:17:59 +00:00
|
|
|
assert self.channel._put('myqueue', 'mydata') is None
|