Skip to content

Commit

Permalink
Fix iOS 15 tests (#4320)
Browse files Browse the repository at this point in the history
  • Loading branch information
vegaro authored and nyeu committed Oct 1, 2024
1 parent 01ad3c2 commit 4700d9c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
11 changes: 4 additions & 7 deletions Sources/Diagnostics/DiagnosticsTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}

}
27 changes: 24 additions & 3 deletions Tests/StoreKitUnitTests/ProductsManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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!

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -206,3 +226,4 @@ class ProductsManagerDiagnosticsTrackingTests: ProductsManagerTests {
#endif

}
// swiftlint:enable type_name
24 changes: 18 additions & 6 deletions Tests/UnitTests/Mocks/MockOperationDispatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//

import Foundation
import XCTest

@testable import RevenueCat

Expand Down Expand Up @@ -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<Void, Never> {
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")
}
}
}

Expand Down

0 comments on commit 4700d9c

Please sign in to comment.