From 16531285d51a76171bb61faf681af31f8f80ddd8 Mon Sep 17 00:00:00 2001 From: Shai Berger Date: Sun, 2 Feb 2020 17:10:56 +0200 Subject: [PATCH] Issue 701 (#1141) * 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 --- kombu/transport/redis.py | 2 +- t/unit/transport/test_redis.py | 7 ++++--- tox.ini | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/kombu/transport/redis.py b/kombu/transport/redis.py index 78ecfedb..16a42fc4 100644 --- a/kombu/transport/redis.py +++ b/kombu/transport/redis.py @@ -124,7 +124,7 @@ def Mutex(client, name, expire): try: with client.pipeline(True) as pipe: pipe.watch(name) - if pipe.get(name) == lock_id: + if bytes_to_str(pipe.get(name)) == lock_id: pipe.multi() pipe.delete(name) pipe.execute() diff --git a/t/unit/transport/test_redis.py b/t/unit/transport/test_redis.py index 6d503ec2..572e8a9c 100644 --- a/t/unit/transport/test_redis.py +++ b/t/unit/transport/test_redis.py @@ -14,6 +14,7 @@ from kombu.exceptions import InconsistencyError, VersionMismatch from kombu.five import Empty, Queue as _Queue, bytes_if_py2 from kombu.transport import virtual from kombu.utils import eventio # patch poll +from kombu.utils.encoding import str_to_bytes from kombu.utils.json import dumps @@ -1363,13 +1364,13 @@ class test_Mutex: client.setnx.return_value = True client.pipeline = ContextMock() 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 with redis.Mutex(client, 'foo1', 100): held = True assert held client.setnx.assert_called_with('foo1', lock_id) - pipe.get.return_value = 'yyy' + pipe.get.return_value = b'yyy' held = False with redis.Mutex(client, 'foo1', 100): held = True @@ -1377,7 +1378,7 @@ class test_Mutex: # Did not win client.expire.reset_mock() - pipe.get.return_value = lock_id + pipe.get.return_value = str_to_bytes(lock_id) client.setnx.return_value = False with pytest.raises(redis.MutexHeld): held = False diff --git a/tox.ini b/tox.ini index 16b97ee9..4ac097ae 100644 --- a/tox.ini +++ b/tox.ini @@ -28,7 +28,7 @@ deps= flake8,flakeplus,pydocstyle: -r{toxinidir}/requirements/pkgutils.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 = 2.7,flakeplus,flake8,linkcheck,cov: python2.7