-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: handle status codes for retrying in uploader (#95)
* feat: handle status codes for retrying in uploader
- Loading branch information
1 parent
8f253ce
commit 85b89ed
Showing
6 changed files
with
147 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import Foundation | ||
import Common | ||
import XCTest | ||
|
||
@testable import Confidence | ||
|
||
final class MinSizeFlushPolicy: FlushPolicy { | ||
private var maxSize = 5 | ||
private var size = 0 | ||
func reset() { | ||
size = 0 | ||
} | ||
|
||
func hit(event: ConfidenceEvent) { | ||
size += 1 | ||
} | ||
|
||
func shouldFlush() -> Bool { | ||
return size >= maxSize | ||
} | ||
} | ||
|
||
final class ImmidiateFlushPolicy: FlushPolicy { | ||
private var size = 0 | ||
|
||
func reset() { | ||
size = 0 | ||
} | ||
|
||
func hit(event: ConfidenceEvent) { | ||
size += 1 | ||
} | ||
|
||
func shouldFlush() -> Bool { | ||
return size > 0 | ||
} | ||
} | ||
|
||
final class EventSenderEngineTest: XCTestCase { | ||
func testAddingEventsWithSizeFlushPolicyWorks() throws { | ||
let flushPolicies = [MinSizeFlushPolicy()] | ||
let uploader = EventUploaderMock() | ||
let eventSenderEngine = EventSenderEngineImpl( | ||
clientSecret: "CLIENT_SECRET", | ||
uploader: uploader, | ||
storage: EventStorageMock(), | ||
flushPolicies: flushPolicies | ||
) | ||
|
||
let expectation = XCTestExpectation(description: "Upload finished") | ||
let cancellable = uploader.subject.sink { _ in | ||
expectation.fulfill() | ||
} | ||
|
||
var events: [ConfidenceEvent] = [] | ||
for i in 0..<5 { | ||
events.append(ConfidenceEvent( | ||
name: "\(i)", | ||
payload: [:], | ||
eventTime: Date.backport.now) | ||
) | ||
eventSenderEngine.emit(definition: "\(i)", payload: [:], context: [:]) | ||
} | ||
|
||
wait(for: [expectation], timeout: 5) | ||
let uploadRequest = try XCTUnwrap(uploader.calledRequest) | ||
XCTAssertTrue(uploadRequest.map { $0.eventDefinition } == events.map { $0.name }) | ||
|
||
uploader.reset() | ||
eventSenderEngine.emit(definition: "Hello", payload: [:], context: [:]) | ||
XCTAssertNil(uploader.calledRequest) | ||
cancellable.cancel() | ||
} | ||
|
||
func testRemoveEventsFromStorageOnBadRequest() throws { | ||
MockedClientURLProtocol.mockedOperation = .badRequest | ||
let client = RemoteConfidenceClient( | ||
options: ConfidenceClientOptions(credentials: ConfidenceClientCredentials.clientSecret(secret: "")), | ||
session: MockedClientURLProtocol.mockedSession(), | ||
metadata: ConfidenceMetadata(name: "", version: "")) | ||
|
||
let flushPolicies = [ImmidiateFlushPolicy()] | ||
let storage = EventStorageMock() | ||
let eventSenderEngine = EventSenderEngineImpl( | ||
clientSecret: "CLIENT_SECRET", | ||
uploader: client, | ||
storage: storage, | ||
flushPolicies: flushPolicies | ||
) | ||
eventSenderEngine.emit(definition: "testEvent", payload: ConfidenceStruct(), context: ConfidenceStruct()) | ||
let expectation = expectation(description: "events batched") | ||
storage.eventsRemoved{ | ||
expectation.fulfill() | ||
} | ||
wait(for: [expectation], timeout: 2) | ||
|
||
XCTAssertEqual(storage.isEmpty(), true) | ||
} | ||
|
||
func testKeepEventsInStorageForRetry() throws { | ||
MockedClientURLProtocol.mockedOperation = .needRetryLater | ||
let client = RemoteConfidenceClient( | ||
options: ConfidenceClientOptions(credentials: ConfidenceClientCredentials.clientSecret(secret: "")), | ||
session: MockedClientURLProtocol.mockedSession(), | ||
metadata: ConfidenceMetadata(name: "", version: "")) | ||
|
||
let flushPolicies = [ImmidiateFlushPolicy()] | ||
let storage = EventStorageMock() | ||
let eventSenderEngine = EventSenderEngineImpl( | ||
clientSecret: "CLIENT_SECRET", | ||
uploader: client, | ||
storage: storage, | ||
flushPolicies: flushPolicies | ||
) | ||
|
||
eventSenderEngine.emit(definition: "testEvent", payload: ConfidenceStruct(), context: ConfidenceStruct()) | ||
|
||
XCTAssertEqual(storage.isEmpty(), false) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters