Skip to content

Commit

Permalink
fix(xadd): Accept odd number of parameters (#1111)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-nau authored Jan 21, 2022
1 parent 04fcbca commit b6805ed
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/commands/xadd.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
export function xadd(stream, id, ...args) {
if (!stream || !id || args.length === 0 || args.length % 2 !== 0) {
if (
!stream ||
!id ||
args.length === 0 ||
(args.includes('~') ? args.length < 4 : args.length % 2 !== 0)
) {
throw new Error("ERR wrong number of arguments for 'xadd' command");
}
if (!this.data.has(stream)) {
Expand Down
22 changes: 22 additions & 0 deletions test/commands/xadd.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ describe('xadd', () => {
expect(redis.data.get(`stream:stream:${id}`)).toEqual({
polled: false,
});
})
.then(() =>
redis.xadd(
'stream',
'MAXLEN',
'~',
'50',
'*',
'reading',
'{"key": "value"}'
)
)
.then((id) => {
expect(id).toBe('MAXLEN-0');
expect(redis.data.get('stream')).toEqual([
['1-0', ['key', 'val']],
['2-0', ['key', 'val']],
['MAXLEN-0', ['~', '50', '*', 'reading', '{"key": "value"}']],
]);
expect(redis.data.get(`stream:stream:${id}`)).toEqual({
polled: false,
});
});
});

Expand Down

0 comments on commit b6805ed

Please sign in to comment.