SKTestSession.clearTransactions() does not seem to clear finishing transactions

Originator:rito.0305
Number:rdar://FB9814502 Date Originated:Dec 20, 2021 at 3:41 AM
Status:Open Resolved:
Product:StoreKit Testing in Xcode Product Version:
Classification: Reproducible:
 
I wrote a minimal test case reproducing this issue.
https://github.com/manicmaniac/StoreKitTestingExample/blob/87ebb21232a1345e3db9500cc19029c43c1fd4fe/StoreKitTestingExampleTests/AutoRenewableTests.swift#L43-L82

This test case just purchases a product and confirm that SKPaymentTransactionObserver's callback methods are called as expected.
I call `[SKTestSession clearTransactions]` before each test case runs, and finishes purchased transactions before each test case finishes.
Although the test case passes if run only once, it often fails if repeatedly run.

The reason why it fails is `purchasing` transaction is missing and only `purchased` transaction comes.

I investigated system log of appstored with `$ log stream --predicate 'subsystem == "appstored"'` and found it says -

> Error finishing payment 915 for com.github.manicmaniac.StoreKitTestingExample - Error Domain=AMSErrorDomain Code=301 "Invalid Status Code" UserInfo={NSLocalizedFailureReason=The response has an invalid status code, AMSURL=http://localhost:49402/WebObjects/MZFinance.woa/wa/inAppTransactionDone?REDACTED, AMSStatusCode=404, AMSServerPayload={
>     error = "Transaction not found";
> }, NSLocalizedDescription=Invalid Status Code}

only at the time of test failure.

As a trial, I inserted 0.25 sec wait after each invocation of `[SKPaymentQueue finishTransaction:]`, then the problem has gone.
I think somehow transactions remains after invoking `[SKTestSession clearTransactions]` when `[SKPaymentQueue finishTransaction:]` is called.
Please list the steps you took to reproduce the issue:
1. clone my repo (https://github.com/manicmaniac/StoreKitTestingExample)
2. run test and see it passes
3. delete `RunLoop.main.run(until: Date(timeIntervalSinceNow: 0.25))` at L24 in AutoRenewableTests.swift
4. run test again and see it fails
What did you expect to happen?
- The test case passes without waiting for anything
- `[SKTestSession clearTransactions]` clears all transactions including finishing transactions so that `purchasing` and `purchased` transactions comes in this order
- Or this behavior is documented in API reference
What actually happened?
- The test case fails when I remove 0.25 sec wait
- `purchasing` transaction was missing and only `purchased` transaction comes

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!