Skip to content

faker.datatype.number often gives duplicates #2355

@frankandrobot

Description

@frankandrobot

Pre-Checks

Describe the bug

  • In unit tests running on our CI environment (and occasionally on our macs), the following code has been observed to throw a "duplicate error"---where it cannot find a unique integer
  • fakeId is called 50--100 times in total on the offending test runs.
  • the rate of failure on CI is around 1 in 1000 runs
  • haven't done the math but it is still very surprising because we are generating up the "max safe integer"... which is equal to 2^53 – 1... a very, very large number, so I don't expect 100 generations to render duplicates
export const fakeId = () =>
  faker.unique(() => faker.datatype.number({ max: Number.MAX_SAFE_INTEGER }), undefined, { maxTime: 100 }).toString();

Analysis

  • The mersenne implementation is very sus---why use this at all? For the most part, fakerjs seems to be a test helper, so IMO, this is over engineering. We can likely just use Math.random under the hood
  • Additionally, the implementation of unique is also sus on CI environments. Namely, this block:
    https://github.com/faker-js/faker/blob/next/src/modules/helpers/unique.ts#L114-L122
  • Most likely what's happening is the following---in our CI environments, we have a known issue with slowness. The mersenne implementation will unnecessarily return duplicates. And when both happen, now - startTime will be greater than maxTime, resulting in the thrown error

Suggestion:

  • A workaround we can do on our end is pass a very large maxTime.
  • but unique should rely on maxRetries only
  • faker should have an option to opt-out of using mersenne

Minimal reproduction code

  • see above

Additional Context

No response

Environment Info

OS: linux/macosx
Node: 16
faker: 7.3.0

Which module system do you use?

  • CJS
  • ESM

Used Package Manager

npm

Metadata

Metadata

Assignees

Labels

c: bugSomething isn't workinghas workaroundWorkaround provided or linkedm: helpersSomething is referring to the helpers modulem: numberSomething is referring to the number modulep: 1-normalNothing urgent

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions