Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maxQueueSize wasn't respected when capturing events #116

Merged
merged 2 commits into from
Mar 12, 2024
Merged
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
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()
}
}
}