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

Issue when dealing with Buffer keys #1242

Open
renatocron opened this issue Feb 26, 2023 · 0 comments
Open

Issue when dealing with Buffer keys #1242

renatocron opened this issue Feb 26, 2023 · 0 comments

Comments

@renatocron
Copy link

renatocron commented Feb 26, 2023

I am encoding an 8-byte key and passing it as a Buffer object to the mocked ioredis. While writing tests, I encountered this issue with IDs that encoded to different buffers but overwrite the same key on the fake server.

Redis server do overwrite the key, thankfully (^_^)

Here's a code to reproduce this issue:

const FakeRedis = require('ioredis-mock')

function id2Buffer(id) {
    // expect 32 u int
    const buffer = Buffer.alloc(8);
    buffer.write('#:');
    buffer.writeUIntLE(id, 2, 6); // Supports up to 48 bits of accuracy.

    return buffer;
}

const redis = new FakeRedis();

const id1 = 3294967294;
const id2 = id1 + 1; // works as excpected if changed to 2 or to -127 (other negatives numbers seens to also be in the same 'range' that cause the conflict)
const [idBuf1, idBuf2] = [id2Buffer(id1), id2Buffer(id2)];

(async () => {
    await redis.set(idBuf1, 'a');
    await redis.set(idBuf2, 'b');

    const r1 = await redis.get(idBuf1);
    const r2 = await redis.get(idBuf2);

    console.log({ r1, r2 }); // prints { r1: 'b', r2: 'b' } instead of { r1: 'a', r2: 'b' }

    const keys = await redis.keys('*');
    console.log(keys); // prints just one key
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants