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
48a9d63a
Unverified
Commit
48a9d63a
authored
Nov 11, 2020
by
Yossi Gottlieb
Committed by
GitHub
Nov 11, 2020
Browse files
Add timer module API tests. (#8041)
parent
d5059ba5
Changes
3
Hide whitespace changes
Inline
Side-by-side
tests/modules/Makefile
View file @
48a9d63a
...
@@ -25,7 +25,8 @@ TEST_MODULES = \
...
@@ -25,7 +25,8 @@ TEST_MODULES = \
auth.so
\
auth.so
\
keyspace_events.so
\
keyspace_events.so
\
blockedclient.so
\
blockedclient.so
\
getkeys.so
getkeys.so
\
timer.so
.PHONY
:
all
.PHONY
:
all
...
...
tests/modules/timer.c
0 → 100644
View file @
48a9d63a
#define REDISMODULE_EXPERIMENTAL_API
#include "redismodule.h"
static
void
timer_callback
(
RedisModuleCtx
*
ctx
,
void
*
data
)
{
RedisModuleString
*
keyname
=
data
;
RedisModuleCallReply
*
reply
;
reply
=
RedisModule_Call
(
ctx
,
"INCR"
,
"s"
,
keyname
);
if
(
reply
!=
NULL
)
RedisModule_FreeCallReply
(
reply
);
RedisModule_FreeString
(
ctx
,
keyname
);
}
int
test_createtimer
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
{
if
(
argc
!=
3
)
{
RedisModule_WrongArity
(
ctx
);
return
REDISMODULE_OK
;
}
long
long
period
;
if
(
RedisModule_StringToLongLong
(
argv
[
1
],
&
period
)
==
REDISMODULE_ERR
)
{
RedisModule_ReplyWithError
(
ctx
,
"Invalid time specified."
);
return
REDISMODULE_OK
;
}
RedisModuleString
*
keyname
=
argv
[
2
];
RedisModule_RetainString
(
ctx
,
keyname
);
RedisModuleTimerID
id
=
RedisModule_CreateTimer
(
ctx
,
period
,
timer_callback
,
keyname
);
RedisModule_ReplyWithLongLong
(
ctx
,
id
);
return
REDISMODULE_OK
;
}
int
test_gettimer
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
{
if
(
argc
!=
2
)
{
RedisModule_WrongArity
(
ctx
);
return
REDISMODULE_OK
;
}
long
long
id
;
if
(
RedisModule_StringToLongLong
(
argv
[
1
],
&
id
)
==
REDISMODULE_ERR
)
{
RedisModule_ReplyWithError
(
ctx
,
"Invalid id specified."
);
return
REDISMODULE_OK
;
}
uint64_t
remaining
;
RedisModuleString
*
keyname
;
if
(
RedisModule_GetTimerInfo
(
ctx
,
id
,
&
remaining
,
(
void
**
)
&
keyname
)
==
REDISMODULE_ERR
)
{
RedisModule_ReplyWithNull
(
ctx
);
}
else
{
RedisModule_ReplyWithArray
(
ctx
,
2
);
RedisModule_ReplyWithString
(
ctx
,
keyname
);
RedisModule_ReplyWithLongLong
(
ctx
,
remaining
);
}
return
REDISMODULE_OK
;
}
int
test_stoptimer
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
{
if
(
argc
!=
2
)
{
RedisModule_WrongArity
(
ctx
);
return
REDISMODULE_OK
;
}
long
long
id
;
if
(
RedisModule_StringToLongLong
(
argv
[
1
],
&
id
)
==
REDISMODULE_ERR
)
{
RedisModule_ReplyWithError
(
ctx
,
"Invalid id specified."
);
return
REDISMODULE_OK
;
}
int
ret
=
0
;
RedisModuleString
*
keyname
;
if
(
RedisModule_StopTimer
(
ctx
,
id
,
(
void
**
)
&
keyname
)
==
REDISMODULE_OK
)
{
RedisModule_FreeString
(
ctx
,
keyname
);
ret
=
1
;
}
RedisModule_ReplyWithLongLong
(
ctx
,
ret
);
return
REDISMODULE_OK
;
}
int
RedisModule_OnLoad
(
RedisModuleCtx
*
ctx
,
RedisModuleString
**
argv
,
int
argc
)
{
REDISMODULE_NOT_USED
(
argv
);
REDISMODULE_NOT_USED
(
argc
);
if
(
RedisModule_Init
(
ctx
,
"timer"
,
1
,
REDISMODULE_APIVER_1
)
==
REDISMODULE_ERR
)
return
REDISMODULE_ERR
;
if
(
RedisModule_CreateCommand
(
ctx
,
"test.createtimer"
,
test_createtimer
,
""
,
0
,
0
,
0
)
==
REDISMODULE_ERR
)
return
REDISMODULE_ERR
;
if
(
RedisModule_CreateCommand
(
ctx
,
"test.gettimer"
,
test_gettimer
,
""
,
0
,
0
,
0
)
==
REDISMODULE_ERR
)
return
REDISMODULE_ERR
;
if
(
RedisModule_CreateCommand
(
ctx
,
"test.stoptimer"
,
test_stoptimer
,
""
,
0
,
0
,
0
)
==
REDISMODULE_ERR
)
return
REDISMODULE_ERR
;
return
REDISMODULE_OK
;
}
tests/unit/moduleapi/timer.tcl
0 → 100644
View file @
48a9d63a
set testmodule
[
file normalize tests/modules/timer.so
]
start_server
{
tags
{
"modules"
}}
{
r module load $testmodule
test
{
RM_CreateTimer: a sequence of timers work
}
{
# We can't guarantee same-ms but we try using MULTI/EXEC
r multi
for
{
set i 0
}
{
$i
< 20
}
{
incr i
}
{
r test.createtimer 10 timer-incr-key
}
r exec
after 500
assert_equal 20
[
r get timer-incr-key
]
}
test
{
RM_GetTimer: basic sanity
}
{
# Getting non-existing timer
assert_equal
{}
[
r test.gettimer 0
]
# Getting a real timer
set id
[
r test.createtimer 10000 timer-incr-key
]
set info
[
r test.gettimer $id
]
assert_equal
"timer-incr-key"
[
lindex $info 0
]
set remaining
[
lindex $info 1
]
assert
{
$remaining
< 10000 && $remaining > 1
}
}
test
{
RM_StopTimer: basic sanity
}
{
r set
"timer-incr-key"
0
set id
[
r test.createtimer 1000 timer-incr-key
]
assert_equal 1
[
r test.stoptimer $id
]
# Wait to be sure timer doesn't execute
after 2000
assert_equal 0
[
r get timer-incr-key
]
# Stop non-existing timer
assert_equal 0
[
r test.stoptimer $id
]
}
test
{
Timer appears non-existing after it fires
}
{
r set
"timer-incr-key"
0
set id
[
r test.createtimer 10 timer-incr-key
]
# verify timer fired
after 500
assert_equal 1
[
r get timer-incr-key
]
# verify id does not exist
assert_equal
{}
[
r test.gettimer $id
]
}
}
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