Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vahidlazio committed Apr 8, 2024
1 parent 98b6f17 commit a2d0abd
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
52 changes: 52 additions & 0 deletions Tests/ConfidenceProviderTests/EventSenderEngineTest.swift
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)
}
}

46 changes: 46 additions & 0 deletions Tests/ConfidenceProviderTests/EventUploaderMock.swift
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)
}

}

0 comments on commit a2d0abd

Please sign in to comment.