Skip to content

Commit

Permalink
fix: try fix crash (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
qingzhuozhen authored Feb 13, 2024
1 parent 82c5b5e commit 0536666
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
push:
branches:
- main
- 1.1.x

jobs:
test:
Expand Down
13 changes: 12 additions & 1 deletion Sources/Amplitude/Utilities/IdentifyInterceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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? {
Expand All @@ -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
}
Expand Down
29 changes: 29 additions & 0 deletions Tests/AmplitudeTests/AmplitudeSessionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions Tests/AmplitudeTests/AmplitudeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion release.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
"branches": ["main"],
"branches": ["main", "1.1.x"],
"plugins": [
["@semantic-release/commit-analyzer", {
"preset": "angular",
Expand Down

0 comments on commit 0536666

Please sign in to comment.