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

HTTPClientTests: fixed failing test with missing assertions #2262

Merged
merged 4 commits into from
Feb 6, 2023

Conversation

NachoSoto
Copy link
Contributor

@NachoSoto NachoSoto commented Feb 3, 2023

HTTPClientTest.testRequestIsRetriedIfResponseFromETagManagerIsNil was actually failing (the request wasn't successful), but because it had no expectations, we didn't notice until we introduced Code Coverage.
As you can see in the Codecov diff:
Screenshot 2023-02-06 at 09 11 37

Most of the time that code didn't have coverage (meaning we weren't testing it). The reason for why is uncovered in this PR.
The random execution order meant that sometimes (most of the time) tests like testHandlesRealErrorConditions were running first, which did this:

let response = HTTPStubsResponse.emptySuccessResponse
response.error = error
return response

That seemingly innocuous code was modifying HTTPStubsResponse.emptySuccessResponse, defined in an extension in that file. Being used to value types nobody thought much of that, but unfortunately HTTPStubsResponse is a class, therefore it's a mutable type with reference semantics. Because of that, every test executed after any of these modifying the response would receive a failure, but it was going unnoticed.

This PR also introduces a couple improvements to how we wait for several concurrent requests (which I thought was the issue at first).

@NachoSoto NachoSoto added the test Adding missing tests or correcting existing tests label Feb 3, 2023
@NachoSoto NachoSoto requested a review from a team February 3, 2023 20:00
}
}

expect(result).to(beSuccess())
expect(requests.value) == 2
Copy link
Contributor Author

Choose a reason for hiding this comment

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

⚠️ This test basically had no expectations. Most of the time it wasn't going through the part of the code that it was meant to test, but we didn't notice until we saw it in the test coverage diff!

@NachoSoto NachoSoto changed the title HTTPClientTests: failing test to expose race condition HTTPClientTests: fixed failing test with missing assertions Feb 6, 2023
@NachoSoto NachoSoto marked this pull request as ready for review February 6, 2023 17:15
@codecov
Copy link

codecov bot commented Feb 6, 2023

Codecov Report

Merging #2262 (d034fa2) into main (ec69c2d) will increase coverage by 0.02%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##             main    #2262      +/-   ##
==========================================
+ Coverage   85.96%   85.98%   +0.02%     
==========================================
  Files         183      183              
  Lines       12119    12119              
==========================================
+ Hits        10418    10421       +3     
+ Misses       1701     1698       -3     
Impacted Files Coverage Δ
...hasing/StoreKit2/StoreKit2StorefrontListener.swift 100.00% <0.00%> (+13.63%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@aboedo
Copy link
Member

aboedo commented Feb 6, 2023

great catch!

@NachoSoto
Copy link
Contributor Author

Updated.

@NachoSoto NachoSoto merged commit fc16fcc into main Feb 6, 2023
@NachoSoto NachoSoto deleted the coverage-flaky-results branch February 6, 2023 21:06
NachoSoto added a commit that referenced this pull request Feb 6, 2023
Similarly to #2262, test execution ordering was leading to [flaky code coverage results](https://app.codecov.io/gh/RevenueCat/purchases-ios/pull/2262) with this.
Because we didn't have any test coverage, only if some other test changed the `Storefront`, this test was calling the delegate code.

This adds explicit test coverage using a `MockAsyncSequence`.

- Added `StorefrontType` to `StoreKit2StorefrontListenerDelegate` method
- `Storefront` initializers are no longer `Optional` `init?`
- Simplified `StoreKit2StorefrontListenerTests` by moving `AvailabilityChecks` to `setUp`
NachoSoto added a commit that referenced this pull request Feb 8, 2023
Similarly to #2262, test execution ordering was leading to [flaky code coverage results](https://app.codecov.io/gh/RevenueCat/purchases-ios/pull/2262) with this.
Because we didn't have any test coverage, only if some other test changed the `Storefront`, this test was calling the delegate code.

This adds explicit test coverage using a `MockAsyncSequence`.

- Added `StorefrontType` to `StoreKit2StorefrontListenerDelegate` method
- `Storefront` initializers are no longer `Optional` `init?`
- Simplified `StoreKit2StorefrontListenerTests` by moving `AvailabilityChecks` to `setUp`
NachoSoto added a commit that referenced this pull request Feb 8, 2023
…#2265)

Similarly to #2262, test execution ordering was leading to [flaky code
coverage
results](https://app.codecov.io/gh/RevenueCat/purchases-ios/pull/2262)
with this.

![Screenshot 2023-02-06 at 10 48
45](https://user-images.githubusercontent.com/685609/217058841-33a07262-7959-4732-9706-7426d8f4efb7.png)

Because we didn't have any test coverage, only if some other test
changed the `Storefront`, this test was calling the delegate code.

This adds explicit test coverage using a `MockAsyncSequence`.

### Other changes:
- Added `StorefrontType` to `StoreKit2StorefrontListenerDelegate` method
- `Storefront` initializers are no longer `Optional` `init?`
- Simplified `StoreKit2StorefrontListenerTests` by moving
`AvailabilityChecks` to `setUp`
NachoSoto pushed a commit that referenced this pull request Feb 8, 2023
**This is an automatic release.**

### Dependency Updates
* Bump fastlane-plugin-revenuecat_internal from `738f255` to `9255366`
(#2264) via dependabot[bot] (@dependabot[bot])
* Update `Gemfile.lock` (#2254) via Cesar de la Vega (@vegaro)
### Other Changes
* `HTTPClient`: added support for sending `X-Nonce` (#2214) via
NachoSoto (@NachoSoto)
* `Configuration`: added (`internal` for now) API to load public key
(#2215) via NachoSoto (@NachoSoto)
* Replaced `Any` uses for workaround with `Box` (#2250) via NachoSoto
(@NachoSoto)
* `HTTPClientTests`: fixed failing test with missing assertions (#2262)
via NachoSoto (@NachoSoto)
* `HTTPClientTests`: refactored tests to use `waitUntil` (#2257) via
NachoSoto (@NachoSoto)
* PurchaseTester: Add Receipt Inspector UI (#2249) via Andy Boedo
(@aboedo)
* Adds dependabot (#2259) via Cesar de la Vega (@vegaro)
* `StoreKit1WrapperTests`: avoid using `Bool.random` to fix flaky code
coverage (#2258) via NachoSoto (@NachoSoto)
* `IntroEligibilityCalculator`: changed logic to handle products with no
subscription group (#2247) via NachoSoto (@NachoSoto)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
test Adding missing tests or correcting existing tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants