Skip to content

Conversation

ajpallares
Copy link
Member

@ajpallares ajpallares commented Jul 17, 2025

The test testCallToGetCustomerInfoWithPendingTransactionsPostsReceiptOnlyOnce() is randomly failing. This PR attempts to fix it.

More details

The test is sometimes failing with this error:

OfflineStoreKitIntegrationTests.swift:262: Entitlement is not active: <EntitlementInfo: "
identifier=premium,
isActive=false,
willRenew=true,
periodType=PeriodType(rawValue: 1),
latestPurchaseDate=Optional(2025-07-17 15:45:36 +0000),
originalPurchaseDate=Optional(2025-07-17 15:45:36 +0000),
expirationDate=Optional(2025-07-17 15:45:36 +0000),
...
>

The error shows that the entitlement is not active while it should be active. But if you notice the dates in the log, latestPurchaseDate is exactly the same as expirationDate. This makes me think that, if the test runs fast enough, the lines

try await self.purchaseMonthlyProduct(allowOfflineEntitlements: true)
try self.testSession.forceRenewalOfSubscription(
    productIdentifier: await self.monthlyPackage.storeProduct.productIdentifier
)

can produce two transactions with the same purchase_date (and the first one having also the same expires_date as purchase_date). This has just happened to me when running the test locally:

DEBUG: ℹ️ PostReceiptDataOperation: Posting receipt (source: 'queue') (note: the contents might not be up-to-date, but it will be refreshed with Apple's servers):
{...,
"in_app_purchases":[

{"transaction_id":"7","is_in_intro_offer_period":false,"original_transaction_id":"6","purchase_date":"2025-07-17T17:09:33Z","original_purchase_date":"2025-07-17T17:09:33Z","expires_date":"2025-07-17T17:10:03Z","product_id":"com.revenuecat.monthly_4.99.1_week_intro","product_type":-1,"quantity":1},

{"transaction_id":"6","is_in_intro_offer_period":true,"purchase_date":"2025-07-17T17:09:33Z","expires_date":"2025-07-17T17:09:33Z","product_id":"com.revenuecat.monthly_4.99.1_week_intro","product_type":-1,"quantity":1}

],
...}

I suspect that, because both transactions have the same purchase_date, the first one may be randomly picked between the two. In this case, the test then would fail because that transaction (with same expires_date as purchase_date) doesn't grant the entitlement anymore.
This is confirmed by the test error message having periodType=PeriodType(rawValue: 1), which is the PeriodType.intro enum case and it showing that latestPurchaseDate == expirationDate. Only the first transaction is the intro offer one.

My reasoning is that by adding 1 second delay between the two transactions will make sure that the two transactions always have a different purchase_date, so that the second transaction is always the one being picked as the latest one

@ajpallares ajpallares requested review from a team July 17, 2025 17:47
@ajpallares ajpallares marked this pull request as ready for review July 17, 2025 17:47
@@ -250,6 +250,7 @@ class OfflineStoreKit1IntegrationTests: BaseOfflineStoreKitIntegrationTests {
self.serverDown()

try await self.purchaseMonthlyProduct(allowOfflineEntitlements: true)
try await Task.sleep(for: .seconds(1))
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be worth adding some comments explaining the reason for this one?

Copy link
Member Author

@ajpallares ajpallares Jul 17, 2025

Choose a reason for hiding this comment

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

Yep, good point! It can be short, saying that we want to make sure that the two transactions have a different purchase_date. Thanks for the suggestion!

Copy link

emerge-tools bot commented Jul 17, 2025

📸 Snapshot Test

1 modified, 704 unchanged

Name Added Removed Modified Renamed Unchanged Errored Approval
RevenueCat
com.revenuecat.PaywallsTester
0 0 0 0 235 0 N/A
RevenueCat
com.revenuecat.PaywallsTester.mac-catalyst-scaled-to-match-ipad
0 0 0 0 235 0 N/A
RevenueCat
com.revenuecat.PaywallsTester.mac-catalyst-optimized-for-mac
0 0 1 0 234 0 ✅ Approved

🛸 Powered by Emerge Tools

@ajpallares ajpallares enabled auto-merge (squash) July 17, 2025 18:14
@ajpallares ajpallares merged commit a2bbcff into main Jul 17, 2025
12 checks passed
@ajpallares ajpallares deleted the potential-fix-storekit-test branch July 17, 2025 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants