* Make Redis-Mutex test fail as it should (#701)

See https://github.com/celery/kombu/issues/701

* Fix issue #701 - decode lock_id returned from Redis

Thanks @zaro
This commit is contained in:
Shai Berger 2020-02-02 17:10:56 +02:00 committed by GitHub
parent 7cb4a0afe5
commit 16531285d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 5 deletions

View File

@ -124,7 +124,7 @@ def Mutex(client, name, expire):
try: try:
with client.pipeline(True) as pipe: with client.pipeline(True) as pipe:
pipe.watch(name) pipe.watch(name)
if pipe.get(name) == lock_id: if bytes_to_str(pipe.get(name)) == lock_id:
pipe.multi() pipe.multi()
pipe.delete(name) pipe.delete(name)
pipe.execute() pipe.execute()

View File

@ -14,6 +14,7 @@ from kombu.exceptions import InconsistencyError, VersionMismatch
from kombu.five import Empty, Queue as _Queue, bytes_if_py2 from kombu.five import Empty, Queue as _Queue, bytes_if_py2
from kombu.transport import virtual from kombu.transport import virtual
from kombu.utils import eventio # patch poll from kombu.utils import eventio # patch poll
from kombu.utils.encoding import str_to_bytes
from kombu.utils.json import dumps from kombu.utils.json import dumps
@ -1363,13 +1364,13 @@ class test_Mutex:
client.setnx.return_value = True client.setnx.return_value = True
client.pipeline = ContextMock() client.pipeline = ContextMock()
pipe = client.pipeline.return_value pipe = client.pipeline.return_value
pipe.get.return_value = lock_id pipe.get.return_value = str_to_bytes(lock_id) # redis gives bytes
held = False held = False
with redis.Mutex(client, 'foo1', 100): with redis.Mutex(client, 'foo1', 100):
held = True held = True
assert held assert held
client.setnx.assert_called_with('foo1', lock_id) client.setnx.assert_called_with('foo1', lock_id)
pipe.get.return_value = 'yyy' pipe.get.return_value = b'yyy'
held = False held = False
with redis.Mutex(client, 'foo1', 100): with redis.Mutex(client, 'foo1', 100):
held = True held = True
@ -1377,7 +1378,7 @@ class test_Mutex:
# Did not win # Did not win
client.expire.reset_mock() client.expire.reset_mock()
pipe.get.return_value = lock_id pipe.get.return_value = str_to_bytes(lock_id)
client.setnx.return_value = False client.setnx.return_value = False
with pytest.raises(redis.MutexHeld): with pytest.raises(redis.MutexHeld):
held = False held = False

View File

@ -28,7 +28,7 @@ deps=
flake8,flakeplus,pydocstyle: -r{toxinidir}/requirements/pkgutils.txt flake8,flakeplus,pydocstyle: -r{toxinidir}/requirements/pkgutils.txt
commands = pip install -U -r{toxinidir}/requirements/dev.txt commands = pip install -U -r{toxinidir}/requirements/dev.txt
pytest -rxs -xv --cov=kombu --cov-report=xml --no-cov-on-fail {posargs} python -bb -m pytest -rxs -xv --cov=kombu --cov-report=xml --no-cov-on-fail {posargs}
basepython = basepython =
2.7,flakeplus,flake8,linkcheck,cov: python2.7 2.7,flakeplus,flake8,linkcheck,cov: python2.7