redict/tests/modules/timer.c

108 lines
3.3 KiB
C
Raw Normal View History

2024-03-21 09:30:47 -04:00
// SPDX-FileCopyrightText: 2024 Redict Contributors
// SPDX-FileCopyrightText: 2024 Salvatore Sanfilippo <antirez at gmail dot com>
//
// SPDX-License-Identifier: BSD-3-Clause
// SPDX-License-Identifier: LGPL-3.0-only
2020-11-11 15:57:33 -05:00
2024-03-21 05:49:18 -04:00
#include "redictmodule.h"
2020-11-11 15:57:33 -05:00
static void timer_callback(RedictModuleCtx *ctx, void *data)
2020-11-11 15:57:33 -05:00
{
RedictModuleString *keyname = data;
RedictModuleCallReply *reply;
2020-11-11 15:57:33 -05:00
reply = RedictModule_Call(ctx, "INCR", "s", keyname);
2020-11-11 15:57:33 -05:00
if (reply != NULL)
RedictModule_FreeCallReply(reply);
RedictModule_FreeString(ctx, keyname);
2020-11-11 15:57:33 -05:00
}
int test_createtimer(RedictModuleCtx *ctx, RedictModuleString **argv, int argc)
2020-11-11 15:57:33 -05:00
{
if (argc != 3) {
RedictModule_WrongArity(ctx);
return REDICTMODULE_OK;
2020-11-11 15:57:33 -05:00
}
long long period;
if (RedictModule_StringToLongLong(argv[1], &period) == REDICTMODULE_ERR) {
RedictModule_ReplyWithError(ctx, "Invalid time specified.");
return REDICTMODULE_OK;
2020-11-11 15:57:33 -05:00
}
RedictModuleString *keyname = argv[2];
RedictModule_RetainString(ctx, keyname);
2020-11-11 15:57:33 -05:00
RedictModuleTimerID id = RedictModule_CreateTimer(ctx, period, timer_callback, keyname);
RedictModule_ReplyWithLongLong(ctx, id);
2020-11-11 15:57:33 -05:00
return REDICTMODULE_OK;
2020-11-11 15:57:33 -05:00
}
int test_gettimer(RedictModuleCtx *ctx, RedictModuleString **argv, int argc)
2020-11-11 15:57:33 -05:00
{
if (argc != 2) {
RedictModule_WrongArity(ctx);
return REDICTMODULE_OK;
2020-11-11 15:57:33 -05:00
}
long long id;
if (RedictModule_StringToLongLong(argv[1], &id) == REDICTMODULE_ERR) {
RedictModule_ReplyWithError(ctx, "Invalid id specified.");
return REDICTMODULE_OK;
2020-11-11 15:57:33 -05:00
}
uint64_t remaining;
RedictModuleString *keyname;
if (RedictModule_GetTimerInfo(ctx, id, &remaining, (void **)&keyname) == REDICTMODULE_ERR) {
RedictModule_ReplyWithNull(ctx);
2020-11-11 15:57:33 -05:00
} else {
RedictModule_ReplyWithArray(ctx, 2);
RedictModule_ReplyWithString(ctx, keyname);
RedictModule_ReplyWithLongLong(ctx, remaining);
2020-11-11 15:57:33 -05:00
}
return REDICTMODULE_OK;
2020-11-11 15:57:33 -05:00
}
int test_stoptimer(RedictModuleCtx *ctx, RedictModuleString **argv, int argc)
2020-11-11 15:57:33 -05:00
{
if (argc != 2) {
RedictModule_WrongArity(ctx);
return REDICTMODULE_OK;
2020-11-11 15:57:33 -05:00
}
long long id;
if (RedictModule_StringToLongLong(argv[1], &id) == REDICTMODULE_ERR) {
RedictModule_ReplyWithError(ctx, "Invalid id specified.");
return REDICTMODULE_OK;
2020-11-11 15:57:33 -05:00
}
int ret = 0;
RedictModuleString *keyname;
if (RedictModule_StopTimer(ctx, id, (void **) &keyname) == REDICTMODULE_OK) {
RedictModule_FreeString(ctx, keyname);
2020-11-11 15:57:33 -05:00
ret = 1;
}
RedictModule_ReplyWithLongLong(ctx, ret);
return REDICTMODULE_OK;
2020-11-11 15:57:33 -05:00
}
int RedictModule_OnLoad(RedictModuleCtx *ctx, RedictModuleString **argv, int argc) {
REDICTMODULE_NOT_USED(argv);
REDICTMODULE_NOT_USED(argc);
if (RedictModule_Init(ctx,"timer",1,REDICTMODULE_APIVER_1)== REDICTMODULE_ERR)
return REDICTMODULE_ERR;
2020-11-11 15:57:33 -05:00
if (RedictModule_CreateCommand(ctx,"test.createtimer", test_createtimer,"",0,0,0) == REDICTMODULE_ERR)
return REDICTMODULE_ERR;
if (RedictModule_CreateCommand(ctx,"test.gettimer", test_gettimer,"",0,0,0) == REDICTMODULE_ERR)
return REDICTMODULE_ERR;
if (RedictModule_CreateCommand(ctx,"test.stoptimer", test_stoptimer,"",0,0,0) == REDICTMODULE_ERR)
return REDICTMODULE_ERR;
2020-11-11 15:57:33 -05:00
return REDICTMODULE_OK;
2020-11-11 15:57:33 -05:00
}