Skip to content

Commit

Permalink
copy(with: VerificationResult): optimization to avoid copies (#2639)
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoSoto authored Jun 14, 2023
1 parent 8c25e97 commit ea173c0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Sources/Identity/CustomerInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ extension CustomerInfo {

/// Creates a copy of this ``CustomerInfo`` modifying only the ``VerificationResult``.
func copy(with entitlementVerification: VerificationResult) -> Self {
guard entitlementVerification != self.data.entitlementVerification else { return self }

var copy = self.data
copy.entitlementVerification = entitlementVerification
return .init(data: copy)
Expand Down
2 changes: 2 additions & 0 deletions Sources/Networking/HTTPClient/HTTPResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ extension HTTPResponse {
}

func copy(with newVerificationResult: VerificationResult) -> Self {
guard newVerificationResult != self.verificationResult else { return self }

return .init(
statusCode: self.statusCode,
responseHeaders: self.responseHeaders,
Expand Down
57 changes: 57 additions & 0 deletions Tests/UnitTests/Networking/HTTPResponseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,51 @@ class HTTPResponseTests: TestCase {
expect(response.requestDate).to(beNil())
}

func testCopyWithSameVerificationResult() throws {
self.verifyCopy(of: try Self.sampleResponse.copy(with: .verified),
onlyModifiesEntitlementVerification: .verified)
}

func testCopyWithVerificationResultVerified() throws {
self.verifyCopy(of: try Self.sampleResponse,
onlyModifiesEntitlementVerification: .verified)
}

func testCopyWithVerificationResultFailedVerified() throws {
self.verifyCopy(of: try Self.sampleResponse,
onlyModifiesEntitlementVerification: .failed)
}

func testCopyWithVerificationResultNotRequested() throws {
self.verifyCopy(of: try Self.sampleResponse.copy(with: .verified),
onlyModifiesEntitlementVerification: .notRequested)
}

// MARK: -

private func verifyCopy<T: Equatable>(
of response: HTTPResponse<T>,
onlyModifiesEntitlementVerification newVerification: VerificationResult
) {
let copy = response.copy(with: newVerification)
expect(copy.verificationResult) == newVerification
expect(copy.statusCode) == response.statusCode
expect(copy.responseHeaders).to(haveCount(response.responseHeaders.count))
expect(copy.body) == response.body
expect(copy.requestDate) == response.requestDate
}

private static var sampleResponse: HTTPResponse<Data> {
get throws {
return .create(
body: try CustomerInfo.emptyInfo.prettyPrintedData,
headers: [
"X-Header": "true"
]
)
}
}

}

private extension HTTPResponse where Body == HTTPEmptyResponseBody {
Expand All @@ -90,6 +135,18 @@ private extension HTTPResponse where Body == HTTPEmptyResponseBody {

}

private extension HTTPResponse where Body == Data {

static func create(body: Data, headers: HTTPResponse.Headers) -> Self {
return .init(statusCode: .success,
responseHeaders: headers,
body: body,
requestDate: Date(),
verificationResult: .notRequested)
}

}

// MARK: - HTTPResponseBody

class HTTPResponseBodyTests: TestCase {
Expand Down
4 changes: 4 additions & 0 deletions Tests/UnitTests/Purchasing/CustomerInfoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,10 @@ class BasicCustomerInfoTests: TestCase {
== Set(["onemonth_freetrial", "twomonth_freetrial", "threemonth_freetrial"])
}

func testCopyWithSameVerificationResult() throws {
expect(self.customerInfo.copy(with: .notRequested)) === self.customerInfo
}

func testCopyWithVerificationResultVerified() throws {
self.verifyCopy(of: self.customerInfo,
onlyModifiesEntitlementVerification: .verified)
Expand Down

0 comments on commit ea173c0

Please sign in to comment.