/** * Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0 */ import { afterAll, afterEach, beforeAll, describe, expect, it, } from "@jest/globals"; import { setTimeout as sleep } from "node:timers/promises"; import { gte } from "semver"; import { ValkeyCluster } from "../../utils/TestUtils"; import { BitwiseOperation, ClientPauseMode, ClientSideCache, ClientTrackingInfo, ClosingError, ClusterBatch, ConfigurationError, Decoder, FlushMode, FunctionListResponse, FunctionRestorePolicy, FunctionStatsSingleResponse, GeoUnit, GlideClusterClient, GlideRecord, GlideReturnType, GlideString, InfoOptions, ListDirection, MemoryStats, ProtocolVersion, RequestError, Routes, ScoreFilter, Script, SlotKeyTypes, SortOrder, convertGlideRecordToRecord, convertRecordToGlideRecord, } from "../build-ts"; import { runBaseTests } from "./SharedTests"; import { assertClientTrackingInfo, assertConnected, assertMemoryStatsDbEntry, assertMemoryStatsFields, batchTest, checkClusterResponse, checkFunctionListResponse, checkFunctionStatsResponse, createLongRunningLuaScript, createLuaLibWithLongRunningFunction, flushClient, generateLuaLibCode, flattenClusterResponseArrays, getClientConfigurationOption, getClientCount, getFirstResult, getRandomKey, getUnixSeconds, getServerVersion, intoArray, intoString, parseEndpoints, PRIMARY_SLOT_ROUTE_OPTION, triggerLatencySpike, validateBatchResponse, waitForNotBusy, socketDrainDelay, } from "./TestUtilities"; const TIMEOUT = 50000; const CLEANUP_TIMEOUT = 60000; // afterAll timeout: cluster teardown on EC2 runners can take >10s describe("GlideClusterClient", () => { let testsFailed = 0; let cluster: ValkeyCluster; let azCluster: ValkeyCluster; let client: GlideClusterClient; let azClient: GlideClusterClient; let lastProtocol: ProtocolVersion | undefined; let pooledClient: GlideClusterClient | undefined; let pooledAzClient: GlideClusterClient | undefined; beforeAll(async () => { const clusterAddresses = global.CLUSTER_ENDPOINTS; if (clusterAddresses) { // Initialize current cluster from existing addresses cluster = await ValkeyCluster.initFromExistingCluster( true, parseEndpoints(clusterAddresses), getServerVersion, ); // Add small delay between cluster initializations to prevent socket contention await new Promise((resolve) => setTimeout(resolve, 100)); // Initialize cluster from existing addresses for AzAffinity test azCluster = await ValkeyCluster.initFromExistingCluster( true, parseEndpoints(clusterAddresses), getServerVersion, ); } else { cluster = await ValkeyCluster.createCluster( true, 3, 1, getServerVersion, ); // Add small delay between cluster creations to prevent socket contention await new Promise((resolve) => setTimeout(resolve, 100)); azCluster = await ValkeyCluster.createCluster( true, 3, 4, getServerVersion, ); } }, 120000); afterEach(async () => { await flushClient(client); await flushClient(azClient); // Close clients that were created by standalone tests (not pooled). if (client && client !== pooledClient) { client.close(); client = undefined!; } if (azClient && azClient !== pooledAzClient) { azClient.close(); azClient = undefined!; } }); afterAll(async () => { client?.close(); azClient?.close(); if (testsFailed === 0) { if (cluster) await cluster.close(); await socketDrainDelay(); if (azCluster) await azCluster.close(); } else { if (cluster) await cluster.close(true); await socketDrainDelay(); if (azCluster) await azCluster.close(true); } }, CLEANUP_TIMEOUT); runBaseTests({ init: async (protocol, configOverrides) => { const configCurrent = getClientConfigurationOption( cluster.getAddresses(), protocol, configOverrides, ); // Recreate client if config changed or client is dead if (configOverrides || !client || protocol !== lastProtocol) { client?.close(); client = await GlideClusterClient.createClient(configCurrent); } else { try { await client.ping(); } catch { client = await GlideClusterClient.createClient(configCurrent); } } const configNew = getClientConfigurationOption( azCluster.getAddresses(), protocol, configOverrides, ); // Recreate azClient if config changed or client is dead if (configOverrides || !azClient || protocol !== lastProtocol) { azClient?.close(); azClient = await GlideClusterClient.createClient(configNew); } else { try { await azClient.ping(); } catch { azClient = await GlideClusterClient.createClient(configNew); } } lastProtocol = protocol; pooledClient = client; pooledAzClient = azClient; testsFailed += 1; return { client, azClient, cluster, azCluster, }; }, close: (testSucceeded: boolean) => { if (testSucceeded) { testsFailed -= 1; } }, timeout: TIMEOUT, }); it( "register_custom_client_info_names", async () => { if (cluster.checkIfServerVersionLessThan("7.2.0")) return; // Test default library name const defaultClient = await GlideClusterClient.createClient( getClientConfigurationOption( cluster.getAddresses(), ProtocolVersion.RESP3, ), ); try { const info = (await defaultClient.customCommand( ["CLIENT", "INFO"], { route: "allNodes" }, )) as GlideRecord; for (const entry of info) { expect(entry.value).toContain("lib-name=GlideJS"); } } finally { defaultClient.close(); } // Test libName override only const overrideOnlyClient = await GlideClusterClient.createClient( getClientConfigurationOption( cluster.getAddresses(), ProtocolVersion.RESP3, { libName: "custom-client" }, ), ); try { const info = (await overrideOnlyClient.customCommand( ["CLIENT", "INFO"], { route: "allNodes" }, )) as GlideRecord; for (const entry of info) { expect(entry.value).toContain("lib-name=custom-client"); } } finally { overrideOnlyClient.close(); } // Test clientInfoTag only (appended to default GlideJS) const tagOnlyClient = await GlideClusterClient.createClient( getClientConfigurationOption( cluster.getAddresses(), ProtocolVersion.RESP3, { clientInfoTag: "framework:1.2" }, ), ); try { const info = (await tagOnlyClient.customCommand( ["CLIENT", "INFO"], { route: "allNodes" }, )) as GlideRecord; for (const entry of info) { expect(entry.value).toContain( "lib-name=GlideJS(framework:1.2)", ); } } finally { tagOnlyClient.close(); } // Test combined libName + clientInfoTag const combinedClient = await GlideClusterClient.createClient( getClientConfigurationOption( cluster.getAddresses(), ProtocolVersion.RESP3, { libName: "custom-client", clientInfoTag: "framework:1.2", }, ), ); try { const info = (await combinedClient.customCommand( ["CLIENT", "INFO"], { route: "allNodes" }, )) as GlideRecord; for (const entry of info) { expect(entry.value).toContain( "lib-name=custom-client(framework:1.2)", ); } } finally { combinedClient.close(); } }, TIMEOUT, ); it("clientInfoTag_rejects_whitespace", async () => { await expect( GlideClusterClient.createClient( getClientConfigurationOption( cluster.getAddresses(), ProtocolVersion.RESP3, { clientInfoTag: "has space" }, ), ), ).rejects.toThrow(ConfigurationError); await expect( GlideClusterClient.createClient( getClientConfigurationOption( cluster.getAddresses(), ProtocolVersion.RESP3, { clientInfoTag: "has\ttab" }, ), ), ).rejects.toThrow(ConfigurationError); await expect( GlideClusterClient.createClient( getClientConfigurationOption( cluster.getAddresses(), ProtocolVersion.RESP3, { clientInfoTag: "has\nnewline" }, ), ), ).rejects.toThrow(ConfigurationError); }); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "clientPauseAll then clientUnpause_%p", async (protocol) => { client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol, { requestTimeout: 10000, }), ); const key = "clientPauseAll_then_clientUnpause_key"; expect(await client.set(key, "before")).toEqual("OK"); expect( await client.clientPause(2000, ClientPauseMode.ALL, { route: "allPrimaries", }), ).toEqual("OK"); let setDone = false; const set = client.set(key, "after").then((r) => { setDone = true; return r; }); let unpauseDone = false; const unpause = client .clientUnpause({ route: "allPrimaries" }) .then((r) => { unpauseDone = true; return r; }); await sleep(300); // Verify that none of the commands completes during the pause window. expect(setDone).toBe(false); expect(unpauseDone).toBe(false); // Verify that all commands complete once pause expires naturally. expect(await set).toEqual("OK"); expect(await unpause).toEqual("OK"); expect(await client.get(key)).toEqual("after"); }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "clientPauseWrite then clientUnpause_%p", async (protocol) => { client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol, { requestTimeout: 10000, }), ); const key = "clientPauseWrite_then_clientUnpause_key"; expect(await client.set(key, "before")).toEqual("OK"); expect( await client.clientPause(2000, ClientPauseMode.WRITE, { route: "allPrimaries", }), ).toEqual("OK"); // Reads are not blocked by PAUSE WRITE. expect(await client.get(key)).toEqual("before"); let setDone = false; const set = client.set(key, "after").then((r) => { setDone = true; return r; }); await sleep(300); // Verify that SET has not completed because server is paused. expect(setDone).toBe(false); expect( await client.clientUnpause({ route: "allPrimaries" }), ).toEqual("OK"); // Verify that SET completes once pause expires, and the new value // is visible. expect(await set).toEqual("OK"); expect(await client.get(key)).toEqual("after"); }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( `info with server and replication_%p`, async (protocol) => { client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const info_server = getFirstResult( await client.info({ sections: [InfoOptions.Server] }), ); expect(info_server).toEqual(expect.stringContaining("# Server")); const infoReplicationValues = Object.values( await client.info({ sections: [InfoOptions.Replication] }), ); const replicationInfo = intoArray(infoReplicationValues); for (const item of replicationInfo) { expect(item).toContain("role:master"); expect(item).toContain("# Replication"); } }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( `info with server and randomNode route_%p`, async (protocol) => { client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const result = await client.info({ sections: [InfoOptions.Server], route: "randomNode", }); expect(result).toEqual(expect.stringContaining("# Server")); expect(result).toEqual(expect.not.stringContaining("# Errorstats")); }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( `route by address reaches correct node_%p`, async (protocol) => { // returns the line that contains the word "myself", up to that point. This is done because the values after it might change with time. const cleanResult = (value: string) => { return ( value .split("\n") .find((line: string) => line.includes("myself")) ?.split("myself")[0] ?? "" ); }; client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const result = cleanResult( intoString( await client.customCommand(["cluster", "nodes"], { route: "randomNode", }), ), ); // check that routing without explicit port works const host = result.split(" ")[1].split("@")[0] ?? ""; if (!host) { throw new Error("No host could be parsed"); } const secondResult = cleanResult( intoString( await client.customCommand(["cluster", "nodes"], { route: { type: "routeByAddress", host, }, }), ), ); expect(result).toEqual(secondResult); const [host2, port] = host.split(":"); // check that routing with explicit port works const thirdResult = cleanResult( intoString( await client.customCommand(["cluster", "nodes"], { route: { type: "routeByAddress", host: host2, port: Number(port), }, }), ), ); expect(result).toEqual(thirdResult); }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( `fail routing by address if no port is provided_%p`, async (protocol) => { client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); await expect( client.info({ route: { type: "routeByAddress", host: "foo", }, }), ).rejects.toThrow(RequestError); }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( `dump and restore custom command_%p`, async (protocol) => { client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const key = getRandomKey(); const value = "value"; const valueEncoded = Buffer.from(value); expect(await client.set(key, value)).toEqual("OK"); // Since DUMP gets binary results, we cannot use the default decoder (string) here, so we expected to get an error. await expect(client.customCommand(["DUMP", key])).rejects.toThrow( /invalid utf-8 sequence|incomplete utf-8 byte sequence/, ); const dumpResult = await client.customCommand(["DUMP", key], { decoder: Decoder.Bytes, }); expect(await client.del([key])).toEqual(1); if (dumpResult instanceof Buffer) { // check the delete expect(await client.get(key)).toEqual(null); expect( await client.customCommand( ["RESTORE", key, "0", dumpResult], { decoder: Decoder.Bytes }, ), ).toEqual("OK"); // check the restore expect(await client.get(key)).toEqual(value); expect( await client.get(key, { decoder: Decoder.Bytes }), ).toEqual(valueEncoded); } }, TIMEOUT, ); it.each([ [ProtocolVersion.RESP2, true], [ProtocolVersion.RESP2, false], [ProtocolVersion.RESP3, true], [ProtocolVersion.RESP3, false], ])( `config get and config set batch test with protocol=%p and isAtomic=%p`, async (protocol, isAtomic) => { client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const batch = new ClusterBatch(isAtomic) .configSet({ timeout: "1000" }) .configGet(["timeout"]); const result = await client.exec(batch, true); expect(result).toEqual([ "OK", convertRecordToGlideRecord({ timeout: "1000" }), ]); if (!cluster.checkIfServerVersionLessThan("7.0.0")) { const batch = new ClusterBatch(isAtomic) .configSet({ timeout: "2000", "cluster-node-timeout": "16000", }) .configGet(["timeout", "cluster-node-timeout"]); const result = (await client.exec( batch, true, )) as GlideRecord[]; const convertedResult = [ result[0], convertGlideRecordToRecord(result[1]), ]; expect(convertedResult).toEqual([ "OK", { timeout: "2000", "cluster-node-timeout": "16000", }, ]); } }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( `config get with wildcard and multi node route %p`, async (protocol) => { client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const result = await client.configGet(["*file"], { route: "allPrimaries", }); Object.values( result as Record>, ).forEach((resp) => { const keys = Object.keys(resp); expect(keys.length).toBeGreaterThan(5); expect(keys).toContain("pidfile"); expect(keys).toContain("logfile"); }); }, TIMEOUT, ); describe.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "Protocol is RESP2 = %s", (protocol) => { describe.each([Decoder.String, Decoder.Bytes])( "Decoder String = %s", (decoder) => { describe.each([true, false])( "isAtomic = %s", (isAtomic) => { it( "can send batches", async () => { client = await GlideClusterClient.createClient( getClientConfigurationOption( cluster.getAddresses(), protocol, ), ); const batch = new ClusterBatch(isAtomic); const expectedRes = await batchTest( batch, cluster, decoder, ); if ( !cluster.checkIfServerVersionLessThan( "7.0.0", ) ) { batch.publish("message", "key", true); expectedRes.push([ 'publish("message", "key", true)', 0, ]); batch.pubsubShardChannels(); expectedRes.push([ "pubsubShardChannels()", [], ]); batch.pubsubShardNumSub([]); expectedRes.push([ "pubsubShardNumSub()", [], ]); } const result = await client.exec( batch, true, { decoder: Decoder.String, }, ); validateBatchResponse(result, expectedRes); }, TIMEOUT, ); }, ); }, ); }, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( `can return null on WATCH transaction failures_%p`, async (protocol) => { const client1 = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const client2 = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const transaction = new ClusterBatch(true); transaction.get("key"); const result1 = await client1.watch(["key"]); expect(result1).toEqual("OK"); const result2 = await client2.set("key", "foo"); expect(result2).toEqual("OK"); const result3 = await client1.exec(transaction, true); expect(result3).toBeNull(); client1.close(); client2.close(); }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( `echo with all nodes routing_%p`, async (protocol) => { client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const message = getRandomKey(); const echoDict = await client.echo(message, { route: "allNodes" }); expect(typeof echoDict).toBe("object"); expect(intoArray(echoDict)).toEqual( expect.arrayContaining(intoArray([message])), ); }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( `check that multi key command returns a cross slot error`, async (protocol) => { const client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const promises: Promise[] = [ client.blpop(["abc", "zxy", "lkn"], 0.1), client.rename("abc", "zxy"), client.msetnx({ abc: "xyz", def: "abc", hij: "def" }), client.brpop(["abc", "zxy", "lkn"], 0.1), client.bitop(BitwiseOperation.AND, "abc", ["zxy", "lkn"]), client.smove("abc", "zxy", "value"), client.renamenx("abc", "zxy"), client.sinter(["abc", "zxy", "lkn"]), client.sinterstore("abc", ["zxy", "lkn"]), client.zinterstore("abc", ["zxy", "lkn"]), client.zunionstore("abc", ["zxy", "lkn"]), client.sunionstore("abc", ["zxy", "lkn"]), client.sunion(["abc", "zxy", "lkn"]), client.pfcount(["abc", "zxy", "lkn"]), client.pfmerge("abc", ["def", "ghi"]), client.sdiff(["abc", "zxy", "lkn"]), client.sdiffstore("abc", ["zxy", "lkn"]), client.sortStore("abc", "zyx"), client.sortStore("abc", "zyx", { isAlpha: true }), client.bzpopmax(["abc", "def"], 0.5), client.bzpopmin(["abc", "def"], 0.5), client.xread({ abc: "0-0", zxy: "0-0", lkn: "0-0" }), client.xreadgroup("_", "_", { abc: ">", zxy: ">", lkn: ">" }), ]; if (gte(cluster.getVersion(), "6.2.0")) { promises.push( client.blmove( "abc", "def", ListDirection.LEFT, ListDirection.LEFT, 0.2, ), client.zdiff(["abc", "zxy", "lkn"]), client.zdiffWithScores(["abc", "zxy", "lkn"]), client.zdiffstore("abc", ["zxy", "lkn"]), client.copy("abc", "zxy", { replace: true }), client.geosearchstore( "abc", "zxy", { member: "_" }, { radius: 5, unit: GeoUnit.METERS }, ), client.zrangeStore("abc", "zyx", { start: 0, end: -1 }), client.zinter(["abc", "zxy", "lkn"]), client.zinterWithScores(["abc", "zxy", "lkn"]), client.zunion(["abc", "zxy", "lkn"]), client.zunionWithScores(["abc", "zxy", "lkn"]), ); } if (gte(cluster.getVersion(), "7.0.0")) { promises.push( client.sintercard(["abc", "zxy", "lkn"]), client.zintercard(["abc", "zxy", "lkn"]), client.zmpop(["abc", "zxy", "lkn"], ScoreFilter.MAX), client.bzmpop(["abc", "zxy", "lkn"], ScoreFilter.MAX, 0.1), client.lcs("abc", "xyz"), client.lcsLen("abc", "xyz"), client.lcsIdx("abc", "xyz"), client.lmpop(["abc", "def"], ListDirection.LEFT, { count: 1, }), client.blmpop(["abc", "def"], ListDirection.RIGHT, 0.1, { count: 1, }), ); } await Promise.allSettled(promises).then((results) => { results.forEach((result) => { expect(result.status).toBe("rejected"); if (result.status === "rejected") { expect(result.reason.message).toContain("CrossSlot"); } }); }); client.close(); }, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( `check that multi key command routed to multiple nodes`, async (protocol) => { const client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); await client.exists(["abc", "zxy", "lkn"]); await client.unlink(["abc", "zxy", "lkn"]); await client.del(["abc", "zxy", "lkn"]); await client.mget(["abc", "zxy", "lkn"]); await client.mset({ abc: "1", zxy: "2", lkn: "3" }); await client.touch(["abc", "zxy", "lkn"]); await client.watch(["ghi", "zxy", "lkn"]); client.close(); }, ); it.each([ [ProtocolVersion.RESP2, true], [ProtocolVersion.RESP2, false], [ProtocolVersion.RESP3, true], [ProtocolVersion.RESP3, false], ])( "object freq batch test with protocol=%p and isAtomic=%p", async (protocol, isAtomic) => { const client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const key = getRandomKey(); const maxmemoryPolicyKey = "maxmemory-policy"; const config = await client.configGet([maxmemoryPolicyKey]); const maxmemoryPolicy = config[maxmemoryPolicyKey] as string; try { const batch = new ClusterBatch(isAtomic); batch.configSet({ [maxmemoryPolicyKey]: "allkeys-lfu", }); batch.set(key, "foo"); batch.objectFreq(key); const response = await client.exec(batch, true); expect(response).not.toBeNull(); if (response != null) { expect(response.length).toEqual(3); expect(response[0]).toEqual("OK"); expect(response[1]).toEqual("OK"); expect(response[2]).toBeGreaterThanOrEqual(0); } } finally { expect( await client.configSet({ [maxmemoryPolicyKey]: maxmemoryPolicy, }), ).toEqual("OK"); } client.close(); }, ); it.each([ [ProtocolVersion.RESP2, true], [ProtocolVersion.RESP2, false], [ProtocolVersion.RESP3, true], [ProtocolVersion.RESP3, false], ])( "object idletime batch test with protocol=%p and isAtomic=%p", async (protocol, isAtomic) => { const client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const key = getRandomKey(); const maxmemoryPolicyKey = "maxmemory-policy"; const config = await client.configGet([maxmemoryPolicyKey]); const maxmemoryPolicy = config[maxmemoryPolicyKey] as string; try { const batch = new ClusterBatch(isAtomic); batch.configSet({ // OBJECT IDLETIME requires a non-LFU maxmemory-policy [maxmemoryPolicyKey]: "allkeys-random", }); batch.set(key, "foo"); batch.objectIdletime(key); const response = await client.exec(batch, true); expect(response).not.toBeNull(); if (response != null) { expect(response.length).toEqual(3); // batch.configSet({[maxmemoryPolicyKey]: "allkeys-random"}); expect(response[0]).toEqual("OK"); // batch.set(key, "foo"); expect(response[1]).toEqual("OK"); // batch.objectIdletime(key); expect(response[2]).toBeGreaterThanOrEqual(0); } } finally { expect( await client.configSet({ [maxmemoryPolicyKey]: maxmemoryPolicy, }), ).toEqual("OK"); } client.close(); }, ); it.each([ [ProtocolVersion.RESP2, true], [ProtocolVersion.RESP2, false], [ProtocolVersion.RESP3, true], [ProtocolVersion.RESP3, false], ])( "object refcount batch test with protocol=%p and isAtomic=%p", async (protocol, isAtomic) => { const client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const key = getRandomKey(); const batch = new ClusterBatch(isAtomic); batch.set(key, "foo"); batch.objectRefcount(key); const response = await client.exec(batch, true); expect(response).not.toBeNull(); if (response != null) { expect(response.length).toEqual(2); expect(response[0]).toEqual("OK"); // batch.set(key, "foo"); expect(response[1]).toBeGreaterThanOrEqual(1); // batch.objectRefcount(key); } client.close(); }, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( `lolwut test_%p`, async (protocol) => { client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); // Check for version string in LOLWUT output (dual string contains approach) const serverVersion = cluster.getVersion().trim(); // test with multi-node route const result1 = await client.lolwut({ route: "allNodes" }); const result1Str = intoString(result1); expect( result1Str.includes("ver") && result1Str.includes(serverVersion), ).toBe(true); const result2 = await client.lolwut({ version: 2, parameters: [10, 20], route: "allNodes", }); const result2Str = intoString(result2); expect( result2Str.includes("ver") && result2Str.includes(serverVersion), ).toBe(true); // test with single-node route const result3 = await client.lolwut({ route: "randomNode" }); const result3Str = intoString(result3); expect( result3Str.includes("ver") && result3Str.includes(serverVersion), ).toBe(true); const result4 = await client.lolwut({ version: 2, parameters: [10, 20], route: "randomNode", }); const result4Str = intoString(result4); expect( result4Str.includes("ver") && result4Str.includes(serverVersion), ).toBe(true); // Test LOLWUT version 9 (available in Valkey 9.0.0+) if (cluster.checkIfServerVersionLessThan("9.0.0") === false) { // Test with version 9 and 2 parameters on all nodes const result5 = await client.lolwut({ version: 9, parameters: [30, 4], route: "allNodes", }); const result5Str = intoString(result5); expect( result5Str.includes("ver") && result5Str.includes(serverVersion), ).toBe(true); // Test with version 9 and 4 parameters on random node const result6 = await client.lolwut({ version: 9, parameters: [40, 20, 1, 2], route: "randomNode", }); const result6Str = intoString(result6); expect( result6Str.includes("ver") && result6Str.includes(serverVersion), ).toBe(true); } // batch tests for (const isAtomic of [true, false]) { const batch = new ClusterBatch(isAtomic); batch.lolwut(); batch.lolwut({ version: 5 }); batch.lolwut({ parameters: [1, 2] }); batch.lolwut({ version: 6, parameters: [42] }); const results = await client.exec(batch, true); if (results) { for (const element of results) { const elementStr = intoString(element); expect( elementStr.includes("ver") && elementStr.includes(serverVersion), ).toBe(true); } } else { throw new Error("Invalid LOLWUT batch test results."); } } client.close(); }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "copy test_%p", async (protocol) => { const client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); if (cluster.checkIfServerVersionLessThan("6.2.0")) return; const source = `{key}-${getRandomKey()}`; const destination = `{key}-${getRandomKey()}`; const value1 = getRandomKey(); const value2 = getRandomKey(); // neither key exists expect( await client.copy(source, destination, { replace: true }), ).toEqual(false); expect(await client.copy(Buffer.from(source), destination)).toEqual( false, ); // source exists, destination does not expect(await client.set(source, value1)).toEqual("OK"); expect( await client.copy(source, Buffer.from(destination), { replace: false, }), ).toEqual(true); expect(await client.get(destination)).toEqual(value1); // new value for source key expect(await client.set(source, value2)).toEqual("OK"); // both exists, no REPLACE expect( await client.copy( Buffer.from(source), Buffer.from(destination), ), ).toEqual(false); expect( await client.copy(source, destination, { replace: false }), ).toEqual(false); expect(await client.get(destination)).toEqual(value1); // both exists, with REPLACE expect( await client.copy(source, Buffer.from(destination), { replace: true, }), ).toEqual(true); expect(await client.get(destination)).toEqual(value2); // batch tests for (const isAtomic of [true, false]) { expect(await client.flushall()).toEqual("OK"); const batch = new ClusterBatch(isAtomic); batch.set(source, value1); batch.copy(source, destination, { replace: true }); batch.get(destination); const results = await client.exec(batch, true); expect(results).toEqual(["OK", true, value1]); } client.close(); }, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "migrate test_%p", async (protocol) => { client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const key = getRandomKey(); // Non-existent key returns NOKEY const result = await client.migrate( "localhost", cluster.getAddresses()[0][1], key, 0, 5000, ); expect(result).toEqual("NOKEY"); client.close(); }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "select test %p", async (protocol) => { if (cluster.checkIfServerVersionLessThan("9.0.0")) return; client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); expect(await client.select(0)).toEqual("OK"); const key = getRandomKey(); const value = getRandomKey(); const result = await client.set(key, value); expect(result).toEqual("OK"); expect(await client.select(1)).toEqual("OK"); expect(await client.get(key)).toEqual(null); expect(await client.flushdb()).toEqual("OK"); expect(await client.dbsize()).toEqual(0); expect(await client.select(0)).toEqual("OK"); expect(await client.get(key)).toEqual(value); client.close(); }, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "copy with DB test_%p", async (protocol) => { if (cluster.checkIfServerVersionLessThan("9.0.0")) return; const client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const source = `{key}-${getRandomKey()}`; const destination = `{key}-${getRandomKey()}`; const value1 = getRandomKey(); const value2 = getRandomKey(); const index1 = 1; const index2 = 2; // neither key exists expect( await client.copy(source, destination, { destinationDB: index1, replace: false, }), ).toEqual(false); // source exists, destination does not expect(await client.set(source, value1)).toEqual("OK"); expect( await client.copy(source, destination, { destinationDB: index1, replace: false, }), ).toEqual(true); expect(await client.select(1)).toEqual("OK"); expect(await client.get(destination)).toEqual(value1); // new value for source key expect(await client.select(0)).toEqual("OK"); expect(await client.set(source, value2)).toEqual("OK"); // no REPLACE, copying to existing key on DB 1, non-existing key on DB 2 expect( await client.copy(Buffer.from(source), destination, { destinationDB: index1, replace: false, }), ).toEqual(false); expect( await client.copy(source, Buffer.from(destination), { destinationDB: index2, replace: false, }), ).toEqual(true); // new value only gets copied to DB 2 expect(await client.select(1)).toEqual("OK"); expect(await client.get(destination)).toEqual(value1); expect(await client.customCommand(["SELECT", "2"])).toEqual("OK"); expect(await client.get(destination)).toEqual(value2); // both exists, with REPLACE, when value isn't the same, source always get copied to // destination expect(await client.select(0)).toEqual("OK"); expect( await client.copy( Buffer.from(source), Buffer.from(destination), { destinationDB: index1, replace: true, }, ), ).toEqual(true); expect(await client.select(1)).toEqual("OK"); expect(await client.get(destination)).toEqual(value2); // batch tests for (const isAtomic of [true, false]) { const batch = new ClusterBatch(isAtomic); batch.customCommand(["SELECT", "1"]); batch.set(source, value1); batch.copy(source, destination, { destinationDB: index1, replace: true, }); batch.get(destination); const results = await client.exec(batch, true); expect(results).toEqual(["OK", "OK", true, value1]); } client.close(); }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "flushdb flushall dbsize test_%p", async (protocol) => { const client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); expect(await client.dbsize()).toBeGreaterThanOrEqual(0); expect(await client.set(getRandomKey(), getRandomKey())).toEqual( "OK", ); expect(await client.dbsize()).toBeGreaterThan(0); expect(await client.flushall()).toEqual("OK"); expect(await client.dbsize()).toEqual(0); expect(await client.set(getRandomKey(), getRandomKey())).toEqual( "OK", ); expect(await client.dbsize()).toEqual(1); expect(await client.flushdb({ mode: FlushMode.ASYNC })).toEqual( "OK", ); expect(await client.dbsize()).toEqual(0); expect(await client.set(getRandomKey(), getRandomKey())).toEqual( "OK", ); expect(await client.dbsize()).toEqual(1); expect(await client.flushdb({ mode: FlushMode.SYNC })).toEqual( "OK", ); expect(await client.dbsize()).toEqual(0); client.close(); }, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "sort sortstore sort_store sortro sort_ro sortreadonly test_%p", async (protocol) => { const client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const key1 = "{sort}" + getRandomKey(); const key2 = "{sort}" + getRandomKey(); const key3 = "{sort}" + getRandomKey(); const key4 = "{sort}" + getRandomKey(); const key5 = "{sort}" + getRandomKey(); expect(await client.sort(key3)).toEqual([]); expect(await client.lpush(key1, ["2", "1", "4", "3"])).toEqual(4); expect(await client.sort(Buffer.from(key1))).toEqual([ "1", "2", "3", "4", ]); // test binary decoder expect(await client.sort(key1, { decoder: Decoder.Bytes })).toEqual( [ Buffer.from("1"), Buffer.from("2"), Buffer.from("3"), Buffer.from("4"), ], ); // sort RO if (!cluster.checkIfServerVersionLessThan("7.0.0")) { expect(await client.sortReadOnly(key3)).toEqual([]); expect(await client.sortReadOnly(Buffer.from(key3))).toEqual( [], ); // test binary decoder expect( await client.sortReadOnly(key1, { decoder: Decoder.Bytes }), ).toEqual([ Buffer.from("1"), Buffer.from("2"), Buffer.from("3"), Buffer.from("4"), ]); } // sort with store expect(await client.sortStore(key1, key2)).toEqual(4); expect( await client.sortStore(Buffer.from(key1), Buffer.from(key2)), ).toEqual(4); expect(await client.lrange(key2, 0, -1)).toEqual([ "1", "2", "3", "4", ]); // SORT with strings require ALPHA expect( await client.rpush(key3, ["2", "1", "a", "x", "c", "4", "3"]), ).toEqual(7); await expect(client.sort(key3)).rejects.toThrow(RequestError); expect(await client.sort(key3, { isAlpha: true })).toEqual([ "1", "2", "3", "4", "a", "c", "x", ]); for (const isAtomic of [true, false]) { await client.del([key5, key4]); // check batch and options const batch = new ClusterBatch(isAtomic) .lpush(key4, ["3", "1", "2"]) .sort(key4, { orderBy: SortOrder.DESC, limit: { count: 2, offset: 0 }, }) .sortStore(key4, key5, { orderBy: SortOrder.ASC, limit: { count: 100, offset: 1 }, }) .lrange(key5, 0, -1); if (!cluster.checkIfServerVersionLessThan("7.0.0")) { batch.sortReadOnly(key4, { orderBy: SortOrder.DESC, limit: { count: 2, offset: 0 }, }); } const result = await client.exec(batch, true); const expectedResult = [3, ["3", "2"], 2, ["2", "3"]]; if (!cluster.checkIfServerVersionLessThan("7.0.0")) { expectedResult.push(["3", "2"]); } expect(result).toEqual(expectedResult); } client.close(); }, ); describe.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "Protocol is RESP2 = %s", (protocol) => { describe.each([true, false])( "Single node route = %s", (singleNodeRoute) => { it( "function load function list function stats", async () => { if (cluster.checkIfServerVersionLessThan("7.0.0")) return; const client = await GlideClusterClient.createClient( getClientConfigurationOption( cluster.getAddresses(), protocol, ), ); try { const libName = "mylib1C" + getRandomKey().replaceAll("-", ""); const funcName = "myfunc1c" + getRandomKey().replaceAll("-", ""); const code = generateLuaLibCode( libName, new Map([[funcName, "return args[1]"]]), true, ); const route: Routes = singleNodeRoute ? { type: "primarySlotKey", key: "1" } : "allPrimaries"; let functionList = await client.functionList({ libNamePattern: libName, route: route, }); checkClusterResponse( functionList as object, singleNodeRoute, (value) => expect(value).toEqual([]), ); let functionStats = await client.functionStats({ route: route, }); checkClusterResponse( functionStats as object, singleNodeRoute, (value) => checkFunctionStatsResponse( value as FunctionStatsSingleResponse, [], 0, 0, ), ); // load the library expect(await client.functionLoad(code)).toEqual( libName, ); functionList = await client.functionList({ libNamePattern: libName, route: route, }); let expectedDescription = new Map< string, string | null >([[funcName, null]]); let expectedFlags = new Map([ [funcName, ["no-writes"]], ]); checkClusterResponse( functionList, singleNodeRoute, (value) => checkFunctionListResponse( value as FunctionListResponse, libName, expectedDescription, expectedFlags, ), ); functionStats = await client.functionStats({ route: route, }); checkClusterResponse( functionStats as object, singleNodeRoute, (value) => checkFunctionStatsResponse( value as FunctionStatsSingleResponse, [], 1, 1, ), ); // call functions from that library to confirm that it works let fcall = await client.fcallWithRoute( funcName, ["one", "two"], { route: route }, ); checkClusterResponse( fcall as object, singleNodeRoute, (value) => expect(value).toEqual("one"), ); fcall = await client.fcallReadonlyWithRoute( funcName, ["one", "two"], { route: route }, ); checkClusterResponse( fcall as object, singleNodeRoute, (value) => expect(value).toEqual("one"), ); // re-load library without replace await expect( client.functionLoad(code), ).rejects.toThrow( `Library '${libName}' already exists`, ); // re-load library with replace expect( await client.functionLoad(code, { replace: true, }), ).toEqual(libName); // overwrite lib with new code const func2Name = "myfunc2c" + getRandomKey().replaceAll("-", ""); const newCode = generateLuaLibCode( libName, new Map([ [funcName, "return args[1]"], [func2Name, "return #args"], ]), true, ); expect( await client.functionLoad(newCode, { replace: true, }), ).toEqual(libName); functionList = await client.functionList({ libNamePattern: libName, withCode: true, route: route, }); expectedDescription = new Map< string, string | null >([ [funcName, null], [func2Name, null], ]); expectedFlags = new Map([ [funcName, ["no-writes"]], [func2Name, ["no-writes"]], ]); checkClusterResponse( functionList, singleNodeRoute, (value) => checkFunctionListResponse( value as FunctionListResponse, libName, expectedDescription, expectedFlags, newCode, ), ); functionStats = await client.functionStats({ route: route, }); checkClusterResponse( functionStats as object, singleNodeRoute, (value) => checkFunctionStatsResponse( value as FunctionStatsSingleResponse, [], 1, 2, ), ); fcall = await client.fcallWithRoute( func2Name, ["one", "two"], { route: route }, ); checkClusterResponse( fcall as object, singleNodeRoute, (value) => expect(value).toEqual(2), ); fcall = await client.fcallReadonlyWithRoute( func2Name, ["one", "two"], { route: route }, ); checkClusterResponse( fcall as object, singleNodeRoute, (value) => expect(value).toEqual(2), ); } finally { expect(await client.functionFlush()).toEqual( "OK", ); client.close(); } }, TIMEOUT, ); it( "function flush", async () => { if (cluster.checkIfServerVersionLessThan("7.0.0")) return; const client = await GlideClusterClient.createClient( getClientConfigurationOption( cluster.getAddresses(), protocol, ), ); try { const libName = "mylib1C" + getRandomKey().replaceAll("-", ""); const funcName = "myfunc1c" + getRandomKey().replaceAll("-", ""); const code = generateLuaLibCode( libName, new Map([[funcName, "return args[1]"]]), true, ); const route: Routes = singleNodeRoute ? { type: "primarySlotKey", key: "1" } : "allPrimaries"; const functionList1 = await client.functionList( { route: route }, ); checkClusterResponse( functionList1 as object, singleNodeRoute, (value) => expect(value).toEqual([]), ); // load the library expect( await client.functionLoad(code, { route: route, }), ).toEqual(libName); // flush functions expect( await client.functionFlush({ mode: FlushMode.SYNC, route: route, }), ).toEqual("OK"); expect( await client.functionFlush({ mode: FlushMode.ASYNC, route: route, }), ).toEqual("OK"); const functionList2 = await client.functionList(); checkClusterResponse( functionList2 as object, singleNodeRoute, (value) => expect(value).toEqual([]), ); // Attempt to re-load library without overwriting to ensure FLUSH was effective expect( await client.functionLoad(code, { route: route, }), ).toEqual(libName); } finally { expect(await client.functionFlush()).toEqual( "OK", ); client.close(); } }, TIMEOUT, ); it( "function delete", async () => { if (cluster.checkIfServerVersionLessThan("7.0.0")) return; const client = await GlideClusterClient.createClient( getClientConfigurationOption( cluster.getAddresses(), protocol, ), ); try { const libName = "mylib1C" + getRandomKey().replaceAll("-", ""); const funcName = "myfunc1c" + getRandomKey().replaceAll("-", ""); const code = generateLuaLibCode( libName, new Map([[funcName, "return args[1]"]]), true, ); const route: Routes = singleNodeRoute ? { type: "primarySlotKey", key: "1" } : "allPrimaries"; let functionList = await client.functionList({ route: route, }); checkClusterResponse( functionList as object, singleNodeRoute, (value) => expect(value).toEqual([]), ); // load the library expect( await client.functionLoad(code, { route: route, }), ).toEqual(libName); // Delete the function expect( await client.functionDelete(libName, { route, }), ).toEqual("OK"); functionList = await client.functionList({ libNamePattern: libName, withCode: true, route: route, }); checkClusterResponse( functionList as object, singleNodeRoute, (value) => expect(value).toEqual([]), ); // Delete a non-existing library await expect( client.functionDelete(libName, { route }), ).rejects.toThrow(`Library not found`); } finally { expect(await client.functionFlush()).toEqual( "OK", ); client.close(); } }, TIMEOUT, ); it( "function kill with route", async () => { if (cluster.checkIfServerVersionLessThan("7.0.0")) return; const config = getClientConfigurationOption( cluster.getAddresses(), protocol, { requestTimeout: 10000 }, ); const client = await GlideClusterClient.createClient(config); const testClient = await GlideClusterClient.createClient(config); try { const libName = "function_kill_no_write_with_route_" + singleNodeRoute; const funcName = "deadlock_with_route_" + singleNodeRoute; const code = createLuaLibWithLongRunningFunction( libName, funcName, 6, true, ); const route: Routes = singleNodeRoute ? { type: "primarySlotKey", key: "1" } : "allPrimaries"; expect(await client.functionFlush()).toEqual( "OK", ); // nothing to kill await expect( client.functionKill({ route }), ).rejects.toThrow(/notbusy/i); // load the lib expect( await client.functionLoad(code, { replace: true, route: route, }), ).toEqual(libName); try { // call the function without await const promise = testClient .fcallWithRoute(funcName, [], { route: route, }) .catch((e) => expect( (e as Error).message, ).toContain("Script killed"), ); let killed = false; let timeout = 4000; await new Promise((resolve) => setTimeout(resolve, 1000), ); while (timeout >= 0) { try { expect( await client.functionKill({ route, }), ).toEqual("OK"); killed = true; break; } catch { // do nothing } await new Promise((resolve) => setTimeout(resolve, 500), ); timeout -= 500; } expect(killed).toBeTruthy(); await promise; } finally { await waitForNotBusy(client); } } finally { expect(await client.functionFlush()).toEqual( "OK", ); client.close(); testClient.close(); } }, TIMEOUT, ); it("function dump function restore", async () => { if (cluster.checkIfServerVersionLessThan("7.0.0")) return; const config = getClientConfigurationOption( cluster.getAddresses(), protocol, ); const client = await GlideClusterClient.createClient(config); const route: Routes = singleNodeRoute ? { type: "primarySlotKey", key: "1" } : "allPrimaries"; expect( await client.functionFlush({ mode: FlushMode.SYNC, route: route, }), ).toEqual("OK"); try { // dumping an empty lib let response = await client.functionDump({ route }); if (singleNodeRoute) { expect(response.byteLength).toBeGreaterThan(0); } else { Object.values(response).forEach((d: Buffer) => expect(d.byteLength).toBeGreaterThan(0), ); } const name1 = "Foster"; const name2 = "Dogster"; // function $name1 returns first argument // function $name2 returns argument array len let code = generateLuaLibCode( name1, new Map([ [name1, "return args[1]"], [name2, "return #args"], ]), false, ); expect( await client.functionLoad(code, { route: route, }), ).toEqual(name1); const flist = await client.functionList({ withCode: true, route: route, }); response = await client.functionDump({ route }); const dump = ( singleNodeRoute ? response : Object.values(response)[0] ) as Buffer; // restore without cleaning the lib and/or overwrite option causes an error await expect( client.functionRestore(dump, { route: route }), ).rejects.toThrow( `Library ${name1} already exists`, ); // APPEND policy also fails for the same reason (name collision) await expect( client.functionRestore(dump, { policy: FunctionRestorePolicy.APPEND, route: route, }), ).rejects.toThrow( `Library ${name1} already exists`, ); // REPLACE policy succeeds expect( await client.functionRestore(dump, { policy: FunctionRestorePolicy.REPLACE, route: route, }), ).toEqual("OK"); // but nothing changed - all code overwritten expect( await client.functionList({ withCode: true, route: route, }), ).toEqual(flist); // create lib with another name, but with the same function names expect( await client.functionFlush({ mode: FlushMode.SYNC, route: route, }), ).toEqual("OK"); code = generateLuaLibCode( name2, new Map([ [name1, "return args[1]"], [name2, "return #args"], ]), false, ); expect( await client.functionLoad(code, { route: route, }), ).toEqual(name2); // REPLACE policy now fails due to a name collision await expect( client.functionRestore(dump, { route: route }), ).rejects.toThrow( new RegExp( `Function ${name1}|${name2} already exists`, ), ); // FLUSH policy succeeds, but deletes the second lib expect( await client.functionRestore(dump, { policy: FunctionRestorePolicy.FLUSH, route: route, }), ).toEqual("OK"); expect( await client.functionList({ withCode: true, route: route, }), ).toEqual(flist); // call restored functions let res = await client.fcallWithRoute( name1, ["meow", "woem"], { route: route }, ); if (singleNodeRoute) { expect(res).toEqual("meow"); } else { Object.values( res as Record, ).forEach((r) => expect(r).toEqual("meow")); } res = await client.fcallWithRoute( name2, ["meow", "woem"], { route: route }, ); if (singleNodeRoute) { expect(res).toEqual(2); } else { Object.values( res as Record, ).forEach((r) => expect(r).toEqual(2)); } } finally { expect(await client.functionFlush()).toEqual("OK"); client.close(); } }); it( "invoke script with route invokeScriptWithRoute %p", async () => { const client = await GlideClusterClient.createClient( getClientConfigurationOption( cluster.getAddresses(), protocol, ), ); const route: Routes = singleNodeRoute ? { type: "primarySlotKey", key: "1" } : "allPrimaries"; const script = new Script( Buffer.from("return {ARGV[1]}"), ); try { const arg = getRandomKey(); let res = await client.invokeScriptWithRoute( script, { args: [Buffer.from(arg)], route }, ); if (singleNodeRoute) { expect(res).toEqual([arg]); } else { Object.values( res as Record, ).forEach((value) => expect(value).toEqual([arg]), ); } res = await client.invokeScriptWithRoute( script, { args: [arg], route, decoder: Decoder.Bytes, }, ); if (singleNodeRoute) { expect(res).toEqual([Buffer.from(arg)]); } else { Object.values( res as Record, ).forEach((value) => expect(value).toEqual([ Buffer.from(arg), ]), ); } } finally { script.release(); client.close(); } }, TIMEOUT, ); }, ); it( "function kill key based write function", async () => { if (cluster.checkIfServerVersionLessThan("7.0.0")) return; const config = getClientConfigurationOption( cluster.getAddresses(), protocol, { requestTimeout: 10000 }, ); const client = await GlideClusterClient.createClient(config); const testClient = await GlideClusterClient.createClient(config); try { const libName = "function_kill_key_based_write_function"; const funcName = "deadlock_write_function_with_key_based_route"; const key = libName; const code = createLuaLibWithLongRunningFunction( libName, funcName, 6, false, ); const route: Routes = { type: "primarySlotKey", key: key, }; expect(await client.functionFlush()).toEqual("OK"); // nothing to kill await expect( client.functionKill({ route }), ).rejects.toThrow(/notbusy/i); // load the lib expect( await client.functionLoad(code, { replace: true, route: route, }), ).toEqual(libName); let promise = null; try { // call the function without await promise = testClient.fcall(funcName, [key], []); let foundUnkillable = false; let timeout = 4000; await new Promise((resolve) => setTimeout(resolve, 1000), ); while (timeout >= 0) { try { // valkey kills a function with 5 sec delay // but this will always throw an error in the test await client.functionKill({ route }); } catch (err) { // looking for an error with "unkillable" in the message // at that point we can break the loop if ( (err as Error).message .toLowerCase() .includes("unkillable") ) { foundUnkillable = true; break; } } await new Promise((resolve) => setTimeout(resolve, 500), ); timeout -= 500; } expect(foundUnkillable).toBeTruthy(); } finally { // If function wasn't killed, and it didn't time out - it blocks the server and cause rest // test to fail. Wait for the function to complete (we cannot kill it) expect(await promise).toContain("Timed out"); } } finally { expect(await client.functionFlush()).toEqual("OK"); client.close(); testClient.close(); } }, TIMEOUT, ); it("function dump function restore in transaction", async () => { if (cluster.checkIfServerVersionLessThan("7.0.0")) return; const config = getClientConfigurationOption( cluster.getAddresses(), protocol, ); const client = await GlideClusterClient.createClient(config); const route: SlotKeyTypes = { key: getRandomKey(), type: "primarySlotKey", }; expect(await client.functionFlush()).toEqual("OK"); try { const name1 = "Foster"; const name2 = "Dogster"; // function returns first argument const code = generateLuaLibCode( name1, new Map([[name2, "return args[1]"]]), false, ); expect( await client.functionLoad(code, { replace: true, route: route, }), ).toEqual(name1); // Verify functionDump for (const isAtomic of [true, false]) { const batch = new ClusterBatch(isAtomic).functionDump(); const result = await client.exec(batch, true, { decoder: Decoder.Bytes, route: route, }); if (!result) { throw new Error( "Batch execution failed: result is null", ); } const data = result[0] as Buffer; // Verify functionRestore const restoreBatch = new ClusterBatch(isAtomic) .functionRestore( data, FunctionRestorePolicy.REPLACE, ) .fcall(name2, [], ["meow"]); expect( await client.exec(restoreBatch, true, { route: route, }), ).toEqual(["OK", "meow"]); } } finally { expect(await client.functionFlush()).toEqual("OK"); client.close(); } }); }, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( `randomKey test_%p`, async (protocol) => { client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const key = getRandomKey(); // setup: delete all keys expect(await client.flushall({ mode: FlushMode.SYNC })).toEqual( "OK", ); // no keys exist so randomKey returns null expect(await client.randomKey()).toBeNull(); expect(await client.set(key, "foo")).toEqual("OK"); // `key` should be the only existing key, so randomKey should return `key` expect(await client.randomKey({ decoder: Decoder.Bytes })).toEqual( Buffer.from(key), ); expect(await client.randomKey({ route: "allPrimaries" })).toEqual( key, ); client.close(); }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "watch test_%p", async (protocol) => { const client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const key1 = "{key}-1" + getRandomKey(); const key2 = "{key}-2" + getRandomKey(); const key3 = "{key}-3" + getRandomKey(); const key4 = "{key}-4" + getRandomKey(); const setFoobarTransaction = new ClusterBatch(true); const setHelloTransaction = new ClusterBatch(true); // Returns null when a watched key is modified before it is executed in a transaction command. // Transaction commands are not performed. expect(await client.watch([key1, key2, key3])).toEqual("OK"); expect(await client.set(key2, "hello")).toEqual("OK"); setFoobarTransaction .set(key1, "foobar") .set(key2, "foobar") .set(key3, "foobar"); let results = await client.exec(setFoobarTransaction, true); expect(results).toEqual(null); // sanity check expect(await client.get(key1)).toEqual(null); expect(await client.get(key2)).toEqual("hello"); expect(await client.get(key3)).toEqual(null); // Transaction executes command successfully with a read command on the watch key before // transaction is executed. expect(await client.watch([key1, key2, Buffer.from(key3)])).toEqual( "OK", ); expect(await client.get(key2)).toEqual("hello"); results = await client.exec(setFoobarTransaction, true); expect(results).toEqual(["OK", "OK", "OK"]); // sanity check expect(await client.get(key1)).toEqual("foobar"); expect(await client.get(key2)).toEqual("foobar"); expect(await client.get(key3)).toEqual("foobar"); // Transaction executes command successfully with unmodified watched keys expect(await client.watch([key1, key2, key3])).toEqual("OK"); results = await client.exec(setFoobarTransaction, true); expect(results).toEqual(["OK", "OK", "OK"]); // sanity check expect(await client.get(key1)).toEqual("foobar"); expect(await client.get(key2)).toEqual("foobar"); expect(await client.get(key3)).toEqual("foobar"); // Transaction executes command successfully with a modified watched key but is not in the // transaction. expect(await client.watch([key4])).toEqual("OK"); setHelloTransaction .set(key1, "hello") .set(key2, "hello") .set(key3, "hello"); results = await client.exec(setHelloTransaction, true); expect(results).toEqual(["OK", "OK", "OK"]); // sanity check expect(await client.get(key1)).toEqual("hello"); expect(await client.get(key2)).toEqual("hello"); expect(await client.get(key3)).toEqual("hello"); // WATCH can not have an empty String array parameter await expect(client.watch([])).rejects.toThrow(RequestError); client.close(); }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "unwatch test_%p", async (protocol) => { const client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const key1 = "{key}-1" + getRandomKey(); const key2 = "{key}-2" + getRandomKey(); const setFoobarTransaction = new ClusterBatch(true); // UNWATCH returns OK when there no watched keys expect(await client.unwatch()).toEqual("OK"); // Transaction executes successfully after modifying a watched key then calling UNWATCH expect(await client.watch([key1, key2])).toEqual("OK"); expect(await client.set(key2, "hello")).toEqual("OK"); expect(await client.unwatch()).toEqual("OK"); expect(await client.unwatch({ route: "allPrimaries" })).toEqual( "OK", ); setFoobarTransaction.set(key1, "foobar").set(key2, "foobar"); const results = await client.exec(setFoobarTransaction, true); expect(results).toEqual(["OK", "OK"]); // sanity check expect(await client.get(key1)).toEqual("foobar"); expect(await client.get(key2)).toEqual("foobar"); client.close(); }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "script kill unkillable test_%p", async (protocol) => { const config = getClientConfigurationOption( cluster.getAddresses(), protocol, { requestTimeout: 10000 }, ); const client1 = await GlideClusterClient.createClient(config); const client2 = await GlideClusterClient.createClient(config); // Verify that script kill raises an error when no script is running await expect(client1.scriptKill()).rejects.toThrow( "No scripts in execution right now", ); // Create a long-running script const longScript = new Script(createLongRunningLuaScript(5, true)); let promise = null; try { // call the script without await promise = client2.invokeScript(longScript, { keys: ["{key}-" + getRandomKey()], }); let foundUnkillable = false; let timeout = 4000; await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait until the script starts running while (timeout >= 0) { try { await client1.ping(); } catch (err) { if ( (err as Error).message .toLowerCase() .includes("valkey is busy running a script") ) { break; } if ( timeout <= 2000 && (err as Error).message .toLowerCase() .includes("no scripts in execution right now") ) { promise = client2.invokeScript(longScript, { keys: ["{key}-" + getRandomKey()], }); await new Promise((resolve) => setTimeout(resolve, 1000), ); } } timeout -= 500; } timeout = 4000; while (timeout >= 0) { try { // keep trying to kill until we get an "OK" await client1.scriptKill(); } catch (err) { // a RequestError may occur if the script is not yet running // sleep and try again if ( (err as Error).message .toLowerCase() .includes("unkillable") ) { foundUnkillable = true; break; } if ( timeout <= 2000 && (err as Error).message .toLowerCase() .includes("no scripts in execution right now") ) { promise = client2.invokeScript(longScript, { keys: ["{key}-" + getRandomKey()], }); await new Promise((resolve) => setTimeout(resolve, 2000), ); } } await new Promise((resolve) => setTimeout(resolve, 500)); timeout -= 500; } expect(foundUnkillable).toBeTruthy(); } finally { // If script wasn't killed, and it didn't time out - it blocks the server and cause the // test to fail. Wait for the script to complete (we cannot kill it) longScript.release(); expect(await promise).toContain("Timed out"); client1.close(); client2.close(); } }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "should handle connection timeout when client is blocked by long-running command (protocol: %p)", async (protocol) => { // Create a client configuration with a generous request timeout const config = getClientConfigurationOption( cluster.getAddresses(), protocol, { requestTimeout: 20000 }, // Long timeout to allow debugging operations (sleep for 7 seconds) ); // Initialize the primary client const client = await GlideClusterClient.createClient(config); try { // Run a long-running DEBUG SLEEP command using the first client (client) const debugCommandPromise = client.customCommand( ["DEBUG", "sleep", "7"], { route: "allNodes" }, // Sleep for 7 seconds ); // Function that tries to create a client with a short connection timeout (100ms) const failToCreateClient = async () => { await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait for 1 second before retry await expect( GlideClusterClient.createClient({ advancedConfiguration: { connectionTimeout: 100 }, // 100ms connection timeout ...config, // Include the rest of the config }), ).rejects.toThrow(/timed?\s*out/i); // Ensure it throws a timeout error }; // Function that verifies that a larger connection timeout allows connection const connectWithLargeTimeout = async () => { await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait for 1 second before retry const longerTimeoutClient = await GlideClusterClient.createClient({ advancedConfiguration: { connectionTimeout: 10000 }, // 10s connection timeout ...config, // Include the rest of the config }); expect(await client.set("x", "y")).toEqual("OK"); longerTimeoutClient.close(); // Close the client after successful connection }; // Run both the long-running DEBUG SLEEP command and the client creation attempt in parallel await Promise.all([ debugCommandPromise, // Run the long-running command failToCreateClient(), // Attempt to create the client with a short timeout ]); // Run all tasks: fail short timeout, succeed with large timeout, and run the debug command await Promise.all([ debugCommandPromise, // Run the long-running command connectWithLargeTimeout(), // Verify a long timeout (10s) allows successful connection ]); } finally { // Clean up the test client and ensure everything is flushed and closed client.close(); } }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "should respect connection timeout duration (protocol: %p)", async (protocol) => { // Create a client configuration const config = getClientConfigurationOption( cluster.getAddresses(), protocol, { requestTimeout: 20000 }, ); // Initialize the primary client to block the server const client = await GlideClusterClient.createClient(config); try { // Run a long-running DEBUG SLEEP command on all nodes (7 seconds) const debugCommandPromise = client.customCommand( ["DEBUG", "sleep", "7"], { route: "allNodes" }, ); // Wait a bit for the sleep to start await new Promise((resolve) => setTimeout(resolve, 500)); // Try to connect with 3000ms timeout - should fail and take ~3 seconds const startTime = Date.now(); await expect( GlideClusterClient.createClient({ advancedConfiguration: { connectionTimeout: 3000 }, ...config, }), ).rejects.toThrow(/timed?\s*out/i); const elapsed = Date.now() - startTime; // Verify the timeout was respected (should be around 3000ms, allow some buffer) expect(elapsed).toBeGreaterThanOrEqual(2500); // At least 2.5s expect(elapsed).toBeLessThan(6000); // Less than 6s (well before the 7s sleep ends) // Wait for the debug command to complete await debugCommandPromise; } finally { client.close(); } }, TIMEOUT, ); it.each([ [ProtocolVersion.RESP2, 5], [ProtocolVersion.RESP2, 100], [ProtocolVersion.RESP2, 1500], [ProtocolVersion.RESP3, 5], [ProtocolVersion.RESP3, 100], [ProtocolVersion.RESP3, 1500], ])( "test inflight requests limit of %p with protocol %p", async (protocol, inflightRequestsLimit) => { const config = getClientConfigurationOption( cluster.getAddresses(), protocol, { inflightRequestsLimit }, ); const client = await GlideClusterClient.createClient(config); try { const key1 = `{nonexistinglist}:1-${getRandomKey()}`; const tasks: Promise<[GlideString, GlideString] | null>[] = []; // Start inflightRequestsLimit blocking tasks for (let i = 0; i < inflightRequestsLimit; i++) { tasks.push(client.blpop([key1], 0)); } // This task should immediately fail due to reaching the limit await expect(client.blpop([key1], 0)).rejects.toThrow( RequestError, ); // Verify that all previous tasks are still pending const timeoutPromise = new Promise((resolve) => setTimeout(resolve, 100), ); const allTasksStatus = await Promise.race([ Promise.any( tasks.map((task) => task.then(() => "resolved")), ), timeoutPromise.then(() => "pending"), ]); expect(allTasksStatus).toBe("pending"); } finally { await client.close(); } }, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "should return valid statistics using protocol %p", async (protocol) => { let glideClientForTesting; try { // Create a GlideClusterClient instance for testing glideClientForTesting = await GlideClusterClient.createClient( getClientConfigurationOption( cluster.getAddresses(), protocol, { requestTimeout: 2000, }, ), ); // Fetch statistics using get_statistics method const stats = glideClientForTesting.getStatistics(); // Assertions to check if stats object has correct structure expect(typeof stats).toBe("object"); expect(stats).toHaveProperty("total_connections"); expect(stats).toHaveProperty("total_clients"); expect(stats).toHaveProperty("total_values_compressed"); expect(stats).toHaveProperty("total_values_decompressed"); expect(stats).toHaveProperty("total_original_bytes"); expect(stats).toHaveProperty("total_bytes_compressed"); expect(stats).toHaveProperty("total_bytes_decompressed"); expect(stats).toHaveProperty("compression_skipped_count"); expect(stats).toHaveProperty("subscription_out_of_sync_count"); expect(stats).toHaveProperty( "subscription_last_sync_timestamp", ); expect(Object.keys(stats)).toHaveLength(10); } finally { // Ensure the client is properly closed glideClientForTesting?.close(); } }, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "should handle crosslot pipeline using protocol %p", async (protocol) => { const client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol, { requestTimeout: 2000, }), ); try { const pipeline = new ClusterBatch(false); // This are known keys for crosslot pipeline.set("abc", "value"); pipeline.get("xyz"); const results = await client.exec(pipeline, true); expect(results).toEqual(["OK", null]); } finally { client.close(); } }, ); it.each([ [ProtocolVersion.RESP2, true], [ProtocolVersion.RESP2, false], [ProtocolVersion.RESP3, true], [ProtocolVersion.RESP3, false], ])( "should handle route batch using protocol %p and isAtomic=%p", async (protocol, isAtomic) => { const client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol, { requestTimeout: 2000, }), ); try { expect(await client.configResetStat()).toEqual("OK"); const key = getRandomKey(); const pipeline = new ClusterBatch(isAtomic); pipeline.set(key, "value"); pipeline.get(key); const results = await client.exec(pipeline, true, { route: { type: "primarySlotKey", key: key }, }); expect(results).toEqual(["OK", "value"]); // Check that no MOVED error occurred const errorStats = (await client.info({ sections: [InfoOptions.Errorstats], route: "allNodes", })) as Record; for (const infoStr of Object.values(errorStats)) { expect(infoStr).toBe("# Errorstats\r\n"); } } finally { client.close(); } }, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "batch with retry configurations using protocol %p", async (protocol) => { const client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol, { requestTimeout: 2000, }), ); try { expect(await client.configResetStat()).toEqual("OK"); const key = getRandomKey(); const transaction = new ClusterBatch(true); transaction.set(key, "value"); transaction.get(key); await expect( client.exec(transaction, true, { retryStrategy: { retryConnectionError: true, retryServerError: true, }, }), ).rejects.toThrow( "Retry strategy is not supported for atomic batches.", ); const pipeline = new ClusterBatch(false); pipeline.set(key, "value"); pipeline.get(key); expect( await client.exec(pipeline, true, { retryStrategy: { retryConnectionError: true, retryServerError: true, }, }), ).toEqual(["OK", "value"]); } finally { client.close(); } }, ); describe("AZAffinity Read Strategy Tests", () => { it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "should route GET commands to all replicas with the same AZ using protocol %p", async (protocol) => { // Skip test if version is below 8.0.0 if (cluster.checkIfServerVersionLessThan("8.0.0")) return; const az = "us-east-1a"; let client_for_config_set; let client_for_testing_az; try { // Stage 1: Configure nodes client_for_config_set = await GlideClusterClient.createClient( getClientConfigurationOption( azCluster.getAddresses(), protocol, ), ); await client_for_config_set.configResetStat(); await client_for_config_set.configSet( { "availability-zone": az }, { route: "allNodes" }, ); // Stage 2: Create AZ affinity client and verify configuration client_for_testing_az = await GlideClusterClient.createClient( getClientConfigurationOption( azCluster.getAddresses(), protocol, { readFrom: "AZAffinity", clientAz: az, }, ), ); const azs = (await client_for_testing_az.configGet( ["availability-zone"], { route: "allNodes" }, )) as Record>; Object.values(azs).forEach((nodeResponse) => expect(nodeResponse["availability-zone"]).toEqual( "us-east-1a", ), ); const get_calls_per_replica = 25; const get_calls = 100; const get_cmdstat = `cmdstat_get:calls=${get_calls_per_replica}`; // Stage 3: Set test data and perform GET operations await client_for_testing_az.set("foo", "testvalue"); for (let i = 0; i < get_calls; i++) { await client_for_testing_az.get("foo"); } // Stage 4: Verify GET commands were routed correctly const info_result = (await client_for_testing_az.info({ sections: [InfoOptions.All], route: "allNodes", })) as Record; const matching_entries_count = Object.values( info_result, ).filter((infoStr) => { // Check if this is a replica node AND it has the expected number of GET calls const isReplicaNode = infoStr.includes("role:slave") || infoStr.includes("role:replica"); return isReplicaNode && infoStr.includes(get_cmdstat); }).length; expect(matching_entries_count).toBe(4); } finally { // Cleanup await client_for_config_set?.configSet( { "availability-zone": "" }, { route: "allNodes" }, ); client_for_config_set?.close(); client_for_testing_az?.close(); } }, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "should route commands to single replica with AZ using protocol %p", async (protocol) => { // Skip test if version is below 8.0.0 if (cluster.checkIfServerVersionLessThan("8.0.0")) return; const az = "us-east-1a"; const get_calls = 3; const get_cmdstat = `cmdstat_get:calls=${get_calls}`; let client_for_config_set; let client_for_testing_az; try { // Stage 1: Configure nodes client_for_config_set = await GlideClusterClient.createClient( getClientConfigurationOption( azCluster.getAddresses(), protocol, ), ); await client_for_config_set.configSet( { "availability-zone": "" }, { route: "allNodes" }, ); await client_for_config_set.configResetStat(); await client_for_config_set.configSet( { "availability-zone": az }, { route: { type: "replicaSlotKey", key: "foo" } }, ); // Stage 2: Create AZ affinity client and verify configuration client_for_testing_az = await GlideClusterClient.createClient( getClientConfigurationOption( azCluster.getAddresses(), protocol, { readFrom: "AZAffinity", clientAz: az, }, ), ); for (let i = 0; i < get_calls; i++) { await client_for_testing_az.get("foo"); } // Stage 4: Verify GET commands were routed correctly const info_result = (await client_for_testing_az.info({ sections: [InfoOptions.All], route: "allNodes", })) as Record; // Process the info_result to check that only one replica has the GET calls const matching_entries_count = Object.values( info_result, ).filter((infoStr) => { return ( infoStr.includes(get_cmdstat) && infoStr.includes(`availability_zone:${az}`) ); }).length; expect(matching_entries_count).toBe(1); // Check that only one node has the availability zone set to az const changed_az_count = Object.values(info_result).filter( (infoStr) => { return infoStr.includes(`availability_zone:${az}`); }, ).length; expect(changed_az_count).toBe(1); } finally { await client_for_config_set?.configSet( { "availability-zone": "" }, { route: "allNodes" }, ); client_for_config_set?.close(); client_for_testing_az?.close(); } }, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "should route commands to a replica when AZ does not exist using protocol %p", async (protocol) => { // Skip test if version is below 8.0.0 if (cluster.checkIfServerVersionLessThan("8.0.0")) return; const get_calls = 4; const replica_calls = 1; const get_cmdstat = `cmdstat_get:calls=${replica_calls}`; let client_for_testing_az; try { // Create a client configured for AZAffinity with a non-existing AZ client_for_testing_az = await GlideClusterClient.createClient( getClientConfigurationOption( azCluster.getAddresses(), protocol, { readFrom: "AZAffinity", clientAz: "non-existing-az", }, ), ); // Reset command stats on all nodes await client_for_testing_az.configResetStat({ route: "allNodes", }); // Issue GET commands for (let i = 0; i < get_calls; i++) { await client_for_testing_az.get("foo"); } // Fetch command stats from all nodes const info_result = (await client_for_testing_az.info({ sections: [InfoOptions.Commandstats], route: "allNodes", })) as Record; // Inline matching logic const matchingEntriesCount = Object.values( info_result, ).filter((nodeResponses) => { return nodeResponses.includes(get_cmdstat); }).length; // Validate that the get calls were distributed across replicas, each replica recieved 1 get call expect(matchingEntriesCount).toBe(4); } finally { // Cleanup: Close the client after test execution client_for_testing_az?.close(); } }, ); }); describe("AZAffinityReplicasAndPrimary Read Strategy Tests", () => { it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "should route GET commands to primary with the same AZ using protocol %p", async (protocol) => { // Skip test if version is below 8.0.0 if (cluster.checkIfServerVersionLessThan("8.0.0")) return; const az = "us-east-1a"; const other_az = "us-east-1b"; const get_calls = 4; let client_for_config_set; let client_for_testing_az; try { // Stage 1: Configure nodes client_for_config_set = await GlideClusterClient.createClient( getClientConfigurationOption( azCluster.getAddresses(), protocol, { requestTimeout: 3000 }, ), ); // Set all nodes for us-east-1b await client_for_config_set.configResetStat(); await client_for_config_set.configSet( { "availability-zone": other_az }, { route: "allNodes" }, ); // Set AZ for one primary (the last one) to match the client's AZ await client_for_config_set.configSet( { "availability-zone": az }, { route: { type: "primarySlotId", id: 12182 } }, ); // Create client and verify configuration client_for_testing_az = await GlideClusterClient.createClient( getClientConfigurationOption( azCluster.getAddresses(), protocol, { requestTimeout: 3000, readFrom: "AZAffinityReplicasAndPrimary", clientAz: az, }, ), ); // Stage 3: Set test data and perform GET operations const key = "foo_{12182}"; // Key targets slot 12182 await client_for_testing_az.set(key, "testvalue"); for (let i = 0; i < get_calls; i++) { await client_for_testing_az.get(key); } // Stage 4: Verify GET commands were routed correctly const info_result = (await client_for_testing_az.info({ sections: [InfoOptions.All], route: "allNodes", })) as Record; let matching_entries_count = 0; let total_get_calls = 0; Object.entries(info_result).forEach(([nodeId, infoStr]) => { const isPrimaryNode = infoStr.includes("role:master") || infoStr.includes("role:primary"); // 1. Use the correct regex pattern const azMatch = infoStr.match( /availability_zone:(\S+)/, ); const nodeAZ = azMatch ? azMatch[1] : "unknown"; const isInClientAZ = nodeAZ === az; const getCalls = parseInt( (infoStr.match(/cmdstat_get:calls=(\d+)/) || [])[1] || "0", ); total_get_calls += getCalls; if ( isPrimaryNode && getCalls === get_calls && isInClientAZ ) { matching_entries_count++; } else if (!isInClientAZ && getCalls > 0) { throw new Error( `WARNING: GET calls to node not in client AZ: ${nodeId}`, ); } }); expect(matching_entries_count).toBe(1); // We expect only one primary in the client's AZ to have all the calls expect(total_get_calls).toBe(get_calls); // We expect the total number of GET calls to match our input } finally { client_for_config_set?.close(); client_for_testing_az?.close(); } }, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "should route GET commands to all nodes (primary and replicas) with allNodes strategy using protocol %p", async (protocol) => { if (cluster.checkIfServerVersionLessThan("8.0.0")) return; let client; try { client = await GlideClusterClient.createClient( getClientConfigurationOption( cluster.getAddresses(), protocol, { readFrom: "allNodes" }, ), ); await client.configResetStat({ route: "allNodes" }); const get_calls = 200; for (let i = 0; i < get_calls; i++) { await client.get("foo"); } const info_result = (await client.info({ sections: [InfoOptions.All], route: "allNodes", })) as Record; const nodes_with_gets = Object.values(info_result).filter( (info) => info.includes("cmdstat_get:calls="), ).length; expect(nodes_with_gets).toBeGreaterThan(1); const primary_received_gets = Object.values( info_result, ).some( (info) => (info.includes("role:master") || info.includes("role:primary")) && info.includes("cmdstat_get:calls="), ); expect(primary_received_gets).toBe(true); const replica_received_gets = Object.values( info_result, ).some( (info) => info.includes("role:slave") && info.includes("cmdstat_get:calls="), ); expect(replica_received_gets).toBe(true); // Verify total GET calls const total_get_calls = Object.values(info_result) .filter((info) => info.includes("cmdstat_get:calls=")) .reduce((sum, info) => { const match = info.match(/cmdstat_get:calls=(\d+)/); return sum + (match ? parseInt(match[1]) : 0); }, 0); expect(total_get_calls).toBe(get_calls); } finally { client?.close(); } }, ); }); describe("AZAffinityAllNodes Read Strategy Tests", () => { it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "should split GET commands evenly between primary and replica in the same AZ using protocol %p", async (protocol) => { // Skip test if version is below 8.0.0 if (cluster.checkIfServerVersionLessThan("8.0.0")) return; const az = "us-east-1a"; const other_az = "us-east-1b"; const get_calls = 4; const nodes_in_same_az = 2; // one primary + one replica const per_node_get_calls = get_calls / nodes_in_same_az; const per_node_cmdstat = `cmdstat_get:calls=${per_node_get_calls}`; let client_for_config_set; let client_for_testing_az; try { // Stage 1: Configure nodes. client_for_config_set = await GlideClusterClient.createClient( getClientConfigurationOption( azCluster.getAddresses(), protocol, { requestTimeout: 3000 }, ), ); // Reset stats and place every node in the other AZ first. await client_for_config_set.configResetStat({ route: "allNodes", }); await client_for_config_set.configSet( { "availability-zone": other_az }, { route: "allNodes" }, ); // Move the primary and one replica of slot 12182 ("foo") into the client's AZ. await client_for_config_set.configSet( { "availability-zone": az }, { route: { type: "primarySlotId", id: 12182 } }, ); await client_for_config_set.configSet( { "availability-zone": az }, { route: { type: "replicaSlotId", id: 12182 } }, ); // Stage 2: Create the AZ affinity client AFTER configuration so it // discovers the AZs on connect. client_for_testing_az = await GlideClusterClient.createClient( getClientConfigurationOption( azCluster.getAddresses(), protocol, { requestTimeout: 3000, readFrom: "AZAffinityAllNodes", clientAz: az, }, ), ); // Stage 3: Perform GET operations against slot 12182. const key = "foo_{12182}"; // Key targets slot 12182 for (let i = 0; i < get_calls; i++) { await client_for_testing_az.get(key); } // Stage 4: Verify GET commands were split evenly across the in-AZ // primary and replica, and never landed on out-of-AZ nodes. const info_result = (await client_for_testing_az.info({ sections: [InfoOptions.All], route: "allNodes", })) as Record; let matching_entries_count = 0; let total_get_calls = 0; Object.entries(info_result).forEach(([nodeId, infoStr]) => { if (!infoStr.includes("cmdstat_get:calls=")) return; const azMatch = infoStr.match( /availability_zone:(\S+)/, ); const nodeAZ = azMatch ? azMatch[1] : "unknown"; const isInClientAZ = nodeAZ === az; const getCalls = parseInt( (infoStr.match(/cmdstat_get:calls=(\d+)/) || [])[1] || "0", ); total_get_calls += getCalls; if ( isInClientAZ && infoStr.includes(per_node_cmdstat) ) { matching_entries_count++; } else if (!isInClientAZ && getCalls > 0) { throw new Error( `WARNING: GET calls to node not in client AZ: ${nodeId}`, ); } }); // The in-AZ primary and replica should evenly split the GET calls. expect(matching_entries_count).toBe(nodes_in_same_az); expect(total_get_calls).toBe(get_calls); } finally { // Cleanup await client_for_config_set?.configSet( { "availability-zone": "" }, { route: "allNodes" }, ); client_for_config_set?.close(); client_for_testing_az?.close(); } }, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "should fall back to all nodes when no node is in the client's AZ using protocol %p", async (protocol) => { // Skip test if version is below 8.0.0 if (cluster.checkIfServerVersionLessThan("8.0.0")) return; let client_for_config_set; let client_for_testing_az; try { // Stage 1: Reset stats and clear all AZ assignments so that the // client's AZ ("non-existing-az") matches no node, triggering the // all-nodes fallback. client_for_config_set = await GlideClusterClient.createClient( getClientConfigurationOption( azCluster.getAddresses(), protocol, { requestTimeout: 3000 }, ), ); await client_for_config_set.configSet( { "availability-zone": "" }, { route: "allNodes" }, ); await client_for_config_set.configResetStat({ route: "allNodes", }); // Determine how many nodes serve the shard that owns "foo" // (primary + connected replicas). const replication_info = (await client_for_config_set.info({ sections: [InfoOptions.Replication], route: { type: "primarySlotKey", key: "foo" }, })) as string; const replicas_match = replication_info.match( /connected_slaves:(\d+)/, ); const n_replicas = replicas_match ? parseInt(replicas_match[1]) : 0; const nodes_in_shard = n_replicas + 1; // primary + replicas expect(nodes_in_shard).toBeGreaterThan(1); // One GET per shard node under round-robin fallback. const get_calls = nodes_in_shard; // Stage 2: Create a client with an AZ that no node belongs to. client_for_testing_az = await GlideClusterClient.createClient( getClientConfigurationOption( azCluster.getAddresses(), protocol, { requestTimeout: 3000, readFrom: "AZAffinityAllNodes", clientAz: "non-existing-az", }, ), ); for (let i = 0; i < get_calls; i++) { await client_for_testing_az.get("foo"); } // Stage 3: Verify every node in the shard (primary + replicas) // received traffic, with the total matching the number of calls. const info_result = (await client_for_testing_az.info({ sections: [InfoOptions.All], route: "allNodes", })) as Record; let nodes_with_gets = 0; let total_get_calls = 0; let primary_received_gets = false; let replica_received_gets = false; Object.values(info_result).forEach((infoStr) => { if (!infoStr.includes("cmdstat_get:calls=")) return; nodes_with_gets++; total_get_calls += parseInt( (infoStr.match(/cmdstat_get:calls=(\d+)/) || [])[1] || "0", ); if ( infoStr.includes("role:master") || infoStr.includes("role:primary") ) { primary_received_gets = true; } if ( infoStr.includes("role:slave") || infoStr.includes("role:replica") ) { replica_received_gets = true; } }); expect(nodes_with_gets).toBe(nodes_in_shard); expect(primary_received_gets).toBe(true); expect(replica_received_gets).toBe(true); expect(total_get_calls).toBe(get_calls); } finally { client_for_config_set?.close(); client_for_testing_az?.close(); } }, ); }); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "lazy cluster connection establishes only on first command_%p", async (protocol) => { // Create a monitoring client (eagerly connected) const monitoringClient = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol, { lazyConnect: false, // Explicit eager connection requestTimeout: 3000, }), ); try { // Get initial client count const clientsBeforeLazyInit = await getClientCount(monitoringClient); // Create lazy client const lazyClient = await GlideClusterClient.createClient( getClientConfigurationOption( cluster.getAddresses(), protocol, { lazyConnect: true, // Lazy connection requestTimeout: 3000, }, ), ); try { // Verify no new connections were established const clientsAfterLazyInit = await getClientCount(monitoringClient); expect(clientsAfterLazyInit).toEqual(clientsBeforeLazyInit); // Send first command with lazy client const pingResponse = await lazyClient.ping(); expect(pingResponse).toBeDefined(); // Check client count after first command const clientsAfterFirstCommand = await getClientCount(monitoringClient); // We need to verify the lazy connection is working properly // Connection count assertions are racy: prior tests may still be // closing connections asynchronously. Assert only directional change. // Verify the ping worked (which means the lazy connection was established) expect(pingResponse).toBeDefined(); // Less strict assertion about connection count: // The connections shouldn't decrease, and they may increase expect(clientsAfterFirstCommand).toBeGreaterThanOrEqual( clientsBeforeLazyInit, ); } finally { await lazyClient.close(); } } finally { await monitoringClient.close(); } }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "lazy cluster connection with non-existent host_%p", async (protocol) => { const nonExistentHost = "non-existent-host-that-does-not-resolve"; const baseConfig = { addresses: [{ host: nonExistentHost, port: 7000 }], protocol, requestTimeout: 1000, }; // Test 1: Eager connection to non-existent host should fail immediately await expect( GlideClusterClient.createClient({ ...baseConfig, lazyConnect: false, }), ).rejects.toThrow(/connect|connection|resolve|network|host/i); // Test 2: Lazy connection to non-existent host should succeed in client creation const lazyClient = await GlideClusterClient.createClient({ ...baseConfig, lazyConnect: true, }); try { // But command execution should fail with appropriate error await expect(lazyClient.ping()).rejects.toThrow( /connect|connection|resolve|network|host/i, ); } finally { lazyClient.close(); } }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "should pass database id for cluster client_%p", async (protocol) => { // Skip test if version is below 9.0.0 (Valkey 9) if (cluster.checkIfServerVersionLessThan("9.0.0")) return; const client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol, { databaseId: 1, }), ); try { // Simple test to verify the client works with the database ID expect(await client.ping()).toEqual("PONG"); } finally { client.close(); } }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "move test_%p", async (protocol) => { // Skip test if version is below 9.0.0 (Valkey 9) if (cluster.checkIfServerVersionLessThan("9.0.0")) return; const client_db0 = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol, { databaseId: 0, }), ); const client_db1 = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol, { databaseId: 1, }), ); try { const key1 = "{key}-1" + getRandomKey(); const key2 = "{key}-2" + getRandomKey(); const value = getRandomKey(); // Test moving non-existent key expect(await client_db0.move(key1, 1)).toEqual(false); // Set a key in database 0 and move it to database 1 expect(await client_db0.set(key1, value)).toEqual("OK"); expect(await client_db0.get(key1)).toEqual(value); expect(await client_db0.move(Buffer.from(key1), 1)).toEqual( true, ); expect(await client_db0.get(key1)).toEqual(null); expect(await client_db1.get(key1)).toEqual(value); // Test error with invalid database number await expect(client_db0.move(key1, -1)).rejects.toThrow( RequestError, ); // batch tests for (const isAtomic of [true, false]) { expect(await client_db0.flushall()).toEqual("OK"); expect(await client_db1.flushall()).toEqual("OK"); const batch = new ClusterBatch(isAtomic); batch.move(key2, 1); batch.set(key2, value); batch.move(key2, 1); batch.get(key2); const results = await client_db0.exec(batch, true); expect(results).toEqual([false, "OK", true, null]); // Verify key exists in database 1 expect(await client_db1.get(key2)).toEqual(value); } } finally { client_db0.close(); client_db1.close(); } }, TIMEOUT, ); it( "tcp nodelay configuration", async () => { const config = getClientConfigurationOption( cluster.getAddresses(), ProtocolVersion.RESP3, ); // Test default (undefined - not set) const defaultClient = await GlideClusterClient.createClient(config); expect(await defaultClient.ping()).toBe("PONG"); expect(await defaultClient.set("key", "value")).toBe("OK"); expect(await defaultClient.get("key")).toBe("value"); defaultClient.close(); // Test explicit true const clientTrue = await GlideClusterClient.createClient({ ...config, advancedConfiguration: { tcpNoDelay: true, connectionTimeout: 10000, }, }); expect(await clientTrue.ping()).toBe("PONG"); expect(await clientTrue.set("key2", "value2")).toBe("OK"); expect(await clientTrue.get("key2")).toBe("value2"); clientTrue.close(); // Test explicit false const clientFalse = await GlideClusterClient.createClient({ ...config, advancedConfiguration: { tcpNoDelay: false, connectionTimeout: 10000, }, }); expect(await clientFalse.ping()).toBe("PONG"); expect(await clientFalse.set("key3", "value3")).toBe("OK"); expect(await clientFalse.get("key3")).toBe("value3"); clientFalse.close(); }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "latencyHistory with route_%p", async (protocol) => { client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const beforeSpike = await getUnixSeconds(client); await triggerLatencySpike(client); // Multi-node (default route) const multiHistory = await client.latencyHistory("command"); const allEntries = flattenClusterResponseArrays(multiHistory); expect(allEntries.length).toBeGreaterThan(0); for (const entry of allEntries) { expect(entry.time).toBeGreaterThanOrEqual(beforeSpike); expect(entry.latency).toBeGreaterThan(0); } // Single-node route (primary node) const singleHistory = await client.latencyHistory( "command", PRIMARY_SLOT_ROUTE_OPTION, ); expect(Array.isArray(singleHistory)).toBe(true); const singleEntries = flattenClusterResponseArrays(singleHistory); expect(singleEntries.length).toBeGreaterThan(0); for (const entry of singleEntries) { expect(entry.time).toBeGreaterThanOrEqual(beforeSpike); expect(entry.latency).toBeGreaterThan(0); } // Non-existent event returns empty const unknown = await client.latencyHistory("nonexistent"); expect(flattenClusterResponseArrays(unknown).length).toBe(0); client.close(); }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "latencyLatest with route_%p", async (protocol) => { client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const beforeSpike = await getUnixSeconds(client); await triggerLatencySpike(client); // Multi-node (default route) const multiLatest = await client.latencyLatest(); const allEntries = flattenClusterResponseArrays(multiLatest); expect(allEntries.length).toBeGreaterThanOrEqual(1); const commandInfo = allEntries.find( (info) => info.eventName === "command", ); expect(commandInfo).toBeDefined(); expect(commandInfo!.latestTime).toBeGreaterThanOrEqual(beforeSpike); expect(commandInfo!.latestDuration).toBeGreaterThan(0); expect(commandInfo!.maxDuration).toBeGreaterThanOrEqual( commandInfo!.latestDuration, ); // Single-node route (primary node) const singleLatest = await client.latencyLatest( PRIMARY_SLOT_ROUTE_OPTION, ); expect(Array.isArray(singleLatest)).toBe(true); const singleEntries = flattenClusterResponseArrays(singleLatest); expect(singleEntries.length).toBeGreaterThanOrEqual(1); const singleCommand = singleEntries.find( (info) => info.eventName === "command", ); expect(singleCommand).toBeDefined(); expect(singleCommand!.latestDuration).toBeGreaterThan(0); expect(singleCommand!.maxDuration).toBeGreaterThanOrEqual( singleCommand!.latestDuration, ); client.close(); }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "latencyReset with route_%p", async (protocol) => { client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); // Trigger spike then reset with route await triggerLatencySpike(client); const resetResult = await client.latencyReset(undefined, { route: "allNodes", }); expect(resetResult).toBeGreaterThan(0); // Trigger spike then reset specific event with route await triggerLatencySpike(client); const specificReset = await client.latencyReset(["command"], { route: "allNodes", }); expect(specificReset).toBeGreaterThan(0); client.close(); }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "memoryDoctor with allNodes route %p", async (protocol) => { client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const result = await client.memoryDoctor({ route: "allNodes" }); const reports = Object.values(result as Record); expect(reports.length).toBeGreaterThan(0); for (const report of reports) { expect(typeof report).toBe("string"); expect(report.length).toBeGreaterThan(0); } client.close(); }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "memoryMallocStats with allNodes route %p", async (protocol) => { client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const result = await client.memoryMallocStats({ route: "allNodes", }); const reports = Object.values(result as Record); expect(reports.length).toBeGreaterThan(0); for (const report of reports) { expect(typeof report).toBe("string"); expect(report.length).toBeGreaterThan(0); } client.close(); }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "memoryPurge with allNodes route %p", async (protocol) => { client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); expect(await client.memoryPurge({ route: "allNodes" })).toBe("OK"); client.close(); }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "memoryStats with allNodes route %p", async (protocol) => { client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const key = getRandomKey(); await client.set(key, "allNodesMemTest"); const result = await client.memoryStats({ route: "allNodes" }); const statsList = Object.values( result as Record, ); expect(statsList.length).toBeGreaterThan(0); for (const stats of statsList) { assertMemoryStatsFields(stats, cluster.getVersion()); } client.close(); }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "memoryDoctor with randomNode route %p", async (protocol) => { client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const result = await client.memoryDoctor({ route: "randomNode" }); expect(typeof result).toBe("string"); expect((result as string).length).toBeGreaterThan(0); client.close(); }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "memoryMallocStats with randomNode route %p", async (protocol) => { client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const result = await client.memoryMallocStats({ route: "randomNode", }); expect(typeof result).toBe("string"); expect((result as string).length).toBeGreaterThan(0); client.close(); }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "memoryPurge with randomNode route %p", async (protocol) => { client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); expect(await client.memoryPurge({ route: "randomNode" })).toBe( "OK", ); client.close(); }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "memoryStats with single node route %p", async (protocol) => { client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); // Write a key and route to its node to ensure db entry exists const key = `memoryStats_single_node_${protocol}`; await client.set(key, "value"); const result = await client.memoryStats({ route: { type: "primarySlotKey", key }, }); const stats = result as MemoryStats; assertMemoryStatsFields(stats, cluster.getVersion()); expect(stats.db[0]).toBeDefined(); assertMemoryStatsDbEntry(stats.db[0]); client.close(); }, TIMEOUT, ); it( "should connect with IPv4 address", async () => { const address = { host: cluster.getAddresses()[0][0], port: cluster.getAddresses()[0][1], }; const client = await GlideClusterClient.createClient({ addresses: [address], }); await assertConnected(client); client.close(); }, TIMEOUT, ); it( "should connect with IPv6 address", async () => { // Skip if no IPv6 endpoint is available in this environment if (!cluster.getAddresses().some(([host]) => host.includes(":"))) { return; } const address = { host: cluster .getAddresses() .find(([host]) => host.includes(":"))![0], port: cluster .getAddresses() .find(([host]) => host.includes(":"))![1], }; const client = await GlideClusterClient.createClient({ addresses: [address], }); await assertConnected(client); client.close(); }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "clientTrackingInfo with cache off and default route_%p", async (protocol) => { client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const info = (await client.clientTrackingInfo()) as ClientTrackingInfo; assertClientTrackingInfo(info, false); }, TIMEOUT, ); it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "clientTrackingInfo with cache off and multi-node route_%p", async (protocol) => { client = await GlideClusterClient.createClient( getClientConfigurationOption(cluster.getAddresses(), protocol), ); const multiInfo = (await client.clientTrackingInfo({ route: "allPrimaries", })) as Record; expect(Object.keys(multiInfo).length).toBeGreaterThan(0); for (const nodeInfo of Object.values(multiInfo)) { assertClientTrackingInfo(nodeInfo, false); } }, TIMEOUT, ); it( "clientTrackingInfo with cache on and default route", async () => { const cache = new ClientSideCache({ maxCacheKb: 1, entryTtlMs: 60000, serverAssisted: true, }); client = await GlideClusterClient.createClient( getClientConfigurationOption( cluster.getAddresses(), ProtocolVersion.RESP3, { clientSideCache: cache }, ), ); const info = (await client.clientTrackingInfo()) as ClientTrackingInfo; assertClientTrackingInfo(info, true); }, TIMEOUT, ); it.each([false, true])( "close detaches a client with a blocked command from the server (lazyConnect: %p)", async (lazyConnect) => { const config = getClientConfigurationOption( cluster.getAddresses(), ProtocolVersion.RESP3, ); const observer = await GlideClusterClient.createClient(config); // A lazy client has no server-side id until it connects, and other test // files may have their own XREADGROUP blocked on a shared cluster, so // identify the blocked client by a unique name. const blockedName = `blocked_close_${lazyConnect}_${getRandomKey()}`; const blocked = await GlideClusterClient.createClient({ ...config, lazyConnect, clientName: blockedName, }); const key = getRandomKey(); const group = getRandomKey(); const consumer = getRandomKey(); // CLIENT LIST lines for the blocked client, across every node. A RESP3 // multi-node reply is a GlideRecord, RESP2 gives a Record. const blockedClientLines = async () => { const response = (await observer.customCommand( ["CLIENT", "LIST"], { route: "allNodes" }, )) as GlideRecord | Record; const perNode = Array.isArray(response) ? convertGlideRecordToRecord(response) : response; return Object.values(perNode) .flatMap((list) => list.split("\n")) .filter((line) => line.includes(`name=${blockedName} `)); }; const isAttached = async () => (await blockedClientLines()).length > 0; const isBlocked = async () => (await blockedClientLines()).some((line) => / flags=\S*b/.test(line), ); // Resolves true as soon as the predicate holds, false at the deadline. const poll = async ( predicate: () => Promise, deadlineMs: number, ) => { const deadline = Date.now() + deadlineMs; while (Date.now() < deadline) { if (await predicate()) return true; await sleep(5); } return predicate(); }; // Resolves true once the predicate has held at every poll for `holdMs`, // false if that does not happen before the deadline. const pollStable = async ( predicate: () => Promise, holdMs: number, deadlineMs: number, ) => { const deadline = Date.now() + deadlineMs; let heldSince: number | undefined; while (Date.now() < deadline) { if (await predicate()) { heldSince ??= Date.now(); if (Date.now() - heldSince >= holdMs) return true; } else { heldSince = undefined; } await sleep(5); } return false; }; try { expect( await observer.xgroupCreate(key, group, "$", { mkStream: true, }), ).toEqual("OK"); const pending = blocked.xreadgroup( group, consumer, { [key]: ">" }, { block: 30000 }, ); if (lazyConnect) { // Close before the lazy connection is established. The queued // command must not stay connected and blocked after close(). blocked.close(); } else { expect(await poll(isBlocked, 2000)).toBe(true); blocked.close(); } await expect(pending).rejects.toThrow(ClosingError); // The server must drop the connection promptly, not when BLOCK expires. const isDetached = async () => !(await isAttached()); if (lazyConnect) { // The client is not attached before its handshake either, and // close() may land while that handshake is in progress: the // connections then show up and are killed as soon as the cluster // client is built. Require the client to be gone and stay gone. expect(await pollStable(isDetached, 500, 5000)).toBe(true); } else { expect(await poll(isDetached, 1000)).toBe(true); } // An entry added now must not be claimed by the closed consumer. expect( await observer.xadd(key, [["field", "value"]]), ).not.toBeNull(); const [pendingCount] = await observer.xpending(key, group); expect(pendingCount).toEqual(0); } finally { await observer.del([key]); observer.close(); } }, TIMEOUT, ); });