Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
3adf10b8
Commit
3adf10b8
authored
Nov 04, 2019
by
Oran Agra
Browse files
Test coverage for new module APIs: dbsize, flushall, randomkey, lru get/set
parent
720d1fd3
Changes
2
Show whitespace changes
Inline
Side-by-side
tests/modules/misc.c
View file @
3adf10b8
...
...
@@ -40,6 +40,65 @@ int test_call_info(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
return
REDISMODULE_OK
;
}
int
test_flushall
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
{
REDISMODULE_NOT_USED
(
argv
);
REDISMODULE_NOT_USED
(
argc
);
RedisModule_ResetDataset
(
1
,
0
);
RedisModule_ReplyWithCString
(
ctx
,
"Ok"
);
return
REDISMODULE_OK
;
}
int
test_dbsize
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
{
REDISMODULE_NOT_USED
(
argv
);
REDISMODULE_NOT_USED
(
argc
);
long
long
ll
=
RedisModule_DbSize
(
ctx
);
RedisModule_ReplyWithLongLong
(
ctx
,
ll
);
return
REDISMODULE_OK
;
}
int
test_randomkey
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
{
REDISMODULE_NOT_USED
(
argv
);
REDISMODULE_NOT_USED
(
argc
);
RedisModuleString
*
str
=
RedisModule_RandomKey
(
ctx
);
RedisModule_ReplyWithString
(
ctx
,
str
);
RedisModule_FreeString
(
ctx
,
str
);
return
REDISMODULE_OK
;
}
int
test_getlru
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
{
if
(
argc
<
2
)
{
RedisModule_WrongArity
(
ctx
);
return
REDISMODULE_OK
;
}
RedisModuleString
*
keyname
=
argv
[
1
];
RedisModuleKey
*
key
=
RedisModule_OpenKey
(
ctx
,
keyname
,
REDISMODULE_READ
|
REDISMODULE_OPEN_KEY_NOTOUCH
);
long
long
lru
,
lfu
;
RedisModule_GetLRUOrLFU
(
key
,
&
lfu
,
&
lru
);
RedisModule_ReplyWithLongLong
(
ctx
,
lru
);
RedisModule_CloseKey
(
key
);
return
REDISMODULE_OK
;
}
int
test_setlru
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
{
if
(
argc
<
3
)
{
RedisModule_WrongArity
(
ctx
);
return
REDISMODULE_OK
;
}
RedisModuleString
*
keyname
=
argv
[
1
];
RedisModuleKey
*
key
=
RedisModule_OpenKey
(
ctx
,
keyname
,
REDISMODULE_WRITE
|
REDISMODULE_OPEN_KEY_NOTOUCH
);
long
long
lru
;
RedisModule_StringToLongLong
(
argv
[
2
],
&
lru
);
RedisModule_SetLRUOrLFU
(
key
,
-
1
,
lru
);
RedisModule_ReplyWithCString
(
ctx
,
"Ok"
);
RedisModule_CloseKey
(
key
);
return
REDISMODULE_OK
;
}
int
RedisModule_OnLoad
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
{
REDISMODULE_NOT_USED
(
argv
);
REDISMODULE_NOT_USED
(
argc
);
...
...
@@ -50,6 +109,16 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
return
REDISMODULE_ERR
;
if
(
RedisModule_CreateCommand
(
ctx
,
"test.call_info"
,
test_call_info
,
""
,
0
,
0
,
0
)
==
REDISMODULE_ERR
)
return
REDISMODULE_ERR
;
if
(
RedisModule_CreateCommand
(
ctx
,
"test.flushall"
,
test_flushall
,
""
,
0
,
0
,
0
)
==
REDISMODULE_ERR
)
return
REDISMODULE_ERR
;
if
(
RedisModule_CreateCommand
(
ctx
,
"test.dbsize"
,
test_dbsize
,
""
,
0
,
0
,
0
)
==
REDISMODULE_ERR
)
return
REDISMODULE_ERR
;
if
(
RedisModule_CreateCommand
(
ctx
,
"test.randomkey"
,
test_randomkey
,
""
,
0
,
0
,
0
)
==
REDISMODULE_ERR
)
return
REDISMODULE_ERR
;
if
(
RedisModule_CreateCommand
(
ctx
,
"test.setlru"
,
test_setlru
,
""
,
0
,
0
,
0
)
==
REDISMODULE_ERR
)
return
REDISMODULE_ERR
;
if
(
RedisModule_CreateCommand
(
ctx
,
"test.getlru"
,
test_getlru
,
""
,
0
,
0
,
0
)
==
REDISMODULE_ERR
)
return
REDISMODULE_ERR
;
return
REDISMODULE_OK
;
}
tests/unit/moduleapi/misc.tcl
View file @
3adf10b8
...
...
@@ -16,4 +16,23 @@ start_server {tags {"modules"}} {
assert
{
[
string match
"*cmdstat_module*"
$info
]
}
}
test
{
test module db commands
}
{
r set x foo
set key
[
r test.randomkey
]
assert_equal $key
"x"
assert_equal
[
r test.dbsize
]
1
r test.flushall
assert_equal
[
r test.dbsize
]
0
}
test
{
test modle lru api
}
{
r set x foo
set lru
[
r test.getlru x
]
assert
{
$lru <= 1
}
r test.setlru x 100
set idle
[
r object idletime x
]
assert
{
$idle >= 100
}
set lru
[
r test.getlru x
]
assert
{
$lru >= 100
}
}
}
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