Skip to content

Add support for LMPOP #1843

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import hashlib
import time
import warnings
from typing import List, Optional

from redis.exceptions import ConnectionError, DataError, NoScriptError, RedisError

Expand Down Expand Up @@ -1926,6 +1927,25 @@ def brpoplpush(self, src, dst, timeout=0):
timeout = 0
return self.execute_command("BRPOPLPUSH", src, dst, timeout)

def lmpop(
self,
num_keys: int,
*args: List[str],
direction: str = None,
count: Optional[int] = 1,
) -> List:
"""
Pop ``count`` values (default 1) first non-empty list key from the list
of args provided key names.

For more information check https://redis.io/commands/lmpop
"""
args = [num_keys] + list(args) + [direction]
if count != 1:
args.extend(["COUNT", count])

return self.execute_command("LMPOP", *args)

def lindex(self, name, index):
"""
Return the item from list ``name`` at position ``index``
Expand Down
16 changes: 16 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
REDIS_INFO = {}
default_redis_url = "redis://localhost:6379/9"
default_redismod_url = "redis://localhost:36379"
default_redis_unstable_url = "redis://localhost:6378"

# default ssl client ignores verification for the purpose of testing
default_redis_ssl_url = "rediss://localhost:6666"
Expand Down Expand Up @@ -54,6 +55,14 @@ def pytest_addoption(parser):
" defaults to `%(default)s`",
)

parser.addoption(
"--redis-unstable-url",
default=default_redis_unstable_url,
action="store",
help="Redis unstable (latest version) connection string "
"defaults to %(default)s`",
)


def _get_info(redis_url):
client = redis.Redis.from_url(redis_url)
Expand Down Expand Up @@ -357,6 +366,13 @@ def master_host(request):
yield parts.hostname, parts.port


@pytest.fixture()
def unstable_r(request):
url = request.config.getoption("--redis-unstable-url")
with _get_client(redis.Redis, request, from_url=url) as client:
yield client


def wait_for_command(client, monitor, command, key=None):
# issue a command with a key name that's local to this process.
# if we find a command with our key before the command we're waiting
Expand Down
11 changes: 11 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1473,6 +1473,17 @@ def test_brpoplpush_empty_string(self, r):
r.rpush("a", "")
assert r.brpoplpush("a", "b") == b""

@pytest.mark.onlynoncluster
# @skip_if_server_version_lt("7.0.0") turn on after redis 7 release
def test_lmpop(self, unstable_r):
unstable_r.rpush("foo", "1", "2", "3", "4", "5")
result = [b"foo", [b"1", b"2"]]
assert unstable_r.lmpop("2", "bar", "foo", direction="LEFT", count=2) == result
with pytest.raises(redis.ResponseError):
unstable_r.lmpop("2", "bar", "foo", direction="up", count=2)
unstable_r.rpush("bar", "a", "b", "c", "d")
assert unstable_r.lmpop("2", "bar", "foo", direction="LEFT") == [b"bar", [b"a"]]

def test_lindex(self, r):
r.rpush("a", "1", "2", "3")
assert r.lindex("a", "0") == b"1"
Expand Down