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

Fix portal & server generate password test sometimes fail #4666

Merged

Conversation

pkong-ds
Copy link
Contributor

@pkong-ds pkong-ds commented Aug 27, 2024

Failing examples

The math

Why it happened

Think it happened at least 3 times since it got merged. Was curious why so I did some math.

all combinations of `[1-9]{8}` = 9^8
all combinations of `[0-9]{8}` = 10^8


P(1st trial fail) = 1
    because `if (!ranOnce) return 0`
P(2nd trial fail) = [1 - (9^8 / 10^8)] = 0.5695
P(3nd trial fail) = [1 - (9^8 / 10^8)]^2 = 0.5695^2 = 0.3243
...
P(10th trial fail) = [1 - (9^8 / 10^8)]^9 = 0.5695^9 = 0.00630

0.00630 = test will fail 6 times out of 1000 CI runs, which is not that unlikely, so it failing 3 times is kind of expected.

How to prevent

Say we aim for only fail once in 100,000 CI runs

[1 - (9^8 / 10^8)]^n <= 0.000001
n >= 24.53

Which gives the solution n = 25. So I increased to 25 here

How do I know it works?

Ran below locally, let's see if it fails 🕵️

for i in {1..100000}; do npx jest passwordGenerator.test.ts --silent || (echo "Failed after $i attempts" && break); echo $i; done

EDIT2: ran to 816710774 w/o failing, will stop for now

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.534 s, estimated 2 s
10774
 PASS  src/util/passwordGenerator.test.ts

@pkong-ds pkong-ds force-pushed the fix-portal-password-generator-test branch from 2821969 to e384312 Compare August 27, 2024 16:56
@pkong-ds pkong-ds changed the title Fix portal generate password test often fail Fix portal generate password test sometimes fail Aug 27, 2024
@louischan-oursky louischan-oursky self-requested a review August 28, 2024 03:01
@louischan-oursky louischan-oursky self-assigned this Aug 28, 2024
@louischan-oursky
Copy link
Collaborator

Thanks for the calculation. But we do not want to modify the original behavior of 10 times. Instead we should make it able to use N instead of 10 in the test only. Can you also apply the same logic to the server generator? They are twin.

@pkong-ds
Copy link
Contributor Author

okok, lemme update after linter is ready 🙏

@pkong-ds pkong-ds force-pushed the fix-portal-password-generator-test branch from e384312 to b675e0d Compare August 28, 2024 09:33
@@ -74,9 +74,11 @@ export function generatePasswordWithSource(

export function internalGeneratePasswordWithSource(
source: RandSource,
policy: PasswordPolicyConfig
policy: PasswordPolicyConfig,
overrideMaxTrials?: number
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not make it optional. Make it required. But the public API must remain unchanged.

@pkong-ds pkong-ds force-pushed the fix-portal-password-generator-test branch from b675e0d to 7002603 Compare August 28, 2024 19:38
@pkong-ds
Copy link
Contributor Author

Updated, thanks! CI pass locally

@pkong-ds pkong-ds changed the title Fix portal generate password test sometimes fail Fix portal & server generate password test sometimes fail Aug 28, 2024
@louischan-oursky louischan-oursky force-pushed the fix-portal-password-generator-test branch from 7002603 to 03e1461 Compare August 29, 2024 03:28
@louischan-oursky louischan-oursky merged commit ba44d85 into authgear:main Aug 29, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants