Skip to content

Commit 4d158f9

Browse files
committed
clean PR
1 parent cafdc63 commit 4d158f9

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { strict as assert } from 'node:assert';
2+
import testUtils, { GLOBAL } from '../test-utils';
3+
import LATENCY_RESET, { LATENCY_EVENTS } from './LATENCY_RESET';
4+
import { parseArgs } from './generic-transformers';
5+
6+
describe('LATENCY RESET', function () {
7+
8+
9+
it('transformArguments with no events', () => {
10+
assert.deepEqual(
11+
parseArgs(LATENCY_RESET),
12+
[
13+
'LATENCY',
14+
'RESET'
15+
]
16+
);
17+
});
18+
19+
it('transformArguments with one event', () => {
20+
assert.deepEqual(
21+
parseArgs(LATENCY_RESET, LATENCY_EVENTS.COMMAND),
22+
[
23+
'LATENCY',
24+
'RESET',
25+
'command'
26+
]
27+
);
28+
});
29+
30+
it('transformArguments with multiple events', () => {
31+
assert.deepEqual(
32+
parseArgs(LATENCY_RESET, LATENCY_EVENTS.COMMAND, LATENCY_EVENTS.FORK),
33+
[
34+
'LATENCY',
35+
'RESET',
36+
'command',
37+
'fork'
38+
]
39+
);
40+
});
41+
42+
43+
testUtils.testWithClient('client.latencyReset', async client => {
44+
45+
await client.configSet('latency-monitor-threshold', '1');
46+
47+
48+
await client.sendCommand(['DEBUG', 'SLEEP', '0.1']);
49+
50+
51+
const latestLatencyBeforeReset = await client.latencyLatest();
52+
assert.ok(latestLatencyBeforeReset.length > 0, 'Expected latency events to be recorded before first reset.');
53+
assert.equal(latestLatencyBeforeReset[0][0], 'command', 'Expected "command" event to be recorded.');
54+
assert.ok(Number(latestLatencyBeforeReset[0][2]) >= 100, 'Expected latest latency for "command" to be at least 100ms.');
55+
56+
57+
const replyAll = await client.latencyReset();
58+
59+
assert.equal(typeof replyAll, 'number');
60+
assert.ok(replyAll >= 0);
61+
62+
63+
const latestLatencyAfterAllReset = await client.latencyLatest();
64+
assert.deepEqual(latestLatencyAfterAllReset, [], 'Expected no latency events after resetting all.');
65+
66+
67+
await client.sendCommand(['DEBUG', 'SLEEP', '0.05']);
68+
const latestLatencyBeforeSpecificReset = await client.latencyLatest();
69+
assert.ok(latestLatencyBeforeSpecificReset.length > 0, 'Expected latency events before specific reset.');
70+
71+
72+
const replySpecific = await client.latencyReset(LATENCY_EVENTS.COMMAND);
73+
assert.equal(typeof replySpecific, 'number');
74+
assert.ok(replySpecific >= 0);
75+
76+
77+
const latestLatencyAfterSpecificReset = await client.latencyLatest();
78+
assert.deepEqual(latestLatencyAfterSpecificReset, [], 'Expected no latency events after specific reset of "command".');
79+
80+
81+
await client.sendCommand(['DEBUG', 'SLEEP', '0.02']);
82+
83+
84+
const latestLatencyBeforeMultipleReset = await client.latencyLatest();
85+
assert.ok(latestLatencyBeforeMultipleReset.length > 0, 'Expected latency events before multiple reset.');
86+
87+
88+
const replyMultiple = await client.latencyReset(LATENCY_EVENTS.COMMAND, LATENCY_EVENTS.FORK);
89+
assert.equal(typeof replyMultiple, 'number');
90+
assert.ok(replyMultiple >= 0);
91+
92+
const latestLatencyAfterMultipleReset = await client.latencyLatest();
93+
assert.deepEqual(latestLatencyAfterMultipleReset, [], 'Expected no latency events after multiple specified resets.');
94+
95+
}, {
96+
97+
...GLOBAL.SERVERS.OPEN,
98+
clientOptions: {
99+
socket: {
100+
connectTimeout: 300000
101+
}
102+
}
103+
});
104+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { CommandParser } from '../client/parser';
2+
import { Command } from '../RESP/types';
3+
import { LATENCY_EVENTS, LatencyEvent } from './LATENCY_GRAPH';
4+
5+
export { LATENCY_EVENTS, LatencyEvent };
6+
7+
export default {
8+
NOT_KEYED_COMMAND: true,
9+
IS_READ_ONLY: false,
10+
/**
11+
* Constructs the LATENCY RESET command
12+
* * @param parser - The command parser
13+
* @param events - The latency events to reset. If not specified, all events are reset.
14+
* @see https://redis.io/commands/latency-reset/
15+
*/
16+
parseCommand(parser: CommandParser, ...events: Array<LatencyEvent>) {
17+
const args = ['LATENCY', 'RESET'];
18+
if (events.length > 0) {
19+
args.push(...events);
20+
}
21+
parser.push(...args);
22+
},
23+
transformReply: undefined as unknown as () => number
24+
} as const satisfies Command;

packages/client/lib/commands/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ import LATENCY_DOCTOR from './LATENCY_DOCTOR';
171171
import LATENCY_GRAPH from './LATENCY_GRAPH';
172172
import LATENCY_HISTORY from './LATENCY_HISTORY';
173173
import LATENCY_LATEST from './LATENCY_LATEST';
174+
import LATENCY_RESET from './LATENCY_RESET';
174175
import LCS_IDX_WITHMATCHLEN from './LCS_IDX_WITHMATCHLEN';
175176
import LCS_IDX from './LCS_IDX';
176177
import LCS_LEN from './LCS_LEN';
@@ -706,6 +707,8 @@ export default {
706707
latencyHistory: LATENCY_HISTORY,
707708
LATENCY_LATEST,
708709
latencyLatest: LATENCY_LATEST,
710+
LATENCY_RESET,
711+
latencyReset: LATENCY_RESET,
709712
LCS_IDX_WITHMATCHLEN,
710713
lcsIdxWithMatchLen: LCS_IDX_WITHMATCHLEN,
711714
LCS_IDX,

0 commit comments

Comments
 (0)