From ab78945df66e55bf602267619a051d83b93473e2 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto <5731772+marandaneto@users.noreply.github.com> Date: Tue, 12 Mar 2024 15:49:07 +0100 Subject: [PATCH 1/3] `maxQueueSize` wasn't respected when capturing events (#116) --- CHANGELOG.md | 2 ++ PostHog/PostHogQueue.swift | 6 ++++++ PostHogTests/PostHogQueueTest.swift | 31 +++++++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index df419dc7f..62718b098 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/PostHog/PostHogQueue.swift b/PostHog/PostHogQueue.swift index ed556fb33..7c135de03 100644 --- a/PostHog/PostHogQueue.swift +++ b/PostHog/PostHogQueue.swift @@ -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()) diff --git a/PostHogTests/PostHogQueueTest.swift b/PostHogTests/PostHogQueueTest.swift index 109acf3b2..32aa945a0 100644 --- a/PostHogTests/PostHogQueueTest.swift +++ b/PostHogTests/PostHogQueueTest.swift @@ -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) @@ -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() + } } } From 3f12921025298929a40927ac392b83b8e6ad6e7e Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Tue, 12 Mar 2024 16:01:35 +0100 Subject: [PATCH 2/3] changelog 3.2.4 --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62718b098..4b6c81bcb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## Next +## 3.2.4 - 2024-03-12 + - `maxQueueSize` wasn't respected when capturing events [#116](https://github.com/PostHog/posthog-ios/pull/116) ## 3.2.3 - 2024-03-05 From d127599034e08f12f340e5e2da13090a894d55d6 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Tue, 12 Mar 2024 16:01:53 +0100 Subject: [PATCH 3/3] Update version --- PostHog.podspec | 2 +- PostHog/PostHogVersion.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/PostHog.podspec b/PostHog.podspec index 95be96bd6..a26f3b975 100644 --- a/PostHog.podspec +++ b/PostHog.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "PostHog" - s.version = "3.2.3" + s.version = "3.2.4" s.summary = "The hassle-free way to add posthog to your iOS app." s.description = <<-DESC diff --git a/PostHog/PostHogVersion.swift b/PostHog/PostHogVersion.swift index 3ebe95224..6e384a12a 100644 --- a/PostHog/PostHogVersion.swift +++ b/PostHog/PostHogVersion.swift @@ -9,7 +9,7 @@ import Foundation // if you change this, make sure to also change it in the podspec and check if the script scripts/bump-version.sh still works // This property is internal only -public var postHogVersion = "3.2.3" +public var postHogVersion = "3.2.4" // This property is internal only public var postHogSdkName = "posthog-ios"