Use array_matcher to fix flaky tests
What does this MR do and why?
Associated issue: #464351 (closed)
Trying to fix a flaky test in ee/spec/workers/work_items/rolledup_dates/update_milestone_related_work_item_dates_event_handler_spec.rb
. The test is randomly failing as we are expecting it to receive an array of 2 work items. The order of the array can randomly change during execution and the test will fail, as it will try to match the array and the order. But we don't care about the order of the array, we only care that the array contains those 2 work items. In order to fix that we can use the match_array
method, that ignores the order of the array and checks that the elements are the same.
You can see in the screenshot that the test failed as the order of the array is different but the elements are the same.
You can see more failures here: #464351 (comment 1927567702)
MR acceptance checklist
Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.
Screenshots or screen recordings
Screenshots are required for UI changes, and strongly recommended for all other merge requests.
Before | After |
---|---|
How to set up and validate locally
As this is a flaky test it is hard to replicated locally, but if you modify the spec file a bit you can replicate it.
In line 82: if you change the code to reverse the milestone_work_items
array you will see that the original spec is failing.
You can replicate that by changing the line 82 to:
expect_next_instance_of(service_class, milestone_work_items.reverse) do |service|
If we add the match_array
method, reversing the array does not matter anymore as the rspec checks for the content of the array and not the order
You can replicate that by changing the line 82 to:
expect_next_instance_of(service_class, match_array(milestone_work_items.reverse)) do |service|