Skip to content

Commit

Permalink
ref #2565 - handle null message in XAUTOCLAIM
Browse files Browse the repository at this point in the history
  • Loading branch information
leibale committed Jul 19, 2023
1 parent fdd1978 commit c9dae34
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
23 changes: 16 additions & 7 deletions packages/client/lib/commands/XAUTOCLAIM.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import XAUTOCLAIM from './XAUTOCLAIM';

describe('XAUTOCLAIM', () => {
describe.only('XAUTOCLAIM', () => {
testUtils.isVersionGreaterThanHook([6, 2]);

describe('transformArguments', () => {
Expand Down Expand Up @@ -31,26 +31,35 @@ describe('XAUTOCLAIM', () => {
}
});

const [, , id, , reply] = await Promise.all([
const [, , id1, id2, , , reply] = await Promise.all([
client.xGroupCreate('key', 'group', '$', {
MKSTREAM: true
}),
client.xGroupCreateConsumer('key', 'group', 'consumer'),
client.xAdd('key', '*', message),
client.xAdd('key', '*', message),
client.xReadGroup('group', 'consumer', {
key: 'key',
id: '>'
}),
client.xTrim('key', 'MAXLEN', 1),
client.xAutoClaim('key', 'group', 'consumer', 0, '0-0')
]);

assert.deepEqual(reply, {
nextId: '0-0',
messages: [{
id,
message
}],
deletedMessages: testUtils.isVersionGreaterThan([7, 0]) ? [] : undefined
...(testUtils.isVersionGreaterThan([7, 0]) ? {
messages: [{
id: id2,
message
}],
deletedMessages: [id1]
} : {
messages: [null, {
id: id2,
message
}]
})
});
}, {
client: GLOBAL.SERVERS.OPEN,
Expand Down
10 changes: 6 additions & 4 deletions packages/client/lib/commands/XAUTOCLAIM.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { RedisArgument, TuplesReply, BlobStringReply, ArrayReply, UnwrapReply, Command } from '../RESP/types';
import { StreamMessagesRawReply, transformStreamMessagesReply } from './generic-transformers';
import { RedisArgument, TuplesReply, BlobStringReply, ArrayReply, NullReply, UnwrapReply, Command } from '../RESP/types';
import { StreamMessageRawReply, isNullReply, transformStreamMessageReply } from './generic-transformers';

export interface XAutoClaimOptions {
COUNT?: number;
}

export type XAutoClaimRawReply = TuplesReply<[
nextId: BlobStringReply,
messages: StreamMessagesRawReply,
messages: ArrayReply<StreamMessageRawReply | NullReply>,
deletedMessages: ArrayReply<BlobStringReply>
]>;

Expand Down Expand Up @@ -40,7 +40,9 @@ export default {
transformReply(reply: UnwrapReply<XAutoClaimRawReply>) {
return {
nextId: reply[0],
messages: transformStreamMessagesReply(reply[1]),
messages: (reply[1] as unknown as UnwrapReply<typeof reply[1]>).map(message => {
return isNullReply(message) ? null : transformStreamMessageReply(message);
}),
deletedMessages: reply[2]
};
}
Expand Down
6 changes: 5 additions & 1 deletion packages/client/lib/commands/generic-transformers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { UnwrapReply, ArrayReply, BlobStringReply, BooleanReply, CommandArguments, DoubleReply, MapReply, NullReply, NumberReply, RedisArgument, TuplesReply } from '../RESP/types';
import { UnwrapReply, ArrayReply, BlobStringReply, BooleanReply, CommandArguments, DoubleReply, MapReply, NullReply, NumberReply, RedisArgument, TuplesReply, RespType } from '../RESP/types';

export function isNullReply(reply: unknown): reply is NullReply {
return reply === null;
}

export const transformBooleanReply = {
2: (reply: NumberReply<0 | 1>) => reply as unknown as UnwrapReply<typeof reply> === 1,
Expand Down

0 comments on commit c9dae34

Please sign in to comment.