-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(client): add XNACK command with options and tests #3238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { strict as assert } from 'node:assert'; | ||
| import XNACK from './XNACK'; | ||
| import { parseArgs } from './generic-transformers'; | ||
| import testUtils, { GLOBAL } from '../test-utils'; | ||
|
|
||
| describe('XNACK', () => { | ||
| describe('transformArguments', () => { | ||
| it('string - SILENT', () => { | ||
| assert.deepEqual( | ||
| parseArgs(XNACK, 'key', 'group', 'SILENT', '0-0'), | ||
| ['XNACK', 'key', 'group', 'SILENT', 'IDS', '1', '0-0'] | ||
| ); | ||
| }); | ||
|
|
||
| it('array - FAIL', () => { | ||
| assert.deepEqual( | ||
| parseArgs(XNACK, 'key', 'group', 'FAIL', ['0-0', '1-0']), | ||
| ['XNACK', 'key', 'group', 'FAIL', 'IDS', '2', '0-0', '1-0'] | ||
| ); | ||
| }); | ||
|
|
||
| it('array - FATAL', () => { | ||
| assert.deepEqual( | ||
| parseArgs(XNACK, 'key', 'group', 'FATAL', ['0-0', '1-0', '2-0']), | ||
| ['XNACK', 'key', 'group', 'FATAL', 'IDS', '3', '0-0', '1-0', '2-0'] | ||
| ); | ||
| }); | ||
|
|
||
| it('with RETRYCOUNT', () => { | ||
| assert.deepEqual( | ||
| parseArgs(XNACK, 'key', 'group', 'FAIL', '0-0', { | ||
| RETRYCOUNT: 7 | ||
| }), | ||
| ['XNACK', 'key', 'group', 'FAIL', 'IDS', '1', '0-0', 'RETRYCOUNT', '7'] | ||
| ); | ||
| }); | ||
|
|
||
| it('with FORCE', () => { | ||
| assert.deepEqual( | ||
| parseArgs(XNACK, 'key', 'group', 'FAIL', ['0-0', '1-0'], { | ||
| FORCE: true | ||
| }), | ||
| ['XNACK', 'key', 'group', 'FAIL', 'IDS', '2', '0-0', '1-0', 'FORCE'] | ||
| ); | ||
| }); | ||
|
|
||
| it('with RETRYCOUNT and FORCE', () => { | ||
| assert.deepEqual( | ||
| parseArgs(XNACK, 'key', 'group', 'FAIL', ['0-0', '1-0'], { | ||
| RETRYCOUNT: 3, | ||
| FORCE: true | ||
| }), | ||
| ['XNACK', 'key', 'group', 'FAIL', 'IDS', '2', '0-0', '1-0', 'RETRYCOUNT', '3', 'FORCE'] | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| testUtils.testWithClient('xNack', async client => { | ||
|
|
||
| const key = `xnack:tmp:${Date.now()}`; | ||
| const group = 'group'; | ||
| const consumer = 'consumer-1'; | ||
|
|
||
| await client.del(key); | ||
| await client.xGroupCreate(key, group, '0', { MKSTREAM: true }); | ||
|
|
||
| const id1 = await client.xAdd(key, '*', { field: '1' }); | ||
| const id2 = await client.xAdd(key, '*', { field: '2' }); | ||
| const id3 = await client.xAdd(key, '*', { field: '3' }); | ||
| const id4 = await client.xAdd(key, '*', { field: '4' }); | ||
| const id5 = await client.xAdd(key, '*', { field: '5' }); | ||
| const id6 = await client.xAdd(key, '*', { field: '6' }); | ||
|
|
||
| await client.xReadGroup(group, consumer, { | ||
| key, | ||
| id: '>' | ||
| }); | ||
|
|
||
| const reply = await client.xNack(key, group, 'FAIL', [id1, id2]); | ||
| const replyWithRetryCount = await client.xNack(key, group, 'FAIL', id3, { | ||
| RETRYCOUNT: 7 | ||
| }); | ||
| const replyWithForce = await client.xNack(key, group, 'FAIL', id4, { | ||
| FORCE: true | ||
| }); | ||
| const replyWithRetryCountAndForce = await client.xNack(key, group, 'FAIL', [id5, id6], { | ||
| RETRYCOUNT: 3, | ||
| FORCE: true | ||
| }); | ||
|
|
||
| assert.equal(reply, 2); | ||
| assert.equal(replyWithRetryCount, 1); | ||
| assert.equal(replyWithForce, 1); | ||
| assert.equal(replyWithRetryCountAndForce, 2); | ||
|
|
||
| assert.equal(typeof reply, 'number'); | ||
| assert.equal(typeof replyWithRetryCount, 'number'); | ||
| assert.equal(typeof replyWithForce, 'number'); | ||
| assert.equal(typeof replyWithRetryCountAndForce, 'number'); | ||
| }, { | ||
| ...GLOBAL.SERVERS.OPEN, | ||
| minimumDockerVersion: [8, 8] | ||
| }); | ||
|
|
||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { CommandParser } from '../client/parser'; | ||
| import { Command, NumberReply, RedisArgument } from '../RESP/types'; | ||
| import { RedisVariadicArgument } from './generic-transformers'; | ||
|
|
||
| export type XNackMode = 'SILENT' | 'FAIL' | 'FATAL'; | ||
| export interface XNackOptions { | ||
| RETRYCOUNT?: number; | ||
| FORCE?: boolean; | ||
| } | ||
|
|
||
| export default { | ||
| IS_READ_ONLY: false, | ||
| /** | ||
| * Constructs the XNACK command to negatively acknowledge one or more pending stream entries. | ||
| * Added since Redis 8.8. | ||
| * | ||
| * @param parser - The command parser | ||
| * @param key - The stream key | ||
| * @param group - The consumer group name | ||
| * @param mode - NACK mode: SILENT, FAIL, or FATAL | ||
| * @param id - One or more message IDs to nack | ||
| * @param options - Additional options for retry count and force handling | ||
| * @see https://redis.io/commands/xnack/ | ||
| */ | ||
| parseCommand( | ||
| parser: CommandParser, | ||
| key: RedisArgument, | ||
| group: RedisArgument, | ||
| mode: XNackMode, | ||
| id: RedisVariadicArgument, | ||
| options?: XNackOptions | ||
| ) { | ||
| parser.push('XNACK'); | ||
| parser.pushKey(key); | ||
| parser.push(group, mode, 'IDS'); | ||
| parser.pushVariadicWithLength(id); | ||
|
|
||
| if (options?.RETRYCOUNT !== undefined) { | ||
| parser.push('RETRYCOUNT', options.RETRYCOUNT.toString()); | ||
| } | ||
|
|
||
| if (options?.FORCE) { | ||
| parser.push('FORCE'); | ||
| } | ||
| }, | ||
| transformReply: undefined as unknown as () => NumberReply | ||
| } as const satisfies Command; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to test with cluster as well?