Skip to content

CacheableNetworkOperation: fixed race condition in new test#1932

Merged
NachoSoto merged 3 commits into
mainfrom
operation-queue-reuse-test
Sep 23, 2022
Merged

CacheableNetworkOperation: fixed race condition in new test#1932
NachoSoto merged 3 commits into
mainfrom
operation-queue-reuse-test

Conversation

@NachoSoto

@NachoSoto NachoSoto commented Sep 22, 2022

Copy link
Copy Markdown
Contributor

The test introduced by #1926 passed locally, but sometimes failed in CI due to a race condition.

What we're testing is that the second request reuses the callback of the first one, if called while the first one is still in progress. However, nothing in the test ensures that's the case.
This introduces an optional delay in MockHTTPClient so we can ensure that.

Another reason that test was failing randomly is because we had another test modifying global state, which interferes with every other test. We run tests in parallel, so it was making the new test that requires debug logs to fail randomly, if this test happened to run before.

Note: because of the nature of these 2 tests, they don't actually wait for the whole response to finish, so they still pass in ~0.006s

@NachoSoto NachoSoto added the test label Sep 22, 2022
@NachoSoto NachoSoto requested a review from a team September 22, 2022 22:56
@NachoSoto NachoSoto force-pushed the operation-queue-reuse-test branch from 66a14d6 to c076800 Compare September 22, 2022 23:05
@NachoSoto

Copy link
Copy Markdown
Contributor Author

Huh this is still failing in iOS 14 😶

@NachoSoto NachoSoto force-pushed the operation-queue-reuse-test branch from d3ef6d5 to 4c4960e Compare September 22, 2022 23:38
@NachoSoto

Copy link
Copy Markdown
Contributor Author

For some reason debug logs aren't enabled when running the iOS 15 tests, but can't figure out why. We're compiling on debug, and Logger sets the default level to .debug... nothing else changes it.

@NachoSoto NachoSoto force-pushed the operation-queue-reuse-test branch 2 times, most recently from edaa387 to b50f56f Compare September 22, 2022 23:48
@NachoSoto

Copy link
Copy Markdown
Contributor Author

Tests are still failing. I need to figure out why on iOS 15 the Logger.logLevel is being set to Release in CI.

@NachoSoto

Copy link
Copy Markdown
Contributor Author

In the mean time it would be nice to merge #1928 so we're running tests in the device we actually want them too.

@NachoSoto NachoSoto force-pushed the operation-queue-reuse-test branch 2 times, most recently from 033f471 to f49d789 Compare September 23, 2022 00:02

@joshdholtz joshdholtz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM 🚀

@NachoSoto NachoSoto force-pushed the operation-queue-reuse-test branch 3 times, most recently from b1f9ccc to e16064f Compare September 23, 2022 18:41
Comment on lines 212 to 222

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

⚠️ I'm removing this test to fix tests in CI for 2 reasons:

  • It's testing a deprecated method
  • It's modifying global state, which interferes with every other test. We run tests in parallel, so this is making the new test that requires debug logs to fail randomly, if this test happened to run before (it was changing the level back to .info).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

is there any way to

  • back up the previous value
  • run this atomically
  • return the previous value?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

like, deprecated or not, if we break this, it's still a problem

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

run this atomically

We would have to block the entire Logger from logging anything, and / or block all other tests from running. I can't think of a way to do that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

like, deprecated or not, if we break this, it's still a problem

For sure. I just can't think of a way to test this global state without messing with every other test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Do you have any ideas? :/

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I do not 😞

Comment thread fastlane/Fastfile Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

⚠️ Explicitly setting these to Debug.

Fastlane's scan documents that Release is the default. However, if we don't pass the value, scan wasn't passing any configuration to xcodebuild, so it was relying on the scheme's default, which is Debug.
Instead of relying on that, which might change in the future, this makes the configuration explicit.

@NachoSoto

Copy link
Copy Markdown
Contributor Author

Can I get another review of this?

Comment on lines 212 to 222

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

is there any way to

  • back up the previous value
  • run this atomically
  • return the previous value?

The test introduced by #1926 passed locally, but sometimes failed in CI due to a race condition.

What we're testing is that the second request reuses the callback of the first one, **if called while the first one is still in progress**. However, nothing in the test ensures that's the case.
This introduces an optional delay in `MockHTTPClient` so we can ensure that.

_Note: because of the nature of these 2 tests, they don't actually wait for the whole response to finish, so they still pass in ~0.006s_
@NachoSoto NachoSoto force-pushed the operation-queue-reuse-test branch from e16064f to 0a02104 Compare September 23, 2022 19:03
@NachoSoto NachoSoto merged commit 68d0a00 into main Sep 23, 2022
@NachoSoto NachoSoto deleted the operation-queue-reuse-test branch September 23, 2022 19:40
NachoSoto added a commit that referenced this pull request Sep 27, 2022
**This is an automatic release.**

### New Features
* 🚨 `StoreKit 2` is now enabled by default 🚨 (#1922) via NachoSoto
(@NachoSoto)
* Extracted `PurchasesType` and `PurchasesSwiftType` (#1912) via
NachoSoto (@NachoSoto)
### Bugfixes
* `StoreKit 1`: changed result of cancelled purchases to be consistent
with `StoreKit 2` (#1910) via NachoSoto (@NachoSoto)
* `PaymentQueueWrapper`: handle promotional purchase requests from App
Store when SK1 is disabled (#1901) via NachoSoto (@NachoSoto)
### Other Changes
* Fixed iOS 12 tests (#1936) via NachoSoto (@NachoSoto)
* `CacheableNetworkOperation`: fixed race condition in new test (#1932)
via NachoSoto (@NachoSoto)
* `BasePurchasesTests`: changed default back to SK1 (#1935) via
NachoSoto (@NachoSoto)
* `Logger`: refactored default `LogLevel` definition (#1934) via
NachoSoto (@NachoSoto)
* `AppleReceipt`: refactored declarations into nested types (#1933) via
NachoSoto (@NachoSoto)
* `Integration Tests`: relaunch tests when retrying failures (#1925) via
NachoSoto (@NachoSoto)
* `CircleCI`: downgraded release jobs to Xcode 13.x (#1927) via
NachoSoto (@NachoSoto)
* `ErrorUtils`: added test to verify that `PublicError`s can be
`catch`'d as `ErrorCode` (#1924) via NachoSoto (@NachoSoto)
* `StoreKitIntegrationTests`: print `AppleReceipt` data whenever
`verifyEntitlementWentThrough` fails (#1929) via NachoSoto (@NachoSoto)
* `OperationQueue`: log debug message when requests are found in cache
and skipped (#1926) via NachoSoto (@NachoSoto)
* `GetCustomerInfoAPI`: avoid making a request if there's any
`PostReceiptDataOperation` in progress (#1911) via NachoSoto
(@NachoSoto)
* `PurchaseTester`: allow HTTP requests and enable setting `ProxyURL`
(#1917) via NachoSoto (@NachoSoto)
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