Skip to content
Open
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
10 changes: 9 additions & 1 deletion Sources/Aptabase/AptabaseClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ class AptabaseClient {
dispatcher = EventDispatcher(appKey: appKey, baseUrl: baseUrl, env: env)
}

/// Begins tracking an event in the asynchronously.
public func trackEvent(_ eventName: String, with props: [String: AnyCodableValue] = [:]) {
Task {
await trackEvent(eventName, with: props)
}
}

public func trackEvent(_ eventName: String, with props: [String: AnyCodableValue] = [:]) async {
let now = Date()
if lastTouched.distance(to: now) > AptabaseClient.sessionTimeout {
sessionId = AptabaseClient.newSessionId()
Expand All @@ -41,7 +48,8 @@ class AptabaseClient {
deviceModel: env.deviceModel
),
props: props)
dispatcher.enqueue(evt)

await dispatcher.enqueue(evt)
}

public func startPolling() {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Aptabase/EventDispatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protocol URLSessionProtocol {

extension URLSession: URLSessionProtocol {}

public class EventDispatcher {
public actor EventDispatcher {
private var events = ConcurrentQueue<Event>()
private let maximumBatchSize = 25
private let headers: [String: String]
Expand Down
37 changes: 26 additions & 11 deletions Tests/AptabaseTests/EventDispatcherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,32 @@ final class EventDispatcherTests: XCTestCase {
}

func testFlushSingleItem() async {
dispatcher.enqueue(newEvent("app_started"))
await dispatcher.enqueue(newEvent("app_started"))

await dispatcher.flush()
XCTAssertEqual(session.requestCount, 1)
}


func testFlushMultipleCalls() async {
for i in 0...50 {
await dispatcher.enqueue(newEvent("app_event_\(i)"))
}

await withTaskGroup { group in
group.addTask { [self] in
await dispatcher.flush()
}
group.addTask { [self] in
await dispatcher.flush()
}
}
}

func testFlushShouldBatchMultipleItems() async {
dispatcher.enqueue(newEvent("app_started"))
dispatcher.enqueue(newEvent("item_created"))
dispatcher.enqueue(newEvent("item_deleted"))
await dispatcher.enqueue(newEvent("app_started"))
await dispatcher.enqueue(newEvent("item_created"))
await dispatcher.enqueue(newEvent("item_deleted"))

await dispatcher.flush()
XCTAssertEqual(session.requestCount, 1)

Expand All @@ -67,10 +82,10 @@ final class EventDispatcherTests: XCTestCase {
}

func testFlushShouldRetryAfterFailure() async {
dispatcher.enqueue(newEvent("app_started"))
dispatcher.enqueue(newEvent("item_created"))
dispatcher.enqueue(newEvent("item_deleted"))
await dispatcher.enqueue(newEvent("app_started"))
await dispatcher.enqueue(newEvent("item_created"))
await dispatcher.enqueue(newEvent("item_deleted"))


session.statusCode = 500
await dispatcher.flush()
Expand Down