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 IdentityLinker test #10807

Merged
merged 2 commits into from
Jun 12, 2024
Merged

Conversation

mitchellhenke
Copy link
Contributor

🛠 Summary of changes

I got a test failure this morning due to a time-based flakiness:

Failures:
  1) IdentityLinker#link_identity identity.verified_at the request is includes identity proofing and verified_at is not null does not set the timestamp
     Failure/Error:
       expect(
         user.last_identity.verified_at,
       ).to be_within(1.second).of(1.week.ago)
       expected 2024-06-05 00:46:12.000000000 +0200 to be within 1 of 2024-06-05 00:46:13.011929850 +0200
     # ./spec/services/identity_linker_spec.rb:236:in `block (5 levels) in <top (required)>'
     # ./spec/support/fake_analytics.rb:181:in `block (2 levels) in <top (required)>'

This PR freezes time for the test to make it more reliable

changelog: Internal, Continuous Integration, Fix flaky IdentityLinker test
@mitchellhenke mitchellhenke requested a review from a team June 12, 2024 16:50
@mitchellhenke mitchellhenke merged commit dc53280 into main Jun 12, 2024
2 checks passed
@mitchellhenke mitchellhenke deleted the mitchellhenke/fix-flaky-test-6 branch June 12, 2024 17:10
brandemix pushed a commit to brandemix/18F-identity-idp that referenced this pull request Jun 17, 2024
* Fix flaky IdentityLinker test

changelog: Internal, Continuous Integration, Fix flaky IdentityLinker test

* fix extraneous "is" in test description
it 'does not set the timestamp' do
freeze_time
Copy link
Member

Choose a reason for hiding this comment

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

Does this risk bleeding into tests run after this one? In other tests we use around like this, to ensure the teardown:

around do |example|
  feeze_time { example.run }
end

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 teardown here should take care of it: https://api.rubyonrails.org/v7.1.1/classes/ActiveSupport/Testing/TimeHelpers.html#method-i-after_teardown

I did some testing and it seems to be called after every test.

Copy link
Member

Choose a reason for hiding this comment

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

Interesting. It definitely seems easier to just have the one line of code, though generally speaking I feel a little uneasy about any sort of side-effecty code like this inside a test case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was hoping to have the freeze_time block around it, but the helper doesn't like nested time traveling blocks:

     RuntimeError:

       Calling `travel_to` with a block, when we have previously already made a call to `travel_to`, can lead to confusing time stubbing.

       Instead of:

          travel_to 2.days.from_now do
            # 2 days from today
            travel_to 3.days.from_now do
              # 5 days from today
            end
          end

       preferred way to achieve above is:

          travel 2.days do
            # 2 days from today
          end

          travel 5.days do
            # 5 days from today
          end

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.

3 participants