Skip to content

Commit 1833a66

Browse files
authored
Update Sessions to send events when FIID empty (#11161)
1 parent 171f818 commit 1833a66

File tree

2 files changed

+64
-11
lines changed

2 files changed

+64
-11
lines changed

FirebaseSessions/Sources/SessionCoordinator.swift

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,39 @@ class SessionCoordinator: SessionCoordinatorProtocol {
3939
callback: @escaping (Result<Void, FirebaseSessionsError>)
4040
-> Void) {
4141
/// Order of execution
42-
/// 1. Fetch the installations Id. If successful, move to 3. Else, drop sending the event.
42+
/// 1. Fetch the installations Id. Regardless of success, move to step 2
4343
/// 2. Log the event. If successful, all is good. Else, log the message with error.
44-
installations.installationID { result in
45-
switch result {
46-
case let .success(fiid):
47-
event.setInstallationID(installationId: fiid)
48-
self.fireLogger.logEvent(event: event) { logResult in
49-
switch logResult {
50-
case .success():
51-
Logger.logDebug("Successfully logged Session Start event to GoogleDataTransport")
44+
/// 3. If there was a FireLog error, expose it to the callback. Otherwise expose the FIID
45+
/// error if it exists. Otherwise, success.
46+
fillInFIID(event: event) { fiidResult in
47+
self.fireLogger.logEvent(event: event) { logResult in
48+
switch logResult {
49+
case .success():
50+
Logger.logDebug("Successfully logged Session Start event to GoogleDataTransport")
51+
52+
switch fiidResult {
53+
case .success(()):
5254
callback(.success(()))
5355
case let .failure(error):
54-
callback(.failure(FirebaseSessionsError.DataTransportError(error)))
56+
callback(.failure(error))
5557
}
58+
case let .failure(error):
59+
callback(.failure(FirebaseSessionsError.DataTransportError(error)))
5660
}
61+
}
62+
}
63+
}
64+
65+
private func fillInFIID(event: SessionStartEvent,
66+
callback: @escaping (Result<Void, FirebaseSessionsError>)
67+
-> Void) {
68+
installations.installationID { result in
69+
switch result {
70+
case let .success(fiid):
71+
event.setInstallationID(installationId: fiid)
72+
callback(.success(()))
5773
case let .failure(error):
74+
event.setInstallationID(installationId: "")
5875
callback(.failure(FirebaseSessionsError.SessionInstallationsError(error)))
5976
}
6077
}

FirebaseSessions/Tests/Unit/SessionCoordinatorTests.swift

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,43 @@ class SessionCoordinatorTests: XCTestCase {
111111
}
112112

113113
// We should have logged the event, but with a failed result
114-
XCTAssertNil(fireLogger.loggedEvent)
114+
XCTAssertNotNil(fireLogger.loggedEvent)
115+
XCTAssertFalse(resultSuccess)
116+
}
117+
118+
func test_attemptLoggingSessionStart_handlesGDTAndInstallationsError() throws {
119+
let fireLogError = NSError(domain: "DataTransportError", code: -2)
120+
fireLogger.result = .failure(fireLogError)
121+
installations
122+
.result = .failure(FirebaseSessionsError
123+
.SessionInstallationsError(NSError(domain: "TestInstallationsError", code: -1)))
124+
125+
let event = SessionStartEvent(sessionInfo: defaultSessionInfo, appInfo: appInfo, time: time)
126+
127+
// Start success so it must be set to false
128+
var resultSuccess = true
129+
coordinator.attemptLoggingSessionStart(event: event) { result in
130+
switch result {
131+
case .success(()):
132+
resultSuccess = true
133+
case let .failure(err):
134+
resultSuccess = false
135+
// Result should use the FireLog error if there's an error in both
136+
// Installations and FireLog
137+
XCTAssertEqual(err, FirebaseSessionsError.DataTransportError(fireLogError))
138+
}
139+
}
140+
141+
// Make sure we've set the Installation ID to empty because the FIID
142+
// fetch failed
143+
assertEqualProtoString(
144+
event.proto.session_data.firebase_installation_id,
145+
expected: "",
146+
fieldName: "installation_id"
147+
)
148+
149+
// We should have logged the event, but with a failed result
150+
XCTAssertEqual(fireLogger.loggedEvent, event)
115151
XCTAssertFalse(resultSuccess)
116152
}
117153
}

0 commit comments

Comments
 (0)