summaryrefslogtreecommitdiff
path: root/tests/modules
diff options
context:
space:
mode:
authorguybe7 <guy.benoish@redislabs.com>2022-07-27 13:40:05 +0200
committerGitHub <noreply@github.com>2022-07-27 14:40:05 +0300
commit45c99d7092926c0774608520b9a399dbd40f9714 (patch)
treef1c7194f4ab562ba3cb0c0d74a2355100f6932c3 /tests/modules
parent00097bf4aa9e28477ee5d9361569f3212fb7493a (diff)
downloadredis-45c99d7092926c0774608520b9a399dbd40f9714.tar.gz
Adds RM_Microseconds and RM_CachedMicroseconds (#11016)
RM_Microseconds Return the wall-clock Unix time, in microseconds RM_CachedMicroseconds Returns a cached copy of the Unix time, in microseconds. It is updated in the server cron job and before executing a command. It is useful for complex call stacks, such as a command causing a key space notification, causing a module to execute a RedisModule_Call, causing another notification, etc. It makes sense that all these callbacks would use the same clock.
Diffstat (limited to 'tests/modules')
-rw-r--r--tests/modules/keyspace_events.c13
1 files changed, 13 insertions, 0 deletions
diff --git a/tests/modules/keyspace_events.c b/tests/modules/keyspace_events.c
index 58670164d..c0f79bf37 100644
--- a/tests/modules/keyspace_events.c
+++ b/tests/modules/keyspace_events.c
@@ -30,10 +30,14 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
+#define _DEFAULT_SOURCE /* For usleep */
#include "redismodule.h"
#include <stdio.h>
#include <string.h>
+#include <unistd.h>
+
+ustime_t cached_time = 0;
/** stores all the keys on which we got 'loaded' keyspace notification **/
RedisModuleDict *loaded_event_log = NULL;
@@ -59,6 +63,12 @@ static int KeySpace_NotificationLoaded(RedisModuleCtx *ctx, int type, const char
static int KeySpace_NotificationGeneric(RedisModuleCtx *ctx, int type, const char *event, RedisModuleString *key) {
REDISMODULE_NOT_USED(type);
+ if (cached_time) {
+ RedisModule_Assert(cached_time == RedisModule_CachedMicroseconds());
+ usleep(1);
+ RedisModule_Assert(cached_time != RedisModule_Microseconds());
+ }
+
if (strcmp(event, "del") == 0) {
RedisModuleString *copykey = RedisModule_CreateStringPrintf(ctx, "%s_copy", RedisModule_StringPtrLen(key, NULL));
RedisModuleCallReply* rep = RedisModule_Call(ctx, "DEL", "s!", copykey);
@@ -158,6 +168,8 @@ static int cmdDelKeyCopy(RedisModuleCtx *ctx, RedisModuleString **argv, int argc
if (argc != 2)
return RedisModule_WrongArity(ctx);
+ cached_time = RedisModule_CachedMicroseconds();
+
RedisModuleCallReply* rep = RedisModule_Call(ctx, "DEL", "s!", argv[1]);
if (!rep) {
RedisModule_ReplyWithError(ctx, "NULL reply returned");
@@ -165,6 +177,7 @@ static int cmdDelKeyCopy(RedisModuleCtx *ctx, RedisModuleString **argv, int argc
RedisModule_ReplyWithCallReply(ctx, rep);
RedisModule_FreeCallReply(rep);
}
+ cached_time = 0;
return REDISMODULE_OK;
}