Skip to content

Commit

Permalink
fix(client): XCLAIM & XAUTOCLAIM after a TRIM might return nils
Browse files Browse the repository at this point in the history
  • Loading branch information
Calyhre committed Jul 21, 2023
1 parent a7d5bc7 commit eb2c37e
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 15 deletions.
64 changes: 63 additions & 1 deletion packages/client/lib/commands/XAUTOCLAIM.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,4 +39,66 @@ 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: testUtils.isVersionGreaterThan([7, 0]) ? [{
id: id2,
message: Object.create(null, { 'bar': {
value: 'baz',
configurable: true,
enumerable: true
} })
}] : [null, {
id: id2,
message: Object.create(null, { 'bar': {
value: 'baz',
configurable: true,
enumerable: true
} })
}]
}
);
}, GLOBAL.SERVERS.OPEN);
});
6 changes: 3 additions & 3 deletions packages/client/lib/commands/XAUTOCLAIM.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RedisCommandArgument, RedisCommandArguments } from '.';
import { StreamMessagesReply, transformStreamMessagesReply } from './generic-transformers';
import { StreamMessagesNullReply, transformStreamMessagesNullReply } from './generic-transformers';

export const FIRST_KEY_INDEX = 1;

Expand Down Expand Up @@ -28,12 +28,12 @@ type XAutoClaimRawReply = [RedisCommandArgument, Array<any>];

interface XAutoClaimReply {
nextId: RedisCommandArgument;
messages: StreamMessagesReply;
messages: StreamMessagesNullReply;
}

export function transformReply(reply: XAutoClaimRawReply): XAutoClaimReply {
return {
nextId: reply[0],
messages: transformStreamMessagesReply(reply[1])
messages: transformStreamMessagesNullReply(reply[1])
};
}
40 changes: 40 additions & 0 deletions packages/client/lib/commands/XCLAIM.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,44 @@ describe('XCLAIM', () => {
[]
);
}, GLOBAL.SERVERS.OPEN);

testUtils.testWithClient('client.xClaim with a message', 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.xClaim('key', 'group', 'consumer', 1, id),
[{
id,
message: Object.create(null, { 'foo': {
value: 'bar',
configurable: true,
enumerable: true
} })
}]
);
}, GLOBAL.SERVERS.OPEN);

testUtils.testWithClient('client.xClaim with a trimmed message', 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: '>' }),
client.xTrim('key', 'MAXLEN', 0),
]);

assert.deepEqual(
await client.xClaim('key', 'group', 'consumer', 1, id),
testUtils.isVersionGreaterThan([7, 0]) ? []: [null]
);
}, GLOBAL.SERVERS.OPEN);
});
2 changes: 1 addition & 1 deletion packages/client/lib/commands/XCLAIM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ export function transformArguments(
return args;
}

export { transformStreamMessagesReply as transformReply } from './generic-transformers';
export { transformStreamMessagesNullReply as transformReply } from './generic-transformers';
33 changes: 33 additions & 0 deletions packages/client/lib/commands/generic-transformers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
transformStringNumberInfinityArgument,
transformTuplesReply,
transformStreamMessagesReply,
transformStreamMessagesNullReply,
transformStreamsMessagesReply,
transformSortedSetWithScoresReply,
pushGeoCountArgument,
Expand Down Expand Up @@ -219,6 +220,38 @@ describe('Generic Transformers', () => {
);
});

it('transformStreamMessagesNullReply', () => {
assert.deepEqual(
transformStreamMessagesNullReply([null, ['0-0', ['0key', '0value']]]),
[null, {
id: '0-0',
message: Object.create(null, {
'0key': {
value: '0value',
configurable: true,
enumerable: true
}
})
}]
);
});

it('transformStreamMessagesNullReply', () => {
assert.deepEqual(
transformStreamMessagesNullReply([null, ['0-1', ['11key', '11value']]]),
[null, {
id: '0-1',
message: Object.create(null, {
'11key': {
value: '11value',
configurable: true,
enumerable: true
}
})
}]
);
});

describe('transformStreamsMessagesReply', () => {
it('null', () => {
assert.equal(
Expand Down
28 changes: 18 additions & 10 deletions packages/client/lib/commands/generic-transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,27 @@ export interface StreamMessageReply {
message: Record<string, RedisCommandArgument>;
}

export type StreamMessagesReply = Array<StreamMessageReply>;
export function transformStreamMessageReply([id, message]: Array<any>): StreamMessageReply {
return {
id,
message: transformTuplesReply(message)
};
}

export function transformStreamMessagesReply(reply: Array<any>): StreamMessagesReply {
const messages = [];
export function transformStreamMessageNullReply(reply: Array<any>): StreamMessageReply | null {
if (reply === null) return null;
return transformStreamMessageReply(reply);
}

for (const [id, message] of reply) {
messages.push({
id,
message: transformTuplesReply(message)
});
}

return messages;
export type StreamMessagesReply = Array<StreamMessageReply>;
export function transformStreamMessagesReply(reply: Array<any>): StreamMessagesReply {
return reply.map(transformStreamMessageReply);
}

export type StreamMessagesNullReply = Array<StreamMessageReply | null>;
export function transformStreamMessagesNullReply(reply: Array<any>): StreamMessagesNullReply {
return reply.map(transformStreamMessageNullReply);
}

export type StreamsMessagesReply = Array<{
Expand Down

0 comments on commit eb2c37e

Please sign in to comment.