From f2d19d53f35f3119ed00aa29d019f53630f4bfc6 Mon Sep 17 00:00:00 2001 From: Kevin Boyer Date: Mon, 17 Jun 2024 10:57:15 -0400 Subject: [PATCH] Fix flaky test test_bulk_upsert (#229) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Ticket n/a ## Changes - Make the IDs deterministic and guaranteed to be unique ## Context for reviewers - Since the IDs were random, there was a small chance (but [higher than you might intuitively expect](https://en.wikipedia.org/wiki/Birthday_problem)!) of generating a collision. This would trigger integrity errors or cause the behavior of the test to be not what you'd expect (a new "insert" would actually be a "modify", for example) ## Testing First, (temporarily) add pytest-repeat: `poetry add pytest-repeat` Then run the test several thousand times: `make test args="tests/src/db/test_bulk_ops.py --count 2500"` You should observe some failures because a duplicate ID is generated: Screenshot 2024-06-17 at 8 37 58 AM Then, re-run after applying the patch: Screenshot 2024-06-17 at 8 53 47 AM --- app/tests/src/db/test_bulk_ops.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/tests/src/db/test_bulk_ops.py b/app/tests/src/db/test_bulk_ops.py index 8b72d14..69ea88d 100644 --- a/app/tests/src/db/test_bulk_ops.py +++ b/app/tests/src/db/test_bulk_ops.py @@ -15,9 +15,14 @@ class Number: num: int +_next_id = 0 + + def get_random_number_object() -> Number: + global _next_id + _next_id += 1 return Number( - id=str(random.randint(1000000, 9999999)), + id=str(_next_id), num=random.randint(1, 10000), )