From 1a2245184852fe7899356ac35ac85ad7b2cf58de Mon Sep 17 00:00:00 2001 From: Ricardo Dias Date: Fri, 8 Nov 2024 14:50:48 +0000 Subject: [PATCH] Adds new module API test to test the scripting engine module API This commit adds a simple test to test the new module API that allows to implement new scripting engines as modules. Signed-off-by: Ricardo Dias --- src/modules/Makefile | 2 +- src/modules/helloscripting.c | 2 +- tests/modules/Makefile | 3 +- tests/modules/helloscripting.c | 1 + tests/unit/moduleapi/scriptingengine.tcl | 38 ++++++++++++++++++++++++ 5 files changed, 43 insertions(+), 3 deletions(-) create mode 120000 tests/modules/helloscripting.c create mode 100644 tests/unit/moduleapi/scriptingengine.tcl diff --git a/src/modules/Makefile b/src/modules/Makefile index 5053da0a9c..605573b986 100644 --- a/src/modules/Makefile +++ b/src/modules/Makefile @@ -23,7 +23,7 @@ endif all: helloworld.so hellotype.so helloblock.so hellocluster.so hellotimer.so hellodict.so hellohook.so helloacl.so helloscripting.so .c.xo: - $(CC) -I. $(CFLAGS) $(SHOBJ_CFLAGS) -fPIC -c $< -o $@ + $(CC) -I.. -I. $(CFLAGS) $(SHOBJ_CFLAGS) -fPIC -c $< -o $@ helloworld.xo: ../valkeymodule.h diff --git a/src/modules/helloscripting.c b/src/modules/helloscripting.c index 1b8c33b2dd..4de67b40e5 100644 --- a/src/modules/helloscripting.c +++ b/src/modules/helloscripting.c @@ -1,4 +1,4 @@ -#include "../valkeymodule.h" +#include "valkeymodule.h" #include #include diff --git a/tests/modules/Makefile b/tests/modules/Makefile index 1690b9b627..3ece6f9603 100644 --- a/tests/modules/Makefile +++ b/tests/modules/Makefile @@ -64,7 +64,8 @@ TEST_MODULES = \ moduleauthtwo.so \ rdbloadsave.so \ crash.so \ - cluster.so + cluster.so \ + helloscripting.so .PHONY: all diff --git a/tests/modules/helloscripting.c b/tests/modules/helloscripting.c new file mode 120000 index 0000000000..b35cd02ee6 --- /dev/null +++ b/tests/modules/helloscripting.c @@ -0,0 +1 @@ +../../src/modules/helloscripting.c \ No newline at end of file diff --git a/tests/unit/moduleapi/scriptingengine.tcl b/tests/unit/moduleapi/scriptingengine.tcl new file mode 100644 index 0000000000..97af751b0c --- /dev/null +++ b/tests/unit/moduleapi/scriptingengine.tcl @@ -0,0 +1,38 @@ +set testmodule [file normalize tests/modules/helloscripting.so] + +set HELLO_PROGRAM "#!hello name=mylib\nFUNCTION foo\nARGS 0\nRETURN\nFUNCTION bar\nCONSTI 432\nRETURN" + +start_server {tags {"modules"}} { + r module load $testmodule + + r function load $HELLO_PROGRAM + + test {Call scripting engine function: calling foo works} { + r fcall foo 0 134 + } {134} + + test {Call scripting engine function: calling bar works} { + r fcall bar 0 + } {432} + + test {Replace function library and call functions} { + set result [r function load replace "#!hello name=mylib\nFUNCTION foo\nARGS 0\nRETURN\nFUNCTION bar\nCONSTI 500\nRETURN"] + assert_equal $result "mylib" + + set result [r fcall foo 0 132] + assert_equal $result 132 + + set result [r fcall bar 0] + assert_equal $result 500 + } + + test {List scripting engine functions} { + r function load replace "#!hello name=mylib\nFUNCTION foobar\nARGS 0\nRETURN" + r function list + } {{library_name mylib engine HELLO functions {{name foobar description {} flags {}}}}} + + test {Unload scripting engine module} { + set result [r module unload helloengine] + assert_equal $result "OK" + } +}