From 28fce4bc74936bb46912bb60a1b997c266846f0d Mon Sep 17 00:00:00 2001 From: NachoSoto Date: Fri, 10 Nov 2023 09:25:23 -0800 Subject: [PATCH] `Integration Tests`: fix potential crash on `tearDown` (#3401) I ran into this locally. We re-initialize `Purchases` on some tests to simulate re-starting apps. If a test fails mid-test, `Purchases.shared` might not be defined. --- Sources/Purchasing/Purchases/Purchases.swift | 5 ++++- .../BaseBackendIntegrationTests.swift | 1 - 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Sources/Purchasing/Purchases/Purchases.swift b/Sources/Purchasing/Purchases/Purchases.swift index 6076bd08ab..a6c5ff9d95 100644 --- a/Sources/Purchasing/Purchases/Purchases.swift +++ b/Sources/Purchasing/Purchases/Purchases.swift @@ -629,7 +629,10 @@ public typealias StartPurchaseBlock = (@escaping PurchaseCompletedBlock) -> Void } static func clearSingleton() { - Self.purchases.value = nil + self.purchases.modify { purchases in + purchases?.delegate = nil + purchases = nil + } } /// - Parameter purchases: this is an `@autoclosure` to be able to clear the previous instance diff --git a/Tests/BackendIntegrationTests/BaseBackendIntegrationTests.swift b/Tests/BackendIntegrationTests/BaseBackendIntegrationTests.swift index 5db61ec0e5..eae182dea9 100644 --- a/Tests/BackendIntegrationTests/BaseBackendIntegrationTests.swift +++ b/Tests/BackendIntegrationTests/BaseBackendIntegrationTests.swift @@ -194,7 +194,6 @@ private extension BaseBackendIntegrationTests { // - These run *before* `tearDown`. // - They run in LIFO order. self.addTeardownBlock { - Purchases.shared.delegate = nil Purchases.clearSingleton() // Note: this captures the boolean to avoid race conditions when Nimble tries