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

CustomerInfoManagerTests: wait for getAndCacheCustomerInfo to finish #2555

Merged
merged 1 commit into from
May 30, 2023
Merged
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
79 changes: 27 additions & 52 deletions Tests/UnitTests/Identity/CustomerInfoManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ class BaseCustomerInfoManagerTests: TestCase {
systemInfo: self.mockSystemInfo)
}

@discardableResult
func fetchAndCacheCustomerInfo(isAppBackground: Bool = true) throws -> Result<CustomerInfo, BackendError> {
let result = waitUntilValue { completion in
self.customerInfoManager.fetchAndCacheCustomerInfo(appUserID: Self.appUserID,
isAppBackgrounded: isAppBackground,
completion: completion)
}

return try XCTUnwrap(result)
}
}

class CustomerInfoManagerTests: BaseCustomerInfoManagerTests {
Expand All @@ -66,41 +76,33 @@ class CustomerInfoManagerTests: BaseCustomerInfoManagerTests {
self.customerInfoMonitorDisposable?()
}

func testFetchAndCacheCustomerInfoAllowOfflineCustomerInfo() {
func testFetchAndCacheCustomerInfoAllowOfflineCustomerInfo() throws {
self.mockOfflineEntitlementsManager.stubbedShouldComputeOfflineCustomerInfo = true

self.customerInfoManager.fetchAndCacheCustomerInfo(appUserID: Self.appUserID,
isAppBackgrounded: true,
completion: nil)
try self.fetchAndCacheCustomerInfo(isAppBackground: true)

expect(self.mockBackend.invokedGetSubscriberDataCount) == 1
expect(self.mockBackend.invokedGetSubscriberDataParameters?.allowComputingOffline) == true
}

func testFetchAndCacheCustomerInfoDontAllowOfflineCustomerInfo() {
func testFetchAndCacheCustomerInfoDontAllowOfflineCustomerInfo() throws {
self.mockOfflineEntitlementsManager.stubbedShouldComputeOfflineCustomerInfo = false

self.customerInfoManager.fetchAndCacheCustomerInfo(appUserID: Self.appUserID,
isAppBackgrounded: true,
completion: nil)
try self.fetchAndCacheCustomerInfo(isAppBackground: true)

expect(self.mockBackend.invokedGetSubscriberDataCount) == 1
expect(self.mockBackend.invokedGetSubscriberDataParameters?.allowComputingOffline) == false
}

func testFetchAndCacheCustomerInfoCallsBackendWithRandomDelayIfAppBackgrounded() {
self.customerInfoManager.fetchAndCacheCustomerInfo(appUserID: Self.appUserID,
isAppBackgrounded: true,
completion: nil)
func testFetchAndCacheCustomerInfoCallsBackendWithRandomDelayIfAppBackgrounded() throws {
try self.fetchAndCacheCustomerInfo(isAppBackground: true)

expect(self.mockBackend.invokedGetSubscriberDataCount) == 1
expect(self.mockBackend.invokedGetSubscriberDataParameters?.randomDelay) == true
}

func testFetchAndCacheCustomerInfoCallsBackendWithoutRandomDelayIfAppForegrounded() {
self.customerInfoManager.fetchAndCacheCustomerInfo(appUserID: Self.appUserID,
isAppBackgrounded: false,
completion: nil)
func testFetchAndCacheCustomerInfoCallsBackendWithoutRandomDelayIfAppForegrounded() throws {
try self.fetchAndCacheCustomerInfo(isAppBackground: false)

expect(self.mockBackend.invokedGetSubscriberDataCount) == 1
expect(self.mockBackend.invokedGetSubscriberDataParameters?.randomDelay) == false
Expand All @@ -110,55 +112,33 @@ class CustomerInfoManagerTests: BaseCustomerInfoManagerTests {
let mockError: BackendError = .missingAppUserID()
mockBackend.stubbedGetCustomerInfoResult = .failure(mockError)

let receivedError = waitUntilValue { completed in
self.customerInfoManager.fetchAndCacheCustomerInfo(appUserID: Self.appUserID,
isAppBackgrounded: false) { result in
completed(result.error)
}
}

let receivedError = try self.fetchAndCacheCustomerInfo(isAppBackground: false).error
expect(receivedError) == mockError
}

func testFetchAndCacheCustomerInfoClearsCustomerInfoTimestampIfBackendError() {
func testFetchAndCacheCustomerInfoClearsCustomerInfoTimestampIfBackendError() throws {
mockBackend.stubbedGetCustomerInfoResult = .failure(.missingAppUserID())

waitUntil { completed in
self.customerInfoManager.fetchAndCacheCustomerInfo(appUserID: Self.appUserID,
isAppBackgrounded: false) { _ in
completed()
}
}
try self.fetchAndCacheCustomerInfo(isAppBackground: false)

expect(self.mockDeviceCache.clearCustomerInfoCacheTimestampCount) == 1
}

func testFetchAndCacheCustomerInfoCachesIfSuccessful() {
func testFetchAndCacheCustomerInfoCachesIfSuccessful() throws {
mockBackend.stubbedGetCustomerInfoResult = .success(mockCustomerInfo)

let receivedCustomerInfo = waitUntilValue { completed in
self.customerInfoManager.fetchAndCacheCustomerInfo(appUserID: Self.appUserID,
isAppBackgrounded: false) { result in
completed(result.value)
}
}

let receivedCustomerInfo = try self.fetchAndCacheCustomerInfo(isAppBackground: false).value
expect(receivedCustomerInfo) == self.mockCustomerInfo

expect(self.mockDeviceCache.cacheCustomerInfoCount) == 1
expect(self.customerInfoManagerChangesCallCount) == 1
expect(self.customerInfoManagerLastCustomerInfo) == self.mockCustomerInfo
}

func testFetchAndCacheCustomerInfoCallsCompletionOnMainThread() {
func testFetchAndCacheCustomerInfoCallsCompletionOnMainThread() throws {
mockBackend.stubbedGetCustomerInfoResult = .success(mockCustomerInfo)

waitUntil { completed in
self.customerInfoManager.fetchAndCacheCustomerInfo(appUserID: Self.appUserID,
isAppBackgrounded: false) { _ in
completed()
}
}
try self.fetchAndCacheCustomerInfo(isAppBackground: false)

expect(self.mockOperationDispatcher.invokedDispatchAsyncOnMainThreadCount) == 1
}
Expand Down Expand Up @@ -198,15 +178,10 @@ class CustomerInfoManagerTests: BaseCustomerInfoManagerTests {
expect(self.mockBackend.invokedGetSubscriberDataCount) == 1
}

func testFetchAndCacheCustomerInfoIfStaleFetchesIfCacheEmpty() {
func testFetchAndCacheCustomerInfoIfStaleFetchesIfCacheEmpty() throws {
mockDeviceCache.stubbedIsCustomerInfoCacheStale = false

waitUntil { completed in
self.customerInfoManager.fetchAndCacheCustomerInfoIfStale(appUserID: Self.appUserID,
isAppBackgrounded: false) { _ in
completed()
}
}
try self.fetchAndCacheCustomerInfo(isAppBackground: false)

expect(self.mockBackend.invokedGetSubscriberDataCount) == 1
}
Expand Down