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

copy(with: VerificationResult): optimization to avoid copies #2639

Merged
merged 4 commits into from
Jun 14, 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
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 }
NachoSoto marked this conversation as resolved.
Show resolved Hide resolved

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