Skip to content

Commit 772218b

Browse files
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 Checklist
1 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/AppDelegate.m‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ - (BOOL)application:(UIApplication *)application
1414
}
1515

1616
- (void)didInitializeImplicitFlutterEngine:(NSObject<FlutterImplicitEngineBridge> *)engineBridge {
17+
// Plugin registration eventually starts listening to StoreKit transactions in the background,
18+
// which would run concurrently with the test cases during unit tests and auto-finish
19+
// transactions under test. Skip it when running unit tests.
20+
if (NSClassFromString(@"XCTestCase") != nil) {
21+
return;
22+
}
1723
[GeneratedPluginRegistrant registerWithRegistry:engineBridge.pluginRegistry];
1824
}
1925

‎packages/in_app_purchase/in_app_purchase_storekit/example/macos/Runner/MainFlutterWindow.swift‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ class MainFlutterWindow: NSWindow {
1212
self.contentViewController = flutterViewController
1313
self.setFrame(windowFrame, display: true)
1414

15-
RegisterGeneratedPlugins(registry: flutterViewController)
15+
if NSClassFromString("XCTestCase") == nil {
16+
RegisterGeneratedPlugins(registry: flutterViewController)
17+
}
1618

1719
super.awakeFromNib()
1820
}

0 commit comments

Comments
 (0)