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

Tests: fixed flaky failure with asynchronous check #2777

Merged
merged 1 commit into from
Jul 11, 2023
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
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))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only change


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