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

Flaky spec: booth_spec.rb #1343

Conversation

iagirre
Copy link

@iagirre iagirre commented Mar 8, 2018

References

Objectives

Fix the flaky that appeared in spec/features/officing/booth_spec.rb:53, spec/features/officing/booth_spec.rb:82 and spec/features/officing/booth_spec.rb:33.

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

(This explanation is the same as in #1342 PR because the problem is exactly the same).

The problems was that the tests were not reaching to the residence verification page, so they failed in their assertions. To figure out what was going on there, I studied the flow the test follows so that I could find the scenarios where it would crash, and, after doing it, step by step, I determined that the test will fail when:

  • There are more than 1 officer_assignments (booths) for this user, so the page will redirect to the select booth page.
  • There aren't any officer_assignments, so that the user can't even reach the target page (it redirects to the root).

The code that determines that is:

# app/controller/officing/base_controller.rb
	[...]

	def load_officer_assignment
      @officer_assignments ||= current_user.poll_officer.officer_assignments.where(date: Time.current.to_date)
    end

    def verify_officer_assignment
      if @officer_assignments.blank?
        redirect_to officing_root_path, notice: t("officing.residence.flash.not_allowed")
      end
    end

    def verify_booth
      return unless current_booth.blank?
      booths = todays_booths_for_officer(current_user.poll_officer)
      case booths.count
      when 0
        redirect_to officing_root_path
      when 1
        session[:booth_id] = booths.first.id
      else
        redirect_to new_officing_booth_path
      end
    end

    [...]

    def todays_booths_for_officer(officer)
      officer.officer_assignments.by_date(Date.today).map(&:booth).uniq
    end

After trying to reproduce it (without success), I saw the new piece of information about the probable failing hours, and I realized that the problem could be in the moment of creating the objects. If the officer_assignment 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.

To prove that, I forced in the factory the dates of the officer_assignments to an earlier date, and it failed in the exact same way as the reported ones.

Explain why your PR fixes it

I stubed the Date and Time clases to force them to give me a date I can controll. I set that date (01/01/2018) to the objects that depend on it (poll_shift, officer_assignment and polls). This way, it doesn't matter when the test is executed, for it will always be the same date.

Visual Changes (if any)

There aren't, is a flaky.

Notes

  • I added stubs for Date.current, Date.today and Time.current (this are all the functions used to check the shifts and officer assignments) so that they all return a predefined date: 01/01/2018.
  • I stubed them in a before block, so that it works for all the test.
  • I set that date to the objects that depend on it (officer_assignment).

@voodoorai2000
Copy link
Member

👏😌
Running after midnight to doble check 👍

create(:poll_officer_assignment, officer: officer, booth_assignment: ba3, date: Date.today)
create(:poll_officer_assignment, officer: officer, booth_assignment: ba1, date: '2018-01-01')
create(:poll_officer_assignment, officer: officer, booth_assignment: ba2, date: '2018-01-01')
create(:poll_officer_assignment, officer: officer, booth_assignment: ba3, date: '2018-01-01')

Choose a reason for hiding this comment

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

Same doubt as in the other related PRs...can't Date.today be used to define the date attribute?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, I didn't realised before. I've changed it.

Copy link

@MariaCheca MariaCheca left a comment

Choose a reason for hiding this comment

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

👌Left a single comment, can you check it?

…in the booth_spec test and changed all the Date.current functions for a hardcoded date so that it won't give errors when running tests at midnight.
@iagirre iagirre force-pushed the 1202-flaky-booth-officer-with-multiple-booth-assignments-today branch from 216295a to 4863da8 Compare March 14, 2018 07:54
@MariaCheca MariaCheca merged commit 7778e2b into AyuntamientoMadrid:master Mar 14, 2018
@MariaCheca
Copy link

MariaCheca commented Mar 14, 2018

This PR fixes the flaky tests described in #1202, #1203 and #1204, so the backport to Consul will solve these three issues.

@raul-fuentes
Copy link

I'll backport this one

@raul-fuentes
Copy link

so I checked this Pr is not applicable in consul, so there is no backport

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants