Skip to content
Merged

Main #3047

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions packages/client/lib/commands/LATENCY_RESET.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import LATENCY_RESET, { LATENCY_EVENTS } from './LATENCY_RESET';
import { parseArgs } from './generic-transformers';

describe('LATENCY RESET', function () {


it('transformArguments with no events', () => {
assert.deepEqual(
parseArgs(LATENCY_RESET),
[
'LATENCY',
'RESET'
]
);
});

it('transformArguments with one event', () => {
assert.deepEqual(
parseArgs(LATENCY_RESET, LATENCY_EVENTS.COMMAND),
[
'LATENCY',
'RESET',
'command'
]
);
});

it('transformArguments with multiple events', () => {
assert.deepEqual(
parseArgs(LATENCY_RESET, LATENCY_EVENTS.COMMAND, LATENCY_EVENTS.FORK),
[
'LATENCY',
'RESET',
'command',
'fork'
]
);
});


testUtils.testWithClient('client.latencyReset', async client => {

await client.configSet('latency-monitor-threshold', '1');


await client.sendCommand(['DEBUG', 'SLEEP', '0.1']);


const latestLatencyBeforeReset = await client.latencyLatest();
assert.ok(latestLatencyBeforeReset.length > 0, 'Expected latency events to be recorded before first reset.');
assert.equal(latestLatencyBeforeReset[0][0], 'command', 'Expected "command" event to be recorded.');
assert.ok(Number(latestLatencyBeforeReset[0][2]) >= 100, 'Expected latest latency for "command" to be at least 100ms.');


const replyAll = await client.latencyReset();

assert.equal(typeof replyAll, 'number');
assert.ok(replyAll >= 0);


const latestLatencyAfterAllReset = await client.latencyLatest();
assert.deepEqual(latestLatencyAfterAllReset, [], 'Expected no latency events after resetting all.');


await client.sendCommand(['DEBUG', 'SLEEP', '0.05']);
const latestLatencyBeforeSpecificReset = await client.latencyLatest();
assert.ok(latestLatencyBeforeSpecificReset.length > 0, 'Expected latency events before specific reset.');


const replySpecific = await client.latencyReset(LATENCY_EVENTS.COMMAND);
assert.equal(typeof replySpecific, 'number');
assert.ok(replySpecific >= 0);


const latestLatencyAfterSpecificReset = await client.latencyLatest();
assert.deepEqual(latestLatencyAfterSpecificReset, [], 'Expected no latency events after specific reset of "command".');


await client.sendCommand(['DEBUG', 'SLEEP', '0.02']);


const latestLatencyBeforeMultipleReset = await client.latencyLatest();
assert.ok(latestLatencyBeforeMultipleReset.length > 0, 'Expected latency events before multiple reset.');


const replyMultiple = await client.latencyReset(LATENCY_EVENTS.COMMAND, LATENCY_EVENTS.FORK);
assert.equal(typeof replyMultiple, 'number');
assert.ok(replyMultiple >= 0);

const latestLatencyAfterMultipleReset = await client.latencyLatest();
assert.deepEqual(latestLatencyAfterMultipleReset, [], 'Expected no latency events after multiple specified resets.');

}, {

...GLOBAL.SERVERS.OPEN,
clientOptions: {
socket: {
connectTimeout: 300000
}
}
});
});
24 changes: 24 additions & 0 deletions packages/client/lib/commands/LATENCY_RESET.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { CommandParser } from '../client/parser';
import { Command } from '../RESP/types';
import { LATENCY_EVENTS, LatencyEvent } from './LATENCY_GRAPH';

export { LATENCY_EVENTS, LatencyEvent };

export default {
NOT_KEYED_COMMAND: true,
IS_READ_ONLY: false,
/**
* Constructs the LATENCY RESET command
* * @param parser - The command parser
* @param events - The latency events to reset. If not specified, all events are reset.
* @see https://redis.io/commands/latency-reset/
*/
parseCommand(parser: CommandParser, ...events: Array<LatencyEvent>) {
const args = ['LATENCY', 'RESET'];
if (events.length > 0) {
args.push(...events);
}
parser.push(...args);
},
transformReply: undefined as unknown as () => number
} as const satisfies Command;
3 changes: 3 additions & 0 deletions packages/client/lib/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ import LATENCY_DOCTOR from './LATENCY_DOCTOR';
import LATENCY_GRAPH from './LATENCY_GRAPH';
import LATENCY_HISTORY from './LATENCY_HISTORY';
import LATENCY_LATEST from './LATENCY_LATEST';
import LATENCY_RESET from './LATENCY_RESET';
import LCS_IDX_WITHMATCHLEN from './LCS_IDX_WITHMATCHLEN';
import LCS_IDX from './LCS_IDX';
import LCS_LEN from './LCS_LEN';
Expand Down Expand Up @@ -706,6 +707,8 @@ export default {
latencyHistory: LATENCY_HISTORY,
LATENCY_LATEST,
latencyLatest: LATENCY_LATEST,
LATENCY_RESET,
latencyReset: LATENCY_RESET,
LCS_IDX_WITHMATCHLEN,
lcsIdxWithMatchLen: LCS_IDX_WITHMATCHLEN,
LCS_IDX,
Expand Down
Loading