Skip to content

Commit

Permalink
maxQueueSize wasn't respected when capturing events (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto authored Mar 12, 2024
1 parent 542a814 commit ab78945
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Next

- `maxQueueSize` wasn't respected when capturing events [#116](https://github.com/PostHog/posthog-ios/pull/116)

## 3.2.3 - 2024-03-05

- `optOut` wasn't respected in capture methods [#114](https://github.com/PostHog/posthog-ios/pull/114)
Expand Down
6 changes: 6 additions & 0 deletions PostHog/PostHogQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ class PostHogQueue {
}

func add(_ event: PostHogEvent) {
if fileQueue.depth >= config.maxQueueSize {
hedgeLog("Queue is full, dropping oldest event")
// first is always oldest
fileQueue.delete(index: 0)
}

var data: Data?
do {
data = try JSONSerialization.data(withJSONObject: event.toJSON())
Expand Down
31 changes: 29 additions & 2 deletions PostHogTests/PostHogQueueTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import Quick
import XCTest

class PostHogQueueTest: QuickSpec {
func getSut() -> PostHogQueue {
func getSut(flushAt: Int = 1, maxQueueSize: Int = 1000) -> PostHogQueue {
let config = PostHogConfig(apiKey: "123", host: "http://localhost:9001")
config.flushAt = 1
config.flushAt = flushAt
config.maxQueueSize = maxQueueSize
let storage = PostHogStorage(config)
let api = PostHogApi(config)
return PostHogQueue(config, storage, api, nil)
Expand Down Expand Up @@ -64,5 +65,31 @@ class PostHogQueueTest: QuickSpec {

sut.clear()
}

it("add item to queue and rotate queue") {
let sut = self.getSut(flushAt: 3, maxQueueSize: 2)

let event = PostHogEvent(event: "event", distinctId: "distinctId")
let event2 = PostHogEvent(event: "event2", distinctId: "distinctId2")
let event3 = PostHogEvent(event: "event3", distinctId: "distinctId3")
sut.add(event)
sut.add(event2)
sut.add(event3)

expect(sut.depth) == 2

sut.flush()

let events = getBatchedEvents(server)

expect(events.count) == 2

let first = events.first!
let last = events.last!
expect(first.event) == "event2"
expect(last.event) == "event3"

sut.clear()
}
}
}

0 comments on commit ab78945

Please sign in to comment.