kombu/t/unit/transport/test_etcd.py

68 lines
2.1 KiB
Python
Raw Normal View History

from __future__ import annotations
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, patch
import pytest
from kombu.transport.etcd import Channel, Transport
2020-08-16 20:20:19 +00:00
pytest.importorskip('etcd')
2016-09-02 22:35:13 +00:00
class test_Etcd:
def setup(self):
self.connection = Mock()
self.connection.client.transport_options = {}
self.connection.client.port = 2739
self.client = self.patch('etcd.Client').return_value
self.channel = Channel(connection=self.connection)
def test_driver_version(self):
2016-09-02 22:35:13 +00:00
assert Transport(self.connection.client).driver_version()
def test_failed_get(self):
self.channel._acquire_lock = Mock(return_value=False)
self.channel.client.read.side_effect = IndexError
2016-09-02 22:35:13 +00:00
with patch('etcd.Lock'):
with pytest.raises(Empty):
self.channel._get('empty')()
def test_test_purge(self):
2016-09-02 22:35:13 +00:00
with patch('etcd.Lock'):
self.client.delete = Mock(return_value=True)
2016-09-02 22:35:13 +00:00
assert self.channel._purge('foo')
def test_key_prefix(self):
key = self.channel._key_prefix('myqueue')
2016-09-02 22:35:13 +00:00
assert key == 'kombu/myqueue'
def test_create_delete_queue(self):
queue = 'mynewqueue'
2016-09-02 22:35:13 +00:00
with patch('etcd.Lock'):
self.client.write.return_value = self.patch('etcd.EtcdResult')
2016-09-02 22:35:13 +00:00
assert self.channel._new_queue(queue)
self.client.delete.return_value = self.patch('etcd.EtcdResult')
self.channel._delete(queue)
def test_size(self):
2016-09-02 22:35:13 +00:00
with patch('etcd.Lock'):
self.client.read.return_value = self.patch(
'etcd.EtcdResult', _children=[{}, {}])
2016-09-02 22:35:13 +00:00
assert self.channel._size('q') == 2
def test_get(self):
2016-09-02 22:35:13 +00:00
with patch('etcd.Lock'):
self.client.read.return_value = self.patch(
'etcd.EtcdResult',
_children=[{'key': 'myqueue', 'modifyIndex': 1, 'value': '1'}])
2016-09-02 22:35:13 +00:00
assert self.channel._get('myqueue') is not None
def test_put(self):
2016-09-02 22:35:13 +00:00
with patch('etcd.Lock'):
self.client.write.return_value = self.patch('etcd.EtcdResult')
2016-09-02 22:35:13 +00:00
assert self.channel._put('myqueue', 'mydata') is None