Skip to content

Commit

Permalink
Add support for SCRIPT FLUSH subcommand
Browse files Browse the repository at this point in the history
This commit adds support for the SCRIPT FLUSH subcommand which flushes
all the loaded scripts from Redis. The subcommand takes no arguments and
returns OK after completing its work.
  • Loading branch information
SebastiaanZ committed Oct 4, 2020
1 parent 091f15d commit 6ca031c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
1 change: 0 additions & 1 deletion README.rst
Expand Up @@ -310,7 +310,6 @@ scripting
---------

* script debug
* script flush
* script kill


Expand Down
8 changes: 6 additions & 2 deletions fakeredis/_server.py
Expand Up @@ -2316,8 +2316,7 @@ def time(self):
return [str(now_s).encode(), str(now_us).encode()]

# Script commands
# TODO: script flush
# (script debug and script kill will probably not be supported)
# script debug and script kill will probably not be supported

def _convert_redis_arg(self, lua_runtime, value):
# Type checks are exact to avoid issues like bool being a subclass of int.
Expand Down Expand Up @@ -2477,6 +2476,11 @@ def script(self, subcmd, *args):
return sha1
elif casematch(subcmd, b'exists'):
return [int(sha1 in self._server.script_cache) for sha1 in args]
elif casematch(subcmd, b'flush'):
if len(args) != 0:
raise SimpleError(BAD_SUBCOMMAND_MSG.format('SCRIPT'))
self._server.script_cache = {}
return OK
else:
raise SimpleError(BAD_SUBCOMMAND_MSG.format('SCRIPT'))

Expand Down
20 changes: 20 additions & 0 deletions test/test_fakeredis.py
Expand Up @@ -4506,6 +4506,26 @@ def test_script_exists(r):
assert r.script_exists("a", sha1_one, "c", sha1_two, "e", "f") == [0, 1, 0, 1, 0, 0]


@pytest.mark.parametrize("args", [("a",), tuple("abcdefghijklmn")])
def test_script_flush_errors_with_args(r, args):
with pytest.raises(redis.ResponseError):
raw_command(r, "SCRIPT FLUSH %s" % " ".join(args))


def test_script_flush(r):
# generate/load six unique scripts and store their sha1 hash values
sha1_values = [r.script_load("return '%s'" % char) for char in "abcdef"]

# assert the scripts all exist prior to flushing
assert r.script_exists(*sha1_values) == [1] * len(sha1_values)

# flush and assert OK response
assert r.script_flush() is True

# assert none of the scripts exists after flushing
assert r.script_exists(*sha1_values) == [0] * len(sha1_values)


@fake_only
def test_lua_log(r, caplog):
logger = fakeredis._server.LOGGER
Expand Down

0 comments on commit 6ca031c

Please sign in to comment.