Skip to content

Tests: ensure Purchases is not configured multiple times#2100

Merged
NachoSoto merged 2 commits into
mainfrom
purchases-multi-instance-tests
Dec 9, 2022
Merged

Tests: ensure Purchases is not configured multiple times#2100
NachoSoto merged 2 commits into
mainfrom
purchases-multi-instance-tests

Conversation

@NachoSoto

@NachoSoto NachoSoto commented Nov 29, 2022

Copy link
Copy Markdown
Contributor

Continuing to look into CSDK-517, I found this several times in the logs:

Purchases instance already set. Did you mean to configure two Purchases objects?

In order to ensure that tests don't leave around unwanted state, I've turned this error into an assertion only when running tests, as well as fixed those cases.

@NachoSoto NachoSoto added the test label Nov 29, 2022
@NachoSoto NachoSoto requested a review from a team November 29, 2022 21:21
@NachoSoto NachoSoto force-pushed the purchases-multi-instance-tests branch from e4e5099 to dc0acb0 Compare November 29, 2022 21:47
NachoSoto added a commit that referenced this pull request Nov 30, 2022
Follow up to #2100. This has uncovered a huge source of leaks across many tests. Fixing this will provide many benefits:
- Faster test runs
- Lower memory overhead
- Isolated state across each test run
- Ensure that these types don't leak outside of tests too

#if DEBUG

private extension UIApplication {

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.

Why was this an extension on UIApplication before?

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.

For no particular reason :P

Comment on lines +503 to +507
#if DEBUG
if ProcessInfo.isRunningUnitTests {
preconditionFailure(Strings.configure.purchase_instance_already_set.description)
}
#endif

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.

does that mean that any apps that have tests using our SDK will also crash here? If so we should find a way to limit this to just our testing, a customer should be able to do whatever they want in their tests

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.

Oh that's a fair point. But considering this is undesired usage of the SDK, and crashing during tests has no user impact, isn't that a good thing?
Running the SDK concurrently can lead to the concurrent post receipt operations issue.

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 updated this to use a separate environment variable

@NachoSoto NachoSoto force-pushed the purchases-multi-instance-tests branch from dc0acb0 to 3cff32b Compare December 9, 2022 00:49
Continuing to look into [CSDK-517], I found this several times in the logs:
> Purchases instance already set. Did you mean to configure two Purchases objects?

In order to ensure that tests don't leave around unwanted state, I've turned this error into an assertion _only when running tests_, as well as fixed those cases.
@NachoSoto NachoSoto force-pushed the purchases-multi-instance-tests branch from 3cff32b to b0631bf Compare December 9, 2022 00:51
@NachoSoto NachoSoto enabled auto-merge (squash) December 9, 2022 00:54
@NachoSoto NachoSoto merged commit 9bbd5b1 into main Dec 9, 2022
@NachoSoto NachoSoto deleted the purchases-multi-instance-tests branch December 9, 2022 00:59
NachoSoto added a commit that referenced this pull request Dec 12, 2022
Follow up to #2100. This has uncovered a huge source of leaks across many tests. Fixing this will provide many benefits:
- Faster test runs
- Lower memory overhead
- Isolated state across each test run
- Ensure that these types don't leak outside of tests too
NachoSoto added a commit that referenced this pull request Dec 13, 2022
Fixes #2107 and [SDKONCALL-180].

[This
comment](#2107 (comment))
pointed out that parsing a long receipt can freeze the UI for almost a
second.

⚠️ This new assertion (running only in integration tests using the code
added in #2100) has exposed a couple of code paths where we parse
receipts from the main thread!

[SDKONCALL-180]:
https://revenuecats.atlassian.net/browse/SDKONCALL-180?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
NachoSoto added a commit that referenced this pull request Dec 21, 2022
…tances in memory (#2180)

We allow calling `Purchases.configure` multiple times. Likely a
programmer error, but it's common in tests, or for example in `Purchase
Tester` where users can re-configure the SDK at runtime (#1999), which
is where I saw this issue.

When this happens, the SDK was creating the new instance before clearing
the old one from memory (clear thanks to #2082). Notice how the first
`deinit` happens **after** the second `init`:
```
[Purchases] - VERBOSE: Purchases.init: created new Purchases instance: Purchases (0x0000600003ca4200)
[Purchases] - INFO: ℹ️ Purchases instance already set. Did you mean to configure two Purchases objects?
[Purchases] - VERBOSE: Purchases.init: created new Purchases instance: Purchases (0x0000600003c8a100)
[Purchases] - VERBOSE: Purchases.deinit: Purchases (0x0000600003ca4200)
[Purchases] - VERBOSE: Purchases.deinit: Purchases (0x0000600003c8a100)
```

With this change, we avoid that, which could also avoid issues with
concurrent requests ([CSDK-517]):
```
[Purchases] - VERBOSE: Purchases.init: created new Purchases instance: Purchases (0x0000600002b6e600)
[Purchases] - INFO: ℹ️ Purchases instance already set. Did you mean to configure two Purchases objects?
[Purchases] - VERBOSE: Purchases.deinit: Purchases (0x0000600002b6e600)
[Purchases] - VERBOSE: Purchases.init: created new Purchases instance: Purchases (0x0000600002b68100)
[Purchases] - VERBOSE: Purchases.deinit: Purchases (0x0000600002b68100)
```

Unfortunately we don't have an easy way to test this, especially
considering that we prohibit multiple instances in tests (#2100).

[CSDK-517]:
https://revenuecats.atlassian.net/browse/CSDK-517?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
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