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
e7f17981
Commit
e7f17981
authored
Aug 03, 2016
by
antirez
Browse files
Modules: basic call/reply tests in test module.
parent
13f18d2b
Changes
1
Show whitespace changes
Inline
Side-by-side
src/modules/testmodule.c
View file @
e7f17981
...
@@ -31,9 +31,48 @@
...
@@ -31,9 +31,48 @@
*/
*/
#include "../redismodule.h"
#include "../redismodule.h"
#include <string.h>
/* --------------------------------- Helpers -------------------------------- */
/* Return true if the reply and the C null term string matches. */
int
TestMatchReply
(
RedisModuleCallReply
*
reply
,
char
*
str
)
{
RedisModuleString
*
mystr
;
mystr
=
RedisModule_CreateStringFromCallReply
(
reply
);
if
(
!
mystr
)
return
0
;
const
char
*
ptr
=
RedisModule_StringPtrLen
(
mystr
,
NULL
);
return
strcmp
(
ptr
,
str
)
==
0
;
}
/* ------------------------------- Test units ------------------------------- */
/* ------------------------------- Test units ------------------------------- */
/* TEST.CALL -- Test Call() API. */
int
TestCall
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
{
RedisModule_AutoMemory
(
ctx
);
RedisModuleCallReply
*
reply
;
RedisModule_Call
(
ctx
,
"DEL"
,
"c"
,
"mylist"
);
RedisModuleString
*
mystr
=
RedisModule_CreateString
(
ctx
,
"foo"
,
3
);
RedisModule_Call
(
ctx
,
"RPUSH"
,
"csl"
,
"mylist"
,
mystr
,(
long
long
)
1234
);
reply
=
RedisModule_Call
(
ctx
,
"LRANGE"
,
"ccc"
,
"mylist"
,
"0"
,
"-1"
);
long
long
items
=
RedisModule_CallReplyLength
(
reply
);
if
(
items
!=
2
)
goto
fail
;
RedisModuleCallReply
*
item0
,
*
item1
;
item0
=
RedisModule_CallReplyArrayElement
(
reply
,
0
);
item1
=
RedisModule_CallReplyArrayElement
(
reply
,
1
);
if
(
!
TestMatchReply
(
item0
,
"foo"
))
goto
fail
;
if
(
!
TestMatchReply
(
item1
,
"1234"
))
goto
fail
;
RedisModule_ReplyWithSimpleString
(
ctx
,
"OK"
);
return
REDISMODULE_OK
;
fail:
RedisModule_ReplyWithSimpleString
(
ctx
,
"ERR"
);
return
REDISMODULE_OK
;
}
/* TEST.STRING.APPEND -- Test appending to an existing string object. */
/* TEST.STRING.APPEND -- Test appending to an existing string object. */
int
TestStringAppend
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
{
int
TestStringAppend
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
{
RedisModuleString
*
s
=
RedisModule_CreateString
(
ctx
,
"foo"
,
3
);
RedisModuleString
*
s
=
RedisModule_CreateString
(
ctx
,
"foo"
,
3
);
...
@@ -79,6 +118,24 @@ int TestAssertStringReply(RedisModuleCtx *ctx, RedisModuleCallReply *reply, char
...
@@ -79,6 +118,24 @@ int TestAssertStringReply(RedisModuleCtx *ctx, RedisModuleCallReply *reply, char
return
1
;
return
1
;
}
}
/* Return 1 if the reply matches the specified integer, otherwise log errors
* in the server log and return 0. */
int
TestAssertIntegerReply
(
RedisModuleCtx
*
ctx
,
RedisModuleCallReply
*
reply
,
long
long
expected
)
{
if
(
RedisModule_CallReplyType
(
reply
)
!=
REDISMODULE_REPLY_INTEGER
)
{
RedisModule_Log
(
ctx
,
"warning"
,
"Unexpected reply type %d"
,
RedisModule_CallReplyType
(
reply
));
return
0
;
}
long
long
val
=
RedisModule_CallReplyInteger
(
reply
);
if
(
val
!=
expected
)
{
RedisModule_Log
(
ctx
,
"warning"
,
"Unexpected integer reply '%lld' (instead of '%lld')"
,
val
,
expected
);
return
0
;
}
return
1
;
}
#define T(name,...) \
#define T(name,...) \
do { \
do { \
RedisModule_Log(ctx,"warning","Testing %s", name); \
RedisModule_Log(ctx,"warning","Testing %s", name); \
...
@@ -90,6 +147,16 @@ int TestIt(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
...
@@ -90,6 +147,16 @@ int TestIt(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
RedisModule_AutoMemory
(
ctx
);
RedisModule_AutoMemory
(
ctx
);
RedisModuleCallReply
*
reply
;
RedisModuleCallReply
*
reply
;
/* Make sure the DB is empty before to proceed. */
T
(
"dbsize"
,
""
);
if
(
!
TestAssertIntegerReply
(
ctx
,
reply
,
0
))
goto
fail
;
T
(
"ping"
,
""
);
if
(
!
TestAssertStringReply
(
ctx
,
reply
,
"PONG"
,
4
))
goto
fail
;
T
(
"test.call"
,
""
);
if
(
!
TestAssertStringReply
(
ctx
,
reply
,
"OK"
,
2
))
goto
fail
;
T
(
"test.string.append"
,
""
);
T
(
"test.string.append"
,
""
);
if
(
!
TestAssertStringReply
(
ctx
,
reply
,
"foobar"
,
6
))
goto
fail
;
if
(
!
TestAssertStringReply
(
ctx
,
reply
,
"foobar"
,
6
))
goto
fail
;
...
@@ -109,6 +176,10 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
...
@@ -109,6 +176,10 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
if
(
RedisModule_Init
(
ctx
,
"test"
,
1
,
REDISMODULE_APIVER_1
)
if
(
RedisModule_Init
(
ctx
,
"test"
,
1
,
REDISMODULE_APIVER_1
)
==
REDISMODULE_ERR
)
return
REDISMODULE_ERR
;
==
REDISMODULE_ERR
)
return
REDISMODULE_ERR
;
if
(
RedisModule_CreateCommand
(
ctx
,
"test.call"
,
TestCall
,
"write deny-oom"
,
1
,
1
,
1
)
==
REDISMODULE_ERR
)
return
REDISMODULE_ERR
;
if
(
RedisModule_CreateCommand
(
ctx
,
"test.string.append"
,
if
(
RedisModule_CreateCommand
(
ctx
,
"test.string.append"
,
TestStringAppend
,
"write deny-oom"
,
1
,
1
,
1
)
==
REDISMODULE_ERR
)
TestStringAppend
,
"write deny-oom"
,
1
,
1
,
1
)
==
REDISMODULE_ERR
)
return
REDISMODULE_ERR
;
return
REDISMODULE_ERR
;
...
...
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