session icon indicating copy to clipboard operation
session copied to clipboard

Potential bug in unit test for idGenerator

Open U-4-E-A opened this issue 1 year ago • 2 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the issue has not already been raised

Issue

https://github.com/fastify/session/blob/master/test/idGenerator.test.js

Both tests appear to have the same error - they are not using Set.add() to add the newly generated ID to the set so ids.has(id) is checking for the id in an empty Set.

const ids = new Set()

for (let i = 0; i < (cacheSize); ++i) {
  const id = idGen()
  if (ids.has(id)) {
    t.fail('had a collision')
  }
  t.equal(id.length, 32)
}

I believe this should be: -

const ids = new Set()

for (let i = 0; i < (cacheSize); ++i) {
  const id = idGen()
  if (ids.has(id)) {
    t.fail('had a collision')
  }
  t.equal(id.length, 32)
  ids.add(id) <--------------------------------------------
}

U-4-E-A avatar Sep 09 '24 13:09 U-4-E-A

Good catch

climba03003 avatar Sep 09 '24 15:09 climba03003

Thanks for reporting! Would you like to send a Pull Request to address this issue?

mcollina avatar Sep 28 '24 09:09 mcollina