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

OperationQueue: log debug message when requests are found in cache and skipped #1926

Merged
merged 1 commit into from
Sep 21, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extension OperationQueue {
case .firstCallbackAddedToList:
self.addOperation(operation)
case .addedToExistingInFlightList:
Logger.debug(Strings.network.reusing_existing_request_for_operation(operation))
return
}
}
Expand Down
5 changes: 5 additions & 0 deletions Sources/Logging/Strings/NetworkStrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ enum NetworkStrings {

case api_request_completed(_ request: HTTPRequest, httpCode: HTTPStatusCode)
case api_request_started(HTTPRequest)
case reusing_existing_request_for_operation(CacheableNetworkOperation)
case creating_json_error(error: String)
case json_data_received(dataString: String)
case parsing_json_error(error: Error)
Expand All @@ -45,6 +46,10 @@ extension NetworkStrings: CustomStringConvertible {
case let .api_request_started(request):
return "API request started: \(request.method.httpMethod) \(request.path.url?.path ?? "")"

case let .reusing_existing_request_for_operation(operation):
return "Network operation '\(type(of: operation))' found with the same cache key " +
"'\(operation.individualizedCacheKeyPart.prefix(15))...'. Skipping request."

case let .creating_json_error(error):
return "Error creating request with body: \(error)"

Expand Down
18 changes: 18 additions & 0 deletions Tests/UnitTests/Networking/Backend/BackendGetOfferingsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ class BackendGetOfferingsTests: BaseBackendTests {
expect(self.httpClient.calls).toEventually(haveCount(1))
}

func testRepeatedRequestsLogDebugMessage() {
let logger = TestLogHandler()

self.httpClient.mock(
requestPath: .getOfferings(appUserID: Self.userID),
response: .init(statusCode: .success, response: Self.noOfferingsResponse as [String: Any])
)
self.offerings.getOfferings(appUserID: Self.userID, withRandomDelay: false) { _ in }
self.offerings.getOfferings(appUserID: Self.userID, withRandomDelay: false) { _ in }

expect(self.httpClient.calls).toEventually(haveCount(1))

logger.verifyMessageWasLogged(
"Network operation '\(GetOfferingsOperation.self)' found with the same cache key",
level: .debug
)
}

func testGetEntitlementsDoesntCacheForMultipleUserID() {
let response = MockHTTPClient.Response(statusCode: .success,
response: Self.noOfferingsResponse as [String: Any])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"headers" : {
"Authorization" : "Bearer asharedsecret"
},
"request" : {
"body" : null,
"method" : "GET",
"url" : "https://api.revenuecat.com/v1/subscribers/user/offerings"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"headers" : {
"Authorization" : "Bearer asharedsecret"
},
"request" : {
"body" : null,
"method" : "GET",
"url" : "https://api.revenuecat.com/v1/subscribers/user/offerings"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"headers" : {
"Authorization" : "Bearer asharedsecret"
},
"request" : {
"body" : null,
"method" : "GET",
"url" : "https://api.revenuecat.com/v1/subscribers/user/offerings"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"headers" : {
"Authorization" : "Bearer asharedsecret"
},
"request" : {
"body" : null,
"method" : "GET",
"url" : "https://api.revenuecat.com/v1/subscribers/user/offerings"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"headers" : {
"Authorization" : "Bearer asharedsecret"
},
"request" : {
"body" : null,
"method" : "GET",
"url" : "https://api.revenuecat.com/v1/subscribers/user/offerings"
}
}
5 changes: 4 additions & 1 deletion Tests/UnitTests/TestHelpers/TestLogHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ extension TestLogHandler {
line: line,
self.messages
)
.to(containElementSatisfying(Self.entryCondition(message: message, level: level)))
.to(
containElementSatisfying(Self.entryCondition(message: message, level: level)),
description: "Message not found. Logged messages: \(self.messages)"
)
}

func verifyMessageWasNotLogged(
Expand Down