Skip to content

Commit

Permalink
Add eval test cases to scriptingengine.tcl
Browse files Browse the repository at this point in the history
Signed-off-by: Ricardo Dias <[email protected]>
  • Loading branch information
rjd15372 committed Jan 29, 2025
1 parent 2ca6a21 commit 525cc79
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,10 @@ void freeEvalScripts(dict *scripts, list *scripts_lru_list, list *engine_callbac
listNode *node = NULL;
while ((node = listNext(iter)) != NULL) {
callableLazyEvalReset *callback = listNodeValue(node);
callback->engineLazyEvalResetCallback(callback->context);
zfree(callback);
if (callback != NULL) {
callback->engineLazyEvalResetCallback(callback->context);
zfree(callback);
}
}
listReleaseIterator(iter);
listRelease(engine_callbacks);
Expand Down
2 changes: 1 addition & 1 deletion src/valkeymodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ typedef struct ValkeyModuleScriptingEngineCallableLazyEvalReset {

/*
* Callback function used for resetting the EVAL context implemented by an
* an engine. This callback will be called by a background thread when it's
* engine. This callback will be called by a background thread when it's
* ready for resetting the context.
*
* - `context`: a generic pointer to a context object, stored in the
Expand Down
11 changes: 10 additions & 1 deletion tests/modules/helloscripting.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,15 @@ callHelloLangFunction(ValkeyModuleCtx *module_ctx,
ValkeyModule_ReplyWithLongLong(module_ctx, result);
}

static ValkeyModuleScriptingEngineCallableLazyEvalReset *helloResetEvalEnv(ValkeyModuleCtx *module_ctx,
ValkeyModuleScriptingEngineCtx *engine_ctx,
int async) {
VALKEYMODULE_NOT_USED(module_ctx);
VALKEYMODULE_NOT_USED(engine_ctx);
VALKEYMODULE_NOT_USED(async);
return NULL;
}

int ValkeyModule_OnLoad(ValkeyModuleCtx *ctx,
ValkeyModuleString **argv,
int argc) {
Expand All @@ -449,7 +458,7 @@ int ValkeyModule_OnLoad(ValkeyModuleCtx *ctx,
.free_function = engineFreeFunction,
.call_function = callHelloLangFunction,
.get_function_memory_overhead = engineFunctionMemoryOverhead,
.reset_eval_env = NULL,
.reset_eval_env = helloResetEvalEnv,
.get_memory_info = engineGetMemoryInfo,
};

Expand Down
41 changes: 38 additions & 3 deletions tests/unit/moduleapi/scriptingengine.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,54 @@ start_server {tags {"modules"}} {
test {Test function kill} {
set rd [valkey_deferring_client]
r config set busy-reply-threshold 10
r function load REPLACE "#!hello name=mylib\nFUNCTION sleep\nARGS 0\nSLEEP\nARGS 0\nRETURN"
$rd fcall sleep 0 100
r function load REPLACE "#!hello name=mylib\nFUNCTION wait\nARGS 0\nSLEEP\nARGS 0\nRETURN"
$rd fcall wait 0 100
after 1000
catch {r ping} e
assert_match {BUSY*} $e
assert_match {running_script {name sleep command {fcall sleep 0 100} duration_ms *} engines {*}} [r FUNCTION STATS]
assert_match {running_script {name wait command {fcall wait 0 100} duration_ms *} engines {*}} [r FUNCTION STATS]
r function kill
after 1000 ;
assert_equal [r ping] "PONG"
assert_error {ERR Script killed by user with FUNCTION KILL*} {$rd read}
$rd close
}

test {Test eval execution} {
set result [r eval "#!hello\nFUNCTION foo\nARGS 0\nRETURN" 0 145]
assert_equal $result 145
}

test {Test evalsha execution} {
set sha [r script load "#!hello\nFUNCTION foo\nARGS 0\nRETURN"]
set result [r evalsha $sha 0 167]
assert_equal $result 167
}

test {Test script exists} {
set sha [r script load "#!hello\nFUNCTION foo\nARGS 0\nRETURN"]
set result [r script exists $sha]
assert_equal $result 1
}

test {Test script flush sync} {
set sha [r script load "#!hello\nFUNCTION foo\nARGS 0\nRETURN"]
set result [r script exists $sha]
assert_equal $result 1
r script flush SYNC
set result [r script exists $sha]
assert_equal $result 0
}

test {Test script flush async} {
set sha [r script load "#!hello\nFUNCTION foo\nARGS 0\nRETURN"]
set result [r script exists $sha]
assert_equal $result 1
r script flush ASYNC
set result [r script exists $sha]
assert_equal $result 0
}

test {Unload scripting engine module} {
set result [r module unload helloengine]
assert_equal $result "OK"
Expand Down

0 comments on commit 525cc79

Please sign in to comment.