Flaky test: 'generate creates unique IDs using the purely random portion of a UUID'
ProfileIdTest > generate creates unique IDs using the purely random portion of a UUID FAILED
java.lang.AssertionError: All 1000 generated IDs should be unique expected:<1000> but was:<999>
at org.junit.Assert.fail(Assert.java:89)
at org.junit.Assert.failNotEquals(Assert.java:835)
at org.junit.Assert.assertEquals(Assert.java:120)
at com.ichi2.anki.multiprofile.ProfileIdTest.generate creates unique IDs using the purely random portion of a UUID(ProfileIdTest.kt:43)
@criticalAY FYI
I've seen this a couple of times
- https://github.com/ankidroid/Anki-Android/actions/runs/35367359921/job/105674207308?pr=21904
- https://github.com/ankidroid/Anki-Android/actions/runs/34213732676/job/102020639678
I looked into this issue and was able to reproduce/confirm the flaky behavior around ProfileId.generate() and the uniqueness assertion in ProfileIdTest.
From what I understand, the test relies on 1000 randomly generated 8-character UUID prefixes being collision-free, which can occasionally fail because the generated portion is only 32 bits. I’m thinking the test should be made deterministic rather than relying on probabilistic uniqueness, while keeping the current ProfileId format unchanged unless there’s a requirement to change it.
Would it be okay for me to work on this issue and submit a PR with the proposed test fix?
@Rajat7Codes Can I confirm you've read and are following our AI Policy before you begin?
How do you plan to make the test deterministic? If production is random, but the test is deterministic, then the test won't mirror the production code under test
Sorry, I did not know about the Ai Policy. I did use AI actually, to understand the issue and traverse through code. I promise will not be doing the same in here or code editor. Should I try this myself or look elsewhere?
Thanks so much for your honesty!
Better to pick another issue, this issue exists for mentoring, and most of that value will be lost if you already got a solution from an LLM
Hey I wanted to work on this issue, I would appreciate it if you would point me towards the relevant material as I am new to the codebase
Hi, what relevant material do you need?
Sorry, had trouble finding where the code was but I've found it now. From what I can tell, the root cause of the issue is that there is very small chance two of the generated strings can be the same, which increases greatly as the number of tests run increases. The chance is about 1 in every 8600 runs. Still working on an actual solution will get back to you as soon as I am able
@aakarshitv do you still have time for this?