Repository: valkey-io/valkey
File: tests/modules/misc.c (C)

Code before:
int test_keyslot(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
    /* Static check of the ClusterKeySlot + ClusterCanonicalKeyNameInSlot
     * round-trip for all slots. */
    for (unsigned int slot = 0; slot < 16384; slot++) {
        const char *tag = ValkeyModule_ClusterCanonicalKeyNameInSlot(slot);
        ValkeyModuleString *key = ValkeyModule_CreateStringPrintf(ctx, "x{%s}y", tag);
        assert(slot == ValkeyModule_ClusterKeySlot(key));
        ValkeyModule_FreeString(ctx, key);
    }
    if (argc != 2){
        return ValkeyModule_WrongArity(ctx);
    }
    unsigned int slot = ValkeyModule_ClusterKeySlot(argv[1]);
    return ValkeyModule_ReplyWithLongLong(ctx, slot);
}

int test_keyslotC(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
    /* Static check of the ClusterKeySlot + ClusterCanonicalKeyNameInSlot
     * round-trip for all slots. */
    for (unsigned int slot = 0; slot < 16384; slot++) {
        const char *tag = ValkeyModule_ClusterCanonicalKeyNameInSlot(slot);
        ValkeyModuleString *key = ValkeyModule_CreateStringPrintf(ctx, "x{%s}y", tag);

        size_t keylen;
        const char *keyptr = ValkeyModule_StringPtrLen(key, &keylen);
        assert(slot == ValkeyModule_ClusterKeySlotC(keyptr, keylen));
        ValkeyModule_FreeString(ctx, key);
    }
    if (argc != 2){
        return ValkeyModule_WrongArity(ctx);
    }
    size_t arglen;
    const char *argptr = ValkeyModule_StringPtrLen(argv[1], &arglen);
    unsigned int slot = ValkeyModule_ClusterKeySlotC(argptr, arglen);
    return ValkeyModule_ReplyWithLongLong(ctx, slot);
}

int ValkeyModule_OnLoad(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
    VALKEYMODULE_NOT_USED(argv);
    VALKEYMODULE_NOT_USED(argc);
    if (ValkeyModule_Init(ctx,"misc",1,VALKEYMODULE_APIVER_1)== VALKEYMODULE_ERR)
        return VALKEYMODULE_ERR;

    if(ValkeyModule_SubscribeToKeyspaceEvents(ctx, VALKEYMODULE_NOTIFY_KEY_MISS | VALKEYMODULE_NOTIFY_EXPIRED, KeySpace_NotificationModuleKeyMissExpired) != VALKEYMODULE_OK){
        return VALKEYMODULE_ERR;
    }

    if (ValkeyModule_CreateCommand(ctx,"test.call_generic", test_call_generic,"",0,0,0) == VALKEYMODULE_ERR)
        return VALKEYMODULE_ERR;
    if (ValkeyModule_CreateCommand(ctx,"test.call_info", test_call_info,"",0,0,0) == VALKEYMODULE_ERR)
        return VALKEYMODULE_ERR;

Review comment (refactor):
This test case is a lot of duplicated code.

Instead of adding a new test command and test case, let's just add this assert line inside the existing `test_keyslot`.

Code after:
int test_keyslot(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
    /* Static check of the ClusterKeySlot + ClusterCanonicalKeyNameInSlot
     * round-trip for all slots. */
    for (unsigned int slot = 0; slot < 16384; slot++) {
        const char *tag = ValkeyModule_ClusterCanonicalKeyNameInSlot(slot);
        ValkeyModuleString *key = ValkeyModule_CreateStringPrintf(ctx, "x{%s}y", tag);
        assert(slot == ValkeyModule_ClusterKeySlot(key));

        size_t keylen;
        const char *keyptr = ValkeyModule_StringPtrLen(key, &keylen);
        assert(slot == ValkeyModule_ClusterKeySlotC(keyptr, keylen));

        ValkeyModule_FreeString(ctx, key);
    }
    if (argc != 2){
        return ValkeyModule_WrongArity(ctx);
    }
    unsigned int slot = ValkeyModule_ClusterKeySlot(argv[1]);
    return ValkeyModule_ReplyWithLongLong(ctx, slot);
}

int ValkeyModule_OnLoad(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
    VALKEYMODULE_NOT_USED(argv);
    VALKEYMODULE_NOT_USED(argc);
    if (ValkeyModule_Init(ctx,"misc",1,VALKEYMODULE_APIVER_1)== VALKEYMODULE_ERR)
        return VALKEYMODULE_ERR;

    if(ValkeyModule_SubscribeToKeyspaceEvents(ctx, VALKEYMODULE_NOTIFY_KEY_MISS | VALKEYMODULE_NOTIFY_EXPIRED, KeySpace_NotificationModuleKeyMissExpired) != VALKEYMODULE_OK){
        return VALKEYMODULE_ERR;
    }

    if (ValkeyModule_CreateCommand(ctx,"test.call_generic", test_call_generic,"",0,0,0) == VALKEYMODULE_ERR)
        return VALKEYMODULE_ERR;
    if (ValkeyModule_CreateCommand(ctx,"test.call_info", test_call_info,"",0,0,0) == VALKEYMODULE_ERR)
        return VALKEYMODULE_ERR;
    if (ValkeyModule_CreateCommand(ctx,"test.ld_conversion", test_ld_conv, "",0,0,0) == VALKEYMODULE_ERR)
        return VALKEYMODULE_ERR;
    if (ValkeyModule_CreateCommand(ctx,"test.ull_conversion", test_ull_conv, "",0,0,0) == VALKEYMODULE_ERR)
        return VALKEYMODULE_ERR;
    if (ValkeyModule_CreateCommand(ctx,"test.flushall", test_flushall,"",0,0,0) == VALKEYMODULE_ERR)
        return VALKEYMODULE_ERR;
    if (ValkeyModule_CreateCommand(ctx,"test.dbsize", test_dbsize,"",0,0,0) == VALKEYMODULE_ERR)
        return VALKEYMODULE_ERR;
    if (ValkeyModule_CreateCommand(ctx,"test.randomkey", test_randomkey,"",0,0,0) == VALKEYMODULE_ERR)
        return VALKEYMODULE_ERR;
    if (ValkeyModule_CreateCommand(ctx,"test.keyexists", test_keyexists,"",1,1,1) == VALKEYMODULE_ERR)
        return VALKEYMODULE_ERR;
    if (ValkeyModule_CreateCommand(ctx,"test.setlru", test_setlru,"",0,0,0) == VALKEYMODULE_ERR)
        return VALKEYMODULE_ERR;
    if (ValkeyModule_CreateCommand(ctx,"test.getlru", test_getlru,"",0,0,0) == VALKEYMODULE_ERR)
        return VALKEYMODULE_ERR;