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

refactor: CardBrowserTest + fix flaky tests #12263

Merged
merged 6 commits into from
Sep 1, 2022

Conversation

david-allison
Copy link
Member

@david-allison david-allison commented Aug 31, 2022

Purpose / Description

I was investigating the flaky unit tests, and wanted to see what was going on

I could not reproduce the test failure, but a few frustrations occurred:

  • Legacy code before Robolectric.startActivity was introduced
  • @KotlinCleanups
  • @RunInBackground was used, which is pending for removal
  • Some bad unit test asserts
  • One issue with revokeWritePermissions not being reset, which may have caused test failures

I fixed all of these, and introduced advanceRobolectricUiLooper to allow for further migration away from @RunInBackground

I then added another advanceRobolectricUiLooper after creating the Activity, and the problems went away

#11091

How Has This Been Tested?

Test only - tests run about 2 seconds faster expected - we shave 4*500ms off tests

Learning

  • advanceRobolectricUiLooper is still required - onCreateOptionsMenu was not called otherwise due to an update

Checklist

  • You have a descriptive commit message with a short title (first line, max 50 chars).
  • You have commented your code, particularly in hard-to-understand areas
  • You have performed a self-review of your own code
  • UI changes: include screenshots of all affected screens (in particular showing any new or changed strings)
  • UI Changes: You have tested your change using the Google Accessibility Scanner

@david-allison david-allison added Review High Priority Request for high priority review Needs Review labels Aug 31, 2022
@david-allison

This comment was marked as resolved.

@david-allison david-allison added Needs Author Reply Waiting for a reply from the original author and removed Review High Priority Request for high priority review Needs Review labels Aug 31, 2022
Before the introduction of `SingleTaskManager`, all `CollectionTask`s
required `advanceRobolectricLooper()``

After `SingleTaskManager`, CollectionTask no longer required
`advanceRobolectricLooper()` to execute.

Sometimes `advanceRobolectricLooper` was required for
non CollectionTask-based calls (for example: calling
`onCreateOptionsMenu` at the correct time)

We currently use `@RunInBackground` to use the old behaviour.

This is not ideal as `advanceRobolectricLooperWithSleep` adds 500ms
per call to the unit tests

`advanceRobolectricUiLooper` method exists for the migration away
from @RunInBackground, where calls are known to be necessary

The overall aim is:
* Remove `@RunInBackground` and replace with
  `advanceRobolectricUiLooper` calls when necessary
* Remove ALL legacy calls to `advanceRobolectricLooperWithSleep`
  * Lots of these exist as a partial migration from `@RunInBackground`
* Remove ALL legacy calls to `advanceRobolectricLooper`
introduce:
`advanceRobolectricUiLooper` on
- `selectMenuItem`
- `selectOneOfManyCards`

Remove all unnecessary calls to:
- `advanceRobolectricLooperWithSleep`
- grantWritePermissions
- revokeWritePermissions
Introduce `withNoWritePermission` so this error is no longer
possible

We have flaky tests on this class, and it likely is not an issue
but it seems wrong, and the fix is trivial
@david-allison
Copy link
Member Author

david-allison commented Aug 31, 2022

I'm going to run the Windows CI a few times to see if this is fixed by 8f0c0e2

  1. Pass ✅
  2. Pass ✅
  3. Pass ✅
  4. Pass ✅

```
CardBrowserTest > browserIsInMultiSelectModeWhenSelectingAll FAILED
    java.lang.AssertionError:
    Expected: <true>
         but: was <false>
        at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
        at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:6)
        at com.ichi2.anki.CardBrowserTest.browserIsInMultiSelectModeWhenSelectingAll(CardBrowserTest.kt:116)

CardBrowserTest > moveToNonDynamicDeckWorks FAILED
    java.lang.IllegalStateException: Attempted to check card at index 0. 0 cards available
        at com.ichi2.anki.CardBrowser.checkCardsAtPositions(CardBrowser.kt:2533)
        at com.ichi2.anki.CardBrowserTest.moveToNonDynamicDeckWorks(CardBrowserTest.kt:207)
```
@david-allison david-allison marked this pull request as ready for review September 1, 2022 00:50
@david-allison david-allison added Review High Priority Request for high priority review Needs Review and removed Needs Author Reply Waiting for a reply from the original author labels Sep 1, 2022
@david-allison david-allison changed the title refactor: CardBrowserTest + maybe fix flaky tests refactor: CardBrowserTest + fix flaky tests Sep 1, 2022
Copy link
Member

@mikehardy mikehardy left a comment

Choose a reason for hiding this comment

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

All seem sensible - thanks for brooming there and sweeping up

@mikehardy mikehardy merged commit eac6038 into ankidroid:main Sep 1, 2022
@github-actions github-actions bot added this to the 2.16 release milestone Sep 1, 2022
@github-actions github-actions bot removed Needs Review Review High Priority Request for high priority review labels Sep 1, 2022
@david-allison david-allison deleted the fix-CardBrowserTest branch September 1, 2022 19:20
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.

2 participants