Skip to content

Commit 518eb1a

Browse files
committed
fix(client): Isolate exceptional logic to xAutoClaim
1 parent 0d8d05e commit 518eb1a

File tree

4 files changed

+103
-41
lines changed

4 files changed

+103
-41
lines changed

packages/client/lib/commands/XAUTOCLAIM.spec.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('XAUTOCLAIM', () => {
2323
});
2424
});
2525

26-
testUtils.testWithClient('client.xAutoClaim', async client => {
26+
testUtils.testWithClient('client.xAutoClaim without messages', async client => {
2727
await Promise.all([
2828
client.xGroupCreate('key', 'group', '$', {
2929
MKSTREAM: true
@@ -39,4 +39,59 @@ describe('XAUTOCLAIM', () => {
3939
}
4040
);
4141
}, GLOBAL.SERVERS.OPEN);
42+
43+
testUtils.testWithClient('client.xAutoClaim with messages', async client => {
44+
const [,,id,] = await Promise.all([
45+
client.xGroupCreate('key', 'group', '$', {
46+
MKSTREAM: true
47+
}),
48+
client.xGroupCreateConsumer('key', 'group', 'consumer'),
49+
client.xAdd('key', '*', { foo: 'bar' }),
50+
client.xReadGroup('group', 'consumer', { key: 'key', id: '>' })
51+
]);
52+
53+
assert.deepEqual(
54+
await client.xAutoClaim('key', 'group', 'consumer', 1, '0-0'),
55+
{
56+
nextId: '0-0',
57+
messages: [{
58+
id,
59+
message: Object.create(null, { 'foo': {
60+
value: 'bar',
61+
configurable: true,
62+
enumerable: true
63+
} })
64+
}]
65+
}
66+
);
67+
}, GLOBAL.SERVERS.OPEN);
68+
69+
testUtils.testWithClient('client.xAutoClaim with trimmed messages', async client => {
70+
const [,,,,,id2,] = await Promise.all([
71+
client.xGroupCreate('key', 'group', '$', {
72+
MKSTREAM: true
73+
}),
74+
client.xGroupCreateConsumer('key', 'group', 'consumer'),
75+
client.xAdd('key', '*', { foo: 'bar' }),
76+
client.xReadGroup('group', 'consumer', { key: 'key', id: '>' }),
77+
client.xTrim('key', 'MAXLEN', 0),
78+
client.xAdd('key', '*', { bar: 'baz' }),
79+
client.xReadGroup('group', 'consumer', { key: 'key', id: '>' }),
80+
]);
81+
82+
assert.deepEqual(
83+
await client.xAutoClaim('key', 'group', 'consumer', 1, '0-0'),
84+
{
85+
nextId: '0-0',
86+
messages: [{
87+
id: id2,
88+
message: Object.create(null, { 'bar': {
89+
value: 'baz',
90+
configurable: true,
91+
enumerable: true
92+
} })
93+
}]
94+
}
95+
);
96+
}, GLOBAL.SERVERS.OPEN);
4297
});

packages/client/lib/commands/XAUTOCLAIM.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { RedisCommandArgument, RedisCommandArguments } from '.';
2-
import { StreamMessagesReply, transformStreamMessagesReply } from './generic-transformers';
2+
import { StreamMessageReply, transformTuplesReply } from './generic-transformers';
33

44
export const FIRST_KEY_INDEX = 1;
55

@@ -28,12 +28,32 @@ type XAutoClaimRawReply = [RedisCommandArgument, Array<any>];
2828

2929
interface XAutoClaimReply {
3030
nextId: RedisCommandArgument;
31-
messages: StreamMessagesReply;
31+
messages: XAutoClaimMessagesReply;
32+
}
33+
34+
type XAutoClaimMessagesReply = Array<StreamMessageReply | null>;
35+
36+
function transformXAutoClaimMessagesReply(reply: Array<any>): XAutoClaimMessagesReply {
37+
const messages = [];
38+
39+
for (const tuple of reply) {
40+
if (tuple === null) {
41+
continue;
42+
}
43+
44+
const [id, message] = tuple;
45+
messages.push({
46+
id,
47+
message: transformTuplesReply(message)
48+
});
49+
}
50+
51+
return messages;
3252
}
3353

3454
export function transformReply(reply: XAutoClaimRawReply): XAutoClaimReply {
3555
return {
3656
nextId: reply[0],
37-
messages: transformStreamMessagesReply(reply[1])
57+
messages: transformXAutoClaimMessagesReply(reply[1])
3858
};
3959
}

packages/client/lib/commands/generic-transformers.spec.ts

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -194,39 +194,30 @@ describe('Generic Transformers', () => {
194194
);
195195
});
196196

197-
describe('transformStreamMessagesReply', () => {
198-
it('with null', () => {
199-
assert.deepEqual(
200-
transformStreamMessagesReply([null]),
201-
[]
202-
);
203-
})
204-
205-
it('with messages', () => {
206-
assert.deepEqual(
207-
transformStreamMessagesReply([['0-0', ['0key', '0value']], ['1-0', ['1key', '1value']]]),
208-
[{
209-
id: '0-0',
210-
message: Object.create(null, {
211-
'0key': {
212-
value: '0value',
213-
configurable: true,
214-
enumerable: true
215-
}
216-
})
217-
}, {
218-
id: '1-0',
219-
message: Object.create(null, {
220-
'1key': {
221-
value: '1value',
222-
configurable: true,
223-
enumerable: true
224-
}
225-
})
226-
}]
227-
);
228-
})
229-
})
197+
it('transformStreamMessagesReply', () => {
198+
assert.deepEqual(
199+
transformStreamMessagesReply([['0-0', ['0key', '0value']], ['1-0', ['1key', '1value']]]),
200+
[{
201+
id: '0-0',
202+
message: Object.create(null, {
203+
'0key': {
204+
value: '0value',
205+
configurable: true,
206+
enumerable: true
207+
}
208+
})
209+
}, {
210+
id: '1-0',
211+
message: Object.create(null, {
212+
'1key': {
213+
value: '1value',
214+
configurable: true,
215+
enumerable: true
216+
}
217+
})
218+
}]
219+
);
220+
});
230221

231222
describe('transformStreamsMessagesReply', () => {
232223
it('null', () => {

packages/client/lib/commands/generic-transformers.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,6 @@ export type StreamMessagesReply = Array<StreamMessageReply>;
9797
export function transformStreamMessagesReply(reply: Array<any>): StreamMessagesReply {
9898
const messages = [];
9999

100-
if (reply[0] === null) {
101-
return messages;
102-
}
103-
104100
for (const [id, message] of reply) {
105101
messages.push({
106102
id,

0 commit comments

Comments
 (0)