Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PriceFormatterProvider: Sendable conformance and fixed thread-safety #1818

Merged
merged 3 commits into from
Aug 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Sources/Misc/Atomic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,6 @@ extension Atomic: ExpressibleByNilLiteral where T: OptionalType {
}

}

// `@unchecked` because of the mutable `_value`, but it's thread-safety is guaranteed with `Lock`.
extension Atomic: @unchecked Sendable {}
7 changes: 7 additions & 0 deletions Sources/Misc/Lock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@ internal final class Lock {
}

}

#if swift(>=5.7)
extension Lock: Sendable {}
#else
// `NSRecursiveLock` isn't `Sendable` until iOS 16.0 / Swift 5.7
extension Lock: @unchecked Sendable {}
#endif
34 changes: 22 additions & 12 deletions Sources/Misc/PriceFormatterProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import Foundation

/// A `NumberFormatter` provider class for prices.
/// This provider caches the formatter to improve the performance.
class PriceFormatterProvider {
final class PriceFormatterProvider: Sendable {

private var cachedPriceFormatterForSK1: NumberFormatter?
private let cachedPriceFormatterForSK1: Atomic<NumberFormatter?> = nil

func priceFormatterForSK1(with locale: Locale) -> NumberFormatter {
func makePriceFormatterForSK1(with locale: Locale) -> NumberFormatter {
Expand All @@ -27,30 +27,40 @@ class PriceFormatterProvider {
return formatter
}

if self.cachedPriceFormatterForSK1 == nil || self.cachedPriceFormatterForSK1?.locale != locale {
self.cachedPriceFormatterForSK1 = makePriceFormatterForSK1(with: locale)
}
return self.cachedPriceFormatterForSK1.modify { formatter in
guard let formatter = formatter, formatter.locale == locale else {
let newFormatter = makePriceFormatterForSK1(with: locale)
formatter = newFormatter

return newFormatter
}

return self.cachedPriceFormatterForSK1!
return formatter
}
}

private var cachedPriceFormatterForSK2: NumberFormatter?
private let cachedPriceFormatterForSK2: Atomic<NumberFormatter?> = nil

@available(iOS 15.0, tvOS 15.0, watchOS 8.0, macOS 12.0, *)
func priceFormatterForSK2(withCurrencyCode currencyCode: String) -> NumberFormatter {
func makePriceFormatterForSK2(withCurrencyCode currencyCode: String) -> NumberFormatter {
func makePriceFormatterForSK2(with currencyCode: String) -> NumberFormatter {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = .autoupdatingCurrent
formatter.currencyCode = currencyCode
return formatter
}

if self.cachedPriceFormatterForSK2 == nil || self.cachedPriceFormatterForSK2?.currencyCode != currencyCode {
self.cachedPriceFormatterForSK2 = makePriceFormatterForSK2(withCurrencyCode: currencyCode)
}
return self.cachedPriceFormatterForSK2.modify { formatter in
guard let formatter = formatter, formatter.currencyCode == currencyCode else {
let newFormatter = makePriceFormatterForSK2(with: currencyCode)
formatter = newFormatter

return newFormatter
}

return self.cachedPriceFormatterForSK2!
return formatter
}
}

}
16 changes: 8 additions & 8 deletions Tests/StoreKitUnitTests/PriceFormatterProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class PriceFormatterProviderTests: StoreKitConfigTestCase {

func testReturnsCachedPriceFormatterForSK1() {
let locale = Locale(identifier: "en_US")
let firstPriceFormatter = priceFormatterProvider.priceFormatterForSK1(with: locale)
let firstPriceFormatter = self.priceFormatterProvider.priceFormatterForSK1(with: locale)

let secondPriceFormatter = priceFormatterProvider.priceFormatterForSK1(with: locale)
let secondPriceFormatter = self.priceFormatterProvider.priceFormatterForSK1(with: locale)

expect(firstPriceFormatter) === secondPriceFormatter
}
Expand All @@ -41,9 +41,9 @@ class PriceFormatterProviderTests: StoreKitConfigTestCase {
try AvailabilityChecks.iOS15APIAvailableOrSkipTest()

let currencyCode = "USD"
let firstPriceFormatter = priceFormatterProvider.priceFormatterForSK2(withCurrencyCode: currencyCode)
let firstPriceFormatter = self.priceFormatterProvider.priceFormatterForSK2(withCurrencyCode: currencyCode)

let secondPriceFormatter = priceFormatterProvider.priceFormatterForSK2(withCurrencyCode: currencyCode)
let secondPriceFormatter = self.priceFormatterProvider.priceFormatterForSK2(withCurrencyCode: currencyCode)

expect(firstPriceFormatter) === secondPriceFormatter
}
Expand All @@ -52,7 +52,7 @@ class PriceFormatterProviderTests: StoreKitConfigTestCase {
func testSk1PriceFormatterUsesCurrentStorefront() async throws {
try AvailabilityChecks.iOS13APIAvailableOrSkipTest()

testSession.locale = Locale(identifier: "es_ES")
self.testSession.locale = Locale(identifier: "es_ES")
await self.changeStorefront("ESP")

let sk1Fetcher = ProductsFetcherSK1(requestTimeout: Configuration.storeKitRequestTimeoutDefault)
Expand All @@ -62,7 +62,7 @@ class PriceFormatterProviderTests: StoreKitConfigTestCase {
var priceFormatter = try XCTUnwrap(storeProduct.priceFormatter)
expect(priceFormatter.currencyCode) == "EUR"

testSession.locale = Locale(identifier: "en_EN")
self.testSession.locale = Locale(identifier: "en_EN")
await self.changeStorefront("USA")

// Note: this test passes only because the cache is manually
Expand All @@ -81,7 +81,7 @@ class PriceFormatterProviderTests: StoreKitConfigTestCase {
func testSk2PriceFormatterUsesCurrentStorefront() async throws {
try AvailabilityChecks.iOS15APIAvailableOrSkipTest()

testSession.locale = Locale(identifier: "es_ES")
self.testSession.locale = Locale(identifier: "es_ES")
await self.changeStorefront("ESP")

let sk2Fetcher = ProductsFetcherSK2()
Expand All @@ -91,7 +91,7 @@ class PriceFormatterProviderTests: StoreKitConfigTestCase {
var priceFormatter = try XCTUnwrap(storeProduct.priceFormatter)
expect(priceFormatter.currencyCode) == "EUR"

testSession.locale = Locale(identifier: "en_EN")
self.testSession.locale = Locale(identifier: "en_EN")
await self.changeStorefront("USA")

// Note: this test passes only because the cache is manually
Expand Down