Skip to content
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: Emails Budgets Selected/Unselected investment #2695

Conversation

javierv
Copy link
Contributor

@javierv javierv commented Jun 22, 2018

References

Objectives

Fix the flaky specs in spec/features/emails_spec.rb:431 ("Emails Budgets Selected investment") and spec/features/emails_spec.rb:454 ("Emails Budgets Unselected investment").

Explain why the test is flaky, or under which conditions/scenario it fails randomly

The code in both specs is similar, so here's one of them (unnecessary lines omitted):

investment1 = create(:budget_investment, :selected,   author: author1, budget: budget)
investment2 = create(:budget_investment, :selected,   author: author2, budget: budget)
# ...
budget.email_selected
# ...
email = open_last_email
investment = investment2
expect(email).to have_subject("Your investment project '#{investment.code}' has been selected")

The test assumes the last email sent will be the one addressed to the author of the last investment created. However, this isn't always the case. The code in budget.email_selected is:

def email_selected
  investments.selected.each do
  # ...

And this code doesn't always return the selected investments in the order they were created, since postgresql by default doesn't guarantee in which order the records will be returned.

Explain why your PR fixes it

By ordering the selected investments by ID, we ensure the last email is sent to the author of the last created investment, as the specs assume.

Notes

  • The implementation can probably be improved; suggestions are welcomed. IMHO the important thing is knowing these tests were failing because open_last_email doesn't always return the email for the last created investment.

@@ -152,13 +152,13 @@ def investments_orders
end

def email_selected
investments.selected.each do |investment|
investments.selected.order(:id).each do |investment|
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit message says:

Ordering by id is certainly not very elegant; however, ordering by
another field like created_at is dangerous because the record could be
created at (almost) the exact same time.

However, created_at stores the time up to microseconds, so the chance of two records being created at the exact same time is zero.

These tests were failing randomly because there is no guarantee
the methods `Budget#email_selected` and `Budget#email_unselected` will
always send the emails in the same order, because `investments.selected`
and `investments.unselected` don't necessarily return the records in the
order they were created.

Ordering by id is certainly not very elegant; however, ordering by
another field like `created_at` is dangerous because the record could be
created at (almost) the exact same time.

Related to issue consuldemocracy#2446 and issue consuldemocracy#2519.
@javierv javierv force-pushed the 2446_and_2519-fix_flaky_emails_spec branch from e1412da to 37eaf29 Compare July 3, 2018 21:59
@javierv javierv changed the title Fix flaky emails spec. Fix flaky specs: Emails Budgets Selected/Unselected investment Jul 6, 2018
@voodoorai2000
Copy link
Member

Great! 👌
Thanks @javierv!

@voodoorai2000 voodoorai2000 merged commit eb13c83 into consuldemocracy:master Jul 12, 2018
@javierv javierv deleted the 2446_and_2519-fix_flaky_emails_spec branch July 12, 2018 15:09
@javierm javierm self-assigned this Nov 11, 2021
@javierm javierm added the Bug label Nov 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants