-
Notifications
You must be signed in to change notification settings - Fork 35
[BSK] Fix flaky HistoryCoordinator test #2705
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
Conversation
|
|
||
| let expectation1 = XCTestExpectation(description: "Changes auto-committed") | ||
| historyStoringMock.saveCompletion = { expectation1.fulfill() } | ||
| await fulfillment(of: [expectation1], timeout: 5.0) |
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.
Now we wait for the first visit to properly complete before resetting the mock and performing the subsequent visit. Without this step, both visits were being recorded on the mock and the reset step below didn't have an effect.
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.
Pull request overview
This PR fixes a flaky test in the HistoryCoordinator test suite by properly synchronizing async operations. The test was failing intermittently because the mock's saved history entries array could contain visits in reverse order when the first visit hadn't completed before being reset.
Key Changes:
- Added proper synchronization by waiting for the initial visit to complete before resetting the mock state
- Enhanced assertions to verify exact count and boolean value instead of using optional chaining with default values
- Increased timeout values for the async expectations
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
SharedPackages/BrowserServicesKit/Tests/HistoryTests/HistoryCoordinatorTests.swift
Show resolved
Hide resolved
SharedPackages/BrowserServicesKit/Tests/HistoryTests/HistoryCoordinatorTests.swift
Show resolved
Hide resolved
miasma13
left a comment
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.
Works great, thanks for fixing it
<!-- Note: This template is a reminder of our Engineering Expectations and Definition of Done. Remove sections that don't apply to your changes.⚠️ If you're an external contributor, please file an issue before working on a PR. Discussing your changes beforehand will help ensure they align with our roadmap and that your time is well spent. --> Task/Issue URL: https://app.asana.com/1/137249556945/project/1199333091098016/task/1212230953441984?focus=true Tech Design URL: CC: ### Description This PR fixes a flaky HistoryCoordinator test. The issue was that the array of `savedHistoryEntries` would contain both visits (even though we reset the first one) and these two visits could sometimes be in the reverse order, since we weren't properly waiting for the first navigation to complete. The fix is to wait properly for the first visit to complete before resetting the mock. After this change, I see this test pass for me even after 20K iterations. ### Testing Steps <!-- Assume the reviewer is unfamiliar with this part of the app --> 1. Use the "Run Repeatedly" feature to run this test for many iterations (10K at least) and confirm that it passes every time 2. You can also do this same step on `main` to see the test fail sometimes after a few hundred iterations <!-- ### Testability Challenges If you encountered issues writing tests due to any class in the codebase, please report it in the [Testability Challenges Document](https://app.asana.com/1/137249556945/project/1204186595873227/task/1211703869786699) 1. **Check the Document:** First, check the **Testability Challenges Table** to see if the class you encountered is listed. 2. **If the Class Exists:** - Update the **Encounter Count** by increasing it by 1. 3. **If the Class Does Not Exist:** - Add a new entry --> ### Impact and Risks <!-- What's the impact on users if something goes wrong? High: Could affect user privacy, lose user data, break core functionality Medium: Could disrupt specific features or user flows Low: Minor visual changes, small bug fixes, improvement to existing features None: Internal tooling, documentation --> #### What could go wrong? <!-- Describe specific scenarios and how you've addressed them --> ### Quality Considerations <!-- Focus on what matters for your changes: - What edge cases exist? - How does this affect performance? - What monitoring have you added? - What documentation needs updating? - How does this impact privacy/security? --> ### Notes to Reviewer <!-- Anything specific you want reviewers to focus on --> --- ###### Internal references: [Definition of Done](https://app.asana.com/0/1202500774821704/1207634633537039/f) | [Engineering Expectations](https://app.asana.com/0/59792373528535/199064865822552) | [Tech Design Template](https://app.asana.com/0/59792373528535/184709971311943) <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Make the cookiePopupBlocked test deterministic by awaiting the initial visit save and asserting a single saved entry with the flag set. > > - **Tests**: > - `HistoryCoordinatorTests.testWhenCookiePopupBlockedIsCalled_ThenFlagIsSetAndAutoCommitted` > - Add an initial visit and explicitly await its save before resetting mocks. > - Use separate expectations for initial and cookie-popup saves; increase timeouts to `5.0`. > - Tighten assertions to expect exactly one saved entry and `cookiePopupBlocked == true`. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 1aff587. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
Task/Issue URL: https://app.asana.com/1/137249556945/project/1199333091098016/task/1212230953441984?focus=true
Tech Design URL:
CC:
Description
This PR fixes a flaky HistoryCoordinator test.
The issue was that the array of
savedHistoryEntrieswould contain both visits (even though we reset the first one) and these two visits could sometimes be in the reverse order, since we weren't properly waiting for the first navigation to complete.The fix is to wait properly for the first visit to complete before resetting the mock. After this change, I see this test pass for me even after 20K iterations.
Testing Steps
mainto see the test fail sometimes after a few hundred iterationsImpact and Risks
What could go wrong?
Quality Considerations
Notes to Reviewer
Internal references:
Definition of Done | Engineering Expectations | Tech Design Template
Note
Make the cookiePopupBlocked test deterministic by awaiting the initial visit save and asserting a single saved entry with the flag set.
HistoryCoordinatorTests.testWhenCookiePopupBlockedIsCalled_ThenFlagIsSetAndAutoCommitted5.0.cookiePopupBlocked == true.Written by Cursor Bugbot for commit 1aff587. This will update automatically on new commits. Configure here.