Skip to content

Commit

Permalink
fix: rpoplpush should rotate the list when source and destintation ar…
Browse files Browse the repository at this point in the history
…e the same (#1321)
  • Loading branch information
mgrunberg authored Sep 29, 2023
1 parent 6165a8f commit 897af68
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/commands/rpoplpush.js
Original file line number Diff line number Diff line change
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
18 changes: 18 additions & 0 deletions test/integration/commands/rpoplpush.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,23 @@ runTwinSuite('rpoplpush', (command, equals) => {
)
}
)

// @TODO Rewrite test so it runs on a real Redis instance
;(process.env.IS_E2E ? it.skip : 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 897af68

Please sign in to comment.