summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/modules/infotest.c73
-rw-r--r--tests/unit/moduleapi/infotest.tcl26
2 files changed, 99 insertions, 0 deletions
diff --git a/tests/modules/infotest.c b/tests/modules/infotest.c
index d28410932..4cb77ee87 100644
--- a/tests/modules/infotest.c
+++ b/tests/modules/infotest.c
@@ -5,6 +5,7 @@
void InfoFunc(RedisModuleInfoCtx *ctx, int for_crash_report) {
RedisModule_InfoAddSection(ctx, "");
RedisModule_InfoAddFieldLongLong(ctx, "global", -2);
+ RedisModule_InfoAddFieldULongLong(ctx, "uglobal", (unsigned long long)-2);
RedisModule_InfoAddSection(ctx, "Spanish");
RedisModule_InfoAddFieldCString(ctx, "uno", "one");
@@ -29,6 +30,67 @@ void InfoFunc(RedisModuleInfoCtx *ctx, int for_crash_report) {
}
+int info_get(RedisModuleCtx *ctx, RedisModuleString **argv, int argc, char field_type)
+{
+ if (argc != 3 && argc != 4) {
+ RedisModule_WrongArity(ctx);
+ return REDISMODULE_OK;
+ }
+ int err = REDISMODULE_OK;
+ const char *section, *field;
+ section = RedisModule_StringPtrLen(argv[1], NULL);
+ field = RedisModule_StringPtrLen(argv[2], NULL);
+ RedisModuleServerInfoData *info = RedisModule_GetServerInfo(ctx, section);
+ if (field_type=='i') {
+ long long ll = RedisModule_ServerInfoGetFieldSigned(info, field, &err);
+ if (err==REDISMODULE_OK)
+ RedisModule_ReplyWithLongLong(ctx, ll);
+ } else if (field_type=='u') {
+ unsigned long long ll = (unsigned long long)RedisModule_ServerInfoGetFieldUnsigned(info, field, &err);
+ if (err==REDISMODULE_OK)
+ RedisModule_ReplyWithLongLong(ctx, ll);
+ } else if (field_type=='d') {
+ double d = RedisModule_ServerInfoGetFieldDouble(info, field, &err);
+ if (err==REDISMODULE_OK)
+ RedisModule_ReplyWithDouble(ctx, d);
+ } else if (field_type=='c') {
+ const char *str = RedisModule_ServerInfoGetFieldC(info, field);
+ if (str)
+ RedisModule_ReplyWithCString(ctx, str);
+ } else {
+ RedisModuleString *str = RedisModule_ServerInfoGetField(ctx, info, field);
+ if (str) {
+ RedisModule_ReplyWithString(ctx, str);
+ RedisModule_FreeString(ctx, str);
+ } else
+ err=REDISMODULE_ERR;
+ }
+ if (err!=REDISMODULE_OK)
+ RedisModule_ReplyWithError(ctx, "not found");
+ RedisModule_FreeServerInfo(ctx, info);
+ return REDISMODULE_OK;
+}
+
+int info_gets(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
+ return info_get(ctx, argv, argc, 's');
+}
+
+int info_getc(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
+ return info_get(ctx, argv, argc, 'c');
+}
+
+int info_geti(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
+ return info_get(ctx, argv, argc, 'i');
+}
+
+int info_getu(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
+ return info_get(ctx, argv, argc, 'u');
+}
+
+int info_getd(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
+ return info_get(ctx, argv, argc, 'd');
+}
+
int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
REDISMODULE_NOT_USED(argv);
REDISMODULE_NOT_USED(argc);
@@ -37,5 +99,16 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
if (RedisModule_RegisterInfoFunc(ctx, InfoFunc) == REDISMODULE_ERR) return REDISMODULE_ERR;
+ if (RedisModule_CreateCommand(ctx,"info.gets", info_gets,"",0,0,0) == REDISMODULE_ERR)
+ return REDISMODULE_ERR;
+ if (RedisModule_CreateCommand(ctx,"info.getc", info_getc,"",0,0,0) == REDISMODULE_ERR)
+ return REDISMODULE_ERR;
+ if (RedisModule_CreateCommand(ctx,"info.geti", info_geti,"",0,0,0) == REDISMODULE_ERR)
+ return REDISMODULE_ERR;
+ if (RedisModule_CreateCommand(ctx,"info.getu", info_getu,"",0,0,0) == REDISMODULE_ERR)
+ return REDISMODULE_ERR;
+ if (RedisModule_CreateCommand(ctx,"info.getd", info_getd,"",0,0,0) == REDISMODULE_ERR)
+ return REDISMODULE_ERR;
+
return REDISMODULE_OK;
}
diff --git a/tests/unit/moduleapi/infotest.tcl b/tests/unit/moduleapi/infotest.tcl
index 659ee79d7..80a28656c 100644
--- a/tests/unit/moduleapi/infotest.tcl
+++ b/tests/unit/moduleapi/infotest.tcl
@@ -10,6 +10,32 @@ proc field {info property} {
start_server {tags {"modules"}} {
r module load $testmodule log-key 0
+ test {module reading info} {
+ # check string, integer and float fields
+ assert_equal [r info.gets replication role] "master"
+ assert_equal [r info.getc replication role] "master"
+ assert_equal [r info.geti stats expired_keys] 0
+ assert_equal [r info.getd stats expired_stale_perc] 0
+
+ # check signed and unsigned
+ assert_equal [r info.geti infotest infotest_global] -2
+ assert_equal [r info.getu infotest infotest_uglobal] -2
+
+ # the above are always 0, try module info that is non-zero
+ assert_equal [r info.geti infotest_italian infotest_due] 2
+ set tre [r info.getd infotest_italian infotest_tre]
+ assert {$tre > 3.2 && $tre < 3.4 }
+
+ # search using the wrong section
+ catch { [r info.gets badname redis_version] } e
+ assert_match {*not found*} $e
+
+ # check that section filter works
+ assert { [string match "*usec_per_call*" [r info.gets all cmdstat_info.gets] ] }
+ catch { [r info.gets default cmdstat_info.gets] ] } e
+ assert_match {*not found*} $e
+ }
+
test {module info all} {
set info [r info all]
# info all does not contain modules