Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
d044e33c
Unverified
Commit
d044e33c
authored
Apr 11, 2019
by
Salvatore Sanfilippo
Committed by
GitHub
Apr 11, 2019
Browse files
Merge pull request #5961 from yossigo/modules-tests
Modules tests
parents
9e67691f
ec0b6bd2
Changes
7
Show whitespace changes
Inline
Side-by-side
runtest-moduleapi
0 → 100755
View file @
d044e33c
#!/bin/sh
TCL_VERSIONS
=
"8.5 8.6"
TCLSH
=
""
for
VERSION
in
$TCL_VERSIONS
;
do
TCL
=
`
which tclsh
$VERSION
2>/dev/null
`
&&
TCLSH
=
$TCL
done
if
[
-z
$TCLSH
]
then
echo
"You need tcl 8.5 or newer in order to run the Redis test"
exit
1
fi
make
-C
tests/modules
&&
\
$TCLSH
tests/test_helper.tcl
--single
unit/moduleapi/commandfilter
"
${
@
}
"
src/module.c
View file @
d044e33c
...
@@ -753,6 +753,7 @@ void RM_SetModuleAttribs(RedisModuleCtx *ctx, const char *name, int ver, int api
...
@@ -753,6 +753,7 @@ void RM_SetModuleAttribs(RedisModuleCtx *ctx, const char *name, int ver, int api
module
->
usedby
=
listCreate
();
module
->
usedby
=
listCreate
();
module
->
using
=
listCreate
();
module
->
using
=
listCreate
();
module
->
filters
=
listCreate
();
module
->
filters
=
listCreate
();
module
->
in_call
=
0
;
ctx
->
module
=
module
;
ctx
->
module
=
module
;
}
}
...
...
src/modules/Makefile
View file @
d044e33c
...
@@ -13,7 +13,7 @@ endif
...
@@ -13,7 +13,7 @@ endif
.SUFFIXES
:
.c .so .xo .o
.SUFFIXES
:
.c .so .xo .o
all
:
helloworld.so hellotype.so helloblock.so testmodule.so hellocluster.so hellotimer.so hellodict.so
hellofilter.so
all
:
helloworld.so hellotype.so helloblock.so testmodule.so hellocluster.so hellotimer.so hellodict.so
.c.xo
:
.c.xo
:
$(CC)
-I
.
$(CFLAGS)
$(SHOBJ_CFLAGS)
-fPIC
-c
$<
-o
$@
$(CC)
-I
.
$(CFLAGS)
$(SHOBJ_CFLAGS)
-fPIC
-c
$<
-o
$@
...
@@ -47,11 +47,6 @@ hellodict.xo: ../redismodule.h
...
@@ -47,11 +47,6 @@ hellodict.xo: ../redismodule.h
hellodict.so
:
hellodict.xo
hellodict.so
:
hellodict.xo
hellofilter.xo
:
../redismodule.h
hellofilter.so
:
hellofilter.xo
$(LD)
-o
$@
$<
$(SHOBJ_LDFLAGS)
$(LIBS)
-lc
testmodule.xo
:
../redismodule.h
testmodule.xo
:
../redismodule.h
testmodule.so
:
testmodule.xo
testmodule.so
:
testmodule.xo
...
...
tests/modules/Makefile
0 → 100644
View file @
d044e33c
# find the OS
uname_S
:=
$(
shell
sh
-c
'uname -s 2>/dev/null || echo not'
)
# Compile flags for linux / osx
ifeq
($(uname_S),Linux)
SHOBJ_CFLAGS
?=
-W
-Wall
-fno-common
-g
-ggdb
-std
=
c99
-O2
SHOBJ_LDFLAGS
?=
-shared
else
SHOBJ_CFLAGS
?=
-W
-Wall
-dynamic
-fno-common
-g
-ggdb
-std
=
c99
-O2
SHOBJ_LDFLAGS
?=
-bundle
-undefined
dynamic_lookup
endif
.SUFFIXES
:
.c .so .xo .o
all
:
commandfilter.so
.c.xo
:
$(CC)
-I
../../src
$(CFLAGS)
$(SHOBJ_CFLAGS)
-fPIC
-c
$<
-o
$@
commandfilter.xo
:
../../src/redismodule.h
commandfilter.so
:
commandfilter.xo
$(LD)
-o
$@
$<
$(SHOBJ_LDFLAGS)
$(LIBS)
-lc
src
/modules/
hello
filter.c
→
tests
/modules/
command
filter.c
View file @
d044e33c
#define REDISMODULE_EXPERIMENTAL_API
#define REDISMODULE_EXPERIMENTAL_API
#include "
../
redismodule.h"
#include "redismodule.h"
#include <string.h>
#include <string.h>
static
RedisModuleString
*
log_key_name
;
static
RedisModuleString
*
log_key_name
;
static
const
char
log_command_name
[]
=
"
hello
filter.log"
;
static
const
char
log_command_name
[]
=
"
command
filter.log"
;
static
const
char
ping_command_name
[]
=
"
hello
filter.ping"
;
static
const
char
ping_command_name
[]
=
"
command
filter.ping"
;
static
const
char
unregister_command_name
[]
=
"
hello
filter.unregister"
;
static
const
char
unregister_command_name
[]
=
"
command
filter.unregister"
;
static
int
in_log_command
=
0
;
static
int
in_log_command
=
0
;
static
RedisModuleCommandFilter
*
filter
=
NULL
;
static
RedisModuleCommandFilter
*
filter
=
NULL
;
int
Hello
Filter_UnregisterCommand
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
int
Command
Filter_UnregisterCommand
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
{
{
(
void
)
argc
;
(
void
)
argc
;
(
void
)
argv
;
(
void
)
argv
;
...
@@ -23,7 +23,7 @@ int HelloFilter_UnregisterCommand(RedisModuleCtx *ctx, RedisModuleString **argv,
...
@@ -23,7 +23,7 @@ int HelloFilter_UnregisterCommand(RedisModuleCtx *ctx, RedisModuleString **argv,
return
REDISMODULE_OK
;
return
REDISMODULE_OK
;
}
}
int
Hello
Filter_PingCommand
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
int
Command
Filter_PingCommand
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
{
{
(
void
)
argc
;
(
void
)
argc
;
(
void
)
argv
;
(
void
)
argv
;
...
@@ -39,7 +39,7 @@ int HelloFilter_PingCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int a
...
@@ -39,7 +39,7 @@ int HelloFilter_PingCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int a
return
REDISMODULE_OK
;
return
REDISMODULE_OK
;
}
}
int
Hello
Filter_LogCommand
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
int
Command
Filter_LogCommand
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
{
{
RedisModuleString
*
s
=
RedisModule_CreateString
(
ctx
,
""
,
0
);
RedisModuleString
*
s
=
RedisModule_CreateString
(
ctx
,
""
,
0
);
...
@@ -74,9 +74,9 @@ int HelloFilter_LogCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int ar
...
@@ -74,9 +74,9 @@ int HelloFilter_LogCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int ar
return
REDISMODULE_OK
;
return
REDISMODULE_OK
;
}
}
void
Hello
Filter_CommandFilter
(
RedisModuleCommandFilterCtx
*
filter
)
void
Command
Filter_CommandFilter
(
RedisModuleCommandFilterCtx
*
filter
)
{
{
if
(
in_log_command
)
return
;
/* don't process our own RM_Call() from
Hello
Filter_LogCommand() */
if
(
in_log_command
)
return
;
/* don't process our own RM_Call() from
Command
Filter_LogCommand() */
/* Fun manipulations:
/* Fun manipulations:
* - Remove @delme
* - Remove @delme
...
@@ -117,7 +117,7 @@ void HelloFilter_CommandFilter(RedisModuleCommandFilterCtx *filter)
...
@@ -117,7 +117,7 @@ void HelloFilter_CommandFilter(RedisModuleCommandFilterCtx *filter)
}
}
int
RedisModule_OnLoad
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
{
int
RedisModule_OnLoad
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
{
if
(
RedisModule_Init
(
ctx
,
"
hello
filter"
,
1
,
REDISMODULE_APIVER_1
)
if
(
RedisModule_Init
(
ctx
,
"
command
filter"
,
1
,
REDISMODULE_APIVER_1
)
==
REDISMODULE_ERR
)
return
REDISMODULE_ERR
;
==
REDISMODULE_ERR
)
return
REDISMODULE_ERR
;
if
(
argc
!=
2
)
{
if
(
argc
!=
2
)
{
...
@@ -130,18 +130,18 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
...
@@ -130,18 +130,18 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
RedisModule_StringToLongLong
(
argv
[
1
],
&
noself
);
RedisModule_StringToLongLong
(
argv
[
1
],
&
noself
);
if
(
RedisModule_CreateCommand
(
ctx
,
log_command_name
,
if
(
RedisModule_CreateCommand
(
ctx
,
log_command_name
,
Hello
Filter_LogCommand
,
"write deny-oom"
,
1
,
1
,
1
)
==
REDISMODULE_ERR
)
Command
Filter_LogCommand
,
"write deny-oom"
,
1
,
1
,
1
)
==
REDISMODULE_ERR
)
return
REDISMODULE_ERR
;
return
REDISMODULE_ERR
;
if
(
RedisModule_CreateCommand
(
ctx
,
ping_command_name
,
if
(
RedisModule_CreateCommand
(
ctx
,
ping_command_name
,
Hello
Filter_PingCommand
,
"deny-oom"
,
1
,
1
,
1
)
==
REDISMODULE_ERR
)
Command
Filter_PingCommand
,
"deny-oom"
,
1
,
1
,
1
)
==
REDISMODULE_ERR
)
return
REDISMODULE_ERR
;
return
REDISMODULE_ERR
;
if
(
RedisModule_CreateCommand
(
ctx
,
unregister_command_name
,
if
(
RedisModule_CreateCommand
(
ctx
,
unregister_command_name
,
Hello
Filter_UnregisterCommand
,
"write deny-oom"
,
1
,
1
,
1
)
==
REDISMODULE_ERR
)
Command
Filter_UnregisterCommand
,
"write deny-oom"
,
1
,
1
,
1
)
==
REDISMODULE_ERR
)
return
REDISMODULE_ERR
;
return
REDISMODULE_ERR
;
if
((
filter
=
RedisModule_RegisterCommandFilter
(
ctx
,
Hello
Filter_CommandFilter
,
if
((
filter
=
RedisModule_RegisterCommandFilter
(
ctx
,
Command
Filter_CommandFilter
,
noself
?
REDISMODULE_CMDFILTER_NOSELF
:
0
))
noself
?
REDISMODULE_CMDFILTER_NOSELF
:
0
))
==
NULL
)
return
REDISMODULE_ERR
;
==
NULL
)
return
REDISMODULE_ERR
;
...
...
tests/test_helper.tcl
View file @
d044e33c
...
@@ -63,7 +63,6 @@ set ::all_tests {
...
@@ -63,7 +63,6 @@ set ::all_tests {
unit/lazyfree
unit/lazyfree
unit/wait
unit/wait
unit/pendingquerybuf
unit/pendingquerybuf
modules/commandfilter
}
}
# Index to the next test to run in the ::all_tests list.
# Index to the next test to run in the ::all_tests list.
set ::next_test 0
set ::next_test 0
...
...
tests/module
s
/commandfilter.tcl
→
tests/
unit/
module
api
/commandfilter.tcl
View file @
d044e33c
set testmodule
[
file normalize
src
/modules/
hello
filter.so
]
set testmodule
[
file normalize
tests
/modules/
command
filter.so
]
start_server
{
tags
{
"modules"
}}
{
start_server
{
tags
{
"modules"
}}
{
r module load $testmodule log-key 0
r module load $testmodule log-key 0
...
@@ -27,7 +27,7 @@ start_server {tags {"modules"}} {
...
@@ -27,7 +27,7 @@ start_server {tags {"modules"}} {
test
{
Command Filter applies on RM_Call
()
commands
}
{
test
{
Command Filter applies on RM_Call
()
commands
}
{
r del log-key
r del log-key
r
hello
filter.ping
r
command
filter.ping
r lrange log-key 0 -1
r lrange log-key 0 -1
}
"{ping @log}"
}
"{ping @log}"
...
@@ -39,13 +39,13 @@ start_server {tags {"modules"}} {
...
@@ -39,13 +39,13 @@ start_server {tags {"modules"}} {
test
{
Command Filter applies on Lua redis.call
()
that calls a module
}
{
test
{
Command Filter applies on Lua redis.call
()
that calls a module
}
{
r del log-key
r del log-key
r eval
"redis.call('
hello
filter.ping')"
0
r eval
"redis.call('
command
filter.ping')"
0
r lrange log-key 0 -1
r lrange log-key 0 -1
}
"{ping @log}"
}
"{ping @log}"
test
{
Command Filter is unregistered implicitly on module unload
}
{
test
{
Command Filter is unregistered implicitly on module unload
}
{
r del log-key
r del log-key
r module unload
hello
filter
r module unload
command
filter
r set mykey @log
r set mykey @log
r lrange log-key 0 -1
r lrange log-key 0 -1
}
{}
}
{}
...
@@ -59,14 +59,14 @@ start_server {tags {"modules"}} {
...
@@ -59,14 +59,14 @@ start_server {tags {"modules"}} {
assert_equal
"{set mykey @log}"
[
r lrange log-key 0 -1
]
assert_equal
"{set mykey @log}"
[
r lrange log-key 0 -1
]
# Unregister
# Unregister
r
hello
filter.unregister
r
command
filter.unregister
r del log-key
r del log-key
r set mykey @log
r set mykey @log
r lrange log-key 0 -1
r lrange log-key 0 -1
}
{}
}
{}
r module unload
hello
filter
r module unload
command
filter
r module load $testmodule log-key 1
r module load $testmodule log-key 1
test
{
Command Filter REDISMODULE_CMDFILTER_NOSELF works as expected
}
{
test
{
Command Filter REDISMODULE_CMDFILTER_NOSELF works as expected
}
{
...
@@ -74,10 +74,10 @@ start_server {tags {"modules"}} {
...
@@ -74,10 +74,10 @@ start_server {tags {"modules"}} {
assert_equal
"{set mykey @log}"
[
r lrange log-key 0 -1
]
assert_equal
"{set mykey @log}"
[
r lrange log-key 0 -1
]
r del log-key
r del log-key
r
hello
filter.ping
r
command
filter.ping
assert_equal
{}
[
r lrange log-key 0 -1
]
assert_equal
{}
[
r lrange log-key 0 -1
]
r eval
"redis.call('
hello
filter.ping')"
0
r eval
"redis.call('
command
filter.ping')"
0
assert_equal
{}
[
r lrange log-key 0 -1
]
assert_equal
{}
[
r lrange log-key 0 -1
]
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment