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

fix: Set inForeground to true after a new session has been started #124

Merged
merged 4 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion Sources/Amplitude/Amplitude.swift
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,13 @@ public class Amplitude {
}

func onEnterForeground(timestamp: Int64) {
inForeground = true
let dummySessionStartEvent = BaseEvent(
timestamp: timestamp,
eventType: Constants.AMP_SESSION_START_EVENT
)
let events = sessions.processEvent(event: dummySessionStartEvent, inForeground: false)
// Set inForeground to true only after we have successfully started a new session if needed.
inForeground = true
events.forEach { e in timeline.processEvent(event: e) }
}

Expand Down
37 changes: 37 additions & 0 deletions Tests/AmplitudeTests/AmplitudeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,43 @@ final class AmplitudeTests: XCTestCase {
XCTAssertEqual(events[0].eventType, eventType)
XCTAssertEqual(events[0].sessionId, -1)
}

func testEventProcessingBeforeOnEnterForeground() async {
let configuration = Configuration(
apiKey: "api-key",
storageProvider: storageMem,
identifyStorageProvider: interceptStorageMem,
defaultTracking: DefaultTrackingOptions(sessions: false)
)
let amplitude = Amplitude(configuration: configuration)
amplitude.sessions = SessionsWithDelayedEventStartProcessing(amplitude: amplitude)
let timestamp = Int64(NSDate().timeIntervalSince1970 * 1000)

let oneHourEarlierTimestamp = timestamp - (1 * 60 * 60 * 1000)
amplitude.setSessionId(timestamp: oneHourEarlierTimestamp)

@Sendable
func processStartSessionEvent() async {
amplitude.onEnterForeground(timestamp: timestamp)
}

func processRegularEvent() async {
amplitude.track(eventType: "test_event")
}

// We process the session start event first. The session class will wait for 3 seconds before it processes
// the event
async let task = processStartSessionEvent();
// Sleep for 1 second and process a regular event. This is to try the case where an event gets processed
// before the session start event
sleep(1)
await processRegularEvent();
await task;

// We want to make sure that a new session was started
XCTAssertTrue(amplitude.getSessionId() > oneHourEarlierTimestamp)

}

func testMigrationToApiKeyAndInstanceNameStorage() throws {
let legacyUserId = "legacy-user-id"
Expand Down
9 changes: 9 additions & 0 deletions Tests/AmplitudeTests/Supports/TestUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,12 @@ final class MockPathCreation: PathCreationProtocol {
subject.send(networkPath)
}
}

class SessionsWithDelayedEventStartProcessing: Sessions {
override func processEvent(event: BaseEvent, inForeground: Bool) -> [BaseEvent] {
if (event.eventType == Constants.AMP_SESSION_START_EVENT) {
sleep(3)
}
return super.processEvent(event: event, inForeground: inForeground)
}
}
Loading