From 518eb1a11ad126c4e3ec2e1b59e80f085bb81758 Mon Sep 17 00:00:00 2001 From: Charley DAVID Date: Wed, 19 Jul 2023 21:28:35 +0200 Subject: [PATCH] fix(client): Isolate exceptional logic to xAutoClaim --- .../client/lib/commands/XAUTOCLAIM.spec.ts | 57 ++++++++++++++++++- packages/client/lib/commands/XAUTOCLAIM.ts | 26 ++++++++- .../lib/commands/generic-transformers.spec.ts | 57 ++++++++----------- .../lib/commands/generic-transformers.ts | 4 -- 4 files changed, 103 insertions(+), 41 deletions(-) diff --git a/packages/client/lib/commands/XAUTOCLAIM.spec.ts b/packages/client/lib/commands/XAUTOCLAIM.spec.ts index 4447a06d773..b1d765d4c89 100644 --- a/packages/client/lib/commands/XAUTOCLAIM.spec.ts +++ b/packages/client/lib/commands/XAUTOCLAIM.spec.ts @@ -23,7 +23,7 @@ describe('XAUTOCLAIM', () => { }); }); - testUtils.testWithClient('client.xAutoClaim', async client => { + testUtils.testWithClient('client.xAutoClaim without messages', async client => { await Promise.all([ client.xGroupCreate('key', 'group', '$', { MKSTREAM: true @@ -39,4 +39,59 @@ describe('XAUTOCLAIM', () => { } ); }, GLOBAL.SERVERS.OPEN); + + testUtils.testWithClient('client.xAutoClaim with messages', async client => { + const [,,id,] = await Promise.all([ + client.xGroupCreate('key', 'group', '$', { + MKSTREAM: true + }), + client.xGroupCreateConsumer('key', 'group', 'consumer'), + client.xAdd('key', '*', { foo: 'bar' }), + client.xReadGroup('group', 'consumer', { key: 'key', id: '>' }) + ]); + + assert.deepEqual( + await client.xAutoClaim('key', 'group', 'consumer', 1, '0-0'), + { + nextId: '0-0', + messages: [{ + id, + message: Object.create(null, { 'foo': { + value: 'bar', + configurable: true, + enumerable: true + } }) + }] + } + ); + }, GLOBAL.SERVERS.OPEN); + + testUtils.testWithClient('client.xAutoClaim with trimmed messages', async client => { + const [,,,,,id2,] = await Promise.all([ + client.xGroupCreate('key', 'group', '$', { + MKSTREAM: true + }), + client.xGroupCreateConsumer('key', 'group', 'consumer'), + client.xAdd('key', '*', { foo: 'bar' }), + client.xReadGroup('group', 'consumer', { key: 'key', id: '>' }), + client.xTrim('key', 'MAXLEN', 0), + client.xAdd('key', '*', { bar: 'baz' }), + client.xReadGroup('group', 'consumer', { key: 'key', id: '>' }), + ]); + + assert.deepEqual( + await client.xAutoClaim('key', 'group', 'consumer', 1, '0-0'), + { + nextId: '0-0', + messages: [{ + id: id2, + message: Object.create(null, { 'bar': { + value: 'baz', + configurable: true, + enumerable: true + } }) + }] + } + ); + }, GLOBAL.SERVERS.OPEN); }); diff --git a/packages/client/lib/commands/XAUTOCLAIM.ts b/packages/client/lib/commands/XAUTOCLAIM.ts index 4bf46057bac..7cf7085d6e4 100644 --- a/packages/client/lib/commands/XAUTOCLAIM.ts +++ b/packages/client/lib/commands/XAUTOCLAIM.ts @@ -1,5 +1,5 @@ import { RedisCommandArgument, RedisCommandArguments } from '.'; -import { StreamMessagesReply, transformStreamMessagesReply } from './generic-transformers'; +import { StreamMessageReply, transformTuplesReply } from './generic-transformers'; export const FIRST_KEY_INDEX = 1; @@ -28,12 +28,32 @@ type XAutoClaimRawReply = [RedisCommandArgument, Array]; interface XAutoClaimReply { nextId: RedisCommandArgument; - messages: StreamMessagesReply; + messages: XAutoClaimMessagesReply; +} + +type XAutoClaimMessagesReply = Array; + +function transformXAutoClaimMessagesReply(reply: Array): XAutoClaimMessagesReply { + const messages = []; + + for (const tuple of reply) { + if (tuple === null) { + continue; + } + + const [id, message] = tuple; + messages.push({ + id, + message: transformTuplesReply(message) + }); + } + + return messages; } export function transformReply(reply: XAutoClaimRawReply): XAutoClaimReply { return { nextId: reply[0], - messages: transformStreamMessagesReply(reply[1]) + messages: transformXAutoClaimMessagesReply(reply[1]) }; } diff --git a/packages/client/lib/commands/generic-transformers.spec.ts b/packages/client/lib/commands/generic-transformers.spec.ts index 9ea96568c60..301cab0a75c 100644 --- a/packages/client/lib/commands/generic-transformers.spec.ts +++ b/packages/client/lib/commands/generic-transformers.spec.ts @@ -194,39 +194,30 @@ describe('Generic Transformers', () => { ); }); - describe('transformStreamMessagesReply', () => { - it('with null', () => { - assert.deepEqual( - transformStreamMessagesReply([null]), - [] - ); - }) - - it('with messages', () => { - assert.deepEqual( - transformStreamMessagesReply([['0-0', ['0key', '0value']], ['1-0', ['1key', '1value']]]), - [{ - id: '0-0', - message: Object.create(null, { - '0key': { - value: '0value', - configurable: true, - enumerable: true - } - }) - }, { - id: '1-0', - message: Object.create(null, { - '1key': { - value: '1value', - configurable: true, - enumerable: true - } - }) - }] - ); - }) - }) + it('transformStreamMessagesReply', () => { + assert.deepEqual( + transformStreamMessagesReply([['0-0', ['0key', '0value']], ['1-0', ['1key', '1value']]]), + [{ + id: '0-0', + message: Object.create(null, { + '0key': { + value: '0value', + configurable: true, + enumerable: true + } + }) + }, { + id: '1-0', + message: Object.create(null, { + '1key': { + value: '1value', + configurable: true, + enumerable: true + } + }) + }] + ); + }); describe('transformStreamsMessagesReply', () => { it('null', () => { diff --git a/packages/client/lib/commands/generic-transformers.ts b/packages/client/lib/commands/generic-transformers.ts index b7a94c3616b..5048de9399a 100644 --- a/packages/client/lib/commands/generic-transformers.ts +++ b/packages/client/lib/commands/generic-transformers.ts @@ -97,10 +97,6 @@ export type StreamMessagesReply = Array; export function transformStreamMessagesReply(reply: Array): StreamMessagesReply { const messages = []; - if (reply[0] === null) { - return messages; - } - for (const [id, message] of reply) { messages.push({ id,