From 0536666dd36e4dda86a0a5334de8588c59063f7c Mon Sep 17 00:00:00 2001 From: qingzhuozhen <84748495+qingzhuozhen@users.noreply.github.com> Date: Tue, 13 Feb 2024 10:24:10 -0800 Subject: [PATCH] fix: try fix crash (#117) --- .github/workflows/unit-test.yml | 1 + .../Utilities/IdentifyInterceptor.swift | 13 ++++++++- .../AmplitudeSessionTests.swift | 29 +++++++++++++++++++ Tests/AmplitudeTests/AmplitudeTests.swift | 17 +++++++++++ release.config.js | 2 +- 5 files changed, 60 insertions(+), 2 deletions(-) diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml index 0404bef3..5cdd2ccb 100644 --- a/.github/workflows/unit-test.yml +++ b/.github/workflows/unit-test.yml @@ -5,6 +5,7 @@ on: push: branches: - main + - 1.1.x jobs: test: diff --git a/Sources/Amplitude/Utilities/IdentifyInterceptor.swift b/Sources/Amplitude/Utilities/IdentifyInterceptor.swift index 5b07c919..e66030af 100644 --- a/Sources/Amplitude/Utilities/IdentifyInterceptor.swift +++ b/Sources/Amplitude/Utilities/IdentifyInterceptor.swift @@ -9,6 +9,11 @@ public class IdentifyInterceptor { userId = event.userId deviceId = event.deviceId } + + init(_ newUserId: String?, _ newDeviceId: String?) { + userId = newUserId + deviceId = newDeviceId + } } private let configuration: Configuration @@ -34,6 +39,7 @@ public class IdentifyInterceptor { self.logger?.warn(message: "Minimum `identifyBatchIntervalMillis` is \(Constants.MIN_IDENTIFY_BATCH_INTERVAL_MILLIS).") } self.identifyBatchIntervalMillis = max(identifyBatchIntervalMillis, Constants.MIN_IDENTIFY_BATCH_INTERVAL_MILLIS) + self.lastIdentity = Identity(nil, nil) } public func intercept(event: BaseEvent) -> BaseEvent? { @@ -49,7 +55,12 @@ public class IdentifyInterceptor { private func isIdentityUpdated(_ event: BaseEvent) -> Bool { let eventIdentity = Identity(event) - if eventIdentity != lastIdentity { + guard let currentIdenity = lastIdentity else { + lastIdentity = eventIdentity + return true + } + + if eventIdentity != currentIdenity { lastIdentity = eventIdentity return true } diff --git a/Tests/AmplitudeTests/AmplitudeSessionTests.swift b/Tests/AmplitudeTests/AmplitudeSessionTests.swift index 416d0fef..5235ea19 100644 --- a/Tests/AmplitudeTests/AmplitudeSessionTests.swift +++ b/Tests/AmplitudeTests/AmplitudeSessionTests.swift @@ -105,6 +105,35 @@ final class AmplitudeSessionTests: XCTestCase { XCTAssertEqual(event.eventId, lastEventId+5) } + func testBackgroundOutOfSessionEvent() throws { + let lastEventId: Int64 = 123 + try storageMem.write(key: StorageKey.LAST_EVENT_ID, value: lastEventId) + let customCongiguration = Configuration( + apiKey: "test-out-of-session", + storageProvider: storageMem, + identifyStorageProvider: interceptStorageMem, + minTimeBetweenSessionsMillis: 100, + defaultTracking: DefaultTrackingOptions(sessions: false) + ) + let amplitude = Amplitude(configuration: customCongiguration) + amplitude.setSessionId(timestamp: 800) + let eventCollector = EventCollectorPlugin() + amplitude.add(plugin: eventCollector) + let eventOptions = EventOptions(timestamp: 1000, sessionId: -1) + let eventType = "out of session event" + amplitude.track(eventType: eventType, options: eventOptions) + amplitude.track(event: BaseEvent(userId: "user", timestamp: 1050, eventType: "test event")) + let collectedEvents = eventCollector.events + XCTAssertEqual(collectedEvents.count, 2) + var event = collectedEvents[0] + XCTAssertEqual(event.eventType, eventType) + XCTAssertEqual(event.sessionId, -1) + event = collectedEvents[1] + XCTAssertEqual(event.eventType, "test event") + XCTAssertEqual(event.sessionId, 1000) + XCTAssertEqual(amplitude.getSessionId(), 1000) + } + func testForegroundEventsShouldNotStartNewSession() throws { let lastEventId: Int64 = 123 try storageMem.write(key: StorageKey.LAST_EVENT_ID, value: lastEventId) diff --git a/Tests/AmplitudeTests/AmplitudeTests.swift b/Tests/AmplitudeTests/AmplitudeTests.swift index 3d8da841..de7cc66f 100644 --- a/Tests/AmplitudeTests/AmplitudeTests.swift +++ b/Tests/AmplitudeTests/AmplitudeTests.swift @@ -324,6 +324,23 @@ final class AmplitudeTests: XCTestCase { ]) } + func testOutOfSessionEvent() { + let configuration = Configuration( + apiKey: "api-key", + storageProvider: storageMem, + identifyStorageProvider: interceptStorageMem, + defaultTracking: DefaultTrackingOptions(sessions: false) + ) + let amplitude = Amplitude(configuration: configuration) + let eventOptions = EventOptions(sessionId: -1) + let eventType = "out of session event" + amplitude.track(eventType: eventType, options: eventOptions) + let events = storageMem.events() + XCTAssertEqual(events.count, 1) + XCTAssertEqual(events[0].eventType, eventType) + XCTAssertEqual(events[0].sessionId, -1) + } + func testMigrationToApiKeyAndInstanceNameStorage() throws { let legacyUserId = "legacy-user-id" let config = Configuration( diff --git a/release.config.js b/release.config.js index 8e73d1b6..92a12731 100644 --- a/release.config.js +++ b/release.config.js @@ -1,5 +1,5 @@ module.exports = { - "branches": ["main"], + "branches": ["main", "1.1.x"], "plugins": [ ["@semantic-release/commit-analyzer", { "preset": "angular",