Repository navigation
Commit 772218b
authored
[in_app_purchase_storekit] Fixes Storekit 2 flaky tests (#13213)
Fixes flutter/flutter#174478
Fixes flutter/flutter#193451
1. Increase purchase expectation timeout from `5s` to `10s`:
On every StoreKit 2 purchase, iOS's `storekitd` daemon makes an XPC call to `amsengagementd` before returning the transaction. Normally `amsengagementd` responds in a few milliseconds (~0.2s total per test). However, on CI simulators, `amsengagementd` occasionally stalls while cold-starting or syncing background config bags. When that happens, `storekitd` waits for a hardcoded **6-second internal timeout** before ignoring `amsengagementd` and completing the purchase (~6.5s total).
Because our test timeout was `5s` (`< 6s`), any test that coincided with an `amsengagementd` stall timed out just before StoreKit finished the purchase, and `tearDown()` cleared the session mid-purchase—wedging `storekitd`'s serial transaction queue for the rest of the suite. Bumping the timeout to `10s` gives StoreKit enough headroom to recover from the 6s timeout without affecting normal run times (~0.2s). Very notably, the **tests that I've previously increased the time out to `10s` have never flaked**, so there is good evidence this is the root cause.
2. Skip plugin registration in the host app during XCTests (`NSClassFromString(@"XCTestCase") != nil`):
Because `RunnerTests` runs inside `Runner.app` as its host application, Xcode launches the example Flutter app in the background when running unit tests. Previously, the background example app registered `InAppPurchasePlugin` and listened to StoreKit's process-wide `Transaction.updates` stream, which would automatically finish test transactions behind the unit tests' backs. Skipping `GeneratedPluginRegistrant` when `XCTestCase` is loaded (matching [`camera_avfoundation`](https://github.com/flutter/packages/blob/main/packages/camera/camera_avfoundation/example/ios/Runner/AppDelegate.swift#L18-L25)) prevents the host app from interfering with the tests on both iOS (`AppDelegate.m`) and macOS (`MainFlutterWindow.swift`).
3. Clean up transaction listeners in `tearDown()` + fail fast on errors:
Stops the plugin's `Transaction.updates` listener task in `tearDown()` so tasks don't leak across tests, and fulfills expectations in `.failure` branches so unexpected errors fail immediately instead of waiting for a timeout.
## Pre-Review Checklist1 parent ca5b2e5 commit 772218b
3 files changed
Lines changed: 131 additions & 64 deletions
File tree
- packages/in_app_purchase/in_app_purchase_storekit/example
- ios/Runner
- macos/Runner
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
17 | 23 | | |
18 | 24 | | |
19 | 25 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
16 | 18 | | |
17 | 19 | | |
18 | 20 | | |
| |||
0 commit comments