diff options
author | Viktor Söderqvist <viktor.soderqvist@est.tech> | 2022-06-26 13:34:59 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-26 14:34:59 +0300 |
commit | 6272ca609e67d1f35dda50a82027d7401892e383 (patch) | |
tree | f584684b145807e3c5f0da6f0f3293e2b65ee461 /tests | |
parent | d96cf3639a371574c212d87c70e5a80214fc721f (diff) | |
download | redis-6272ca609e67d1f35dda50a82027d7401892e383.tar.gz |
Add RM_SetClientNameById and RM_GetClientNameById (#10839)
Adding Module APIs to let the module read and set the client name of an arbitrary connection.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/modules/misc.c | 25 | ||||
-rw-r--r-- | tests/unit/moduleapi/misc.tcl | 12 |
2 files changed, 37 insertions, 0 deletions
diff --git a/tests/modules/misc.c b/tests/modules/misc.c index da6ee9f9e..dce78ca2b 100644 --- a/tests/modules/misc.c +++ b/tests/modules/misc.c @@ -270,6 +270,27 @@ int test_clientinfo(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) return REDISMODULE_OK; } +int test_getname(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { + (void)argv; + if (argc != 1) return RedisModule_WrongArity(ctx); + unsigned long long id = RedisModule_GetClientId(ctx); + RedisModuleString *name = RedisModule_GetClientNameById(ctx, id); + if (name == NULL) + return RedisModule_ReplyWithError(ctx, "-ERR No name"); + RedisModule_ReplyWithString(ctx, name); + RedisModule_FreeString(ctx, name); + return REDISMODULE_OK; +} + +int test_setname(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { + if (argc != 2) return RedisModule_WrongArity(ctx); + unsigned long long id = RedisModule_GetClientId(ctx); + if (RedisModule_SetClientNameById(id, argv[1]) == REDISMODULE_OK) + return RedisModule_ReplyWithSimpleString(ctx, "OK"); + else + return RedisModule_ReplyWithError(ctx, strerror(errno)); +} + int test_log_tsctx(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { RedisModuleCtx *tsctx = RedisModule_GetDetachedThreadSafeContext(ctx); @@ -384,6 +405,10 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) return REDISMODULE_ERR; if (RedisModule_CreateCommand(ctx,"test.clientinfo", test_clientinfo,"",0,0,0) == REDISMODULE_ERR) return REDISMODULE_ERR; + if (RedisModule_CreateCommand(ctx,"test.getname", test_getname,"",0,0,0) == REDISMODULE_ERR) + return REDISMODULE_ERR; + if (RedisModule_CreateCommand(ctx,"test.setname", test_setname,"",0,0,0) == REDISMODULE_ERR) + return REDISMODULE_ERR; if (RedisModule_CreateCommand(ctx,"test.redisversion", test_redisversion,"",0,0,0) == REDISMODULE_ERR) return REDISMODULE_ERR; if (RedisModule_CreateCommand(ctx,"test.getclientcert", test_getclientcert,"",0,0,0) == REDISMODULE_ERR) diff --git a/tests/unit/moduleapi/misc.tcl b/tests/unit/moduleapi/misc.tcl index c8a5107f9..3fca1cf7f 100644 --- a/tests/unit/moduleapi/misc.tcl +++ b/tests/unit/moduleapi/misc.tcl @@ -103,6 +103,18 @@ start_server {tags {"modules"}} { assert { [dict get $info flags] == "${ssl_flag}::tracking::" } } + test {test module get/set client name by id api} { + catch { r test.getname } e + assert_equal "-ERR No name" $e + r client setname nobody + catch { r test.setname "name with spaces" } e + assert_match "*Invalid argument*" $e + assert_equal nobody [r client getname] + assert_equal nobody [r test.getname] + r test.setname somebody + assert_equal somebody [r client getname] + } + test {test module getclientcert api} { set cert [r test.getclientcert] |