From 98bcaa2147d23bbc460815fadbd582ea646594c5 Mon Sep 17 00:00:00 2001 From: Andrey Sokolov Date: Tue, 24 Oct 2023 20:21:55 +0400 Subject: [PATCH 1/3] fix: add setSessionId() method --- Sources/Amplitude/Amplitude.swift | 7 ++ Sources/Amplitude/ObjC/ObjCAmplitude.swift | 7 ++ Sources/Amplitude/Sessions.swift | 8 +- .../AmplitudeSessionTests.swift | 97 +++++++++++++++++++ 4 files changed, 117 insertions(+), 2 deletions(-) diff --git a/Sources/Amplitude/Amplitude.swift b/Sources/Amplitude/Amplitude.swift index f14bd79b..e73b95f2 100644 --- a/Sources/Amplitude/Amplitude.swift +++ b/Sources/Amplitude/Amplitude.swift @@ -278,6 +278,13 @@ public class Amplitude { return sessions.sessionId } + @discardableResult + public func setSessionId(timestamp: Int64) -> Amplitude { + let sessionEvents = sessions.assignEventId(events: sessions.startNewSession(timestamp: timestamp)) + sessionEvents.forEach { e in timeline.processEvent(event: e) } + return self + } + @discardableResult public func reset() -> Amplitude { _ = setUserId(userId: nil) diff --git a/Sources/Amplitude/ObjC/ObjCAmplitude.swift b/Sources/Amplitude/ObjC/ObjCAmplitude.swift index eb47b46b..eea95c44 100644 --- a/Sources/Amplitude/ObjC/ObjCAmplitude.swift +++ b/Sources/Amplitude/ObjC/ObjCAmplitude.swift @@ -200,6 +200,13 @@ public class ObjCAmplitude: NSObject { amplitude.getSessionId() } + @objc(setSessionId:) + @discardableResult + public func setSessionId(timestamp: Int64) -> ObjCAmplitude { + amplitude.setSessionId(timestamp: timestamp) + return self + } + @objc @discardableResult public func reset() -> ObjCAmplitude { diff --git a/Sources/Amplitude/Sessions.swift b/Sources/Amplitude/Sessions.swift index 424e684d..c8786a61 100644 --- a/Sources/Amplitude/Sessions.swift +++ b/Sources/Amplitude/Sessions.swift @@ -81,9 +81,13 @@ public class Sessions { result.append(event) } + return assignEventId(events: result) + } + + func assignEventId(events: [BaseEvent]) -> [BaseEvent] { var newLastEventId = self.lastEventId - result.forEach({ event in + events.forEach({ event in if event.eventId == nil { newLastEventId += 1 event.eventId = newLastEventId @@ -92,7 +96,7 @@ public class Sessions { self.lastEventId = newLastEventId - return result + return events } private func isWithinMinTimeBetweenSessions(timestamp: Int64) -> Bool { diff --git a/Tests/AmplitudeTests/AmplitudeSessionTests.swift b/Tests/AmplitudeTests/AmplitudeSessionTests.swift index aa3d8eab..905af594 100644 --- a/Tests/AmplitudeTests/AmplitudeSessionTests.swift +++ b/Tests/AmplitudeTests/AmplitudeSessionTests.swift @@ -419,6 +419,103 @@ final class AmplitudeSessionTests: XCTestCase { XCTAssertEqual(event.eventId, lastEventId+4) } + func testSetSessionIdInBackgroundShouldStartNewSession() throws { + let lastEventId: Int64 = 123 + try storageMem.write(key: StorageKey.LAST_EVENT_ID, value: lastEventId) + + let amplitude = Amplitude(configuration: configuration) + + let eventCollector = EventCollectorPlugin() + amplitude.add(plugin: eventCollector) + + amplitude.track(event: BaseEvent(userId: "user", timestamp: 100, eventType: "test event 1")) + amplitude.setSessionId(timestamp: 150) + amplitude.track(event: BaseEvent(userId: "user", timestamp: 200, eventType: "test event 2")) + + let collectedEvents = eventCollector.events + + XCTAssertEqual(collectedEvents.count, 5) + + var event = collectedEvents[0] + XCTAssertEqual(event.eventType, Constants.AMP_SESSION_START_EVENT) + XCTAssertEqual(event.sessionId, 100) + XCTAssertEqual(event.timestamp, 100) + XCTAssertEqual(event.eventId, lastEventId+1) + + event = collectedEvents[1] + XCTAssertEqual(event.eventType, "test event 1") + XCTAssertEqual(event.sessionId, 100) + XCTAssertEqual(event.timestamp, 100) + XCTAssertEqual(event.eventId, lastEventId+2) + + event = collectedEvents[2] + XCTAssertEqual(event.eventType, Constants.AMP_SESSION_END_EVENT) + XCTAssertEqual(event.sessionId, 100) + XCTAssertEqual(event.timestamp, 100) + XCTAssertEqual(event.eventId, lastEventId+3) + + event = collectedEvents[3] + XCTAssertEqual(event.eventType, Constants.AMP_SESSION_START_EVENT) + XCTAssertEqual(event.sessionId, 150) + XCTAssertEqual(event.timestamp, 150) + XCTAssertEqual(event.eventId, lastEventId+4) + + event = collectedEvents[4] + XCTAssertEqual(event.eventType, "test event 2") + XCTAssertEqual(event.sessionId, 150) + XCTAssertEqual(event.timestamp, 200) + XCTAssertEqual(event.eventId, lastEventId+5) + } + + func testSetSessionIdInForegroundShouldStartNewSession() throws { + let lastEventId: Int64 = 123 + try storageMem.write(key: StorageKey.LAST_EVENT_ID, value: lastEventId) + + let amplitude = Amplitude(configuration: configuration) + + let eventCollector = EventCollectorPlugin() + amplitude.add(plugin: eventCollector) + + amplitude.onEnterForeground(timestamp: 1000) + amplitude.track(event: BaseEvent(userId: "user", timestamp: 1050, eventType: "test event 1")) + amplitude.setSessionId(timestamp: 1100) + amplitude.track(event: BaseEvent(userId: "user", timestamp: 2000, eventType: "test event 2")) + + let collectedEvents = eventCollector.events + + XCTAssertEqual(collectedEvents.count, 5) + + var event = collectedEvents[0] + XCTAssertEqual(event.eventType, Constants.AMP_SESSION_START_EVENT) + XCTAssertEqual(event.sessionId, 1000) + XCTAssertEqual(event.timestamp, 1000) + XCTAssertEqual(event.eventId, lastEventId+1) + + event = collectedEvents[1] + XCTAssertEqual(event.eventType, "test event 1") + XCTAssertEqual(event.sessionId, 1000) + XCTAssertEqual(event.timestamp, 1050) + XCTAssertEqual(event.eventId, lastEventId+2) + + event = collectedEvents[2] + XCTAssertEqual(event.eventType, Constants.AMP_SESSION_END_EVENT) + XCTAssertEqual(event.sessionId, 1000) + XCTAssertEqual(event.timestamp, 1050) + XCTAssertEqual(event.eventId, lastEventId+3) + + event = collectedEvents[3] + XCTAssertEqual(event.eventType, Constants.AMP_SESSION_START_EVENT) + XCTAssertEqual(event.sessionId, 1100) + XCTAssertEqual(event.timestamp, 1100) + XCTAssertEqual(event.eventId, lastEventId+4) + + event = collectedEvents[4] + XCTAssertEqual(event.eventType, "test event 2") + XCTAssertEqual(event.sessionId, 1100) + XCTAssertEqual(event.timestamp, 2000) + XCTAssertEqual(event.eventId, lastEventId+5) + } + func getDictionary(_ props: [String: Any?]) -> NSDictionary { return NSDictionary(dictionary: props as [AnyHashable: Any]) } From e8be1a849ae43e928ec8045ed25d2592c2b867dd Mon Sep 17 00:00:00 2001 From: Andrey Sokolov Date: Thu, 26 Oct 2023 16:26:44 +0400 Subject: [PATCH 2/3] fix: add sessionStart()/sessionEnd() methods --- Sources/Amplitude/Amplitude.swift | 15 +++ Sources/Amplitude/ObjC/ObjCAmplitude.swift | 28 ++++ Sources/Amplitude/Sessions.swift | 21 +++ .../AmplitudeSessionTests.swift | 125 ++++++++++++++++++ 4 files changed, 189 insertions(+) diff --git a/Sources/Amplitude/Amplitude.swift b/Sources/Amplitude/Amplitude.swift index e73b95f2..d1f8b57c 100644 --- a/Sources/Amplitude/Amplitude.swift +++ b/Sources/Amplitude/Amplitude.swift @@ -285,6 +285,21 @@ public class Amplitude { return self } + @discardableResult + public func sessionStart(at: Date = Date()) -> Amplitude { + let timestamp = Int64(at.timeIntervalSince1970 * 1000) + setSessionId(timestamp: timestamp) + return self + } + + @discardableResult + public func sessionEnd(at: Date? = nil) -> Amplitude { + let timestamp = at != nil ? Int64(at!.timeIntervalSince1970 * 1000) : nil + let sessionEvents = sessions.assignEventId(events: sessions.endCurrentSession(timestamp: timestamp)) + sessionEvents.forEach { e in timeline.processEvent(event: e) } + return self + } + @discardableResult public func reset() -> Amplitude { _ = setUserId(userId: nil) diff --git a/Sources/Amplitude/ObjC/ObjCAmplitude.swift b/Sources/Amplitude/ObjC/ObjCAmplitude.swift index eea95c44..d8deed4b 100644 --- a/Sources/Amplitude/ObjC/ObjCAmplitude.swift +++ b/Sources/Amplitude/ObjC/ObjCAmplitude.swift @@ -207,6 +207,34 @@ public class ObjCAmplitude: NSObject { return self } + @objc(sessionStart) + @discardableResult + public func sessionStart() -> ObjCAmplitude { + amplitude.sessionStart() + return self + } + + @objc(sessionStart:) + @discardableResult + public func sessionStart(at: Date) -> ObjCAmplitude { + amplitude.sessionStart(at: at) + return self + } + + @objc(sessionEnd) + @discardableResult + public func sessionEnd() -> ObjCAmplitude { + amplitude.sessionEnd() + return self + } + + @objc(sessionEnd:) + @discardableResult + public func sessionEnd(at: Date) -> ObjCAmplitude { + amplitude.sessionEnd(at: at) + return self + } + @objc @discardableResult public func reset() -> ObjCAmplitude { diff --git a/Sources/Amplitude/Sessions.swift b/Sources/Amplitude/Sessions.swift index c8786a61..82d18c66 100644 --- a/Sources/Amplitude/Sessions.swift +++ b/Sources/Amplitude/Sessions.swift @@ -142,4 +142,25 @@ public class Sessions { return sessionEvents } + + public func endCurrentSession(timestamp: Int64?) -> [BaseEvent] { + var sessionEvents: [BaseEvent] = Array() + let trackingSessionEvents = amplitude.configuration.defaultTracking.sessions + + if trackingSessionEvents && self.sessionId >= 0 { + let sessionEndEvent = BaseEvent( + timestamp: timestamp ?? (self.lastEventTime > 0 ? self.lastEventTime : nil), + sessionId: self.sessionId, + eventType: Constants.AMP_SESSION_END_EVENT + ) + sessionEvents.append(sessionEndEvent) + + if let timestamp = timestamp, timestamp > (self.lastEventTime ?? 0) { + self.lastEventTime = timestamp + } + } + + self.sessionId = -1 + return sessionEvents + } } diff --git a/Tests/AmplitudeTests/AmplitudeSessionTests.swift b/Tests/AmplitudeTests/AmplitudeSessionTests.swift index 905af594..b27d4c16 100644 --- a/Tests/AmplitudeTests/AmplitudeSessionTests.swift +++ b/Tests/AmplitudeTests/AmplitudeSessionTests.swift @@ -516,6 +516,131 @@ final class AmplitudeSessionTests: XCTestCase { XCTAssertEqual(event.eventId, lastEventId+5) } + func testSessionEndInBackgroundShouldEndCurrentSession() throws { + let lastEventId: Int64 = 123 + try storageMem.write(key: StorageKey.LAST_EVENT_ID, value: lastEventId) + + let amplitude = Amplitude(configuration: configuration) + + let eventCollector = EventCollectorPlugin() + amplitude.add(plugin: eventCollector) + + amplitude.track(event: BaseEvent(userId: "user", timestamp: 100, eventType: "test event 1")) + XCTAssertEqual(amplitude.sessionId, 100) + + amplitude.sessionEnd() + XCTAssertEqual(amplitude.sessionId, -1) + + amplitude.track(event: BaseEvent(userId: "user", timestamp: 200, eventType: "test event 2")) + XCTAssertEqual(amplitude.sessionId, 200) + + amplitude.sessionEnd(at: Date(timeIntervalSince1970: 1)) + XCTAssertEqual(amplitude.sessionId, -1) + + let collectedEvents = eventCollector.events + + XCTAssertEqual(collectedEvents.count, 6) + + var event = collectedEvents[0] + XCTAssertEqual(event.eventType, Constants.AMP_SESSION_START_EVENT) + XCTAssertEqual(event.sessionId, 100) + XCTAssertEqual(event.timestamp, 100) + XCTAssertEqual(event.eventId, lastEventId+1) + + event = collectedEvents[1] + XCTAssertEqual(event.eventType, "test event 1") + XCTAssertEqual(event.sessionId, 100) + XCTAssertEqual(event.timestamp, 100) + XCTAssertEqual(event.eventId, lastEventId+2) + + event = collectedEvents[2] + XCTAssertEqual(event.eventType, Constants.AMP_SESSION_END_EVENT) + XCTAssertEqual(event.sessionId, 100) + XCTAssertEqual(event.timestamp, 100) + XCTAssertEqual(event.eventId, lastEventId+3) + + event = collectedEvents[3] + XCTAssertEqual(event.eventType, Constants.AMP_SESSION_START_EVENT) + XCTAssertEqual(event.sessionId, 200) + XCTAssertEqual(event.timestamp, 200) + XCTAssertEqual(event.eventId, lastEventId+4) + + event = collectedEvents[4] + XCTAssertEqual(event.eventType, "test event 2") + XCTAssertEqual(event.sessionId, 200) + XCTAssertEqual(event.timestamp, 200) + XCTAssertEqual(event.eventId, lastEventId+5) + + event = collectedEvents[5] + XCTAssertEqual(event.eventType, Constants.AMP_SESSION_END_EVENT) + XCTAssertEqual(event.sessionId, 200) + XCTAssertEqual(event.timestamp, 1000) + XCTAssertEqual(event.eventId, lastEventId+6) + } + + func testSessionEndInForegroundShouldEndCurrentSession() throws { + let lastEventId: Int64 = 123 + try storageMem.write(key: StorageKey.LAST_EVENT_ID, value: lastEventId) + + let amplitude = Amplitude(configuration: configuration) + + let eventCollector = EventCollectorPlugin() + amplitude.add(plugin: eventCollector) + + amplitude.onEnterForeground(timestamp: 100) + amplitude.track(event: BaseEvent(userId: "user", timestamp: 150, eventType: "test event 1")) + XCTAssertEqual(amplitude.sessionId, 100) + + amplitude.sessionEnd() + XCTAssertEqual(amplitude.sessionId, -1) + + amplitude.track(event: BaseEvent(userId: "user", timestamp: 200, eventType: "test event 2")) + XCTAssertEqual(amplitude.sessionId, 200) + + amplitude.sessionEnd(at: Date(timeIntervalSince1970: 1)) + XCTAssertEqual(amplitude.sessionId, -1) + + let collectedEvents = eventCollector.events + + XCTAssertEqual(collectedEvents.count, 6) + + var event = collectedEvents[0] + XCTAssertEqual(event.eventType, Constants.AMP_SESSION_START_EVENT) + XCTAssertEqual(event.sessionId, 100) + XCTAssertEqual(event.timestamp, 100) + XCTAssertEqual(event.eventId, lastEventId+1) + + event = collectedEvents[1] + XCTAssertEqual(event.eventType, "test event 1") + XCTAssertEqual(event.sessionId, 100) + XCTAssertEqual(event.timestamp, 150) + XCTAssertEqual(event.eventId, lastEventId+2) + + event = collectedEvents[2] + XCTAssertEqual(event.eventType, Constants.AMP_SESSION_END_EVENT) + XCTAssertEqual(event.sessionId, 100) + XCTAssertEqual(event.timestamp, 150) + XCTAssertEqual(event.eventId, lastEventId+3) + + event = collectedEvents[3] + XCTAssertEqual(event.eventType, Constants.AMP_SESSION_START_EVENT) + XCTAssertEqual(event.sessionId, 200) + XCTAssertEqual(event.timestamp, 200) + XCTAssertEqual(event.eventId, lastEventId+4) + + event = collectedEvents[4] + XCTAssertEqual(event.eventType, "test event 2") + XCTAssertEqual(event.sessionId, 200) + XCTAssertEqual(event.timestamp, 200) + XCTAssertEqual(event.eventId, lastEventId+5) + + event = collectedEvents[5] + XCTAssertEqual(event.eventType, Constants.AMP_SESSION_END_EVENT) + XCTAssertEqual(event.sessionId, 200) + XCTAssertEqual(event.timestamp, 1000) + XCTAssertEqual(event.eventId, lastEventId+6) + } + func getDictionary(_ props: [String: Any?]) -> NSDictionary { return NSDictionary(dictionary: props as [AnyHashable: Any]) } From 1e53004836a03ebca09c754b688032ec9eba13f6 Mon Sep 17 00:00:00 2001 From: Andrey Sokolov Date: Fri, 27 Oct 2023 11:44:05 +0400 Subject: [PATCH 3/3] fix: remove sessionStart()/sessionEnd() methods --- Sources/Amplitude/Amplitude.swift | 16 ++-- Sources/Amplitude/ObjC/ObjCAmplitude.swift | 29 +------ Sources/Amplitude/Sessions.swift | 8 +- .../AmplitudeSessionTests.swift | 84 ++++++++----------- 4 files changed, 44 insertions(+), 93 deletions(-) diff --git a/Sources/Amplitude/Amplitude.swift b/Sources/Amplitude/Amplitude.swift index d1f8b57c..ffd9bc89 100644 --- a/Sources/Amplitude/Amplitude.swift +++ b/Sources/Amplitude/Amplitude.swift @@ -280,26 +280,20 @@ public class Amplitude { @discardableResult public func setSessionId(timestamp: Int64) -> Amplitude { - let sessionEvents = sessions.assignEventId(events: sessions.startNewSession(timestamp: timestamp)) + let sessionEvents = sessions.assignEventId( + events: timestamp >= 0 ? sessions.startNewSession(timestamp: timestamp) : sessions.endCurrentSession() + ) sessionEvents.forEach { e in timeline.processEvent(event: e) } return self } @discardableResult - public func sessionStart(at: Date = Date()) -> Amplitude { - let timestamp = Int64(at.timeIntervalSince1970 * 1000) + public func setSessionId(date: Date) -> Amplitude { + let timestamp = Int64(date.timeIntervalSince1970 * 1000) setSessionId(timestamp: timestamp) return self } - @discardableResult - public func sessionEnd(at: Date? = nil) -> Amplitude { - let timestamp = at != nil ? Int64(at!.timeIntervalSince1970 * 1000) : nil - let sessionEvents = sessions.assignEventId(events: sessions.endCurrentSession(timestamp: timestamp)) - sessionEvents.forEach { e in timeline.processEvent(event: e) } - return self - } - @discardableResult public func reset() -> Amplitude { _ = setUserId(userId: nil) diff --git a/Sources/Amplitude/ObjC/ObjCAmplitude.swift b/Sources/Amplitude/ObjC/ObjCAmplitude.swift index d8deed4b..df947349 100644 --- a/Sources/Amplitude/ObjC/ObjCAmplitude.swift +++ b/Sources/Amplitude/ObjC/ObjCAmplitude.swift @@ -200,38 +200,17 @@ public class ObjCAmplitude: NSObject { amplitude.getSessionId() } - @objc(setSessionId:) + @objc(setSessionIdWithTimestamp:) @discardableResult public func setSessionId(timestamp: Int64) -> ObjCAmplitude { amplitude.setSessionId(timestamp: timestamp) return self } - @objc(sessionStart) + @objc(setSessionIdWithDate:) @discardableResult - public func sessionStart() -> ObjCAmplitude { - amplitude.sessionStart() - return self - } - - @objc(sessionStart:) - @discardableResult - public func sessionStart(at: Date) -> ObjCAmplitude { - amplitude.sessionStart(at: at) - return self - } - - @objc(sessionEnd) - @discardableResult - public func sessionEnd() -> ObjCAmplitude { - amplitude.sessionEnd() - return self - } - - @objc(sessionEnd:) - @discardableResult - public func sessionEnd(at: Date) -> ObjCAmplitude { - amplitude.sessionEnd(at: at) + public func setSessionId(date: Date) -> ObjCAmplitude { + amplitude.setSessionId(date: date) return self } diff --git a/Sources/Amplitude/Sessions.swift b/Sources/Amplitude/Sessions.swift index 82d18c66..ad226d23 100644 --- a/Sources/Amplitude/Sessions.swift +++ b/Sources/Amplitude/Sessions.swift @@ -143,21 +143,17 @@ public class Sessions { return sessionEvents } - public func endCurrentSession(timestamp: Int64?) -> [BaseEvent] { + public func endCurrentSession() -> [BaseEvent] { var sessionEvents: [BaseEvent] = Array() let trackingSessionEvents = amplitude.configuration.defaultTracking.sessions if trackingSessionEvents && self.sessionId >= 0 { let sessionEndEvent = BaseEvent( - timestamp: timestamp ?? (self.lastEventTime > 0 ? self.lastEventTime : nil), + timestamp: self.lastEventTime > 0 ? self.lastEventTime : nil, sessionId: self.sessionId, eventType: Constants.AMP_SESSION_END_EVENT ) sessionEvents.append(sessionEndEvent) - - if let timestamp = timestamp, timestamp > (self.lastEventTime ?? 0) { - self.lastEventTime = timestamp - } } self.sessionId = -1 diff --git a/Tests/AmplitudeTests/AmplitudeSessionTests.swift b/Tests/AmplitudeTests/AmplitudeSessionTests.swift index b27d4c16..416d0fef 100644 --- a/Tests/AmplitudeTests/AmplitudeSessionTests.swift +++ b/Tests/AmplitudeTests/AmplitudeSessionTests.swift @@ -525,57 +525,48 @@ final class AmplitudeSessionTests: XCTestCase { let eventCollector = EventCollectorPlugin() amplitude.add(plugin: eventCollector) - amplitude.track(event: BaseEvent(userId: "user", timestamp: 100, eventType: "test event 1")) - XCTAssertEqual(amplitude.sessionId, 100) + amplitude.track(event: BaseEvent(userId: "user", timestamp: 1000, eventType: "test event 1")) + XCTAssertEqual(amplitude.sessionId, 1000) - amplitude.sessionEnd() + amplitude.setSessionId(timestamp: -1) XCTAssertEqual(amplitude.sessionId, -1) - amplitude.track(event: BaseEvent(userId: "user", timestamp: 200, eventType: "test event 2")) - XCTAssertEqual(amplitude.sessionId, 200) - - amplitude.sessionEnd(at: Date(timeIntervalSince1970: 1)) - XCTAssertEqual(amplitude.sessionId, -1) + amplitude.track(event: BaseEvent(userId: "user", timestamp: 2000, eventType: "test event 2")) + XCTAssertEqual(amplitude.sessionId, 2000) let collectedEvents = eventCollector.events - XCTAssertEqual(collectedEvents.count, 6) + XCTAssertEqual(collectedEvents.count, 5) var event = collectedEvents[0] XCTAssertEqual(event.eventType, Constants.AMP_SESSION_START_EVENT) - XCTAssertEqual(event.sessionId, 100) - XCTAssertEqual(event.timestamp, 100) + XCTAssertEqual(event.sessionId, 1000) + XCTAssertEqual(event.timestamp, 1000) XCTAssertEqual(event.eventId, lastEventId+1) event = collectedEvents[1] XCTAssertEqual(event.eventType, "test event 1") - XCTAssertEqual(event.sessionId, 100) - XCTAssertEqual(event.timestamp, 100) + XCTAssertEqual(event.sessionId, 1000) + XCTAssertEqual(event.timestamp, 1000) XCTAssertEqual(event.eventId, lastEventId+2) event = collectedEvents[2] XCTAssertEqual(event.eventType, Constants.AMP_SESSION_END_EVENT) - XCTAssertEqual(event.sessionId, 100) - XCTAssertEqual(event.timestamp, 100) + XCTAssertEqual(event.sessionId, 1000) + XCTAssertEqual(event.timestamp, 1000) XCTAssertEqual(event.eventId, lastEventId+3) event = collectedEvents[3] XCTAssertEqual(event.eventType, Constants.AMP_SESSION_START_EVENT) - XCTAssertEqual(event.sessionId, 200) - XCTAssertEqual(event.timestamp, 200) + XCTAssertEqual(event.sessionId, 2000) + XCTAssertEqual(event.timestamp, 2000) XCTAssertEqual(event.eventId, lastEventId+4) event = collectedEvents[4] XCTAssertEqual(event.eventType, "test event 2") - XCTAssertEqual(event.sessionId, 200) - XCTAssertEqual(event.timestamp, 200) + XCTAssertEqual(event.sessionId, 2000) + XCTAssertEqual(event.timestamp, 2000) XCTAssertEqual(event.eventId, lastEventId+5) - - event = collectedEvents[5] - XCTAssertEqual(event.eventType, Constants.AMP_SESSION_END_EVENT) - XCTAssertEqual(event.sessionId, 200) - XCTAssertEqual(event.timestamp, 1000) - XCTAssertEqual(event.eventId, lastEventId+6) } func testSessionEndInForegroundShouldEndCurrentSession() throws { @@ -587,58 +578,49 @@ final class AmplitudeSessionTests: XCTestCase { let eventCollector = EventCollectorPlugin() amplitude.add(plugin: eventCollector) - amplitude.onEnterForeground(timestamp: 100) - amplitude.track(event: BaseEvent(userId: "user", timestamp: 150, eventType: "test event 1")) - XCTAssertEqual(amplitude.sessionId, 100) + amplitude.onEnterForeground(timestamp: 1000) + amplitude.track(event: BaseEvent(userId: "user", timestamp: 1500, eventType: "test event 1")) + XCTAssertEqual(amplitude.sessionId, 1000) - amplitude.sessionEnd() + amplitude.setSessionId(timestamp: -1) XCTAssertEqual(amplitude.sessionId, -1) - amplitude.track(event: BaseEvent(userId: "user", timestamp: 200, eventType: "test event 2")) - XCTAssertEqual(amplitude.sessionId, 200) - - amplitude.sessionEnd(at: Date(timeIntervalSince1970: 1)) - XCTAssertEqual(amplitude.sessionId, -1) + amplitude.track(event: BaseEvent(userId: "user", timestamp: 2000, eventType: "test event 2")) + XCTAssertEqual(amplitude.sessionId, 2000) let collectedEvents = eventCollector.events - XCTAssertEqual(collectedEvents.count, 6) + XCTAssertEqual(collectedEvents.count, 5) var event = collectedEvents[0] XCTAssertEqual(event.eventType, Constants.AMP_SESSION_START_EVENT) - XCTAssertEqual(event.sessionId, 100) - XCTAssertEqual(event.timestamp, 100) + XCTAssertEqual(event.sessionId, 1000) + XCTAssertEqual(event.timestamp, 1000) XCTAssertEqual(event.eventId, lastEventId+1) event = collectedEvents[1] XCTAssertEqual(event.eventType, "test event 1") - XCTAssertEqual(event.sessionId, 100) - XCTAssertEqual(event.timestamp, 150) + XCTAssertEqual(event.sessionId, 1000) + XCTAssertEqual(event.timestamp, 1500) XCTAssertEqual(event.eventId, lastEventId+2) event = collectedEvents[2] XCTAssertEqual(event.eventType, Constants.AMP_SESSION_END_EVENT) - XCTAssertEqual(event.sessionId, 100) - XCTAssertEqual(event.timestamp, 150) + XCTAssertEqual(event.sessionId, 1000) + XCTAssertEqual(event.timestamp, 1500) XCTAssertEqual(event.eventId, lastEventId+3) event = collectedEvents[3] XCTAssertEqual(event.eventType, Constants.AMP_SESSION_START_EVENT) - XCTAssertEqual(event.sessionId, 200) - XCTAssertEqual(event.timestamp, 200) + XCTAssertEqual(event.sessionId, 2000) + XCTAssertEqual(event.timestamp, 2000) XCTAssertEqual(event.eventId, lastEventId+4) event = collectedEvents[4] XCTAssertEqual(event.eventType, "test event 2") - XCTAssertEqual(event.sessionId, 200) - XCTAssertEqual(event.timestamp, 200) + XCTAssertEqual(event.sessionId, 2000) + XCTAssertEqual(event.timestamp, 2000) XCTAssertEqual(event.eventId, lastEventId+5) - - event = collectedEvents[5] - XCTAssertEqual(event.eventType, Constants.AMP_SESSION_END_EVENT) - XCTAssertEqual(event.sessionId, 200) - XCTAssertEqual(event.timestamp, 1000) - XCTAssertEqual(event.eventId, lastEventId+6) } func getDictionary(_ props: [String: Any?]) -> NSDictionary {