diff --git a/Sources/Diagnostics/DiagnosticsTracker.swift b/Sources/Diagnostics/DiagnosticsTracker.swift index 25160d9e88..5f79d05011 100644 --- a/Sources/Diagnostics/DiagnosticsTracker.swift +++ b/Sources/Diagnostics/DiagnosticsTracker.swift @@ -159,14 +159,11 @@ private extension DiagnosticsTracker { func clearDiagnosticsFileIfTooBig() async { if await self.diagnosticsFileHandler.isDiagnosticsFileTooBig() { await self.diagnosticsFileHandler.emptyDiagnosticsFile() - self.trackMaxEventsStoredLimitReached() + let maxEventsStoredEvent = DiagnosticsEvent(eventType: .maxEventsStoredLimitReached, + properties: [:], + timestamp: self.dateProvider.now()) + await self.diagnosticsFileHandler.appendEvent(diagnosticsEvent: maxEventsStoredEvent) } } - func trackMaxEventsStoredLimitReached() { - self.track(.init(eventType: .maxEventsStoredLimitReached, - properties: [:], - timestamp: self.dateProvider.now())) - } - } diff --git a/Tests/StoreKitUnitTests/ProductsManagerTests.swift b/Tests/StoreKitUnitTests/ProductsManagerTests.swift index 99f8514dac..0965e697b7 100644 --- a/Tests/StoreKitUnitTests/ProductsManagerTests.swift +++ b/Tests/StoreKitUnitTests/ProductsManagerTests.swift @@ -113,8 +113,8 @@ class ProductsManagerTests: StoreKitConfigTestCase { expect(unwrappedFirstProduct.currencyCode) == "EUR" } - func createManager(storeKitVersion: StoreKitVersion, - diagnosticsTracker: DiagnosticsTrackerType? = nil) -> ProductsManager { + fileprivate func createManager(storeKitVersion: StoreKitVersion, + diagnosticsTracker: DiagnosticsTrackerType? = nil) -> ProductsManager { let platformInfo = Purchases.PlatformInfo(flavor: "xyz", version: "123") return ProductsManager( diagnosticsTracker: diagnosticsTracker, @@ -129,8 +129,9 @@ class ProductsManagerTests: StoreKitConfigTestCase { } +// swiftlint:disable type_name @available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *) -class ProductsManagerDiagnosticsTrackingTests: ProductsManagerTests { +class SK1ProductsManagerDiagnosticsTrackingTests: ProductsManagerTests { private var mockDiagnosticsTracker: MockDiagnosticsTracker! @@ -161,6 +162,25 @@ class ProductsManagerDiagnosticsTrackingTests: ProductsManagerTests { expect(params?.errorCode).to(beNil()) } +} +// swiftlint:enable type_name + +// swiftlint:disable type_name +@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *) +class SK2ProductsManagerDiagnosticsTrackingTests: ProductsManagerTests { + + private var mockDiagnosticsTracker: MockDiagnosticsTracker! + + private var productsManager: ProductsManager! + + override func setUpWithError() throws { + try super.setUpWithError() + + try AvailabilityChecks.iOS16APIAvailableOrSkipTest() + + self.mockDiagnosticsTracker = MockDiagnosticsTracker() + } + func testFetchProductsWithIdentifiersSK2TracksCorrectly() throws { let manager = self.createManager(storeKitVersion: .storeKit2, diagnosticsTracker: self.mockDiagnosticsTracker) @@ -206,3 +226,4 @@ class ProductsManagerDiagnosticsTrackingTests: ProductsManagerTests { #endif } +// swiftlint:enable type_name diff --git a/Tests/UnitTests/Mocks/MockOperationDispatcher.swift b/Tests/UnitTests/Mocks/MockOperationDispatcher.swift index 35dfb537fe..260fe6b763 100644 --- a/Tests/UnitTests/Mocks/MockOperationDispatcher.swift +++ b/Tests/UnitTests/Mocks/MockOperationDispatcher.swift @@ -4,6 +4,7 @@ // import Foundation +import XCTest @testable import RevenueCat @@ -90,14 +91,25 @@ class MockOperationDispatcher: OperationDispatcher { if self.forwardToOriginalDispatchOnWorkerThread { super.dispatchOnWorkerThread(jitterableDelay: delay, block: block) } else if self.shouldInvokeDispatchOnWorkerThreadBlock { - let semaphore = DispatchSemaphore(value: 0) - - Task { - await block() - semaphore.signal() + // We want to wait for the async task to finish before leaving this function + // Use a dispatch group to wait for the async task to finish + let dispatchGroup = DispatchGroup() + dispatchGroup.enter() + + // Execute the async task on a background queue to avoid blocking + DispatchQueue.global(qos: .userInitiated).async { + Task { + await block() + dispatchGroup.leave() + } } - semaphore.wait() + // Ensure we wait for the async task to finish + // and fail if it takes too long + let result = dispatchGroup.wait(timeout: .now() + .seconds(10)) + if result == .timedOut { + XCTFail("Dispatch on worker thread timed out") + } } }