diff --git a/packages/client/lib/client/commands-queue.spec.ts b/packages/client/lib/client/commands-queue.spec.ts new file mode 100644 index 00000000000..f15f7c8f5d7 --- /dev/null +++ b/packages/client/lib/client/commands-queue.spec.ts @@ -0,0 +1,26 @@ +import assert from 'node:assert'; +import RedisCommandsQueue from './commands-queue'; + +describe('RedisCommandsQueue', () => { + function createQueue() { + return new RedisCommandsQueue(3, null, () => {}, 'test-client'); + } + + describe('extractAllCommands', () => { + it('returns and removes every queued command, not just the first one', () => { + const queue = createQueue(); + for (let i = 0; i < 5; i++) { + queue.addCommand([`CMD${i}`]).catch(() => {}); + } + + const extracted = queue.extractAllCommands(); + + assert.strictEqual(extracted.length, 5); + assert.deepStrictEqual( + extracted.map(command => command.args?.[0]), + ['CMD0', 'CMD1', 'CMD2', 'CMD3', 'CMD4'], + ); + assert.strictEqual(queue.extractAllCommands().length, 0); + }); + }); +}); diff --git a/packages/client/lib/client/commands-queue.ts b/packages/client/lib/client/commands-queue.ts index af909351382..c9ed472c085 100644 --- a/packages/client/lib/client/commands-queue.ts +++ b/packages/client/lib/client/commands-queue.ts @@ -665,15 +665,16 @@ export default class RedisCommandsQueue { } /** - * Gets all commands from the write queue without removing them. + * Extracts all commands from the write queue. */ extractAllCommands(): CommandToWrite[] { const result: CommandToWrite[] = []; let current = this.#toWrite.head; while (current) { result.push(current.value); - this.#toWrite.remove(current); + const toRemove = current; current = current.next; + this.#toWrite.remove(toRemove); } return result; }