Skip to content

Commit

Permalink
Integration Tests: add tests to verify CustomerInfo+Offerings r…
Browse files Browse the repository at this point in the history
…equest de-dupping (#3013)

This adds more coverage for why we don't actually need #2983.
  • Loading branch information
NachoSoto authored Aug 14, 2023
1 parent 7f46e94 commit 8f2c742
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions Tests/BackendIntegrationTests/OtherIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,85 @@ class OtherIntegrationTests: BaseBackendIntegrationTests {
expect(info.isComputedOffline) == false
}

func testGetCustomerInfoMultipleTimesInParallel() async throws {
let requestCount = 3

let purchases = try self.purchases

// 1. Make sure any existing customer info requests finish
_ = try await purchases.customerInfo()
// 2. Invalidate cache
purchases.invalidateCustomerInfoCache()
self.logger.clearMessages()

// 3. Request customer info multiple times in parallel
await withThrowingTaskGroup(of: Void.self) {
for _ in 0..<requestCount {
$0.addTask { _ = try await purchases.customerInfo() }
}
}

// 4. Verify N-1 requests were de-duped
self.logger.verifyMessageWasLogged(
"Network operation 'GetCustomerInfoOperation' found with the same cache key",
level: .debug,
expectedCount: requestCount - 1
)
self.logger.verifyMessageWasLogged(
Strings.network.api_request_completed(
.init(method: .get,
path: .getCustomerInfo(appUserID: try self.purchases.appUserID)),
httpCode: .notModified
),
level: .debug,
expectedCount: 1
)
}

func testGetCustomerInfoCaching() async throws {
_ = try await self.purchases.customerInfo()

self.logger.clearMessages()

_ = try await self.purchases.customerInfo()

self.logger.verifyMessageWasLogged(Strings.customerInfo.vending_cache, level: .debug)
self.logger.verifyMessageWasNotLogged("API request started")
}

func testGetOfferingsMultipleTimesInParallel() async throws {
let requestCount = 3

let purchases = try self.purchases

// 1. Invalidate cache
purchases.invalidateOfferingsCache()
self.logger.clearMessages()

// 2. Request offerings multiple times in parallel
await withThrowingTaskGroup(of: Void.self) {
for _ in 0..<requestCount {
$0.addTask { _ = try await purchases.offerings() }
}
}

// 3. Verify N-1 requests were de-duped
self.logger.verifyMessageWasLogged(
"Network operation 'GetOfferingsOperation' found with the same cache key",
level: .debug,
expectedCount: requestCount - 1
)
self.logger.verifyMessageWasLogged(
Strings.network.api_request_completed(
.init(method: .get,
path: .getOfferings(appUserID: try self.purchases.appUserID)),
httpCode: .notModified
),
level: .debug,
expectedCount: 1
)
}

func testGetCustomerInfoReturnsNotModified() async throws {
// 1. Fetch user once
_ = try await self.purchases.customerInfo(fetchPolicy: .fetchCurrent)
Expand Down

0 comments on commit 8f2c742

Please sign in to comment.