Skip to content

Commit

Permalink
Tests: fixed flaky failure with asynchronous check (#2777)
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoSoto authored Jul 11, 2023
1 parent 1cab844 commit 1f83693
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Tests/UnitTests/Purchasing/Purchases/BasePurchasesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ extension BasePurchasesTests {
var userID: String?
var originalApplicationVersion: String?
var originalPurchaseDate: Date?
var getSubscriberCallCount = 0
var getCustomerInfoCallCount = 0
var overrideCustomerInfoResult: Result<CustomerInfo, BackendError> = .success(
// swiftlint:disable:next force_try
try! CustomerInfo(data: BasePurchasesTests.emptyCustomerInfoData)
Expand All @@ -387,7 +387,7 @@ extension BasePurchasesTests {
withRandomDelay randomDelay: Bool,
allowComputingOffline: Bool,
completion: @escaping CustomerAPI.CustomerInfoResponseHandler) {
self.getSubscriberCallCount += 1
self.getCustomerInfoCallCount += 1
self.userID = appUserID

let result = self.overrideCustomerInfoResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,13 @@ class PurchasesConfiguringTests: BasePurchasesTests {
func testFirstInitializationFromBackgroundDoesntUpdateCustomerInfoCache() {
self.systemInfo.stubbedIsApplicationBackgrounded = true
self.setupPurchases()
expect(self.backend.getSubscriberCallCount).toEventually(equal(0))
expect(self.backend.getCustomerInfoCallCount).toEventually(equal(0))
}

func testFirstInitializationFromForegroundUpdatesCustomerInfoCacheIfNotInUserDefaults() {
self.systemInfo.stubbedIsApplicationBackgrounded = false
self.setupPurchases()
expect(self.backend.getSubscriberCallCount).toEventually(equal(1))
expect(self.backend.getCustomerInfoCallCount).toEventually(equal(1))
}

func testFirstInitializationFromForegroundUpdatesCustomerInfoCacheIfUserDefaultsCacheStale() {
Expand All @@ -304,7 +304,7 @@ class PurchasesConfiguringTests: BasePurchasesTests {

self.setupPurchases()

expect(self.backend.getSubscriberCallCount).toEventually(equal(1))
expect(self.backend.getCustomerInfoCallCount).toEventually(equal(1))
}

func testFirstInitializationFromForegroundUpdatesCustomerInfoEvenIfCacheValid() {
Expand All @@ -316,7 +316,7 @@ class PurchasesConfiguringTests: BasePurchasesTests {

self.setupPurchases()

expect(self.backend.getSubscriberCallCount).toEventually(equal(1))
expect(self.backend.getCustomerInfoCallCount).toEventually(equal(1))
}

func testProxyURL() {
Expand Down Expand Up @@ -474,7 +474,7 @@ class PurchasesConfiguringTests: BasePurchasesTests {

self.setupPurchases()

expect(self.backend.getSubscriberCallCount).toEventually(equal(0))
expect(self.backend.getCustomerInfoCallCount).toEventually(equal(0))
}

// MARK: - UserDefaults
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,21 @@ class PurchasesDelegateTests: BasePurchasesTests {
}

func testAutomaticallyFetchesCustomerInfoOnDidBecomeActiveIfCacheStale() {
expect(self.backend.getSubscriberCallCount).toEventually(equal(1))
expect(self.backend.getCustomerInfoCallCount).toEventually(equal(1))

self.deviceCache.stubbedIsCustomerInfoCacheStale = true
self.notificationCenter.fireNotifications()

expect(self.backend.getSubscriberCallCount).toEventually(equal(2))
expect(self.backend.getCustomerInfoCallCount).toEventually(equal(2))
}

func testDoesntAutomaticallyFetchCustomerInfoOnDidBecomeActiveIfCacheValid() {
expect(self.backend.getSubscriberCallCount).toEventually(equal(1))
expect(self.backend.getCustomerInfoCallCount).toEventually(equal(1))
self.deviceCache.stubbedIsCustomerInfoCacheStale = false

self.notificationCenter.fireNotifications()

expect(self.backend.getSubscriberCallCount).toEventually(equal(1))
expect(self.backend.getCustomerInfoCallCount).toEventually(equal(1))
}

func testAutomaticallyCallsDelegateOnDidBecomeActiveAndUpdate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@ class PurchasesGetCustomerInfoTests: BasePurchasesTests {
func testDoesntSendCacheIfNoCacheAndCallsBackendAgain() {
self.setupPurchases()

expect(self.backend.getSubscriberCallCount).toEventually(equal(1))
expect(self.backend.getCustomerInfoCallCount).toEventually(equal(1))

self.deviceCache.cachedCustomerInfo = [:]

waitUntil { completion in
self.purchases.getCustomerInfo { (_, _) in completion() }
}

expect(self.backend.getSubscriberCallCount) == 2
expect(self.backend.getCustomerInfoCallCount) == 2
}

func testFetchCustomerInfoWhenCacheStale() {
Expand All @@ -157,7 +157,7 @@ class PurchasesGetCustomerInfoTests: BasePurchasesTests {
self.purchases.getCustomerInfo { (_, _) in completed() }
}

expect(self.backend.getSubscriberCallCount) == 2
expect(self.backend.getCustomerInfoCallCount) == 2
}

func testGetCustomerInfoAfterInvalidatingDoesntReturnCachedVersion() throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class PurchasesLogInTests: BasePurchasesTests {
self.identityManager.mockLogOutError = nil
self.backend.overrideCustomerInfoResult = .success(Self.mockLoggedOutInfo)

expect(self.backend.getSubscriberCallCount) == 1
expect(self.backend.getCustomerInfoCallCount).toEventually(equal(1))

let result = waitUntilValue { completed in
self.purchases.logOut { customerInfo, error in
Expand All @@ -76,7 +76,7 @@ class PurchasesLogInTests: BasePurchasesTests {
expect(result).to(beSuccess())
expect(result?.value) == Self.mockLoggedOutInfo

expect(self.backend.getSubscriberCallCount) == 2
expect(self.backend.getCustomerInfoCallCount) == 2
expect(self.identityManager.invokedLogOutCount) == 1
}

Expand All @@ -94,7 +94,7 @@ class PurchasesLogInTests: BasePurchasesTests {
expect(result).to(beFailure())
expect(result?.error).to(matchError(error))

expect(self.backend.getSubscriberCallCount) == 1
expect(self.backend.getCustomerInfoCallCount) == 1
expect(self.identityManager.invokedLogOutCount) == 1
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,7 @@ class PurchasesPurchasingCustomSetupTests: BasePurchasesTests {
expect(underlyingError.domain) == SKErrorDomain
expect(underlyingError.code) == SKError.Code.paymentCancelled.rawValue

expect(self.backend.getSubscriberCallCount) == 0
expect(self.backend.getCustomerInfoCallCount) == 0
}

@MainActor
Expand Down Expand Up @@ -1066,7 +1066,7 @@ class PurchasesPurchasingCustomSetupTests: BasePurchasesTests {
expect(receivedError).to(matchError(ErrorCode.purchaseCancelledError))
expect(result).to(beNil())

expect(self.backend.getSubscriberCallCount) == 0
expect(self.backend.getCustomerInfoCallCount) == 0
}

// MARK: -
Expand Down

0 comments on commit 1f83693

Please sign in to comment.