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

feat: handle status codes for retrying in uploader #95

Merged
merged 3 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -38,21 +38,27 @@
sendTime: timeString,
sdk: Sdk(id: metadata.name, version: metadata.version)
)

do {
let result: HttpClientResult<PublishEventResponse> =
try await self.httpClient.post(path: ":publish", data: request)
switch result {
case .success(let successData):
guard successData.response.status == .ok else {
guard let status = successData.response.status else {
nickybondarenko marked this conversation as resolved.
Show resolved Hide resolved
throw successData.response.mapStatusToError(error: successData.decodedError)
}
let indexedErrorsCount = successData.decodedData?.errors.count ?? 0
if indexedErrorsCount > 0 {
Logger(subsystem: "com.confidence.client", category: "network").error(
"Backend reported errors for \(indexedErrorsCount) event(s) in batch")
switch status.rawValue {
case 200:
//clean up in case of success

Check failure on line 51 in Sources/Confidence/ConfidenceClient/RemoteConfidenceClient.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Comment Spacing Violation: Prefer at least one space after slashes for comments (comment_spacing)
return true
case 429:
//we shouldn't clean up for rate limiting

Check failure on line 54 in Sources/Confidence/ConfidenceClient/RemoteConfidenceClient.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Comment Spacing Violation: Prefer at least one space after slashes for comments (comment_spacing)
return false
case 400...499:
//if batch couldn't be processed, we should clean it up

Check failure on line 57 in Sources/Confidence/ConfidenceClient/RemoteConfidenceClient.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Comment Spacing Violation: Prefer at least one space after slashes for comments (comment_spacing)
return true
default:
return false
}
return true
case .failure(let errorData):
throw handleError(error: errorData)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,30 @@ final class EventSenderEngineTest: XCTestCase {
XCTAssertNil(uploader.calledRequest)
cancellable.cancel()
}

func testRemoveEventsFromStorageOnBadRequest() async throws {
nickybondarenko marked this conversation as resolved.
Show resolved Hide resolved
MockedClientURLProtocol.mockedOperation = .badRequest
let client = RemoteConfidenceClient(
options: ConfidenceClientOptions(credentials: ConfidenceClientCredentials.clientSecret(secret: "")),
session: MockedClientURLProtocol.mockedSession(),
metadata: ConfidenceMetadata(name: "", version: ""))

let flushPolicies = [MinSizeFlushPolicy()]
let storage = EventStorageMock()
let eventSenderEngine = EventSenderEngineImpl(
clientSecret: "CLIENT_SECRET",
uploader: client,
storage: storage,
flushPolicies: flushPolicies
)
_ = try await client.upload(events: [
NetworkEvent(
eventDefinition: "testEvent",
payload: NetworkStruct.init(fields: [:]),
eventTime: Date.backport.nowISOString
)
])

XCTAssertEqual(try storage.isEmpty(), true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,12 @@
func remove(id: String) throws {
batches.removeValue(forKey: id)
}

internal func isEmpty() throws -> Bool {
if self.events.count == 0 {

Check failure on line 48 in Tests/ConfidenceTests/EventUploaderMock.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Empty Count Violation: Prefer checking `isEmpty` over comparing `count` to zero (empty_count)
return true
} else {
return false
}
}
}
25 changes: 0 additions & 25 deletions Tests/ConfidenceTests/RemoteConfidenceClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,6 @@ class RemoteConfidenceClientTest: XCTestCase {
XCTAssertTrue(processed)
}

func testBadRequestThrows() async throws {
MockedClientURLProtocol.mockedOperation = .badRequest
let client = RemoteConfidenceClient(
options: ConfidenceClientOptions(
credentials: ConfidenceClientCredentials.clientSecret(secret: "")),
session: MockedClientURLProtocol.mockedSession(),
metadata: ConfidenceMetadata(name: "", version: ""))

var caughtError: ConfidenceError?
do {
_ = try await client.upload(events: [
NetworkEvent(
eventDefinition: "testEvent",
payload: NetworkStruct.init(fields: [:]),
eventTime: Date.backport.nowISOString
)
])
} catch {
// swiftlint:disable:next force_cast
caughtError = error as! ConfidenceError?
}
let expectedError = ConfidenceError.badRequest(message: "explanation about malformed request")
XCTAssertEqual(caughtError, expectedError)
}

func testNMalformedResponseThrows() async throws {
MockedClientURLProtocol.mockedOperation = .malformedResponse
let client = RemoteConfidenceClient(
Expand Down
Loading