Conversation
Because: * The hash and luckyNumber tests hashed 1000 Math.random() values and asserted every result was unique. The hash is 32 bits, so by the birthday paradox a collision occurred roughly 1% of the time. This commit: * Uses fixed keys instead of Math.random(). * Replaces the uniqueness assertions with the behaviour actually under test: output range, stability for a given key, and spread across the output range. Fixes FXA-14281
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes unit-test flakiness in fxa-shared experiments tests by removing randomness from hash/luckyNumber inputs and replacing collision-based assertions with deterministic distribution and stability checks.
Changes:
- Replace
Math.random()inputs with deterministic keys (key-0…key-999) for repeatable test runs. - Update
hashandluckyNumbertests to validate range, determinism for a given key, and basic spread across the output space.
Suppressed comments (1)
packages/fxa-shared/test/experiments/base.js:72
- This range check relies on JS coercion; e.g. a string value could still satisfy the comparison. Adding a type assertion keeps the test aligned with the method contract (
luckyNumberreturns anumberinexperiments/base.ts).
const luckyNumber = experiment.luckyNumber(key);
assert.ok(0 <= luckyNumber && luckyNumber <= 1);
});
Comment on lines
+47
to
+49
| hashes.forEach((hash) => { | ||
| assert.ok(0 <= hash && hash < MAX_HASH_VALUE); | ||
| hashes[i] = hash; | ||
| } | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Because
The
hashandluckyNumbertests inpackages/fxa-shared/test/experiments/base.jshashed 1000Math.random()values and asserted every result was unique. The hash is 32 bits, so by the birthday paradox a collision is expected roughly 1% of the time (~1 - e^(-1000²/2^33)) — this flaked the Unit Test job on the v1.342.0 stage deploy pipeline.This pull request
key-0…key-999) instead ofMath.random(), so the tests are deterministic.Issue that this pull request solves
Fixes: FXA-14281
Checklist
Put an
xin the boxes that applyScreenshots (Optional)
N/A
Other information (Optional)
Verified locally: all 20 tests in the file pass.