From 94503d797d4b3c4dff1420aae6373b99d462298e Mon Sep 17 00:00:00 2001 From: Nikita Vasilev Date: Thu, 18 Jan 2024 10:04:32 +0100 Subject: [PATCH] Fix flacky tests --- .../Models/Internal/ProductsRequest.swift | 33 +++++++++++++++++++ .../Internal/Protocols/ISKRequest.swift | 10 ++++++ .../ProductProvider/ProductProvider.swift | 19 ++++++++--- 3 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 Sources/Flare/Classes/Models/Internal/ProductsRequest.swift create mode 100644 Sources/Flare/Classes/Models/Internal/Protocols/ISKRequest.swift diff --git a/Sources/Flare/Classes/Models/Internal/ProductsRequest.swift b/Sources/Flare/Classes/Models/Internal/ProductsRequest.swift new file mode 100644 index 000000000..bc838afad --- /dev/null +++ b/Sources/Flare/Classes/Models/Internal/ProductsRequest.swift @@ -0,0 +1,33 @@ +// +// Flare +// Copyright © 2024 Space Code. All rights reserved. +// + +import Foundation +import StoreKit + +final class ProductsRequest: ISKRequest { + // MARK: Properties + + private let request: SKRequest + + var id: String { request.id } + + // MARK: Initialization + + init(_ request: SKRequest) { + self.request = request + } + + // MARK: Hashable + + func hash(into hasher: inout Hasher) { + hasher.combine(id) + } + + // MARK: Equatable + + static func == (lhs: ProductsRequest, rhs: ProductsRequest) -> Bool { + lhs.id == rhs.id + } +} diff --git a/Sources/Flare/Classes/Models/Internal/Protocols/ISKRequest.swift b/Sources/Flare/Classes/Models/Internal/Protocols/ISKRequest.swift new file mode 100644 index 000000000..1c9baed59 --- /dev/null +++ b/Sources/Flare/Classes/Models/Internal/Protocols/ISKRequest.swift @@ -0,0 +1,10 @@ +// +// Flare +// Copyright © 2024 Space Code. All rights reserved. +// + +import Foundation + +protocol ISKRequest: Hashable { + var id: String { get } +} diff --git a/Sources/Flare/Classes/Providers/ProductProvider/ProductProvider.swift b/Sources/Flare/Classes/Providers/ProductProvider/ProductProvider.swift index 183741bbb..ce5cbdf30 100644 --- a/Sources/Flare/Classes/Providers/ProductProvider/ProductProvider.swift +++ b/Sources/Flare/Classes/Providers/ProductProvider/ProductProvider.swift @@ -1,6 +1,6 @@ // // Flare -// Copyright © 2023 Space Code. All rights reserved. +// Copyright © 2024 Space Code. All rights reserved. // import Concurrency @@ -50,7 +50,7 @@ final class ProductProvider: NSObject, IProductProvider { // MARK: Private /// Dictionary to store request handlers with their corresponding request IDs. - private var handlers: [String: ProductsHandler] = [:] + private var handlers: [ProductsRequest: ProductsHandler] = [:] /// The dispatch queue factory for handling concurrent tasks. private let dispatchQueueFactory: IDispatchQueueFactory @@ -77,7 +77,7 @@ final class ProductProvider: NSObject, IProductProvider { /// - completion: A closure to be called upon completion with the fetched products. private func fetch(request: SKProductsRequest, completion: @escaping ProductsHandler) { dispatchQueue.async { - self.handlers[request.id] = completion + self.handlers[request.request] = completion self.dispatchQueueFactory.main().async { request.start() } @@ -90,7 +90,8 @@ final class ProductProvider: NSObject, IProductProvider { extension ProductProvider: SKProductsRequestDelegate { func request(_ request: SKRequest, didFailWithError error: Error) { dispatchQueue.async { - let handler = self.handlers.removeValue(forKey: request.id) + let handler = self.handlers.removeValue(forKey: request.request) + self.dispatchQueueFactory.main().async { handler?(.failure(IAPError(error: error))) } @@ -99,7 +100,7 @@ extension ProductProvider: SKProductsRequestDelegate { func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) { dispatchQueue.async { - let handler = self.handlers.removeValue(forKey: request.id) + let handler = self.handlers.removeValue(forKey: request.request) guard response.invalidProductIdentifiers.isEmpty else { self.dispatchQueueFactory.main().async { @@ -114,3 +115,11 @@ extension ProductProvider: SKProductsRequestDelegate { } } } + +// MARK: - Helpers + +private extension SKRequest { + var request: ProductsRequest { + ProductsRequest(self) + } +}