-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Closed
Labels
c: bugSomething isn't workingSomething isn't workinghas workaroundWorkaround provided or linkedWorkaround provided or linkedm: helpersSomething is referring to the helpers moduleSomething is referring to the helpers modulem: numberSomething is referring to the number moduleSomething is referring to the number modulep: 1-normalNothing urgentNothing urgent
Milestone
Description
Pre-Checks
- Follow our Code of Conduct.
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Faker issue and not related to a combination with another package.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
- I am willing to provide a PR.
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 thanmaxTime
, resulting in the thrown error
Suggestion:
- A workaround we can do on our end is pass a very large
maxTime
. - but
unique
should rely onmaxRetries
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 workingSomething isn't workinghas workaroundWorkaround provided or linkedWorkaround provided or linkedm: helpersSomething is referring to the helpers moduleSomething is referring to the helpers modulem: numberSomething is referring to the number moduleSomething is referring to the number modulep: 1-normalNothing urgentNothing urgent