-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 flaky specs: Officing Results Add/Edit results #2712
Fix flaky specs: Officing Results Add/Edit results #2712
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @javierv! Thanks a lot for your latest contributions to CONSUL 🎉
I was wondering if is it possible to implement a solution similar to those found in the PRs you mentioned 🤔
Monkey patching is not really my cup of tea 😅 and since we do plan on migrating to Rails 5 (hopefully very soon 🤞 —see #2631, #2627 and #2628), I think it'd be better to keep CONSUL's codebase as clean as possible and refactor if necessary when we jump into the Rails 5 bandwagon 😸
Let me know what you think!
Hi @aitbw. Thank you for your comments 👍! I've removed the monkey patch.
Would you mind helping me with that? I'm not familiar with the source code and even after checking the other PRs am not sure which factories need to be changed 🙀. |
🤔 You added a new solution on the latest commit. Does this solves the flakies as well? @javierv Because if that's the case, I don't think we'd need to implement something similar to the PRs you mentioned |
@aitbw Yes, the latest commit only refactors the code, doing the same thing without monkey patching. According to what I've tested locally, it solves the flakies (EDIT: but since my tests were completely missing the point, it didn't solve the flakies). But it would be great to have another pair of eyes have a look 😉. |
@aitbw Do you think it makes sense to backport the mentioned pull requests in order to complete this one, since they deal with similar flakies? P.S. (Sorry for the off-topic) I've just sent you an email. Hope it's OK 😰! If you haven't received it, it might be inside your spam folder. |
@aitbw I've done some more testing and I've realized I hadn't fixed anything 😅. I had forgotten to make sure factories were assigning dates dynamically. I've added another commit and tested it better, both manually and using Timecop, and it looks like this time the flakies are actually fixed. Somehow I hadn't realized until now that by changing my computer's date this can be tested anytime, and had also forgotten about |
09f8900
to
9285685
Compare
I've realized AyuntamientoMadrid#1351 only fixes one test in
AyuntamientoMadrid#1343 also fixes tests which aren't present on this repo. So only two pull requests would need to be backported. I've updated the description of this pull request accordingly. |
@@ -158,4 +159,7 @@ | |||
within('#total_results') { expect(page).to have_content('66') } | |||
end | |||
|
|||
after do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this block right after the background
block? It's easier to understand what's going to happen that way 😸
@@ -96,4 +100,7 @@ | |||
|
|||
end | |||
|
|||
after do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this block right after the background
block? It's easier to understand what's going to happen that way 😸
Amend if you can, there's no need to create a new commit for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
Hello once again @javierv, thanks big time for this PR 🎉 As usual, I'll be testing this thoroughly locally and on Travis to see how well it fares but seems like it'll do 😸 Re: the tests found on Ayuntamiento Madrid but not here —we have some custom functionality over there we don't plan to backport. Also, there are flakies which you'll only find on Ayuntamiento Madrid so that's why some specs, while they do test the same scenarios, will differ. |
df0a96c
to
d613ac4
Compare
As explained by @iagirre with pinpoint accuracy [1]: "If the officer_assignments are created at 23:59:59 and the rest of the test is executed after 00:00:00, the dates for the objects and the `Date.current` (used to check if there are any shifts today) won't be the same, because the shift will be for, lets say, 07/03/2018 and `Date.current` will be 08/03/2018, so, there are no shifts." Freezing time avoids this issue. Related to issues consuldemocracy#2520 and consuldemocracy#2521. [1] AyuntamientoMadrid#1342
It could have side effects (for example, a conflict after upgrading to Rails 5). Thanks @aitbw for the suggestion!
The tests depending on the date changing were still failing because Date.current was being stubbed after loading the factories. The following lines affected these specific tests: factory :poll_officer_assignment, class: 'Poll::OfficerAssignment' do (...) date Date.current end So if the tests were executed right before midnight, the sequence was: 1. The factories file was loaded, assigning Date.current to the date of every Poll::OfficerAssignment to be created. 2. Time passed, so now it was after midnight. 3. The `travel_to` method freezed time, after midnight. 4. A Poll::OfficerAssignment factory was created, using the date it was before midnight. Using dynamic fixtures solves the problem: factory :poll_officer_assignment, class: 'Poll::OfficerAssignment' do (...) date { Date.current } end Now the sequence is: 1. The factories file is loaded, and since it finds a block, doesn't assign a static value to every Poll::OfficerAssignment to be created. 2. Time passes, so now it's after midnight. 3. The `travel_to` method freezes time, after midnight. 4. A Poll::OfficerAssignment factory was created, and in executes the block, using the current date, that is, after midnight.
As it happened with results (commit 4a559ed), these tests failed if `Date.current` changes between the moment records are created and the moment the rest of the test is executed.
d613ac4
to
735eab8
Compare
@aitbw Thank you for your comments! I pushed the changes quickly so Travis' build would run right before midnight. However, I made a small mistake deleting a couple of empty lines which had nothing to do with this pull request. So I've rebased again. |
Very glad you figured out how this part of the codebase works, not trivial 👏 |
References
Pull Request Flaky spec: Residence Verify booth Unable to find visible css "#officing-booth" AyuntamientoMadrid/consul#1351Forked repo build with failed residence specsObjectives
Fix the flaky specs that appeared in
spec/features/officing/results_spec.rb:48
("Officing Results Add results"),spec/features/officing/results_spec.rb:85
("Officing Results Edit result"),spec/features/officing/residence_spec.rb:35
("Residence Assigned officers Verify voter"),spec/features/officing/residence_spec.rb:83
("Residence Assigned officers Error on Census (year of birth)"),spec/features/officing/residence_spec.rb:49
("Residence Assigned officers Error on verify") andspec/features/officing/residence_spec.rb:61
("Residence Assigned officers Error on Census (document number)").Explain why the test is flaky, or under which conditions/scenario it fails randomly
This explanation is copied from AyuntamientoMadrid#1342 because the problem is exactly the same. All credit to @iagirre.
The problems was that the test was not reaching to the residence verification page, so it couldn't find the select box for the DNI (and that's the error reported in the issue). To figure out what was going on there, [she] studied the flow that the test follows so that [she] could find the scenarios where it would crash, and, after doing it, step by step, [she] determined that the test will fail when:
officer_assignments
(booths) for this user, so the page will redirect to the select booth page, instead to the residence verification page.officer_assignments
, so that the user can't even reach the target page (it redirects to the root).The code that determines that is (this functions are run in
before_action
blocks):After trying to reproduce it (without success), [she] saw the new piece of information about the probable failing hours, and [she] realized that the problem could be in the moment of creating the objects. If the
officer_assignments
are created at 23:59:59 and the rest of the test is executed after 00:00:00, the dates for the objects and the Date.current (used to check if there are any shifts today) won't be the same, because the shift will be for, lets say, 07/03/2018 and Date.current will be 08/03/2018, so, there are no shifts and we find the first scenario described above.To prove that, [she] forced in the
factories.rb
the dates of theofficer_assignments
to an earlier date, and it failed in the exact same way as the reported one.Explain why your PR fixes it
Freezing time ensures the date won't change in the middle of a test, and using dynamic attributes for dates ensures factories don't assign static dates before freezing time. According to the factory bot documentation:
Notes
Gemfile
, but then realized Rails provides helpers to do exactly that and thought it was the easiest way. This decision is of course debatable; comments are welcome!ActiveSupport::Testing::TimeHelpers
won't be necessary. Theafter { travel_back }
block won't be necessary either.