Skip to content

Commit

Permalink
fix: Backport rpoplpush should rotate the list when source and destin…
Browse files Browse the repository at this point in the history
…tation are the same (#1322)
  • Loading branch information
mgrunberg committed Sep 29, 2023
1 parent 6e9bf07 commit 72c62c5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/commands/rpoplpush.js
Expand Up @@ -22,11 +22,18 @@ export function rpoplpush(source, destination) {
const newSource = this.data.get(source)
const item = newSource.pop()

const newDest = this.data.get(destination)
let newDest = newSource // Operate on the same list
if (source !== destination) {
// Operate on two different lists
newDest = this.data.get(destination)
}

newDest.unshift(item)

this.data.set(source, newSource)
this.data.set(destination, newDest)
if (newSource !== newDest) {
this.data.set(destination, newDest)
}

return item
}
Expand Down
14 changes: 14 additions & 0 deletions test/integration/commands/rpoplpush.js
Expand Up @@ -104,5 +104,19 @@ runTwinSuite('rpoplpush', (command, equals) => {
return expect(err.message).toBe('Key bar does not contain a list')
})
})

it('should rotate the list if the source and destination are the same', async () => {
const redis = new Redis({
data: {
foo: ['1', '2'],
},
})

const result = await redis[command]('foo', 'foo', 'LEFT', 'RIGHT');
expect(result).toBe('2');

const list = await redis.lrange('foo', 0, -1)
expect(list).toEqual(['2', '1'])
})
})
})

0 comments on commit 72c62c5

Please sign in to comment.