Files
kv/tests/modules/deferred_reply.c
zeekay 9329feeea4 refactor(module): one module header — kvmodule.h
src carried three headers for one module API: kvmodule.h (ours), valkeymodule.h
(the same API under a different name), and redismodule.h (an adapter mapping
REDISMODULE_* onto KVMODULE_*). We own this fork and carry no backward compat,
so the API has one spelling.

  ValkeyModule* / RedisModule*   -> KVModule*
  VALKEYMODULE_* / REDISMODULE_* -> KVMODULE_*
  every #include                 -> "kvmodule.h"
  src/valkeymodule.h, src/redismodule.h  deleted

grep over src/ returns zero for all four symbol families. modules/zap, which
already spoke KVModule_*, rebuilds against the single header.
2026-07-26 12:52:43 -07:00

92 lines
3.0 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* Test module to verify that addReplyDeferredLen / setDeferredReply work
* correctly when the deferred reply buffer is active.
*
* The module subscribes to NOTIFY_STRING keyspace notifications. When armed,
* the notification callback blocks the client with a reply callback that
* builds a nested array using KVMODULE_POSTPONED_LEN (which internally
* calls addReplyDeferredLen).
*
* Without the fix in networking.c the placeholder node for the inner array
* ends up in c->reply while the outer array header and elements live in
* c->deferred_reply, producing a malformed response after
* commitDeferredReplyBuffer joins the two lists. */
#include "kvmodule.h"
#include <pthread.h>
#include <unistd.h>
static int armed = 0;
static void *UnblockThread(void *arg) {
KVModuleBlockedClient *bc = arg;
usleep(100000); /* 100 ms */
KVModule_UnblockClient(bc, NULL);
return NULL;
}
/* Reply callback – builds a two-element array where the second element is
* itself an array whose length is set via POSTPONED_LEN.
* Expected response: ["first", ["a", "b"]] */
static int ReplyCallback(KVModuleCtx *ctx, KVModuleString **argv, int argc) {
KVMODULE_NOT_USED(ctx);
KVMODULE_NOT_USED(argv);
KVMODULE_NOT_USED(argc);
KVModule_ReplyWithArray(ctx, 2);
KVModule_ReplyWithSimpleString(ctx, "first");
KVModule_ReplyWithArray(ctx, KVMODULE_POSTPONED_LEN);
KVModule_ReplyWithSimpleString(ctx, "a");
KVModule_ReplyWithSimpleString(ctx, "b");
KVModule_ReplySetArrayLength(ctx, 2);
return KVMODULE_OK;
}
static int OnKeyspaceNotification(KVModuleCtx *ctx, int type,
const char *event, KVModuleString *key) {
KVMODULE_NOT_USED(type);
KVMODULE_NOT_USED(event);
KVMODULE_NOT_USED(key);
if (!armed) return KVMODULE_OK;
armed = 0;
KVModuleBlockedClient *bc =
KVModule_BlockClient(ctx, ReplyCallback, NULL, NULL, 0);
if (!bc) return KVMODULE_OK;
pthread_t tid;
if (pthread_create(&tid, NULL, UnblockThread, bc) != 0) {
KVModule_UnblockClient(bc, NULL);
} else {
pthread_detach(tid);
}
return KVMODULE_OK;
}
/* DEFERRED_REPLY.ARM – arms the notification handler so the next
* NOTIFY_STRING event blocks the client. */
static int CmdArm(KVModuleCtx *ctx, KVModuleString **argv, int argc) {
KVMODULE_NOT_USED(argv);
KVMODULE_NOT_USED(argc);
armed = 1;
KVModule_ReplyWithSimpleString(ctx, "OK");
return KVMODULE_OK;
}
int KVModule_OnLoad(KVModuleCtx *ctx, KVModuleString **argv, int argc) {
KVMODULE_NOT_USED(argv);
KVMODULE_NOT_USED(argc);
if (KVModule_Init(ctx, "deferred_reply", 1, KVMODULE_APIVER_1) == KVMODULE_ERR)
return KVMODULE_ERR;
if (KVModule_SubscribeToKeyspaceEvents(ctx, KVMODULE_NOTIFY_STRING,
OnKeyspaceNotification) != KVMODULE_OK)
return KVMODULE_ERR;
if (KVModule_CreateCommand(ctx, "deferred_reply.arm", CmdArm, "", 0, 0, 0) == KVMODULE_ERR)
return KVMODULE_ERR;
return KVMODULE_OK;
}