From e8c993f0fb0cfc29d953b710fd807ea8eb392b89 Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 25 Oct 2011 11:19:15 +0200 Subject: [PATCH] Fixes for the scripting refactoring and new commands. Tests for the new features. --- src/scripting.c | 17 +++++++++++------ tests/unit/scripting.tcl | 28 +++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/scripting.c b/src/scripting.c index 68f3c946..06862e84 100644 --- a/src/scripting.c +++ b/src/scripting.c @@ -500,7 +500,6 @@ void luaSetGlobalArray(lua_State *lua, char *var, robj **elev, int elec) { int luaCreateFunction(redisClient *c, lua_State *lua, char *funcname, robj *body) { sds funcdef = sdsempty(); - lua_pop(lua,1); /* remove the nil from the stack */ funcdef = sdscat(funcdef,"function "); funcdef = sdscatlen(funcdef,funcname,42); funcdef = sdscatlen(funcdef," ()\n",4); @@ -581,12 +580,12 @@ void evalGenericCommand(redisClient *c, int evalsha) { /* Try to lookup the Lua function */ lua_getglobal(lua, funcname); if (lua_isnil(lua,1)) { + lua_pop(lua,1); /* remove the nil from the stack */ /* Function not defined... let's define it if we have the * body of the funciton. If this is an EVALSHA call we can just * return an error. */ if (evalsha) { addReply(c, shared.noscripterr); - lua_pop(lua,1); /* remove the nil from the stack */ return; } if (luaCreateFunction(c,lua,funcname,c->argv[1]) == REDIS_ERR) return; @@ -724,15 +723,21 @@ void scriptCommand(redisClient *c) { addReply(c,shared.czero); } } else if (c->argc == 3 && !strcasecmp(c->argv[1]->ptr,"load")) { - /* We obtain the script SHA1, then check if this function is already - * defined into the Lua state */ char funcname[43]; + sds sha; funcname[0] = 'f'; funcname[1] = '_'; hashScript(funcname+2,c->argv[2]->ptr,sdslen(c->argv[2]->ptr)); - if (luaCreateFunction(c,server.lua,funcname,c->argv[2]) == REDIS_ERR) - return; + sha = sdsnewlen(funcname+2,40); + if (dictFind(server.lua_scripts,sha) == NULL) { + if (luaCreateFunction(c,server.lua,funcname,c->argv[2]) + == REDIS_ERR) { + sdsfree(sha); + return; + } + } + sdsfree(sha); addReply(c,shared.ok); } else { addReplyError(c, "Unknown SCRIPT subcommand or wrong # of args."); diff --git a/tests/unit/scripting.tcl b/tests/unit/scripting.tcl index 5bac5687..51fea5f4 100644 --- a/tests/unit/scripting.tcl +++ b/tests/unit/scripting.tcl @@ -47,8 +47,13 @@ start_server {tags {"scripting"}} { r evalsha 9bd632c7d33e571e9f24556ebed26c3479a87129 0 } {myval} + test {EVALSHA - Do we get an error on invalid SHA1?} { + catch {r evalsha NotValidShaSUM 0} e + set _ $e + } {NOSCRIPT*} + test {EVALSHA - Do we get an error on non defined SHA1?} { - catch {r evalsha ffffffffffffffffffffffffffffffffffffffff 0} e + catch {r evalsha ffd632c7d33e571e9f24556ebed26c3479a87130 0} e set _ $e } {NOSCRIPT*} @@ -162,6 +167,27 @@ start_server {tags {"scripting"}} { } e set e } {*against a key*} + + test {SCRIPTING FLUSH - is able to clear the scripts cache?} { + r set mykey myval + set v [r evalsha 9bd632c7d33e571e9f24556ebed26c3479a87129 0] + assert_equal $v myval + set e "" + r script flush + catch {r evalsha 9bd632c7d33e571e9f24556ebed26c3479a87129 0} e + set e + } {NOSCRIPT*} + + test {SCRIPT EXISTS - can detect already defined scripts?} { + r eval "return 1+1" 0 + r script exists a27e7e8a43702b7046d4f6a7ccf5b60cef6b9bd9 a27e7e8a43702b7046d4f6a7ccf5b60cef6b9bda + } {1 0} + + test {SCRIPT LOAD - is able to register scripts in the scripting cache} { + list \ + [r script load "return 'loaded'"] \ + [r evalsha b534286061d4b9e4026607613b95c06c06015ae8 0] + } {OK loaded} } start_server {tags {"scripting repl"}} { -- 2.45.2