Flaky Test: Fix AtomicInternalId flakiness in Deployment spec by shadowing project scope
-
Please check this box if this contribution uses AI-generated content (including content generated by GitLab Duo features) as outlined in the GitLab DCO & CLA. As a benefit of being a GitLab Community Contributor, you receive complimentary access to GitLab Duo.
What does this MR do and why?
The 'AtomicInternalId' shared example in deployment_spec.rb was
flaky, intermittently failing with ActiveRecord::RecordNotUnique
on the index_deployments_on_project_id_and_iid unique constraint.
This occurred because the shared example was using the same project
instance that had other Deployment records (and thus InternalId
state for deployments) created by top-level let_it_be blocks in the
spec file. When the shared example attempted to generate IIDs for its
new Deployment instance within this pre-populated project scope, it
could lead to IID collisions if the AtomicInternalId's init logic
re-calculated the starting IID based on existing records in a way that
conflicted with already assigned IIDs (especially iid=1).
This commit fixes the flakiness by providing a dedicated, fresh
Project instance (iid_test_project) and its associated
Environment and Deployable specifically for the context of the
it_behaves_like 'AtomicInternalId' block. This ensures that the
IID generation tests within the shared example operate in a clean,
isolated scope, preventing collisions with IIDs from other test setup.
References
Screenshots or screen recordings
N/A
How to set up and validate locally
- Check out this branch.
- Run the
deployment_spec.rbfile multiple times:bundle exec rspec spec/models/deployment_spec.rb --order random - The test should now pass consistently.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.