Skip to content

Commit

Permalink
Fix flacky tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ns-vasilev committed Jan 18, 2024
1 parent 61b1f24 commit 94503d7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
33 changes: 33 additions & 0 deletions Sources/Flare/Classes/Models/Internal/ProductsRequest.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
10 changes: 10 additions & 0 deletions Sources/Flare/Classes/Models/Internal/Protocols/ISKRequest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// Flare
// Copyright © 2024 Space Code. All rights reserved.
//

import Foundation

protocol ISKRequest: Hashable {
var id: String { get }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Flare
// Copyright © 2023 Space Code. All rights reserved.
// Copyright © 2024 Space Code. All rights reserved.
//

import Concurrency
Expand Down Expand Up @@ -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

Expand All @@ -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()
}
Expand All @@ -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)))
}
Expand All @@ -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 {
Expand All @@ -114,3 +115,11 @@ extension ProductProvider: SKProductsRequestDelegate {
}
}
}

// MARK: - Helpers

private extension SKRequest {
var request: ProductsRequest {
ProductsRequest(self)
}
}

0 comments on commit 94503d7

Please sign in to comment.