Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Backport rpoplpush should rotate the list when source and destintation are the same #1322

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'])
})
})
})