-
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.
- Loading branch information
1 parent
98b6f17
commit a2d0abd
Showing
2 changed files
with
98 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import Foundation | ||
import XCTest | ||
|
||
@testable import Confidence | ||
|
||
final class MinSizeFlushPolicy: FlushPolicy { | ||
private var maxSize = 5 | ||
private var size = 0 | ||
func reset() { | ||
size = 0 | ||
} | ||
|
||
func hit(event: Event) { | ||
size += 1 | ||
} | ||
|
||
func shouldFlush() -> Bool { | ||
return size >= maxSize | ||
} | ||
|
||
|
||
} | ||
|
||
final class EventSenderEngineTest: XCTestCase { | ||
func testAddingEventsWithSizeFlushPolicyWorks() throws { | ||
let flushPolicies = [MinSizeFlushPolicy()] | ||
let uploader = EventUploaderMock() | ||
let eventSenderEngine = EventSenderEngineImpl( | ||
clientSecret: "CLIENT_SECRET", | ||
uploader: uploader, | ||
clock: ClockMock(), | ||
storage: EventStorageMock(), | ||
flushPolicies: flushPolicies | ||
) | ||
|
||
var events: [Event] = [] | ||
for i in 0..<5 { | ||
events.append(Event(name: "\(i)")) | ||
eventSenderEngine.send(name: "\(i)") | ||
} | ||
|
||
let expectedRequest = EventBatchRequest(clientSecret: "CLIENT_SECRET", sendTime: Date(), events: events) | ||
let uploadRequest = try XCTUnwrap(uploader.calledRequest) | ||
XCTAssertTrue(uploadRequest.clientSecret == expectedRequest.clientSecret) | ||
XCTAssertTrue(uploadRequest.events == expectedRequest.events) | ||
|
||
uploader.reset() | ||
eventSenderEngine.send(name: "Hello") | ||
XCTAssertNil(uploader.calledRequest) | ||
} | ||
} | ||
|
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,46 @@ | ||
import Foundation | ||
@testable import Confidence | ||
|
||
final class EventUploaderMock: EventsUploader { | ||
var calledRequest: EventBatchRequest? = nil | ||
func upload(request: EventBatchRequest) -> Bool { | ||
calledRequest = request | ||
return true | ||
} | ||
|
||
func reset() { | ||
calledRequest = nil | ||
} | ||
} | ||
|
||
final class ClockMock: Clock { | ||
func now() -> Date { | ||
return Date() | ||
} | ||
} | ||
|
||
final class EventStorageMock: EventStorage { | ||
private var events: [Event] = [] | ||
private var batches: [String: [Event]] = [:] | ||
func startNewBatch() throws { | ||
batches[("\(batches.count)")] = events | ||
events.removeAll() | ||
} | ||
|
||
func writeEvent(event: Event) throws { | ||
events.append(event) | ||
} | ||
|
||
func batchReadyIds() -> [String] { | ||
return batches.map({ batch in batch.0}) | ||
} | ||
|
||
func eventsFrom(id: String) throws -> [Event] { | ||
return batches[id]! | ||
} | ||
|
||
func remove(id: String) throws { | ||
batches.removeValue(forKey: id) | ||
} | ||
|
||
} |